diff --git a/build.gradle b/build.gradle index d443d35..48664ca 100644 --- a/build.gradle +++ b/build.gradle @@ -8,6 +8,8 @@ plugins { id("com.github.johnrengelman.shadow") version "7.1.0" } +group = "dev.tonimatas" +version = "1.0.0" base.archivesName.set("mossy") configurations { @@ -20,7 +22,7 @@ repositories { } dependencies { - implementation("com.github.Minestom:Minestom:-SNAPSHOT") + implementation("net.minestom:minestom-snapshots:7320437640") implementation("org.jetbrains:annotations:24.0.1") } diff --git a/src/main/java/dev/tonimatas/mossy/Mossy.java b/src/main/java/dev/tonimatas/mossy/Mossy.java index 73cfc89..f156277 100644 --- a/src/main/java/dev/tonimatas/mossy/Mossy.java +++ b/src/main/java/dev/tonimatas/mossy/Mossy.java @@ -1,13 +1,13 @@ package dev.tonimatas.mossy; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import dev.tonimatas.mossy.api.MossyDimensionType; +import dev.tonimatas.mossy.manager.DataManager; import dev.tonimatas.mossy.util.ConsoleThread; import net.minestom.server.MinecraftServer; import net.minestom.server.extras.MojangAuth; -import net.minestom.server.instance.AnvilLoader; -import net.minestom.server.instance.Instance; -import net.minestom.server.instance.InstanceContainer; -import net.minestom.server.instance.InstanceManager; +import net.minestom.server.instance.*; import net.minestom.server.instance.block.Block; import dev.tonimatas.mossy.logger.Logger; import dev.tonimatas.mossy.manager.MossyBlockManager; @@ -15,6 +15,7 @@ import dev.tonimatas.mossy.manager.MossyEventManager; public class Mossy { + public static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); public static InstanceContainer overWorld; public static InstanceContainer nether; public static InstanceContainer end; @@ -23,6 +24,7 @@ public static void main(String[] args) { long time = System.currentTimeMillis(); MinecraftServer minecraftServer = MinecraftServer.init(); + MinecraftServer.getBiomeManager().loadVanillaBiomes(); InstanceManager instanceManager = MinecraftServer.getInstanceManager(); MossyDimensionType mossyDimensionType = new MossyDimensionType(); @@ -44,6 +46,7 @@ public static void main(String[] args) { MossyBlockManager.init(); MossyCommandManager.init(); MossyEventManager.init(); + DataManager.init(); MojangAuth.init(); // Premium diff --git a/src/main/java/dev/tonimatas/mossy/data/RecipeData.java b/src/main/java/dev/tonimatas/mossy/data/RecipeData.java new file mode 100644 index 0000000..6247443 --- /dev/null +++ b/src/main/java/dev/tonimatas/mossy/data/RecipeData.java @@ -0,0 +1,122 @@ +package dev.tonimatas.mossy.data; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import dev.tonimatas.mossy.Mossy; +import dev.tonimatas.mossy.launcher.Main; +import dev.tonimatas.mossy.logger.Logger; +import net.minestom.server.MinecraftServer; +import net.minestom.server.entity.Player; +import net.minestom.server.item.ItemStack; +import net.minestom.server.item.Material; +import net.minestom.server.network.packet.server.play.DeclareRecipesPacket; +import net.minestom.server.recipe.Recipe; +import net.minestom.server.recipe.RecipeCategory; +import net.minestom.server.recipe.ShapelessRecipe; +import net.minestom.server.utils.NamespaceID; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.io.InputStream; +import java.util.*; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +public class RecipeData { + private static final String recipesFolder = "data/minecraft/recipes"; + + public static void init() { + List recipes = new ArrayList<>(); + + JarFile jarFile; + + try { + jarFile = new JarFile(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI())); + } catch (Exception e) { + Logger.error("Error on parse recipes"); + return; + } + + for (Iterator it = jarFile.entries().asIterator(); it.hasNext(); ) { + String entry = it.next().getName(); + + if (entry.startsWith(recipesFolder)) recipes.add(entry); + } + + for (String recipe : recipes) { + InputStream inputStream = Main.class.getResourceAsStream("/" + recipe); + if (inputStream == null) continue; + String recipeName = "minecraft:" + recipe.split("/")[recipe.split("/").length - 1].replace(".json", ""); + Scanner scanner = new Scanner(inputStream); + + StringBuilder stringBuilder = new StringBuilder(); + while (scanner.hasNext()) { + stringBuilder.append(scanner.next()); + } + + scanner.close(); + String jsonString = stringBuilder.toString(); + + if (jsonString.contains("tag")) { + Logger.error("The recipe " + recipeName + " has tag. Unsupported at the moment."); + continue; + } + + if (!jsonString.startsWith("{")) continue; + + JsonObject jsonObject = Mossy.gson.fromJson(jsonString, JsonObject.class); + String recipeType = jsonObject.get("type").getAsString(); + + switch (recipeType) { + case "minecraft:crafting_shapeless" -> parseCraftingShapeless(recipeName, jsonObject); + default -> Logger.warn("Incompatible recipe type (" + recipeType + ") for " + recipeName); + } + } + + Logger.info("Recipes loaded successfully. (" + recipes.size() + ")"); + } + + private static void parseCraftingShapeless(String recipeName, JsonObject jsonObject) { + JsonElement group = jsonObject.get("group"); + JsonElement count = jsonObject.getAsJsonObject("result").get("count"); + String category = jsonObject.get("category").getAsString().toUpperCase(Locale.ENGLISH); + List ingredients = jsonObject.getAsJsonArray("ingredients").asList(); + String result = jsonObject.getAsJsonObject("result").get("item").getAsString(); + + addRecipe(new ShapelessRecipe(recipeName, + group == null ? "" : group.getAsString(), RecipeCategory.Crafting.valueOf(category), + getShapelessIngredients(ingredients), + ItemStack.of(Material.fromNamespaceId(NamespaceID.from(result)), + count != null ? count.getAsInt() : 1)) { + @Override + public boolean shouldShow(@NotNull Player player) { + return true; + } + }); + } + + private static List getShapelessIngredients(List jsonElements) { + List ingredients = new ArrayList<>(); + for (JsonElement jsonElement : jsonElements) { + if (jsonElement.isJsonObject()) { + ingredients.add(new DeclareRecipesPacket.Ingredient(List.of(ItemStack.of(Material.fromNamespaceId(jsonElement.getAsJsonObject().get("item").getAsString()))))); + } + + if (jsonElement.isJsonArray()) { + List itemStacks = new ArrayList<>(); + + for (JsonElement jsonElement1 : jsonElement.getAsJsonArray().asList()) { + itemStacks.add(ItemStack.of(Material.fromNamespaceId(jsonElement1.getAsJsonObject().get("item").getAsString()))); + } + + ingredients.add(new DeclareRecipesPacket.Ingredient(itemStacks)); + } + } + + return ingredients; + } + + private static void addRecipe(Recipe recipe) { + MinecraftServer.getRecipeManager().addRecipe(recipe); + } +} diff --git a/src/main/java/dev/tonimatas/mossy/manager/DataManager.java b/src/main/java/dev/tonimatas/mossy/manager/DataManager.java new file mode 100644 index 0000000..f036fe9 --- /dev/null +++ b/src/main/java/dev/tonimatas/mossy/manager/DataManager.java @@ -0,0 +1,9 @@ +package dev.tonimatas.mossy.manager; + +import dev.tonimatas.mossy.data.RecipeData; + +public class DataManager { + public static void init() { + RecipeData.init(); + } +}