From 287be9526fca9dfccfc03af3f0421bc88c98e306 Mon Sep 17 00:00:00 2001 From: direwolf20 <39863894+Direwolf20-MC@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:44:11 -0400 Subject: [PATCH] Item Collector now has a respect pickup delay option as requested in #217 --- .../8202586f691eec5ad0bb88d13a278951d0c130fb | 4 +-- .../assets/justdirethings/lang/en_us.json | 1 + .../client/screens/ItemCollectorScreen.java | 20 ++++++++++++++ .../standardbuttons/ToggleButtonFactory.java | 7 +++++ .../common/blockentities/ItemCollectorBE.java | 22 +++++++++++++++ .../common/network/PacketHandler.java | 1 + .../data/ItemCollectorSettingsPayload.java | 24 +++++++++++++++++ .../handler/ItemCollectorSettingsPacket.java | 27 +++++++++++++++++++ .../datagen/JustDireLanguageProvider.java | 1 + 9 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/direwolf20/justdirethings/common/network/data/ItemCollectorSettingsPayload.java create mode 100644 src/main/java/com/direwolf20/justdirethings/common/network/handler/ItemCollectorSettingsPacket.java diff --git a/src/generated/resources/.cache/8202586f691eec5ad0bb88d13a278951d0c130fb b/src/generated/resources/.cache/8202586f691eec5ad0bb88d13a278951d0c130fb index b78b6495..04c487e0 100644 --- a/src/generated/resources/.cache/8202586f691eec5ad0bb88d13a278951d0c130fb +++ b/src/generated/resources/.cache/8202586f691eec5ad0bb88d13a278951d0c130fb @@ -1,2 +1,2 @@ -// 1.21.1 2024-10-18T20:02:46.3102567 Languages: en_us for mod: justdirethings -c7f1cc2b04789bf34333113ee3c1fcd58eb116b2 assets/justdirethings/lang/en_us.json +// 1.21.1 2024-10-18T20:34:14.9851392 Languages: en_us for mod: justdirethings +8688cf4f7590dcf15b5692827931cc7556bd9a23 assets/justdirethings/lang/en_us.json diff --git a/src/generated/resources/assets/justdirethings/lang/en_us.json b/src/generated/resources/assets/justdirethings/lang/en_us.json index 08b9b999..53d42e5b 100644 --- a/src/generated/resources/assets/justdirethings/lang/en_us.json +++ b/src/generated/resources/assets/justdirethings/lang/en_us.json @@ -418,6 +418,7 @@ "justdirethings.screen.renderarea": "Render Area", "justdirethings.screen.renderparadox": "Render Paradox", "justdirethings.screen.requireequipped": "Activate if Equipped", + "justdirethings.screen.respectpickupdelay": "Respect Item Pickup Delay", "justdirethings.screen.retrieveexp": "Retrieve Level", "justdirethings.screen.rightclicksettings": "Right Click for Settings", "justdirethings.screen.save_close": "Save and Close", diff --git a/src/main/java/com/direwolf20/justdirethings/client/screens/ItemCollectorScreen.java b/src/main/java/com/direwolf20/justdirethings/client/screens/ItemCollectorScreen.java index 90fc6311..bbf8755b 100644 --- a/src/main/java/com/direwolf20/justdirethings/client/screens/ItemCollectorScreen.java +++ b/src/main/java/com/direwolf20/justdirethings/client/screens/ItemCollectorScreen.java @@ -1,17 +1,37 @@ package com.direwolf20.justdirethings.client.screens; import com.direwolf20.justdirethings.client.screens.basescreens.BaseMachineScreen; +import com.direwolf20.justdirethings.client.screens.standardbuttons.ToggleButtonFactory; +import com.direwolf20.justdirethings.client.screens.widgets.GrayscaleButton; +import com.direwolf20.justdirethings.common.blockentities.ItemCollectorBE; import com.direwolf20.justdirethings.common.containers.ItemCollectorContainer; +import com.direwolf20.justdirethings.common.network.data.ItemCollectorSettingsPayload; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Inventory; +import net.neoforged.neoforge.network.PacketDistributor; public class ItemCollectorScreen extends BaseMachineScreen { + public boolean respectPickupDelay = false; public ItemCollectorScreen(ItemCollectorContainer container, Inventory inv, Component name) { super(container, inv, name); + if (container.baseMachineBE instanceof ItemCollectorBE itemCollectorBE) { + respectPickupDelay = itemCollectorBE.respectPickupDelay; + } } @Override public void init() { super.init(); + addRenderableWidget(ToggleButtonFactory.RESPECTPICKUPDELAY(getGuiLeft() + 116, topSectionTop + 62, respectPickupDelay, b -> { + respectPickupDelay = !respectPickupDelay; + ((GrayscaleButton) b).toggleActive(); + saveSettings(); + })); + } + + @Override + public void saveSettings() { + super.saveSettings(); + PacketDistributor.sendToServer(new ItemCollectorSettingsPayload(respectPickupDelay)); } } diff --git a/src/main/java/com/direwolf20/justdirethings/client/screens/standardbuttons/ToggleButtonFactory.java b/src/main/java/com/direwolf20/justdirethings/client/screens/standardbuttons/ToggleButtonFactory.java index 6987c8c9..f1ba19ff 100644 --- a/src/main/java/com/direwolf20/justdirethings/client/screens/standardbuttons/ToggleButtonFactory.java +++ b/src/main/java/com/direwolf20/justdirethings/client/screens/standardbuttons/ToggleButtonFactory.java @@ -79,6 +79,13 @@ public static GrayscaleButton COLLECTEXPBUTTON(int x, int y, boolean startingVal return new GrayscaleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, COLLECT_EXP_BUTTON, COLLECT_EXP_BUTTON_LOCALIZATION, startingValue, onPress); } + private static final ResourceLocation RESPECTPICKUP_BUTTON = ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/jumpboost.png"); + private static final Component RESPECTPICKUP_BUTTON_LOCALIZATION = Component.translatable("justdirethings.screen.respectpickupdelay"); + + public static GrayscaleButton RESPECTPICKUPDELAY(int x, int y, boolean startingValue, Button.OnPress onPress) { + return new GrayscaleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, RESPECTPICKUP_BUTTON, RESPECTPICKUP_BUTTON_LOCALIZATION, startingValue, onPress); + } + private static final ResourceLocation EXTRACT_EXP_BUTTON = ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/remove.png"); private static final Component EXTRACT_EXP_BUTTON_LOCALIZATION = Component.translatable("justdirethings.screen.retrieveexp"); diff --git a/src/main/java/com/direwolf20/justdirethings/common/blockentities/ItemCollectorBE.java b/src/main/java/com/direwolf20/justdirethings/common/blockentities/ItemCollectorBE.java index 63d167f3..3100edc0 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/blockentities/ItemCollectorBE.java +++ b/src/main/java/com/direwolf20/justdirethings/common/blockentities/ItemCollectorBE.java @@ -12,6 +12,8 @@ import com.direwolf20.justdirethings.util.interfacehelpers.RedstoneControlData; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.core.HolderLookup; +import net.minecraft.nbt.CompoundTag; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.item.ItemStack; @@ -34,6 +36,7 @@ public class ItemCollectorBE extends BaseMachineBE implements FilterableBE, Area public FilterData filterData = new FilterData(); public AreaAffectingData areaAffectingData = new AreaAffectingData(getBlockState().getValue(BlockStateProperties.FACING).getOpposite()); public RedstoneControlData redstoneControlData = new RedstoneControlData(); + public boolean respectPickupDelay = false; public ItemCollectorBE(BlockPos pPos, BlockState pBlockState) { super(Registration.ItemCollectorBE.get(), pPos, pBlockState); @@ -67,6 +70,11 @@ public void tickServer() { findItemsAndStore(); } + public void setSettings(boolean respectPickupDelay) { + this.respectPickupDelay = respectPickupDelay; + markDirtyClient(); + } + @Override public FilterBasicHandler getFilterHandler() { return getData(Registration.HANDLER_BASIC_FILTER); @@ -97,6 +105,8 @@ private void findItemsAndStore() { if (handler == null) return; for (ItemEntity itemEntity : entityList) { + if (respectPickupDelay && itemEntity.hasPickUpDelay()) + continue; ItemStack stack = itemEntity.getItem(); if (!isStackValidFilter(stack)) continue; ItemStack leftover = ItemHandlerHelper.insertItemStacked(handler, stack, false); @@ -131,4 +141,16 @@ private IItemHandler getAttachedInventory() { public AreaAffectingData getDefaultAreaData(AreaAffectingBE areaAffectingBE) { return areaAffectingBE.getDefaultAreaData(getBlockState().getValue(BlockStateProperties.FACING).getOpposite()); } + + @Override + public void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) { + super.saveAdditional(tag, provider); + tag.putBoolean("respectPickupDelay", respectPickupDelay); + } + + @Override + public void loadAdditional(CompoundTag tag, HolderLookup.Provider provider) { + super.loadAdditional(tag, provider); + respectPickupDelay = tag.getBoolean("respectPickupDelay"); + } } diff --git a/src/main/java/com/direwolf20/justdirethings/common/network/PacketHandler.java b/src/main/java/com/direwolf20/justdirethings/common/network/PacketHandler.java index c35cfea1..5f76d170 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/network/PacketHandler.java +++ b/src/main/java/com/direwolf20/justdirethings/common/network/PacketHandler.java @@ -41,6 +41,7 @@ public static void registerNetworking(final RegisterPayloadHandlersEvent event) registrar.playToServer(ExperienceHolderPayload.TYPE, ExperienceHolderPayload.STREAM_CODEC, ExperienceHolderPacket.get()::handle); registrar.playToServer(ExperienceHolderSettingsPayload.TYPE, ExperienceHolderSettingsPayload.STREAM_CODEC, ExperienceHolderSettingsPacket.get()::handle); registrar.playToServer(ToolSettingsGUIPayload.TYPE, ToolSettingsGUIPayload.STREAM_CODEC, ToolSettingsGUIPacket.get()::handle); + registrar.playToServer(ItemCollectorSettingsPayload.TYPE, ItemCollectorSettingsPayload.STREAM_CODEC, ItemCollectorSettingsPacket.get()::handle); //Going to Client registrar.playToClient(ClientSoundPayload.TYPE, ClientSoundPayload.STREAM_CODEC, ClientSoundPacket.get()::handle); diff --git a/src/main/java/com/direwolf20/justdirethings/common/network/data/ItemCollectorSettingsPayload.java b/src/main/java/com/direwolf20/justdirethings/common/network/data/ItemCollectorSettingsPayload.java new file mode 100644 index 00000000..9dfdf44d --- /dev/null +++ b/src/main/java/com/direwolf20/justdirethings/common/network/data/ItemCollectorSettingsPayload.java @@ -0,0 +1,24 @@ +package com.direwolf20.justdirethings.common.network.data; + +import com.direwolf20.justdirethings.JustDireThings; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; + +public record ItemCollectorSettingsPayload( + boolean respectPickupDelay +) implements CustomPacketPayload { + public static final Type TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "item_collector_settings")); + + @Override + public Type type() { + return TYPE; + } + + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.BOOL, ItemCollectorSettingsPayload::respectPickupDelay, + ItemCollectorSettingsPayload::new + ); +} diff --git a/src/main/java/com/direwolf20/justdirethings/common/network/handler/ItemCollectorSettingsPacket.java b/src/main/java/com/direwolf20/justdirethings/common/network/handler/ItemCollectorSettingsPacket.java new file mode 100644 index 00000000..62348b40 --- /dev/null +++ b/src/main/java/com/direwolf20/justdirethings/common/network/handler/ItemCollectorSettingsPacket.java @@ -0,0 +1,27 @@ +package com.direwolf20.justdirethings.common.network.handler; + +import com.direwolf20.justdirethings.common.blockentities.ItemCollectorBE; +import com.direwolf20.justdirethings.common.containers.ItemCollectorContainer; +import com.direwolf20.justdirethings.common.network.data.ItemCollectorSettingsPayload; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.neoforged.neoforge.network.handling.IPayloadContext; + +public class ItemCollectorSettingsPacket { + public static final ItemCollectorSettingsPacket INSTANCE = new ItemCollectorSettingsPacket(); + + public static ItemCollectorSettingsPacket get() { + return INSTANCE; + } + + public void handle(final ItemCollectorSettingsPayload payload, final IPayloadContext context) { + context.enqueueWork(() -> { + Player sender = context.player(); + AbstractContainerMenu container = sender.containerMenu; + + if (container instanceof ItemCollectorContainer itemCollectorContainer && itemCollectorContainer.baseMachineBE instanceof ItemCollectorBE itemCollectorBE) { + itemCollectorBE.setSettings(payload.respectPickupDelay()); + } + }); + } +} diff --git a/src/main/java/com/direwolf20/justdirethings/datagen/JustDireLanguageProvider.java b/src/main/java/com/direwolf20/justdirethings/datagen/JustDireLanguageProvider.java index f3eda3d5..028186f2 100644 --- a/src/main/java/com/direwolf20/justdirethings/datagen/JustDireLanguageProvider.java +++ b/src/main/java/com/direwolf20/justdirethings/datagen/JustDireLanguageProvider.java @@ -533,6 +533,7 @@ protected void addTranslations() { add("justdirethings.screen.collectexp", "Collect Experience"); add("justdirethings.screen.pushfluids", "Push Fluids"); add("justdirethings.screen.pullfluids", "Pull Fluids"); + add("justdirethings.screen.respectpickupdelay", "Respect Item Pickup Delay"); //Buttons //add("justdirethings.buttons.save", "Save");