Shaped recipe detection

This commit is contained in:
AJ Ferguson 2021-01-09 04:10:27 -09:00
parent 9bfc5d320c
commit 554ec82ef5
2 changed files with 78 additions and 38 deletions

View file

@ -95,7 +95,11 @@ public class GeyserItemStack {
} }
public ItemStack getItemStack() { public ItemStack getItemStack() {
return isEmpty() ? null : new ItemStack(javaId, amount, nbt); return getItemStack(amount);
}
public ItemStack getItemStack(int newAmount) {
return isEmpty() ? null : new ItemStack(javaId, newAmount, nbt);
} }
public ItemData getItemData(GeyserSession session) { public ItemData getItemData(GeyserSession session) {

View file

@ -50,6 +50,7 @@ import org.geysermc.connector.utils.InventoryUtils;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
@Translator(packet = ServerSetSlotPacket.class) @Translator(packet = ServerSetSlotPacket.class)
@ -82,7 +83,7 @@ public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket>
}); });
} }
private void updateCraftingGrid(GeyserSession session, ServerSetSlotPacket packet, Inventory inventory, InventoryTranslator translator) { private static void updateCraftingGrid(GeyserSession session, ServerSetSlotPacket packet, Inventory inventory, InventoryTranslator translator) {
if (packet.getSlot() == 0) { if (packet.getSlot() == 0) {
int gridSize; int gridSize;
if (translator instanceof PlayerInventoryTranslator) { if (translator instanceof PlayerInventoryTranslator) {
@ -99,16 +100,35 @@ public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket>
int offset = gridSize == 4 ? 28 : 32; int offset = gridSize == 4 ? 28 : 32;
int gridDimensions = gridSize == 4 ? 2 : 3; int gridDimensions = gridSize == 4 ? 2 : 3;
int itemsStart = 0; int firstRow = -1, height = -1;
for (int i = 1; i < inventory.getSize(); i++) { // Slot 0 is, well, the output, so we ignore that int firstCol = -1, width = -1;
if (!inventory.getItem(i).isEmpty()) { for (int row = 0; row < gridDimensions; row++) {
System.out.println(inventory.getItem(i).getItemStack().toString()); for (int col = 0; col < gridDimensions; col++) {
itemsStart = i; if (!inventory.getItem(col + (row * gridDimensions) + 1).isEmpty()) {
break; if (firstRow == -1) {
firstRow = row;
firstCol = col;
} else {
firstCol = Math.min(firstCol, col);
}
height = Math.max(height, row);
width = Math.max(width, col);
}
} }
} }
System.out.println("Items start: " + itemsStart); //empty grid
if (firstRow == -1) {
return;
}
height += -firstRow + 1;
width += -firstCol + 1;
System.out.println("Start Row: " + firstRow);
System.out.println("Start Column: " + firstCol);
System.out.println("Rows: " + height);
System.out.println("Columns: " + width);
//TODO //TODO
recipes: recipes:
@ -118,38 +138,27 @@ public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket>
if (!data.getResult().equals(packet.getItem())) { if (!data.getResult().equals(packet.getItem())) {
continue; continue;
} }
int height = 1; if (data.getWidth() != width || data.getHeight() != height || width * height != data.getIngredients().length) {
int width = 1; continue;
for (int i = 0; i < data.getIngredients().length; i++) { }
System.out.println(height);
System.out.println(width); Ingredient[] ingredients = data.getIngredients();
System.out.println(data.getHeight()); if (!testShapedRecipe(ingredients, inventory, gridDimensions, firstRow, height, firstCol, width)) {
System.out.println(data.getWidth()); Ingredient[] mirroredIngredients = new Ingredient[data.getIngredients().length];
System.out.println(Arrays.toString(data.getIngredients())); for (int row = 0; row < height; row++) {
Ingredient ingredient = data.getIngredients()[i]; for (int col = 0; col < width; col++) {
GeyserItemStack geyserItemStack = inventory.getItem(itemsStart + (width - 1) + ((data.getWidth() - 1) * (gridDimensions - data.getWidth() + height))); mirroredIngredients[col + (row * width)] = ingredients[(width - 1 - col) + (row * width)];
System.out.println(itemsStart + (width - 1) + ((data.getWidth() - 1) * (gridDimensions - data.getWidth() + height)));
boolean inventoryHasItem = false;
for (ItemStack itemStack : ingredient.getOptions()) {
if (geyserItemStack.isEmpty()) {
inventoryHasItem = itemStack == null || itemStack.getId() == 0;
if (inventoryHasItem) {
break;
}
} else if (itemStack.equals(geyserItemStack.getItemStack())) {
inventoryHasItem = true;
break;
} }
} }
if (!inventoryHasItem) {
break recipes; if (Arrays.equals(ingredients, mirroredIngredients)) {
} continue;
width++; } else if (!testShapedRecipe(mirroredIngredients, inventory, gridDimensions, firstRow, height, firstCol, width)) {
if (width > data.getWidth()) { continue;
width = 1;
height++;
} }
} }
System.out.println("FOUND SHAPED RECIPE :)");
System.out.println(recipe);
// Recipe is had, don't sent packet // Recipe is had, don't sent packet
return; return;
} else if (recipe.getType() == RecipeType.CRAFTING_SHAPELESS) { } else if (recipe.getType() == RecipeType.CRAFTING_SHAPELESS) {
@ -168,7 +177,7 @@ public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket>
if (inventoryHasItem) { if (inventoryHasItem) {
break; break;
} }
} else if (itemStack.equals(geyserItemStack.getItemStack())) { } else if (itemStack.equals(geyserItemStack.getItemStack(1))) {
inventoryHasItem = true; inventoryHasItem = true;
break; break;
} }
@ -222,4 +231,31 @@ public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket>
} }
} }
} }
private static boolean testShapedRecipe(Ingredient[] ingredients, Inventory inventory, int gridDimensions, int firstRow, int height, int firstCol, int width) {
int ingredientIndex = 0;
for (int row = firstRow; row < height + firstRow; row++) {
for (int col = firstCol; col < width + firstCol; col++) {
GeyserItemStack geyserItemStack = inventory.getItem(col + (row * gridDimensions) + 1);
Ingredient ingredient = ingredients[ingredientIndex++];
if (ingredient.getOptions().length == 0) {
if (!geyserItemStack.isEmpty()) {
return false;
}
} else {
boolean inventoryHasItem = false;
for (ItemStack item : ingredient.getOptions()) {
if (Objects.equals(geyserItemStack.getItemStack(1), item)) {
inventoryHasItem = true;
break;
}
}
if (!inventoryHasItem) {
return false;
}
}
}
}
return true;
}
} }