From b44c9abf8ea06b79ba72aea6712dada5f6f1beb4 Mon Sep 17 00:00:00 2001 From: Justin Parsell Date: Mon, 25 Oct 2021 00:31:25 -0400 Subject: [PATCH] first attempt to overwrite loottables --- .../parsell/glowstonewire/GlowstoneWire.java | 2 + .../glowstonewire/core/glowLootTables.java | 127 ++++++++++++++++++ .../mixin/LootManagerInjector.java | 27 ++++ .../loot_tables/block/glowstone_wire.json | 19 +++ src/main/resources/glowstonewire.mixins.json | 1 + 5 files changed, 176 insertions(+) create mode 100644 src/main/java/me/parsell/glowstonewire/core/glowLootTables.java create mode 100644 src/main/java/me/parsell/glowstonewire/mixin/LootManagerInjector.java create mode 100644 src/main/resources/data/glowstonewire/loot_tables/block/glowstone_wire.json diff --git a/src/main/java/me/parsell/glowstonewire/GlowstoneWire.java b/src/main/java/me/parsell/glowstonewire/GlowstoneWire.java index 5b0ef0a..41f54f8 100644 --- a/src/main/java/me/parsell/glowstonewire/GlowstoneWire.java +++ b/src/main/java/me/parsell/glowstonewire/GlowstoneWire.java @@ -2,6 +2,7 @@ package me.parsell.glowstonewire; import me.parsell.glowstonewire.core.glowBlocks; import me.parsell.glowstonewire.core.glowItems; +import me.parsell.glowstonewire.core.glowLootTables; import me.parsell.glowstonewire.core.glowRecipes; import me.shedaniel.autoconfig.AutoConfig; import me.shedaniel.autoconfig.serializer.GsonConfigSerializer; @@ -19,5 +20,6 @@ public class GlowstoneWire implements ModInitializer { glowBlocks.init(); glowItems.init(); glowRecipes.init(); + glowLootTables.init(); } } diff --git a/src/main/java/me/parsell/glowstonewire/core/glowLootTables.java b/src/main/java/me/parsell/glowstonewire/core/glowLootTables.java new file mode 100644 index 0000000..0a4a0c3 --- /dev/null +++ b/src/main/java/me/parsell/glowstonewire/core/glowLootTables.java @@ -0,0 +1,127 @@ +package me.parsell.glowstonewire.core; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import me.parsell.glowstonewire.GlowstoneWire; + +public class glowLootTables { + public static JsonObject glowstonelootTable; + + public static void init(){ + if(GlowstoneWire.CONFIG.replaceGlowstoneDrop) { + JsonObject explosion; + JsonObject limitValues, limit; + JsonObject bonus, parametersValues; + JsonObject count, countValues; + JsonArray secondItemArray; + JsonObject secondItem; + + JsonObject levelsValues, silktouch; + JsonArray enchantmentsArray; + JsonObject enchantments, match; + JsonArray firstItemArray; + JsonObject firstItem; + + JsonArray alternativesArray; + JsonObject alternatives; + + JsonArray rollsArray; + JsonObject rolls; + + JsonArray lootPoolsArray; + + explosion = new JsonObject(); + explosion.addProperty("function", "minecraft:explosion_decay"); + + limit = new JsonObject(); + limit.addProperty("function", "minecraft:limit_count"); + limitValues = new JsonObject(); + limitValues.addProperty("max", 4); + limitValues.addProperty("min", 1); + limit.add("limit", limitValues); + + bonus = new JsonObject(); + bonus.addProperty("function", "minecraft:apply_bonus"); + bonus.addProperty("enchantment", "minecraft:fortune"); + bonus.addProperty("formula", "minecraft:uniform_bonus_count"); + parametersValues = new JsonObject(); + parametersValues.addProperty("bonusMultiplier", 1); + bonus.add("parameters", parametersValues); + + count = new JsonObject(); + count.addProperty("function", "minecraft:set_count"); + countValues = new JsonObject(); + countValues.addProperty("min", 2.0); + countValues.addProperty("max", 4.0); + countValues.addProperty("type", "minecraft:uniform"); + count.add("count", countValues); + + secondItemArray = new JsonArray(); + secondItemArray.add(count); + secondItemArray.add(bonus); + secondItemArray.add(limit); + secondItemArray.add(explosion); + + secondItem = new JsonObject(); + secondItem.addProperty("type", "minecraft:item"); + secondItem.add("functions", secondItemArray); + secondItem.addProperty("name", "glowstonewire:glowstone_dust"); + + // + + levelsValues = new JsonObject(); + levelsValues.addProperty("min", 1); + silktouch = new JsonObject(); + silktouch.addProperty("enchantment", "minecraft:silk_touch"); + silktouch.add("levels", levelsValues); + + enchantmentsArray = new JsonArray(); + enchantmentsArray.add(silktouch); + enchantments = new JsonObject(); + enchantments.add("enchantments", enchantmentsArray); + + match = new JsonObject(); + match.addProperty("condition", "minecraft:match_tool"); + match.add("predicate", enchantments); + + firstItemArray = new JsonArray(); + firstItemArray.add(match); + + firstItem = new JsonObject(); + firstItem.addProperty("type", "minecraft:item"); + firstItem.add("conditions", firstItemArray); + firstItem.addProperty("name", "minecraft:glowstone"); + + // + + alternativesArray = new JsonArray(); + alternativesArray.add(firstItem); + alternativesArray.add(secondItem); + + alternatives = new JsonObject(); + alternatives.addProperty("type", "minecraft:alternatives"); + alternatives.add("children", alternativesArray); + + // + + rollsArray = new JsonArray(); + rollsArray.add(alternatives); + + rolls = new JsonObject(); + rolls.addProperty("rolls", 1); + rolls.add("entries", rollsArray); + + // + + lootPoolsArray = new JsonArray(); + lootPoolsArray.add(rolls); + + glowstonelootTable = new JsonObject(); + glowstonelootTable.addProperty("type", "minecraft:block"); + glowstonelootTable.add("pools", lootPoolsArray); + + System.out.println(glowstonelootTable.toString()); + } + } +} diff --git a/src/main/java/me/parsell/glowstonewire/mixin/LootManagerInjector.java b/src/main/java/me/parsell/glowstonewire/mixin/LootManagerInjector.java new file mode 100644 index 0000000..b65ace6 --- /dev/null +++ b/src/main/java/me/parsell/glowstonewire/mixin/LootManagerInjector.java @@ -0,0 +1,27 @@ +package me.parsell.glowstonewire.mixin; + +import java.util.Map; + +import com.google.gson.JsonElement; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import me.parsell.glowstonewire.GlowstoneWire; +import me.parsell.glowstonewire.core.glowLootTables; +import net.minecraft.loot.LootManager; +import net.minecraft.resource.ResourceManager; +import net.minecraft.util.Identifier; +import net.minecraft.util.profiler.Profiler; + +@Mixin(LootManager.class) +public class LootManagerInjector { + @Inject(method = "apply", at = @At("HEAD")) + public void interceptApply(Map map, ResourceManager resourceManager, Profiler profiler, CallbackInfo info) { + if (GlowstoneWire.CONFIG.replaceGlowstoneDrop) { + map.put(new Identifier("minecraft", "glowstone"), glowLootTables.glowstonelootTable); + } + } +} diff --git a/src/main/resources/data/glowstonewire/loot_tables/block/glowstone_wire.json b/src/main/resources/data/glowstonewire/loot_tables/block/glowstone_wire.json new file mode 100644 index 0000000..9073fb3 --- /dev/null +++ b/src/main/resources/data/glowstonewire/loot_tables/block/glowstone_wire.json @@ -0,0 +1,19 @@ +{ + "type": "minecraft:block", + "pools": [ + { + "rolls": 1, + "entries": [ + { + "type": "minecraft:item", + "name": "glowstonewire:glowstone_dust" + } + ], + "conditions": [ + { + "condition": "minecraft:survives_explosion" + } + ] + } + ] + } \ No newline at end of file diff --git a/src/main/resources/glowstonewire.mixins.json b/src/main/resources/glowstonewire.mixins.json index 786365f..dcbafbf 100644 --- a/src/main/resources/glowstonewire.mixins.json +++ b/src/main/resources/glowstonewire.mixins.json @@ -8,6 +8,7 @@ "client": [ "BrewingRecipeRegistryInvoker", "RecipeManagerInjector", + "LootManagerInjector", "FireworkStarRecipeAccessor" ], "injectors": {