Skip to content

Commit

Permalink
Item Collector now has a respect pickup delay option as requested in #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Oct 19, 2024
1 parent c6c1458 commit 287be95
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ItemCollectorContainer> {
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ItemCollectorSettingsPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "item_collector_settings"));

@Override
public Type<ItemCollectorSettingsPayload> type() {
return TYPE;
}

public static final StreamCodec<FriendlyByteBuf, ItemCollectorSettingsPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.BOOL, ItemCollectorSettingsPayload::respectPickupDelay,
ItemCollectorSettingsPayload::new
);
}
Original file line number Diff line number Diff line change
@@ -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());
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 287be95

Please sign in to comment.