Skip to content

Commit

Permalink
improve recipe memory offsetting
Browse files Browse the repository at this point in the history
move used memorized recipe to the front
  • Loading branch information
ghzdude committed Jan 30, 2025
1 parent e59aebd commit 585bdd4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,50 @@ public MemorizedRecipe getRecipeAtIndex(int index) {
return hasRecipe(index) ? getRecipeAtIndex(index).getRecipeResult() : ItemStack.EMPTY;
}

@Nullable
private MemorizedRecipe offsetRecipe(int startIndex) {
MemorizedRecipe previousRecipe = memorizedRecipes[startIndex];
/**
* Offsets recipes from {@code startIndex} to the right, skipping locked recipes
*
* @param startIndex the index to start offsetting recipes
*/
private void offsetRecipe(int startIndex) {
MemorizedRecipe previousRecipe = removeRecipe(startIndex);
for (int i = startIndex + 1; i < memorizedRecipes.length; i++) {
MemorizedRecipe recipe = memorizedRecipes[i];
if (recipe != null && recipe.recipeLocked) continue;
memorizedRecipes[i] = previousRecipe;
memorizedRecipes[i].index = i;
if (recipe == null)
return memorizedRecipes[startIndex] = null;

// we found a null recipe and there's no more recipes to check,
if (recipe == null) return;

previousRecipe = recipe;
}
return previousRecipe;
}

@Nullable
private MemorizedRecipe findOrCreateRecipe(ItemStack resultItemStack) {
// search preexisting recipe with identical recipe result
MemorizedRecipe existing = null;
for (MemorizedRecipe memorizedRecipe : memorizedRecipes) {
if (memorizedRecipe != null &&
ItemStack.areItemStacksEqual(memorizedRecipe.recipeResult, resultItemStack)) {
return memorizedRecipe;
existing = memorizedRecipe;
break;
}
}

// we already have a recipe that matches
// move it to the front
if (existing != null && !existing.recipeLocked) {
int removed = existing.index;
removeRecipe(existing.index);
syncToClient(REMOVE_RECIPE, buffer -> buffer.writeByte(removed));
offsetRecipe(0);
syncToClient(OFFSET_RECIPE, buffer -> buffer.writeByte(0));
existing.index = 0;
return memorizedRecipes[0] = existing;
}

// put new memorized recipe into array
for (int i = 0; i < memorizedRecipes.length; i++) {
MemorizedRecipe memorizedRecipe;
Expand All @@ -90,12 +109,9 @@ private MemorizedRecipe findOrCreateRecipe(ItemStack resultItemStack) {
} else if (memorizedRecipes[i].recipeLocked) {
continue;
} else {
memorizedRecipe = offsetRecipe(i);
final int startIndex = i;
syncToClient(OFFSET_RECIPE, buffer -> buffer.writeByte(startIndex));
if (memorizedRecipe == null) {
memorizedRecipe = new MemorizedRecipe(i);
}
offsetRecipe(i);
memorizedRecipe = new MemorizedRecipe(i);
syncToClient(OFFSET_RECIPE, buffer -> buffer.writeByte(memorizedRecipe.index));
}
memorizedRecipe.initialize(resultItemStack);
return memorizedRecipes[i] = memorizedRecipe;
Expand Down Expand Up @@ -145,10 +161,13 @@ private static void copyInventoryItems(IItemHandler src, IItemHandlerModifiable
}
}

public final void removeRecipe(int index) {
public final MemorizedRecipe removeRecipe(int index) {
if (hasRecipe(index)) {
MemorizedRecipe removed = memorizedRecipes[index];
memorizedRecipes[index] = null;
return removed;
}
return null;
}

public final boolean hasRecipe(int index) {
Expand Down Expand Up @@ -238,7 +257,7 @@ public void readOnServer(int id, PacketBuffer buf) {
public static class MemorizedRecipe {

private final ItemStackHandler craftingMatrix = new ItemStackHandler(9);
private ItemStack recipeResult;
private ItemStack recipeResult = ItemStack.EMPTY;
private boolean recipeLocked = false;
public int timesUsed = 0;
public int index;
Expand Down Expand Up @@ -316,5 +335,14 @@ public MemorizedRecipe copy() {
recipe.timesUsed = this.timesUsed;
return recipe;
}

@Override
public String toString() {
return String.format("MemorizedRecipe{%dx %s, locked: %s, times used: %d}",
getRecipeResult().getCount(),
getRecipeResult().getDisplayName(),
recipeLocked,
timesUsed);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Result onMousePressed(int mouseButton) {
return Result.IGNORE;

var data = MouseData.create(mouseButton);
this.memory.syncToServer(2, buffer -> {
this.memory.syncToServer(CraftingRecipeMemory.MOUSE_CLICK, buffer -> {
buffer.writeByte(this.index);
data.writeToPacket(buffer);
});
Expand Down

0 comments on commit 585bdd4

Please sign in to comment.