Work-in-progress checks for legacy slot checking

This commit is contained in:
Camotoy 2021-01-09 00:38:53 -05:00
parent 9118ec6840
commit 9bfc5d320c
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
2 changed files with 149 additions and 50 deletions

View file

@ -186,7 +186,7 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
} }
} }
//session.sendUpstreamPacket(craftingDataPacket); //commented out for testing session.sendUpstreamPacket(craftingDataPacket);
session.setCraftingRecipes(recipeMap); session.setCraftingRecipes(recipeMap);
session.getUnlockedRecipes().clear(); session.getUnlockedRecipes().clear();
session.setStonecutterRecipes(stonecutterRecipeMap); session.setStonecutterRecipes(stonecutterRecipeMap);

View file

@ -25,6 +25,12 @@
package org.geysermc.connector.network.translators.java.window; package org.geysermc.connector.network.translators.java.window;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.github.steveice10.mc.protocol.data.game.recipe.Ingredient;
import com.github.steveice10.mc.protocol.data.game.recipe.Recipe;
import com.github.steveice10.mc.protocol.data.game.recipe.RecipeType;
import com.github.steveice10.mc.protocol.data.game.recipe.data.ShapedRecipeData;
import com.github.steveice10.mc.protocol.data.game.recipe.data.ShapelessRecipeData;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerId; import com.nukkitx.protocol.bedrock.data.inventory.ContainerId;
import com.nukkitx.protocol.bedrock.data.inventory.CraftingData; import com.nukkitx.protocol.bedrock.data.inventory.CraftingData;
@ -67,17 +73,117 @@ public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket>
InventoryTranslator translator = session.getInventoryTranslator(); InventoryTranslator translator = session.getInventoryTranslator();
if (translator != null) { if (translator != null) {
updateCraftingGrid(session, packet, inventory, translator);
GeyserItemStack newItem = GeyserItemStack.from(packet.getItem());
inventory.setItem(packet.getSlot(), newItem, session);
translator.updateSlot(session, inventory, packet.getSlot());
}
});
}
private void updateCraftingGrid(GeyserSession session, ServerSetSlotPacket packet, Inventory inventory, InventoryTranslator translator) {
if (packet.getSlot() == 0) { if (packet.getSlot() == 0) {
int gridSize = -1; int gridSize;
if (translator instanceof PlayerInventoryTranslator) { if (translator instanceof PlayerInventoryTranslator) {
gridSize = 4; gridSize = 4;
} } else if (translator instanceof CraftingInventoryTranslator) {
if (translator instanceof CraftingInventoryTranslator) {
gridSize = 9; gridSize = 9;
} else {
return;
} }
if (gridSize != -1) {
if (packet.getItem() == null || packet.getItem().getId() == 0) {
return;
}
int offset = gridSize == 4 ? 28 : 32; int offset = gridSize == 4 ? 28 : 32;
int gridWidth = gridSize == 4 ? 2 : 3; int gridDimensions = gridSize == 4 ? 2 : 3;
int itemsStart = 0;
for (int i = 1; i < inventory.getSize(); i++) { // Slot 0 is, well, the output, so we ignore that
if (!inventory.getItem(i).isEmpty()) {
System.out.println(inventory.getItem(i).getItemStack().toString());
itemsStart = i;
break;
}
}
System.out.println("Items start: " + itemsStart);
//TODO
recipes:
for (Recipe recipe : session.getCraftingRecipes().values()) {
if (recipe.getType() == RecipeType.CRAFTING_SHAPED) {
ShapedRecipeData data = (ShapedRecipeData) recipe.getData();
if (!data.getResult().equals(packet.getItem())) {
continue;
}
int height = 1;
int width = 1;
for (int i = 0; i < data.getIngredients().length; i++) {
System.out.println(height);
System.out.println(width);
System.out.println(data.getHeight());
System.out.println(data.getWidth());
System.out.println(Arrays.toString(data.getIngredients()));
Ingredient ingredient = data.getIngredients()[i];
GeyserItemStack geyserItemStack = inventory.getItem(itemsStart + (width - 1) + ((data.getWidth() - 1) * (gridDimensions - data.getWidth() + height)));
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;
}
width++;
if (width > data.getWidth()) {
width = 1;
height++;
}
}
// Recipe is had, don't sent packet
return;
} else if (recipe.getType() == RecipeType.CRAFTING_SHAPELESS) {
ShapelessRecipeData data = (ShapelessRecipeData) recipe.getData();
if (!data.getResult().equals(packet.getItem())) {
continue;
}
for (int i = 0; i < data.getIngredients().length; i++) {
Ingredient ingredient = data.getIngredients()[i];
for (ItemStack itemStack : ingredient.getOptions()) {
boolean inventoryHasItem = false;
for (int j = 0; j < inventory.getSize(); j++) {
GeyserItemStack geyserItemStack = inventory.getItem(j);
if (geyserItemStack.isEmpty()) {
inventoryHasItem = itemStack == null || itemStack.getId() == 0;
if (inventoryHasItem) {
break;
}
} else if (itemStack.equals(geyserItemStack.getItemStack())) {
inventoryHasItem = true;
break;
}
}
if (!inventoryHasItem) {
continue recipes;
}
}
}
// Recipe is had, don't sent packet
return;
}
}
System.out.println("Sending packet!");
ItemData[] ingredients = new ItemData[gridSize]; ItemData[] ingredients = new ItemData[gridSize];
//construct ingredient list and clear slots on client //construct ingredient list and clear slots on client
for (int i = 0; i < gridSize; i++) { for (int i = 0; i < gridSize; i++) {
@ -91,11 +197,11 @@ public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket>
} }
CraftingDataPacket craftPacket = new CraftingDataPacket(); CraftingDataPacket craftPacket = new CraftingDataPacket();
UUID uuid = UUID.fromString("e0a4971a-698c-40fb-95dd-afc8ed16e108"); UUID uuid = UUID.randomUUID();
craftPacket.getCraftingData().add(CraftingData.fromShaped( craftPacket.getCraftingData().add(CraftingData.fromShaped(
uuid.toString(), uuid.toString(),
gridWidth, gridDimensions,
gridWidth, gridDimensions,
Arrays.asList(ingredients), Arrays.asList(ingredients),
Collections.singletonList(ItemTranslator.translateToBedrock(session, packet.getItem())), Collections.singletonList(ItemTranslator.translateToBedrock(session, packet.getItem())),
uuid, uuid,
@ -116,11 +222,4 @@ public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket>
} }
} }
} }
GeyserItemStack newItem = GeyserItemStack.from(packet.getItem());
inventory.setItem(packet.getSlot(), newItem, session);
translator.updateSlot(session, inventory, packet.getSlot());
}
});
}
} }