Skip to content

Commit

Permalink
Add Highlighting to the Crafting Station (GregTechCEu#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruberu authored Aug 17, 2021
1 parent 36ef039 commit d51ae3e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public abstract class InventoryItemSource extends ItemSource {
private StoredItemsChangeCallback changeCallback = null;
protected IItemHandler itemHandler = EmptyHandler.INSTANCE;
private Map<ItemStackKey, Integer> itemStackByAmountMap = new HashMap<>();
private long lastItemHandlerUpdateTick = -1L;
private long lastStoredItemListUpdateTick = -1L;
private boolean cachedRefreshResult = false;

public InventoryItemSource(World world, int priority) {
this.world = world;
Expand Down Expand Up @@ -57,43 +54,30 @@ 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()) {
this.itemHandler = newItemHandler;
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);
}

/**
Expand Down Expand Up @@ -150,7 +134,6 @@ private boolean recomputeItemStackCount() {
if (!checkItemHandlerValid(false)) {
return false;
}
this.lastStoredItemListUpdateTick = world.getTotalWorldTime();
HashMap<ItemStackKey, Integer> amountMap = new HashMap<>();
for (int i = 0; i < itemHandler.getSlots(); i++) {
ItemStack itemStack = itemHandler.getStackInSlot(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -119,6 +121,7 @@ private boolean getIngredientEquivalent(int slot) {
return true;
}
}
inventory.setInventorySlotContents(slot, currentStack);
}
//nothing matched, so return null
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -169,4 +165,8 @@ public void checkNeighbourInventories(BlockPos blockPos) {
this.itemSourceList.addItemHandler(itemSource);
}
}

public CachedRecipeData getCachedRecipeData() {
return this.cachedRecipeData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> textSupplier = () -> Integer.toString(recipeResolver.getItemsCrafted());
widgetGroup.addWidget(new SimpleTextWidget(88, 44 + 20, "", textSupplier));

Expand Down

0 comments on commit d51ae3e

Please sign in to comment.