Add manual recipes as Recipe classes for future usage

This commit is contained in:
Camotoy 2021-01-04 22:15:55 -05:00
parent 1d47dc3f18
commit a88678a5c1
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
5 changed files with 84 additions and 15 deletions

View file

@ -56,7 +56,7 @@ public class ItemRegistry {
* A list of all identifiers that only exist on Java. Used to prevent creative items from becoming these unintentionally.
*/
private static final List<String> JAVA_ONLY_ITEMS = Arrays.asList("minecraft:spectral_arrow", "minecraft:debug_stick",
"minecraft:knowledge_book");
"minecraft:knowledge_book", "minecraft:tipped_arrow", "minecraft:furnace_minecart");
public static final ItemData[] CREATIVE_ITEMS;

View file

@ -26,13 +26,25 @@
package org.geysermc.connector.network.translators.item;
import com.fasterxml.jackson.databind.JsonNode;
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.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtUtils;
import com.nukkitx.protocol.bedrock.data.inventory.CraftingData;
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.utils.FileUtils;
import org.geysermc.connector.utils.LanguageUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
@ -47,7 +59,7 @@ public class RecipeRegistry {
*/
public static int LAST_RECIPE_NET_ID = 0;
//public static final Int2ObjectMap<Recipe>
public static final Int2ObjectMap<Recipe> ALL_CRAFTING_RECIPES = new Int2ObjectOpenHashMap<>();
/**
* A list of all possible leather armor dyeing recipes.
@ -131,7 +143,7 @@ public class RecipeRegistry {
throw new AssertionError(LanguageUtils.getLocaleStringLog("geyser.toolbox.fail.runtime_java"), e);
}
for (JsonNode entry: items.get("leather_armor")) {
for (JsonNode entry : items.get("leather_armor")) {
// This won't be perfect, as we can't possibly send every leather input for every kind of color
// But it does display the correct output from a base leather armor, and besides visuals everything works fine
LEATHER_DYEING_RECIPES.add(getCraftingDataFromJsonNode(entry));
@ -151,6 +163,7 @@ public class RecipeRegistry {
for (JsonNode entry : items.get("tipped_arrows")) {
TIPPED_ARROW_RECIPES.add(getCraftingDataFromJsonNode(entry));
}
System.out.println(ALL_CRAFTING_RECIPES);
}
/**
@ -159,9 +172,13 @@ public class RecipeRegistry {
* @return the {@link CraftingData} to send to the Bedrock client.
*/
private static CraftingData getCraftingDataFromJsonNode(JsonNode node) {
ItemData output = ItemRegistry.getBedrockItemFromJson(node.get("output").get(0));
int netId = LAST_RECIPE_NET_ID++;
int type = node.get("bedrockRecipeType").asInt();
JsonNode outputNode = node.get("output");
ItemEntry outputEntry = ItemRegistry.getItemEntry(outputNode.get("identifier").asText());
ItemData output = getBedrockItemFromIdentifierJson(outputEntry, outputNode);
UUID uuid = UUID.randomUUID();
if (node.get("type").asInt() == 1) {
if (type == 1) {
// Shaped recipe
List<String> shape = new ArrayList<>();
// Get the shape of the recipe
@ -171,10 +188,12 @@ public class RecipeRegistry {
// In recipes.json each recipe is mapped by a letter
Map<String, ItemData> letterToRecipe = new HashMap<>();
Iterator<Map.Entry<String, JsonNode>> iterator = node.get("input").fields();
Iterator<Map.Entry<String, JsonNode>> iterator = node.get("inputs").fields();
while (iterator.hasNext()) {
Map.Entry<String, JsonNode> entry = iterator.next();
letterToRecipe.put(entry.getKey(), ItemRegistry.getBedrockItemFromJson(entry.getValue()));
JsonNode inputNode = entry.getValue();
ItemEntry inputEntry = ItemRegistry.getItemEntry(inputNode.get("identifier").asText());
letterToRecipe.put(entry.getKey(), getBedrockItemFromIdentifierJson(inputEntry, inputNode));
}
List<ItemData> inputs = new ArrayList<>(shape.size() * shape.get(0).length());
@ -188,20 +207,69 @@ public class RecipeRegistry {
}
}
/* Convert into a Java recipe class for autocrafting */
List<Ingredient> ingredients = new ArrayList<>();
for (ItemData input : inputs) {
ingredients.add(new Ingredient(new ItemStack[]{ItemTranslator.translateToJava(input)}));
}
ShapedRecipeData data = new ShapedRecipeData(shape.get(0).length(), shape.size(), "crafting_table",
ingredients.toArray(new Ingredient[0]), ItemTranslator.translateToJava(output));
Recipe recipe = new Recipe(RecipeType.CRAFTING_SHAPED, "", data);
ALL_CRAFTING_RECIPES.put(netId, recipe);
/* Convert end */
return CraftingData.fromShaped(uuid.toString(), shape.get(0).length(), shape.size(),
inputs, Collections.singletonList(output), uuid, "crafting_table", 0, LAST_RECIPE_NET_ID++);
inputs, Collections.singletonList(output), uuid, "crafting_table", 0, netId);
}
List<ItemData> inputs = new ObjectArrayList<>();
for (JsonNode entry : node.get("input")) {
inputs.add(ItemRegistry.getBedrockItemFromJson(entry));
for (JsonNode entry : node.get("inputs")) {
ItemEntry inputEntry = ItemRegistry.getItemEntry(entry.get("identifier").asText());
inputs.add(getBedrockItemFromIdentifierJson(inputEntry, entry));
}
if (node.get("type").asInt() == 5) {
/* Convert into a Java Recipe class for autocrafting */
List<Ingredient> ingredients = new ArrayList<>();
for (ItemData input : inputs) {
ingredients.add(new Ingredient(new ItemStack[]{ItemTranslator.translateToJava(input)}));
}
ShapelessRecipeData data = new ShapelessRecipeData("crafting_table",
ingredients.toArray(new Ingredient[0]), ItemTranslator.translateToJava(output));
Recipe recipe = new Recipe(RecipeType.CRAFTING_SHAPELESS, "", data);
ALL_CRAFTING_RECIPES.put(netId, recipe);
/* Convert end */
if (type == 5) {
// Shulker box
return CraftingData.fromShulkerBox(uuid.toString(),
inputs, Collections.singletonList(output), uuid, "crafting_table", 0, LAST_RECIPE_NET_ID++);
inputs, Collections.singletonList(output), uuid, "crafting_table", 0, netId);
}
return CraftingData.fromShapeless(uuid.toString(),
inputs, Collections.singletonList(output), uuid, "crafting_table", 0, LAST_RECIPE_NET_ID++);
inputs, Collections.singletonList(output), uuid, "crafting_table", 0, netId);
}
private static ItemData getBedrockItemFromIdentifierJson(ItemEntry itemEntry, JsonNode itemNode) {
int count = 1;
short damage = 0;
NbtMap tag = null;
JsonNode damageNode = itemNode.get("bedrockDamage");
if (damageNode != null) {
damage = damageNode.numberValue().shortValue();
}
JsonNode countNode = itemNode.get("count");
if (countNode != null) {
count = countNode.asInt();
}
JsonNode nbtNode = itemNode.get("bedrockNbt");
if (nbtNode != null) {
byte[] bytes = Base64.getDecoder().decode(nbtNode.asText());
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
try {
tag = (NbtMap) NbtUtils.createReaderLE(bais).readTag();
} catch (IOException e) {
e.printStackTrace();
}
}
return ItemData.of(itemEntry.getBedrockId(), damage, count, tag);
}
public static void init() {

View file

@ -70,6 +70,7 @@ public class TippedArrowTranslator extends ItemTranslator {
@Override
public ItemStack translateToJava(ItemData itemData, ItemEntry itemEntry) {
GeyserConnector.getInstance().getLogger().warning(itemData.toString() + " " + itemEntry.getJavaIdentifier());
TippedArrowPotion tippedArrowPotion = TippedArrowPotion.getByBedrockId(itemData.getDamage());
ItemStack itemStack = super.translateToJava(itemData, itemEntry);
if (tippedArrowPotion != null) {

View file

@ -59,7 +59,7 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
public void translate(ServerDeclareRecipesPacket packet, GeyserSession session) {
// Get the last known network ID (first used for the pregenerated recipes) and increment from there.
int netId = RecipeRegistry.LAST_RECIPE_NET_ID + 1;
Int2ObjectMap<Recipe> recipeMap = new Int2ObjectOpenHashMap<>();
Int2ObjectMap<Recipe> recipeMap = new Int2ObjectOpenHashMap<>(RecipeRegistry.ALL_CRAFTING_RECIPES);
Int2ObjectMap<List<StoneCuttingRecipeData>> unsortedStonecutterData = new Int2ObjectOpenHashMap<>();
CraftingDataPacket craftingDataPacket = new CraftingDataPacket();
craftingDataPacket.setCleanRecipes(true);

@ -1 +1 @@
Subproject commit 62e39acaf3859da86189a76fff57b30c2403ef3d
Subproject commit 07f65c3803dcd3f83358ee574e54bf129cad0840