diff --git a/README.md b/README.md index 685d3a5a..f881b500 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,8 @@ pack-version-code: 153 modpack: innercore # Использование ванильного интерфейса верстака, вместо кастомного. -# Внимание: в настоящее время не поддерживается -# и пользовательские рецепты не будут отображаться. +# Внимание: в настоящее время не рекомендуется использовать, +# так как некоторые рецепты отображаются с проблемами. use-legacy-workbench-override: on # Отключение исправления инвентаря для подключенных клиентов, diff --git a/src/main/java/com/zhekasmirnov/apparatus/mcpe/NativeWorkbench.java b/src/main/java/com/zhekasmirnov/apparatus/mcpe/NativeWorkbench.java index d5a0cd9e..14d5a646 100644 --- a/src/main/java/com/zhekasmirnov/apparatus/mcpe/NativeWorkbench.java +++ b/src/main/java/com/zhekasmirnov/apparatus/mcpe/NativeWorkbench.java @@ -2,12 +2,14 @@ package com.zhekasmirnov.apparatus.mcpe; import cn.nukkit.Server; import cn.nukkit.inventory.CraftingManager; -import cn.nukkit.inventory.Recipe; import cn.nukkit.inventory.ShapedRecipe; import cn.nukkit.inventory.ShapelessRecipe; import cn.nukkit.item.Item; +import cn.nukkit.item.RuntimeItemMapping; +import cn.nukkit.item.RuntimeItems; + import com.reider745.InnerCoreServer; -import com.reider745.item.CustomItem; +import com.reider745.hooks.ItemUtils; import com.zhekasmirnov.apparatus.adapter.innercore.game.item.ItemStack; import com.zhekasmirnov.horizon.runtime.logger.Logger; @@ -21,11 +23,12 @@ public class NativeWorkbench { public int resultData; public long resultExtraPtr; public String[] pattern; - public HashMap ingredients; + public HashMap ingredients; public int[] ingredients_; public boolean shapeless = false; - public RecipeRegistry(int resultId, int resultCount, int resultData, long resultExtraPtr, String[] pattern, HashMap ingredients){ + public RecipeRegistry(int resultId, int resultCount, int resultData, long resultExtraPtr, String[] pattern, + HashMap ingredients) { this.resultId = resultId; this.resultCount = resultCount; this.resultData = resultData; @@ -34,7 +37,7 @@ public class NativeWorkbench { this.ingredients = ingredients; } - public RecipeRegistry(int resultId, int resultCount, int resultData, long resultExtraPtr, int[] ingredients){ + public RecipeRegistry(int resultId, int resultCount, int resultData, long resultExtraPtr, int[] ingredients) { this.resultId = resultId; this.resultCount = resultCount; this.resultData = resultData; @@ -43,35 +46,32 @@ public class NativeWorkbench { this.shapeless = true; } } + private static ArrayList recipes = new ArrayList<>(); - private static void addShapedRecipe(int resultId, int resultCount, int resultData, long resultExtraPtr, String[] pattern, int[] ingredients){ - /*if(resultId == 0){ + + private static void addShapedRecipe(int resultId, int resultCount, int resultData, long resultExtraPtr, + String[] pattern, int[] ingredients) { + if (resultId == 0) { Logger.error("Recipe result id 0"); return; } - if(ingredients.length % 3 != 0){ - Logger.error("error ingredients"); - return; - } - final HashMap ingredients_ = new HashMap<>(); - for(int i = 0;i < ingredients.length;i+=3) - ingredients_.put((char) ingredients[i], Item.get(ingredients[i+1], ingredients[i+2])); - recipes.add(new RecipeRegistry(resultId, resultCount, resultData, resultExtraPtr, pattern, ingredients_));*/ + + final HashMap ingredients_ = new HashMap<>(); + for (int i = 0; i < ingredients.length; i += 3) + ingredients_.put((char) ingredients[i], new ItemStack(ingredients[i + 1], 1, ingredients[i + 2])); + recipes.add(new RecipeRegistry(resultId, resultCount, resultData, resultExtraPtr, pattern, ingredients_)); } public static void addShapedRecipe(ItemStack result, String[] pattern, int[] ingredients) { addShapedRecipe(result.id, result.count, result.data, result.getExtraPtr(), pattern, ingredients); } - private static void addShapelessRecipe(int resultId, int resultCount, int resultData, long resultExtraPtr, int[] ingredients){ - if(resultId == 0){ + private static void addShapelessRecipe(int resultId, int resultCount, int resultData, long resultExtraPtr, + int[] ingredients) { + if (resultId == 0) { Logger.error("Recipe result id 0"); return; } - if(ingredients.length % 3 != 0){ - Logger.error("error ingredients"); - return; - } recipes.add(new RecipeRegistry(resultId, resultCount, resultData, resultExtraPtr, ingredients)); } @@ -82,34 +82,79 @@ public class NativeWorkbench { private static final ArrayList removeds = new ArrayList<>(); - public static void removeRecipeByResult(int resultId, int resultCount, int resultData){ - removeds.add(new int[]{resultId, resultCount, resultData}); + public static void removeRecipeByResult(int resultId, int resultCount, int resultData) { + removeds.add(new int[] { resultId, resultCount, resultData }); } - - public static void init(){ + public static void init() { + RuntimeItemMapping mapping = RuntimeItems.getMapping(InnerCoreServer.PROTOCOL); CraftingManager craftingManager = Server.getInstance().getCraftingManager(); - for (int[] item : removeds){ - Item itemInstance = Item.get(item[0], item[2], item[1]); + for (int[] item : removeds) { + // TODO: data -1, shaped recipes support + Item itemInstance = ItemUtils.get(item[0], item[1], item[2]); craftingManager.getShapelessRecipes(InnerCoreServer.PROTOCOL).forEach((uid, recipes) -> { - for(UUID uuid : recipes.keySet()){ + for (UUID uuid : recipes.keySet()) { ShapelessRecipe recipe = recipes.get(uuid); - if(recipe.getResult().equals(itemInstance)) { + if (recipe.getResult().equals(itemInstance)) { recipes.remove(uuid); break; } } }); } - for(RecipeRegistry recipe : recipes) - if(!recipe.shapeless) - craftingManager.registerRecipe(419, new ShapedRecipe(Item.get(recipe.resultId, recipe.resultData, recipe.resultCount), recipe.pattern, recipe.ingredients, new ArrayList<>())); - else { - final Collection ingredients = new ArrayList<>(); - for(int i = 0;i < recipe.ingredients_.length;i+=3) - ingredients.add(Item.get(recipe.ingredients_[i+1], recipe.ingredients_[i+2])); - craftingManager.registerRecipe(419, new ShapelessRecipe(Item.get(recipe.resultId, recipe.resultData, recipe.resultCount), ingredients)); + for (RecipeRegistry recipe : recipes) { + Item result = ItemUtils.get(recipe.resultId, recipe.resultCount, recipe.resultData, recipe.resultExtraPtr); + try { + mapping.toRuntime(result.getId(), result.getDamage()); + } catch (IllegalArgumentException e) { + Logger.warning("NativeWorkbench", + "Unknown legacy2Runtime mapping: id=" + recipe.resultId + ", meta=" + recipe.resultData); + continue; } + boolean accepted = true; + Item item; + if (!recipe.shapeless) { + final Map ingredients = new HashMap<>(); + for (Map.Entry ingredient : recipe.ingredients.entrySet()) { + ItemStack stack = ingredient.getValue(); + item = ItemUtils.get(stack.id, stack.data); + try { + mapping.toRuntime(item.getId(), item.getDamage()); + } catch (IllegalArgumentException e) { + accepted = false; + Logger.warning("NativeWorkbench", "Unknown legacy2Runtime mapping: id=" + stack.id + ", meta=" + + stack.data + " (recipe: id=" + recipe.resultId + ", meta=" + recipe.resultData + ")"); + continue; + } + ingredients.put(ingredient.getKey(), item); + } + if (accepted) { + craftingManager.registerRecipe(419, + new ShapedRecipe(result, recipe.pattern, ingredients, new ArrayList<>())); + } + } else { + final Collection ingredients = new ArrayList<>(); + for (int id, data, i = 0; i < recipe.ingredients_.length; i += 3) { + id = recipe.ingredients_[i + 1]; + data = recipe.ingredients_[i + 2]; + item = ItemUtils.get(id, data); + try { + mapping.toRuntime(item.getId(), item.getDamage()); + } catch (IllegalArgumentException e) { + accepted = false; + Logger.warning("NativeWorkbench", "Unknown legacy2Runtime mapping: id=" + id + ", meta=" + data + + " (recipe: id=" + recipe.resultId + ", meta=" + recipe.resultData + ")"); + continue; + } + ingredients.add(item); + } + if (accepted) { + craftingManager.registerRecipe(419, new ShapelessRecipe(result, ingredients)); + } + } + } + recipes.clear(); + craftingManager.rebuildPacket(); } } diff --git a/src/main/java/com/zhekasmirnov/innercore/api/NativeFurnaceRegistry.java b/src/main/java/com/zhekasmirnov/innercore/api/NativeFurnaceRegistry.java index 1c177c54..6820b289 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/NativeFurnaceRegistry.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/NativeFurnaceRegistry.java @@ -4,6 +4,8 @@ import cn.nukkit.Server; import cn.nukkit.inventory.CraftingManager; import cn.nukkit.inventory.FurnaceRecipe; import cn.nukkit.item.Item; +import cn.nukkit.item.RuntimeItemMapping; +import cn.nukkit.item.RuntimeItems; import cn.nukkit.network.protocol.ProtocolInfo; import java.util.ArrayList; @@ -14,6 +16,10 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; +import com.reider745.InnerCoreServer; +import com.reider745.hooks.ItemUtils; +import com.zhekasmirnov.horizon.runtime.logger.Logger; + /** * Created by zheka on 15.09.2017. */ @@ -96,20 +102,41 @@ public class NativeFurnaceRegistry { } public static void init() { + RuntimeItemMapping mapping = RuntimeItems.getMapping(InnerCoreServer.PROTOCOL); CraftingManager manager = Server.getInstance().getCraftingManager(); + int outputId, outputData, inputId, inputData; for (Map recipe : recipes) { - int type = recipe.get("type"); - switch (type) { - case 0 -> - manager.registerFurnaceRecipe(ProtocolInfo.CURRENT_PROTOCOL, new FurnaceRecipe( - Item.get(recipe.get("outputId"), recipe.get("outputData")), - Item.get(recipe.get("inputId"), recipe.get("inputData")))); + inputId = recipe.get("inputId"); + inputData = recipe.get("inputData"); + final Item input = ItemUtils.get(inputId, inputData); + try { + mapping.toRuntime(input.getId(), input.getDamage()); + } catch (IllegalArgumentException e) { + Logger.warning("NativeFurnace", + "Unknown legacy2Runtime mapping: id=" + inputId + ", meta=" + inputData); + continue; + } + switch (recipe.get("type")) { + case 0 -> { + outputId = recipe.get("outputId"); + outputData = recipe.get("outputData"); + Item output = ItemUtils.get(outputId, outputData); + try { + mapping.toRuntime(output.getId(), output.getDamage()); + } catch (IllegalArgumentException e) { + Logger.warning("NativeFurnace", + "Unknown legacy2Runtime mapping: id=" + outputId + ", meta=" + outputData + + " (recipe: id=" + inputId + ", meta=" + inputData + ")"); + continue; + } + manager.registerFurnaceRecipe(ProtocolInfo.CURRENT_PROTOCOL, new FurnaceRecipe(output, input)); + } case 1 -> { AtomicReference key = new AtomicReference<>(); manager.getFurnaceRecipes(ProtocolInfo.CURRENT_PROTOCOL).forEach((k, v) -> { Item item = v.getInput(); - int data = recipe.get("inputData"); - if (item.getId() == recipe.get("inputId") || (data == -1 || data == item.getDamage())) + if (item.getId() == input.getId() + || (input.getDamage() == -1 || input.getDamage() == item.getDamage())) key.set(k); }); @@ -117,5 +144,7 @@ public class NativeFurnaceRegistry { } } } + recipes.clear(); + manager.rebuildPacket(); } } diff --git a/src/main/java/com/zhekasmirnov/innercore/api/mod/recipes/workbench/WorkbenchRecipeRegistry.java b/src/main/java/com/zhekasmirnov/innercore/api/mod/recipes/workbench/WorkbenchRecipeRegistry.java index 246c61ad..e781307f 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/mod/recipes/workbench/WorkbenchRecipeRegistry.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/mod/recipes/workbench/WorkbenchRecipeRegistry.java @@ -211,6 +211,7 @@ public class WorkbenchRecipeRegistry { return null; } + @Deprecated public static ItemInstance provideRecipe(WorkbenchField field, String prefix) { return provideRecipeForPlayer(field, prefix, NativeAPI.getPlayer()); } diff --git a/src/main/resources/innercore/assets/innercore/scripts/workbench.js b/src/main/resources/innercore/assets/innercore/scripts/workbench.js index cef73f44..c8a98623 100644 --- a/src/main/resources/innercore/assets/innercore/scripts/workbench.js +++ b/src/main/resources/innercore/assets/innercore/scripts/workbench.js @@ -1,22 +1,9 @@ var isLegacyWorkbenchEnabled = false; -Callback.addCallback("CoreConfigured", function(config) { - isLegacyWorkbenchEnabled = config.getBool("gameplay.use_legacy_workbench_override"); +Callback.addCallback("CoreConfigured", function() { + isLegacyWorkbenchEnabled = com.reider745.InnerCoreServer.isLegacyWorkbench(); }); -function open(player, x, y, z){ - var position = Entity.getPosition(player); - var distanceSqr = Math.pow(position[0] - (x + .5), 2) + Math.pow(position[1] - (y + .5), 2) + Math.pow(position[2] - (z + .5), 2); - if (distanceSqr < 15 * 15) { - var blockSource = BlockSource.getDefaultForActor(player); - if (blockSource != null && blockSource.getBlockId(x || 0, y || 0, z || 0) === 58) { - var client = Network.getClientForPlayer(player); - WorkbenchHandler.openFor(client); - WorkbenchHandler.handleRebuildRecipeRequest(client); - } - } -} - /** * Network workbench implementation */ @@ -26,12 +13,6 @@ var Network = MCSystem.getNetwork(); var WorkbenchHandler = { containerByEntity: {}, - setupPacketListeners: function() { - Network.addServerPacket("sys.workbench.open", function(client, coords) { - - }); - }, - setupContainerClientSide: function() { ItemContainer.registerScreenFactory("sys.workbench", function (container, name) { if (name === "main") { @@ -143,27 +124,23 @@ var WorkbenchHandler = { } WorkbenchHandler.setupContainerClientSide(); -WorkbenchHandler.setupPacketListeners(); - -/*Callback.addCallback("ItemUseLocalServer", function (coords, item, block, isExternal, player) { - if (isLegacyWorkbenchEnabled && block.id === 58) { - if (!Entity.isSneaking(player)) { - preventDefault(); - Network.sendToServer("sys.workbench.open", {x: coords.x, y: coords.y, z: coords.z}); - } - } -});*/ Callback.addCallback("ItemUse", function (coords, item, block, isExternal, player) { if (isLegacyWorkbenchEnabled && block.id === 58) { preventDefault(); - if(!Entity.isSneaking(player)){ - open(Number(player), coords.x, coords.y, coords.z) + var position = Entity.getPosition(player); + var distanceSqr = Math.pow(position[0] - (coords.x + .5), 2) + Math.pow(position[1] - (coords.y + .5), 2) + Math.pow(position[2] - (coords.z + .5), 2); + if (distanceSqr < 15 * 15) { + var blockSource = BlockSource.getDefaultForActor(player); + if (blockSource != null && blockSource.getBlockId(coords.x || 0, coords.y || 0, coords.z || 0) === 58) { + var client = Network.getClientForPlayer(player); + WorkbenchHandler.openFor(client); + WorkbenchHandler.handleRebuildRecipeRequest(client); + } } } }); - Callback.addCallback("LevelLeft", function () { WorkbenchHandler.clearAll(); }); diff --git a/src/main/resources/zotecore.yml b/src/main/resources/zotecore.yml index dae6dbe3..a10aab00 100644 --- a/src/main/resources/zotecore.yml +++ b/src/main/resources/zotecore.yml @@ -9,8 +9,8 @@ pack-version-code: 152 # modpack: StoneBlock # Use vanilla crafting table interface, instead of custom one. -# Attention: currently not supported and custom recipes will not be dislayed. -# use-legacy-workbench-override: on +# Attention: currently not recommended and recipes dislayed with bugs. +# use-legacy-workbench-override: off # Disabling inventory fix for connected clients, outdated versions do not # work properly with it, not allowing moving items around inventory.