Skip to content

Commit

Permalink
Sided Handlers working i think!
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Sep 8, 2024
1 parent 0b24b8f commit 12f854a
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21.1 2024-09-07T23:32:11.7388079 Languages: en_us for mod: justdirethings
6df045e49de32b41b0c0ae7b9f14dbd59d6042d3 assets/justdirethings/lang/en_us.json
// 1.21.1 2024-09-08T10:48:40.1210526 Languages: en_us for mod: justdirethings
d5878f400337646026ccd4b5a26fd0191197b510 assets/justdirethings/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,7 @@
"justdirethings.screen.fepertick": "FE/T: %s",
"justdirethings.screen.filter-block": "Filter: Block",
"justdirethings.screen.filter-item": "Filter: Item",
"justdirethings.screen.filteronlyfalse": "Allow any slots",
"justdirethings.screen.filteronlytrue": "Only Allow Filters",
"justdirethings.screen.filteronlytrue": "Filtered Items Only",
"justdirethings.screen.fluid": "%s: %s/%s MB",
"justdirethings.screen.greaterthan": "Greater Than",
"justdirethings.screen.hiderender": "Hide Render",
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/direwolf20/justdirethings/JustDireThings.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.direwolf20.justdirethings;

import com.direwolf20.justdirethings.common.blockentities.EnergyTransmitterBE;
import com.direwolf20.justdirethings.common.blockentities.InventoryHolderBE;
import com.direwolf20.justdirethings.common.blockentities.ParadoxMachineBE;
import com.direwolf20.justdirethings.common.blockentities.PlayerAccessorBE;
import com.direwolf20.justdirethings.common.blockentities.basebe.BaseMachineBE;
Expand Down Expand Up @@ -197,7 +198,14 @@ public boolean canFillFluidType(FluidStack fluid) {
Registration.FluidPlacerT1.get(),
Registration.FluidPlacerT2.get(),
Registration.FluidCollectorT1.get(),
Registration.FluidCollectorT2.get(),
Registration.FluidCollectorT2.get()
);
event.registerBlock(Capabilities.ItemHandler.BLOCK,
(level, pos, state, be, side) -> {
if (be instanceof InventoryHolderBE inventoryHolderBE)
return inventoryHolderBE.getInventoryHolderHandler();
return null;
},
Registration.InventoryHolder.get()
);
event.registerBlock(Capabilities.ItemHandler.BLOCK,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class InventoryHolderScreen extends BaseMachineScreen<InventoryHolderCont
private boolean compareNBT;
private boolean filtersOnly;
private boolean compareCounts;
private boolean automatedFiltersOnly;
private boolean automatedCompareCounts;

public InventoryHolderScreen(InventoryHolderContainer container, Inventory inv, Component name) {
super(container, inv, name);
Expand All @@ -34,6 +36,8 @@ public InventoryHolderScreen(InventoryHolderContainer container, Inventory inv,
this.compareNBT = inventoryHolderBE.compareNBT;
this.filtersOnly = inventoryHolderBE.filtersOnly;
this.compareCounts = inventoryHolderBE.compareCounts;
this.automatedFiltersOnly = inventoryHolderBE.automatedFiltersOnly;
this.automatedCompareCounts = inventoryHolderBE.automatedCompareCounts;
}
}

Expand All @@ -47,14 +51,15 @@ public void init() {
super.init();
addRenderableWidget(ToggleButtonFactory.FILTERONLYBUTTON(getGuiLeft() + 134, topSectionTop + 22, filtersOnly, b -> {
filtersOnly = !filtersOnly;
((GrayscaleButton) b).toggleActive();
saveSettings();
}));
addRenderableWidget(ToggleButtonFactory.COMPARENBTBUTTON(getGuiLeft() + 152, topSectionTop + 22, compareNBT, b -> {
compareNBT = !compareNBT;
((GrayscaleButton) b).toggleActive();
saveSettings();
}));
addRenderableWidget(ToggleButtonFactory.COMPARECOUNTSBUTTON(getGuiLeft() + 152, topSectionTop + 4, compareCounts, b -> {
addRenderableWidget(ToggleButtonFactory.COMPARECOUNTSBUTTON(getGuiLeft() + 134, topSectionTop + 4, compareCounts, b -> {
compareCounts = !compareCounts;
((GrayscaleButton) b).toggleActive();
saveSettings();
Expand All @@ -68,6 +73,16 @@ public void init() {
addRenderableWidget(ToggleButtonFactory.SWAP_INV_BUTTON(getGuiLeft() + 152, topSectionTop + 132, b -> {
PacketDistributor.sendToServer(new InventoryHolderMoveItemsPayload(2));
}));
addRenderableWidget(ToggleButtonFactory.FILTERONLYBUTTON(getGuiLeft() + 26, topSectionTop + 22, automatedFiltersOnly, b -> {
automatedFiltersOnly = !automatedFiltersOnly;
((GrayscaleButton) b).toggleActive();
saveSettings();
}));
addRenderableWidget(ToggleButtonFactory.COMPARECOUNTSBUTTON(getGuiLeft() + 26, topSectionTop + 4, automatedCompareCounts, b -> {
automatedCompareCounts = !automatedCompareCounts;
((GrayscaleButton) b).toggleActive();
saveSettings();
}));
}

@Override
Expand Down Expand Up @@ -113,7 +128,7 @@ protected void renderTooltip(GuiGraphics guiGraphics, int x, int y) {
@Override
public void saveSettings() {
super.saveSettings();
PacketDistributor.sendToServer(new InventoryHolderSettingsPayload(compareNBT, filtersOnly, compareCounts));
PacketDistributor.sendToServer(new InventoryHolderSettingsPayload(compareNBT, filtersOnly, compareCounts, automatedFiltersOnly, automatedCompareCounts));
}

public void renderInventorySection(GuiGraphics guiGraphics, int relX, int relY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,11 @@ public static ToggleButton ALLOWLISTBUTTON(int x, int y, boolean startingValue,
return new ToggleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, ALLOW_LIST_TEXTURES, startingValue, onPress);
}

/** Filter Only Button **/
private static final List<TextureLocalization> FILTER_ONLY_TEXTURES = List.of(
new TextureLocalization(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/allowlistfalse.png"), Component.translatable("justdirethings.screen.filteronlyfalse")),
new TextureLocalization(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/allowlisttrue.png"), Component.translatable("justdirethings.screen.filteronlytrue"))
);
private static final ResourceLocation FILTER_ONLY = ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/allowlisttrue.png");
private static final Component FILTER_ONLY_LOCALIZATION = Component.translatable("justdirethings.screen.filteronlytrue");

public static ToggleButton FILTERONLYBUTTON(int x, int y, boolean startingValue, Button.OnPress onPress) {
return new ToggleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, FILTER_ONLY_TEXTURES, startingValue, onPress);
public static GrayscaleButton FILTERONLYBUTTON(int x, int y, boolean startingValue, Button.OnPress onPress) {
return new GrayscaleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, FILTER_ONLY, FILTER_ONLY_LOCALIZATION, startingValue, onPress);
}

private static final ResourceLocation COMPARE_NBT = ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/matchnbttrue.png");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package com.direwolf20.justdirethings.common.blockentities;

import com.direwolf20.justdirethings.common.blockentities.basebe.BaseMachineBE;
import com.direwolf20.justdirethings.common.capabilities.InventoryHolderItemHandler;
import com.direwolf20.justdirethings.common.containers.handlers.FilterBasicHandler;
import com.direwolf20.justdirethings.setup.Registration;
import com.direwolf20.justdirethings.util.ItemStackKey;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.items.ItemStackHandler;

public class InventoryHolderBE extends BaseMachineBE {
public FilterBasicHandler filterBasicHandler = new FilterBasicHandler(41);
public boolean compareNBT = false;
public boolean filtersOnly = false;
public boolean automatedFiltersOnly = false;
public boolean compareCounts = false;
public boolean automatedCompareCounts = false;
public InventoryHolderBE(BlockEntityType<?> pType, BlockPos pPos, BlockState pBlockState) {
super(pType, pPos, pBlockState);
MACHINE_SLOTS = 41; //Hotbar, Inventory, Armor, and Offhand
Expand All @@ -30,13 +35,51 @@ public void addSavedItem(int slot) {
markDirtyClient();
}

public void saveSettings(boolean compareNBT, boolean filtersOnly, boolean compareCounts) {
public void saveSettings(boolean compareNBT, boolean filtersOnly, boolean compareCounts, boolean automatedFiltersOnly, boolean automatedCompareCounts) {
this.compareNBT = compareNBT;
this.filtersOnly = filtersOnly;
this.compareCounts = compareCounts;
this.automatedFiltersOnly = automatedFiltersOnly;
this.automatedCompareCounts = automatedCompareCounts;
markDirtyClient();
}

public ItemStackHandler getInventoryHolderHandler() {
return new InventoryHolderItemHandler(this, getMachineHandler());
}

public int allowedExtractAmount(int slot, int amount) {
ItemStack stack = filterBasicHandler.getStackInSlot(slot);
if (stack.isEmpty()) {
return amount;
}
if (automatedCompareCounts) {
int amountDesired = getSlotLimit(slot);
int amountHad = getMachineHandler().getStackInSlot(slot).getCount();
if (amountDesired > amountHad)
return 0;
return Math.min(amount, amountHad - amountDesired);
}
return 0;
}

public boolean isStackValidFilter(ItemStack testStack, int slot) {
ItemStackKey key = new ItemStackKey(testStack, compareNBT);
ItemStack stack = filterBasicHandler.getStackInSlot(slot);
if (stack.isEmpty()) {
return !automatedFiltersOnly;
}
return key.equals(new ItemStackKey(stack, compareNBT));
}

public int getSlotLimit(int slot) {
if (!automatedCompareCounts)
return -1;
ItemStack stack = filterBasicHandler.getStackInSlot(slot);
if (stack.isEmpty()) return -1;
return stack.getCount();
}

@Override
public boolean isDefaultSettings() {
return true;
Expand All @@ -48,6 +91,8 @@ public void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) {
tag.putBoolean("compareNBT", compareNBT);
tag.putBoolean("filtersOnly", filtersOnly);
tag.putBoolean("compareCounts", compareCounts);
tag.putBoolean("automatedFiltersOnly", automatedFiltersOnly);
tag.putBoolean("automatedCompareCounts", automatedCompareCounts);
// Create a new CompoundTag to hold all saved items
tag.put("filteredItems", filterBasicHandler.serializeNBT(provider));
}
Expand All @@ -58,6 +103,8 @@ public void loadAdditional(CompoundTag tag, HolderLookup.Provider provider) {
compareNBT = tag.getBoolean("compareNBT");
filtersOnly = tag.getBoolean("filtersOnly");
compareCounts = tag.getBoolean("compareCounts");
automatedFiltersOnly = tag.getBoolean("automatedFiltersOnly");
automatedCompareCounts = tag.getBoolean("automatedCompareCounts");
if (tag.contains("filteredItems")) {
CompoundTag filteredItems = tag.getCompound("filteredItems");
filterBasicHandler.deserializeNBT(provider, filteredItems);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.direwolf20.justdirethings.common.capabilities;

import com.direwolf20.justdirethings.common.blockentities.InventoryHolderBE;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.items.ItemStackHandler;
import org.jetbrains.annotations.NotNull;

public class InventoryHolderItemHandler extends ItemStackHandler {
InventoryHolderBE inventoryHolderBE;
ItemStackHandler sourceHandler;

public InventoryHolderItemHandler(InventoryHolderBE inventoryHolderBE, ItemStackHandler sourceHandler) {
super(sourceHandler.getSlots());
this.inventoryHolderBE = inventoryHolderBE;
this.sourceHandler = sourceHandler;
}

@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (inventoryHolderBE == null) return ItemStack.EMPTY;
int amountAllowed = inventoryHolderBE.allowedExtractAmount(slot, amount);
return sourceHandler.extractItem(slot, amountAllowed, simulate);
}

@Override
public @NotNull ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
if (stack.isEmpty())
return ItemStack.EMPTY;

if (!isItemValid(slot, stack))
return stack;

validateSlotIndex(slot);

ItemStack existing = sourceHandler.getStackInSlot(slot);

int limit = getStackLimit(slot, stack);

if (!existing.isEmpty()) {
if (!ItemStack.isSameItemSameComponents(stack, existing))
return stack;

limit -= existing.getCount();
}

if (limit <= 0)
return stack;

boolean reachedLimit = stack.getCount() > limit;

if (!simulate) {
if (existing.isEmpty()) {
sourceHandler.setStackInSlot(slot, reachedLimit ? stack.copyWithCount(limit) : stack);
} else {
existing.grow(reachedLimit ? limit : stack.getCount());
}
onContentsChanged(slot);
}

return reachedLimit ? stack.copyWithCount(stack.getCount() - limit) : ItemStack.EMPTY;
}

@Override
public boolean isItemValid(int slot, ItemStack itemStack) {
if (inventoryHolderBE == null) return false;
if (inventoryHolderBE.automatedFiltersOnly)
return inventoryHolderBE.isStackValidFilter(itemStack, slot);
return sourceHandler.isItemValid(slot, itemStack);
}

@Override
public int getSlotLimit(int slot) {
if (inventoryHolderBE == null) return 0;
int allowedAmt = inventoryHolderBE.getSlotLimit(slot);
if (allowedAmt == -1)
return Item.ABSOLUTE_MAX_STACK_SIZE;
return allowedAmt;
}

@Override
protected int getStackLimit(int slot, ItemStack stack) {
if (inventoryHolderBE == null) return 0;
int allowedAmt = inventoryHolderBE.getSlotLimit(slot);
if (allowedAmt == -1)
return Math.min(getSlotLimit(slot), stack.getMaxStackSize());
return Math.min(allowedAmt, stack.getMaxStackSize());
}

@Override
public int getSlots() {
return sourceHandler.getSlots();
}

@Override
public @NotNull ItemStack getStackInSlot(int i) {
return sourceHandler.getStackInSlot(i);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public ItemStack quickMoveStack(Player playerIn, int index) {
if (index < MACHINE_SLOTS) { //Machine Slots to Player Inventory
if (this.moveItemStackTo(currentStack, index + MACHINE_SLOTS, index + MACHINE_SLOTS + 1, false)) {
//No-Op
} else if (!this.moveItemStackTo(currentStack, MACHINE_SLOTS, MACHINE_SLOTS + Inventory.INVENTORY_SIZE + 5, true)) {
} else if (!this.moveItemStackTo(currentStack, MACHINE_SLOTS, MACHINE_SLOTS + Inventory.INVENTORY_SIZE + 4, true)) { //This is +4 to avoid going into offhand slot
return ItemStack.EMPTY;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
public record InventoryHolderSettingsPayload(
boolean compareNBT,
boolean filtersOnly,
boolean compareCounts
boolean compareCounts,
boolean automatedFiltersOnly,
boolean automatedCompareCounts
) implements CustomPacketPayload {
public static final Type<InventoryHolderSettingsPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "inventory_holder_settings"));

Expand All @@ -23,6 +25,8 @@ public Type<InventoryHolderSettingsPayload> type() {
ByteBufCodecs.BOOL, InventoryHolderSettingsPayload::compareNBT,
ByteBufCodecs.BOOL, InventoryHolderSettingsPayload::filtersOnly,
ByteBufCodecs.BOOL, InventoryHolderSettingsPayload::compareCounts,
ByteBufCodecs.BOOL, InventoryHolderSettingsPayload::automatedFiltersOnly,
ByteBufCodecs.BOOL, InventoryHolderSettingsPayload::automatedCompareCounts,
InventoryHolderSettingsPayload::new
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void handle(final InventoryHolderSettingsPayload payload, final IPayloadC
AbstractContainerMenu container = sender.containerMenu;

if (container instanceof BaseMachineContainer baseMachineContainer && baseMachineContainer.baseMachineBE instanceof InventoryHolderBE inventoryHolderBE) {
inventoryHolderBE.saveSettings(payload.compareNBT(), payload.filtersOnly(), payload.compareCounts());
inventoryHolderBE.saveSettings(payload.compareNBT(), payload.filtersOnly(), payload.compareCounts(), payload.automatedFiltersOnly(), payload.automatedCompareCounts());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ protected void addTranslations() {
add("justdirethings.screen.pulse", "Pulse");
add("justdirethings.screen.allowlist", "Allow List");
add("justdirethings.screen.denylist", "Deny List");
add("justdirethings.screen.filteronlyfalse", "Allow any slots");
add("justdirethings.screen.filteronlytrue", "Only Allow Filters");
add("justdirethings.screen.filteronlytrue", "Filtered Items Only");
add("justdirethings.screen.renderarea", "Render Area");
add("justdirethings.screen.comparenbt", "Compare NBT");
add("justdirethings.screen.comparecounts", "Compare Stack Sizes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,12 @@ public static void init(IEventBus eventBus) {
return new GeneratorItemHandler(baseMachineBE.MACHINE_SLOTS);
return new GeneratorItemHandler(1);
}).build());
/*public static final Supplier<AttachmentType<InventoryHolderItemHandler>> INVENTORY_HOLDER_ITEM_HANDLER = ATTACHMENT_TYPES.register(
"inventory_holder_item_handler", () -> AttachmentType.serializable(holder -> {
if (holder instanceof InventoryHolderBE inventoryHolderBE)
return new InventoryHolderItemHandler(inventoryHolderBE.MACHINE_SLOTS, inventoryHolderBE);
return new InventoryHolderItemHandler(1);
}).build());*/
public static final Supplier<AttachmentType<GeneratorFluidItemHandler>> GENERATOR_FLUID_ITEM_HANDLER = ATTACHMENT_TYPES.register(
"generator_fluid_item_handler", () -> AttachmentType.serializable(holder -> {
if (holder instanceof BaseMachineBE baseMachineBE)
Expand Down

0 comments on commit 12f854a

Please sign in to comment.