Skip to content

Commit

Permalink
feat: make input slots of Bio-Lab lockable
Browse files Browse the repository at this point in the history
  • Loading branch information
Elenterius committed Oct 14, 2024
1 parent 747d96f commit f67d7ce
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class BioForgeBlockEntity extends BlockEntity implements MenuProvider, Pl

private final BioForgeStateData stateData;
private final FuelHandler fuelHandler;
private final InventoryHandler fuelInventory;
private final InventoryHandler<?> fuelInventory;

private final ContainerOpenersCounter openersCounter = new ContainerOpenersCounter() {

Expand Down Expand Up @@ -150,7 +150,7 @@ public AbstractContainerMenu createMenu(int containerId, Inventory playerInvento
return BioForgeMenu.createServerMenu(containerId, playerInventory, this);
}

public InventoryHandler getFuelInventory() {
public InventoryHandler<?> getFuelInventory() {
return fuelInventory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
import com.github.elenterius.biomancy.init.ModCapabilities;
import com.github.elenterius.biomancy.init.ModRecipes;
import com.github.elenterius.biomancy.init.ModSoundEvents;
import com.github.elenterius.biomancy.inventory.BehavioralItemHandler;
import com.github.elenterius.biomancy.inventory.InventoryHandler;
import com.github.elenterius.biomancy.inventory.InventoryHandlers;
import com.github.elenterius.biomancy.inventory.ItemHandlerUtil;
import com.github.elenterius.biomancy.menu.BioLabMenu;
import com.github.elenterius.biomancy.styles.TextComponentUtil;
import com.github.elenterius.biomancy.util.ILoopingSoundHelper;
import com.github.elenterius.biomancy.util.ItemStackFilter;
import com.github.elenterius.biomancy.util.ItemStackFilterList;
import com.github.elenterius.biomancy.util.SoundUtil;
import com.github.elenterius.biomancy.util.fuel.FluidFuelConsumerHandler;
import com.github.elenterius.biomancy.util.fuel.FuelHandler;
Expand Down Expand Up @@ -69,12 +68,11 @@ public class BioLabBlockEntity extends MachineBlockEntity<BioLabRecipe, BioLabSt

private final BioLabStateData stateData;
private final FuelHandler fuelHandler;
private final InventoryHandler fuelInventory;
private final InventoryHandler<?> fuelInventory;

private final InventoryHandler inputInventory;
private final ItemStackFilterList inputSlotsFilter;
private final InventoryHandler<BehavioralItemHandler.LockableItemStackFilterInput> inputInventory;

private final InventoryHandler outputInventory;
private final InventoryHandler<?> outputInventory;

private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private ILoopingSoundHelper loopingSoundHelper = ILoopingSoundHelper.NULL;
Expand All @@ -85,8 +83,7 @@ public class BioLabBlockEntity extends MachineBlockEntity<BioLabRecipe, BioLabSt
public BioLabBlockEntity(BlockPos worldPosition, BlockState blockState) {
super(ModBlockEntities.BIO_LAB.get(), worldPosition, blockState);

inputSlotsFilter = ItemStackFilterList.of(ItemStackFilter.ALLOW_ALL, INPUT_SLOTS);
inputInventory = InventoryHandlers.filterInput(INPUT_SLOTS, inputSlotsFilter, this::onInventoryChanged);
inputInventory = InventoryHandlers.lockableFilterInput(INPUT_SLOTS, this::onInventoryChanged);

outputInventory = InventoryHandlers.denyInput(OUTPUT_SLOTS, this::onInventoryChanged);

Expand All @@ -95,8 +92,9 @@ public BioLabBlockEntity(BlockPos worldPosition, BlockState blockState) {
optionalCombinedInventory = createCombinedInventory();

fuelHandler = FuelHandler.createNutrientFuelHandler(MAX_FUEL, this::setChanged);
stateData = new BioLabStateData(fuelHandler);
optionalFluidConsumer = LazyOptional.of(() -> new FluidFuelConsumerHandler(fuelHandler));

stateData = new BioLabStateData(fuelHandler, inputInventory.get());
}

private LazyOptional<IItemHandler> createCombinedInventory() {
Expand Down Expand Up @@ -135,15 +133,15 @@ public BioLabStateData getStateData() {
}

@Override
public InventoryHandler getInputInventory() {
public InventoryHandler<BehavioralItemHandler.LockableItemStackFilterInput> getInputInventory() {
return inputInventory;
}

public InventoryHandler getFuelInventory() {
public InventoryHandler<?> getFuelInventory() {
return fuelInventory;
}

public InventoryHandler getOutputInventory() {
public InventoryHandler<?> getOutputInventory() {
return outputInventory;
}

Expand Down Expand Up @@ -185,7 +183,6 @@ protected void saveAdditional(CompoundTag tag) {
tag.put("Fuel", fuelHandler.serializeNBT());
tag.put("FuelSlots", fuelInventory.serializeNBT());
tag.put("InputSlots", inputInventory.serializeNBT());
tag.put("InputSlotsFilter", inputSlotsFilter.serializeNBT());
tag.put("OutputSlots", outputInventory.serializeNBT());
}

Expand All @@ -196,7 +193,6 @@ public void load(CompoundTag tag) {
fuelHandler.deserializeNBT(tag.getCompound("Fuel"));
fuelInventory.deserializeNBT(tag.getCompound("FuelSlots"));
inputInventory.deserializeNBT(tag.getCompound("InputSlots"));
inputSlotsFilter.deserializeNBT(tag.getCompound("InputSlotsFilter"));
outputInventory.deserializeNBT(tag.getCompound("OutputSlots"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,48 @@

import com.github.elenterius.biomancy.crafting.recipe.BioLabRecipe;
import com.github.elenterius.biomancy.crafting.state.FuelConsumingRecipeCraftingStateData;
import com.github.elenterius.biomancy.inventory.BehavioralItemHandler;
import com.github.elenterius.biomancy.util.fuel.IFuelHandler;
import net.minecraft.world.item.crafting.Recipe;

public class BioLabStateData extends FuelConsumingRecipeCraftingStateData<BioLabRecipe> {

public BioLabStateData(IFuelHandler fuelHandler) {
public static final int LOCK_INDEX = 4;

private final BehavioralItemHandler.LockableItemStackFilterInput inputFilterLock;

public BioLabStateData(IFuelHandler fuelHandler, BehavioralItemHandler.LockableItemStackFilterInput inputFilterLock) {
super(fuelHandler);
this.inputFilterLock = inputFilterLock;
}

@Override
protected boolean isRecipeOfInstance(Recipe<?> recipe) {
return recipe instanceof BioLabRecipe;
}

public boolean isFilterLocked() {
return inputFilterLock.isLocked();
}

@Override
public int get(int index) {
if (index == LOCK_INDEX) return inputFilterLock.isLocked() ? 1 : 0;
return super.get(index);
}

@Override
public void set(int index, int value) {
if (index == LOCK_INDEX) {
inputFilterLock.setLocked(value != 0);
return;
}
super.set(index, value);
}

@Override
public int getCount() {
return 5;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public class DecomposerBlockEntity extends MachineBlockEntity<DecomposerRecipe,

private final DecomposerStateData stateData;
private final FuelHandler fuelHandler;
private final InventoryHandler fuelInventory;
private final InventoryHandler inputInventory;
private final InventoryHandler outputInventory;
private final InventoryHandler<?> fuelInventory;
private final InventoryHandler<?> inputInventory;
private final InventoryHandler<?> outputInventory;

private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private ILoopingSoundHelper loopingSoundHelper = ILoopingSoundHelper.NULL;
Expand Down Expand Up @@ -123,15 +123,15 @@ public DecomposerStateData getStateData() {
}

@Override
public InventoryHandler getInputInventory() {
public InventoryHandler<?> getInputInventory() {
return inputInventory;
}

public InventoryHandler getFuelInventory() {
public InventoryHandler<?> getFuelInventory() {
return fuelInventory;
}

public InventoryHandler getOutputInventory() {
public InventoryHandler<?> getOutputInventory() {
return outputInventory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public class DigesterBlockEntity extends MachineBlockEntity<DigestingRecipe, Dig

private final DigesterStateData stateData;
private final FuelHandler fuelHandler;
private final InventoryHandler fuelInventory;
private final InventoryHandler inputInventory;
private final InventoryHandler outputInventory;
private final InventoryHandler<?> fuelInventory;
private final InventoryHandler<?> inputInventory;
private final InventoryHandler<?> outputInventory;

private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private ILoopingSoundHelper loopingSoundHelper = ILoopingSoundHelper.NULL;
Expand Down Expand Up @@ -116,15 +116,15 @@ public DigesterStateData getStateData() {
}

@Override
public InventoryHandler getInputInventory() {
public InventoryHandler<?> getInputInventory() {
return inputInventory;
}

public InventoryHandler getFuelInventory() {
public InventoryHandler<?> getFuelInventory() {
return fuelInventory;
}

public InventoryHandler getOutputInventory() {
public InventoryHandler<?> getOutputInventory() {
return outputInventory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class FleshkinChestBlockEntity extends OwnableContainerBlockEntity implem

public static final int SLOTS = 6 * 7;

private final InventoryHandler inventory;
private final InventoryHandler<?> inventory;

private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);

Expand Down Expand Up @@ -149,7 +149,7 @@ public AbstractContainerMenu createMenu(int containerId, Inventory playerInvento
return FleshkinChestMenu.createServerMenu(containerId, playerInventory, this);
}

public InventoryHandler getInventory() {
public InventoryHandler<?> getInventory() {
return inventory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class StorageSacBlockEntity extends SimpleContainerBlockEntity implements
public static final int SLOTS = 3 * 5;
public static final String TOP5_BY_COUNT_KEY = "Top5ByCount";
public static final String INVENTORY_KEY = "Inventory";
private final InventoryHandler inventory;
private final InventoryHandler<?> inventory;
protected final ItemStackCounter itemCounter = new ItemStackCounter();

private List<ItemStackCounter.CountedItem> top5ItemsByCount = List.of();
Expand All @@ -54,7 +54,7 @@ public AbstractContainerMenu createMenu(int containerId, Inventory playerInvento
return StorageSacMenu.createServerMenu(containerId, playerInventory, this);
}

public InventoryHandler getInventory() {
public InventoryHandler<?> getInventory() {
return inventory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ protected void init() {
super.init();
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
int x = leftPos + 130;
int y = topPos + 34;
if (GuiUtil.isInRect(x, y, 8, 10, mouseX, mouseY)) {
minecraft.gameMode.handleInventoryButtonClick(menu.containerId, 0);
return true;
}

return super.mouseClicked(mouseX, mouseY, button);
}

@Override
protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {
//don't draw any labels
Expand All @@ -43,6 +55,7 @@ protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX,
guiGraphics.blit(BACKGROUND_TEXTURE, leftPos, topPos, 0, 0, imageWidth, imageHeight);
drawProgressBar(guiGraphics, menu.getCraftingProgressNormalized());
drawFuelBar(guiGraphics, menu.getFuelAmountNormalized());
drawLock(guiGraphics, menu.isFilterLocked());
}

private void drawProgressBar(GuiGraphics guiGraphics, float craftingPct) {
Expand All @@ -55,6 +68,11 @@ private void drawFuelBar(GuiGraphics guiGraphics, float fuelPct) {
guiGraphics.blit(BACKGROUND_TEXTURE, leftPos + 36, topPos + 48 + 36 - vHeight, 178, 58 - vHeight, 5, vHeight);
}

private void drawLock(GuiGraphics guiGraphics, boolean isLocked) {
int offset = isLocked ? 0 : 8;
guiGraphics.blit(BACKGROUND_TEXTURE, leftPos + 130, topPos + 34, 178 + offset, 58 + 2, 8, 10);
}

@Override
protected void renderTooltip(GuiGraphics guiGraphics, int mouseX, int mouseY) {
if (menu.getCarried().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
@Mod.EventBusSubscriber(modid = BiomancyMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public final class ModCapabilities {

@Deprecated
public static final Capability<FlagCapImpl> NO_KNOCKBACK_FLAG_CAP = CapabilityManager.get(new CapabilityToken<>() {});

public static final Capability<IItemHandler> ITEM_HANDLER = ForgeCapabilities.ITEM_HANDLER;
public static final Capability<IFluidHandler> FLUID_HANDLER = ForgeCapabilities.FLUID_HANDLER;

Expand Down
Loading

0 comments on commit f67d7ce

Please sign in to comment.