From d51ae3e0b76826c962d224725b258c6095aa3867 Mon Sep 17 00:00:00 2001 From: bruberu <80226372+bruberu@users.noreply.github.com> Date: Mon, 16 Aug 2021 21:46:42 -0500 Subject: [PATCH] Add Highlighting to the Crafting Station (#75) --- .../CraftingStationInputWidgetGroup.java | 70 +++++++++++++++++++ .../sources/InventoryItemSource.java | 21 +----- .../storage/CachedRecipeData.java | 19 ++--- .../storage/CraftingRecipeResolver.java | 12 ++-- .../storage/MetaTileEntityWorkbench.java | 7 +- 5 files changed, 91 insertions(+), 38 deletions(-) create mode 100644 src/main/java/gregtech/api/gui/widgets/CraftingStationInputWidgetGroup.java diff --git a/src/main/java/gregtech/api/gui/widgets/CraftingStationInputWidgetGroup.java b/src/main/java/gregtech/api/gui/widgets/CraftingStationInputWidgetGroup.java new file mode 100644 index 00000000000..1d9b24efa69 --- /dev/null +++ b/src/main/java/gregtech/api/gui/widgets/CraftingStationInputWidgetGroup.java @@ -0,0 +1,70 @@ +package gregtech.api.gui.widgets; + +import gregtech.api.gui.GuiTextures; +import gregtech.api.gui.IRenderContext; +import gregtech.api.gui.Widget; +import gregtech.api.util.Position; +import gregtech.common.metatileentities.storage.CraftingRecipeResolver; +import net.minecraft.network.PacketBuffer; +import net.minecraftforge.items.ItemStackHandler; + +public class CraftingStationInputWidgetGroup extends AbstractWidgetGroup { + protected CraftingRecipeResolver recipeResolver; + protected short tintLocations; + public static final int LIGHT_RED = 0x66FF0000; + + public CraftingStationInputWidgetGroup(int x, int y, ItemStackHandler craftingGrid, CraftingRecipeResolver recipeResolver) { + super(new Position(x, y)); + + //crafting grid + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + this.addWidget(new PhantomSlotWidget(craftingGrid, j + i * 3, x + j * 18, y + i * 18).setBackgroundTexture(GuiTextures.SLOT)); + } + } + + this.recipeResolver = recipeResolver; + } + + @Override + public void drawInBackground(int mouseX, int mouseY, IRenderContext context) { + super.drawInBackground(mouseX, mouseY, context); + if(this.widgets.size() == 9) { // In case someone added more... + for (int i = 0; i < 9; i++) { + Widget widget = widgets.get(i); + if (widget instanceof PhantomSlotWidget && ((tintLocations >> i) & 1) == 0) { // In other words, is this slot usable? + int color = LIGHT_RED; + + PhantomSlotWidget phantomSlotWidget = (PhantomSlotWidget) widget; + drawSolidRect(phantomSlotWidget.getPosition().x, phantomSlotWidget.getPosition().y, + phantomSlotWidget.getSize().getWidth() - 1, phantomSlotWidget.getSize().getWidth() - 1, color); + } + } + } + } + + @Override + public void detectAndSendChanges() { + super.detectAndSendChanges(); + short newTintLocations = getTintLocations(); + if (tintLocations != newTintLocations) { + this.tintLocations = newTintLocations; + writeUpdateInfo(2, buffer -> buffer.writeShort(tintLocations)); + } + } + + private short getTintLocations() { + if(recipeResolver.getCachedRecipeData() != null) { + return recipeResolver.getCachedRecipeData().attemptMatchRecipe(); + } else { + return 511; + } + } + + public void readUpdateInfo(int id, PacketBuffer buffer) { + super.readUpdateInfo(id, buffer); + if (id == 2) { + tintLocations = buffer.readShort(); + } + } +} diff --git a/src/main/java/gregtech/common/inventory/itemsource/sources/InventoryItemSource.java b/src/main/java/gregtech/common/inventory/itemsource/sources/InventoryItemSource.java index 92da85fc946..258680e467d 100644 --- a/src/main/java/gregtech/common/inventory/itemsource/sources/InventoryItemSource.java +++ b/src/main/java/gregtech/common/inventory/itemsource/sources/InventoryItemSource.java @@ -21,9 +21,6 @@ public abstract class InventoryItemSource extends ItemSource { private StoredItemsChangeCallback changeCallback = null; protected IItemHandler itemHandler = EmptyHandler.INSTANCE; private Map itemStackByAmountMap = new HashMap<>(); - private long lastItemHandlerUpdateTick = -1L; - private long lastStoredItemListUpdateTick = -1L; - private boolean cachedRefreshResult = false; public InventoryItemSource(World world, int priority) { this.world = world; @@ -57,13 +54,11 @@ public void setStoredItemsChangeCallback(StoredItemsChangeCallback callback) { } private boolean refreshItemHandler(boolean simulated) { - this.lastItemHandlerUpdateTick = world.getTotalWorldTime(); IItemHandler newItemHandler = computeItemHandler(); if (newItemHandler == null) { if (!simulated && invalidationCallback != null) { invalidationCallback.run(); } - this.cachedRefreshResult = false; return false; } if (!newItemHandler.equals(itemHandler) || newItemHandler.getSlots() != itemHandler.getSlots()) { @@ -71,29 +66,18 @@ private boolean refreshItemHandler(boolean simulated) { if (!simulated) { recomputeItemStackCount(); } - this.cachedRefreshResult = false; return false; } - this.cachedRefreshResult = true; return true; } @Override public UpdateResult update() { - //update stored item list once a second - long currentTick = world.getTotalWorldTime(); - if (currentTick - lastStoredItemListUpdateTick >= 20) { - return recomputeItemStackCount() ? UpdateResult.CHANGED : UpdateResult.STANDBY; - } - return UpdateResult.STANDBY; + return recomputeItemStackCount() ? UpdateResult.CHANGED : UpdateResult.STANDBY; } private boolean checkItemHandlerValid(boolean simulated) { - long currentUpdateTick = world.getTotalWorldTime(); - if (currentUpdateTick != lastItemHandlerUpdateTick) { - return refreshItemHandler(simulated); - } - return cachedRefreshResult; + return refreshItemHandler(simulated); } /** @@ -150,7 +134,6 @@ private boolean recomputeItemStackCount() { if (!checkItemHandlerValid(false)) { return false; } - this.lastStoredItemListUpdateTick = world.getTotalWorldTime(); HashMap amountMap = new HashMap<>(); for (int i = 0; i < itemHandler.getSlots(); i++) { ItemStack itemStack = itemHandler.getStackInSlot(i); diff --git a/src/main/java/gregtech/common/metatileentities/storage/CachedRecipeData.java b/src/main/java/gregtech/common/metatileentities/storage/CachedRecipeData.java index f1830831f9f..0c4b39775c3 100755 --- a/src/main/java/gregtech/common/metatileentities/storage/CachedRecipeData.java +++ b/src/main/java/gregtech/common/metatileentities/storage/CachedRecipeData.java @@ -61,15 +61,17 @@ public boolean performRecipe(EntityPlayer player) { return true; } - public boolean attemptMatchRecipe() { - this.ingredientsMatched = false; + public short attemptMatchRecipe() { + ingredientsMatched = true; + short itemsFound = 0; this.requiredItems.clear(); for (int i = 0; i < inventory.getSizeInventory(); i++) { - if (!getIngredientEquivalent(i)) - return false; //ingredient didn't match, return false + if (getIngredientEquivalent(i)) + itemsFound += 1 << i; //ingredient was found, and indicate in the short of this fact + else + ingredientsMatched = false; } - this.ingredientsMatched = true; - return true; + return itemsFound; } public boolean checkRecipeValid() { @@ -96,12 +98,12 @@ private boolean consumeRecipeItems(boolean simulate) { return true; } - private boolean getIngredientEquivalent(int slot) { + public boolean getIngredientEquivalent(int slot) { ItemStack currentStack = inventory.getStackInSlot(slot); if (currentStack.isEmpty()) { return true; //stack is empty, nothing to return } - ItemStackKey currentStackKey = new ItemStackKey(currentStack); + ItemStackKey currentStackKey = new ItemStackKey(currentStack.copy()); if (simulateExtractItem(currentStackKey)) { //we can extract ingredient equal to the one in the crafting grid, //so just return it without searching equivalent @@ -119,6 +121,7 @@ private boolean getIngredientEquivalent(int slot) { return true; } } + inventory.setInventorySlotContents(slot, currentStack); } //nothing matched, so return null return false; diff --git a/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeResolver.java b/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeResolver.java index 7c741171bf5..f1a074915a7 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeResolver.java +++ b/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeResolver.java @@ -31,7 +31,6 @@ public class CraftingRecipeResolver { private final InventoryCrafting inventoryCrafting = new InventoryCrafting(new DummyContainer(), 3, 3); private IRecipe cachedRecipe = null; private final IInventory craftingResultInventory = new InventoryCraftResult(); - private long timer = 0L; private CachedRecipeData cachedRecipeData = null; private int itemsCrafted = 0; private final CraftingRecipeMemory recipeMemory; @@ -151,16 +150,13 @@ private void updateCurrentRecipe() { } public void update() { - //update item sources every second, it is enough + //update item sources every tick for fast tinting updates //if they are being modified, they will update themselves anyway - if (timer % 20 == 0L) { - this.itemSourceList.update(); - } + this.itemSourceList.update(); //update crafting inventory state if (updateInventoryCrafting()) { updateCurrentRecipe(); } - this.timer++; } public void checkNeighbourInventories(BlockPos blockPos) { @@ -169,4 +165,8 @@ public void checkNeighbourInventories(BlockPos blockPos) { this.itemSourceList.addItemHandler(itemSource); } } + + public CachedRecipeData getCachedRecipeData() { + return this.cachedRecipeData; + } } diff --git a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java index c8b71f494dd..630bab80cad 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java +++ b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java @@ -153,11 +153,8 @@ public static AbstractWidgetGroup createWorkbenchTab(CraftingRecipeResolver reci widgetGroup.addWidget(new CraftingSlotWidget(recipeResolver, 0, 88 - 9, 44 - 9)); //crafting grid - for (int i = 0; i < 3; ++i) { - for (int j = 0; j < 3; ++j) { - widgetGroup.addWidget(new PhantomSlotWidget(craftingGrid, j + i * 3, 8 + j * 18, 17 + i * 18).setBackgroundTexture(GuiTextures.SLOT)); - } - } + widgetGroup.addWidget(new CraftingStationInputWidgetGroup(5, 8, craftingGrid, recipeResolver)); + Supplier textSupplier = () -> Integer.toString(recipeResolver.getItemsCrafted()); widgetGroup.addWidget(new SimpleTextWidget(88, 44 + 20, "", textSupplier));