Skip to content
This repository has been archived by the owner on Jul 13, 2024. It is now read-only.

Commit

Permalink
Start with recipe parser (crafting_shapeless)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonimatasDEV committed Mar 10, 2024
1 parent 0a18852 commit 962bf32
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 5 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}

Expand Down
11 changes: 7 additions & 4 deletions src/main/java/dev/tonimatas/mossy/Mossy.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
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;
import dev.tonimatas.mossy.manager.MossyCommandManager;
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;
Expand All @@ -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();

Expand All @@ -44,6 +46,7 @@ public static void main(String[] args) {
MossyBlockManager.init();
MossyCommandManager.init();
MossyEventManager.init();
DataManager.init();

MojangAuth.init(); // Premium

Expand Down
122 changes: 122 additions & 0 deletions src/main/java/dev/tonimatas/mossy/data/RecipeData.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<JarEntry> 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<JsonElement> 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<DeclareRecipesPacket.Ingredient> getShapelessIngredients(List<JsonElement> jsonElements) {
List<DeclareRecipesPacket.Ingredient> 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<ItemStack> 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);
}
}
9 changes: 9 additions & 0 deletions src/main/java/dev/tonimatas/mossy/manager/DataManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.tonimatas.mossy.manager;

import dev.tonimatas.mossy.data.RecipeData;

public class DataManager {
public static void init() {
RecipeData.init();
}
}

0 comments on commit 962bf32

Please sign in to comment.