From 1b42c0f736c0a373f9af2efc20afae6beef72f78 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Sat, 21 Dec 2024 19:45:51 +0100 Subject: [PATCH] Generate possible drops data roughly --- data-generator/build.gradle.kts | 1 + .../generators/BlocksJsonGenerator.java | 62 ++++++++++++++++--- .../generators/DefaultTagsDataGenerator.java | 8 +-- .../generators/MapColorJavaGenerator.java | 15 ++--- .../soulfiremc/server/data/EquipmentSlot.java | 6 ++ 5 files changed, 67 insertions(+), 25 deletions(-) diff --git a/data-generator/build.gradle.kts b/data-generator/build.gradle.kts index 3752bbf5c..402b85227 100644 --- a/data-generator/build.gradle.kts +++ b/data-generator/build.gradle.kts @@ -15,6 +15,7 @@ repositories { dependencies { implementation("org.vineflower:vineflower:1.10.1") implementation("com.github.javaparser:javaparser-symbol-solver-core:3.26.3") + implementation(libs.reflect) } unimined.minecraft { diff --git a/data-generator/src/main/java/com/soulfiremc/generator/generators/BlocksJsonGenerator.java b/data-generator/src/main/java/com/soulfiremc/generator/generators/BlocksJsonGenerator.java index 90f030e01..a949dd9ce 100644 --- a/data-generator/src/main/java/com/soulfiremc/generator/generators/BlocksJsonGenerator.java +++ b/data-generator/src/main/java/com/soulfiremc/generator/generators/BlocksJsonGenerator.java @@ -21,15 +21,42 @@ import com.google.gson.JsonObject; import com.soulfiremc.generator.mixin.BlockAccessor; import com.soulfiremc.generator.util.BlockSettingsAccessor; -import lombok.SneakyThrows; +import com.soulfiremc.generator.util.MCHelper; +import lombok.extern.slf4j.Slf4j; +import net.lenni0451.reflect.stream.RStream; import net.minecraft.Util; +import net.minecraft.core.Holder; import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.Item; import net.minecraft.world.level.block.*; -import net.minecraft.world.level.block.state.BlockBehaviour; +import net.minecraft.world.level.storage.loot.LootPool; +import net.minecraft.world.level.storage.loot.entries.*; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +@Slf4j public class BlocksJsonGenerator implements IDataGenerator { + private static List fromComposite(CompositeEntryBase compositeEntryBase) { + return RStream.of(compositeEntryBase).withSuper().fields().by("children").get(); + } + + private static void insertNested(LootPoolEntryContainer container, Set drops) { + switch (container) { + case AlternativesEntry alternativesEntry -> { + for (var entry : fromComposite(alternativesEntry)) { + insertNested(entry, drops); + } + } + case DynamicLoot dynamicLoot -> log.debug("Dynamic loot entry found: {}", dynamicLoot); + case LootItem lootItem -> drops.add(RStream.of(lootItem).fields().by("item").>get().unwrapKey().orElseThrow().location()); + default -> throw new IllegalStateException("Unexpected value: " + container); + } + } + @SuppressWarnings("deprecation") - @SneakyThrows public static JsonObject generateBlock(Block block) { var blockDesc = new JsonObject(); @@ -71,16 +98,31 @@ public static JsonObject generateBlock(Block block) { blockDesc.addProperty("blocksMotion", true); } + var lootTableLocation = block.getLootTable(); + if (lootTableLocation.isPresent()) { + var lootTable = MCHelper.getServer().reloadableRegistries().getLootTable(lootTableLocation.get()); + var drops = new LinkedHashSet(); + var pools = RStream.of(lootTable).fields().by("pools").>get(); + for (var pool : pools) { + var entries = RStream.of(pool).fields().by("entries").>get(); + for (var entry : entries) { + insertNested(entry, drops); + } + } + + var array = new JsonArray(); + for (var entry : drops) { + array.add(entry.toString()); + } + + blockDesc.add("possibleDrops", array); + } + if (defaultState.hasOffsetFunction()) { var offsetData = new JsonObject(); - var horizontalOffsetMethod = BlockBehaviour.class.getDeclaredMethod("getMaxHorizontalOffset"); - var verticalOffsetMethod = BlockBehaviour.class.getDeclaredMethod("getMaxVerticalOffset"); - horizontalOffsetMethod.setAccessible(true); - verticalOffsetMethod.setAccessible(true); - - offsetData.addProperty("maxHorizontalOffset", (float) horizontalOffsetMethod.invoke(block)); - offsetData.addProperty("maxVerticalOffset", (float) verticalOffsetMethod.invoke(block)); + offsetData.addProperty("maxHorizontalOffset", RStream.of(block).withSuper().methods().by("getMaxHorizontalOffset").invoke()); + offsetData.addProperty("maxVerticalOffset", RStream.of(block).withSuper().methods().by("getMaxVerticalOffset").invoke()); var blockSettings = ((BlockAccessor) block).properties(); var offsetType = ((BlockSettingsAccessor) blockSettings).soulfire$getOffsetType(); diff --git a/data-generator/src/main/java/com/soulfiremc/generator/generators/DefaultTagsDataGenerator.java b/data-generator/src/main/java/com/soulfiremc/generator/generators/DefaultTagsDataGenerator.java index f0ce7d1a9..370214178 100644 --- a/data-generator/src/main/java/com/soulfiremc/generator/generators/DefaultTagsDataGenerator.java +++ b/data-generator/src/main/java/com/soulfiremc/generator/generators/DefaultTagsDataGenerator.java @@ -24,8 +24,8 @@ import com.google.gson.stream.JsonWriter; import com.soulfiremc.generator.util.MCHelper; import it.unimi.dsi.fastutil.ints.IntList; -import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import net.lenni0451.reflect.stream.RStream; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagNetworkSerialization; @@ -43,8 +43,6 @@ public String getDataName() { return "data/default_tags.json.zip"; } - @SuppressWarnings("unchecked") - @SneakyThrows @Override public byte[] generateDataJson() { var byteOutputStream = new ByteArrayOutputStream(); @@ -62,9 +60,7 @@ public byte[] generateDataJson() { var registryObj = new JsonObject(); - var tagsField = TagNetworkSerialization.NetworkPayload.class.getDeclaredField("tags"); - tagsField.setAccessible(true); - var tags = (Map) tagsField.get(payload); + var tags = RStream.of(payload).fields().by("tags").>get(); for (var tag : tags.entrySet()) { var tagObj = new JsonArray(); diff --git a/data-generator/src/main/java/com/soulfiremc/generator/generators/MapColorJavaGenerator.java b/data-generator/src/main/java/com/soulfiremc/generator/generators/MapColorJavaGenerator.java index 9b14a2bc6..dc8112214 100644 --- a/data-generator/src/main/java/com/soulfiremc/generator/generators/MapColorJavaGenerator.java +++ b/data-generator/src/main/java/com/soulfiremc/generator/generators/MapColorJavaGenerator.java @@ -19,7 +19,7 @@ import com.soulfiremc.generator.util.GeneratorConstants; import com.soulfiremc.generator.util.ResourceHelper; -import lombok.SneakyThrows; +import net.lenni0451.reflect.stream.RStream; import net.minecraft.world.level.material.MapColor; public class MapColorJavaGenerator implements IDataGenerator { @@ -28,22 +28,19 @@ public String getDataName() { return "java/MapColor.java"; } - @SneakyThrows @Override public String generateDataJson() { - var registryField = MapColor.class.getDeclaredField("MATERIAL_COLORS"); - registryField.setAccessible(true); - var registry = (MapColor[]) registryField.get(null); - var colField = MapColor.class.getDeclaredField("col"); - colField.setAccessible(true); + var registry = RStream.of(MapColor.class).fields().by("MATERIAL_COLORS").get(); var colorArray = new String[registry.length]; for (var i = 0; i < registry.length; i++) { - if (registry[i] == null) { + var mapColor = registry[i]; + if (mapColor == null) { colorArray[i] = "null"; continue; } - colorArray[i] = "new MapColor(%d, %s)".formatted(i, colField.get(registry[i])); + var col = RStream.of(mapColor).fields().by("col").get(); + colorArray[i] = "new MapColor(%d, %s)".formatted(i, col); } var base = ResourceHelper.getResourceAsString("/templates/MapColor.java"); diff --git a/server/src/main/java/com/soulfiremc/server/data/EquipmentSlot.java b/server/src/main/java/com/soulfiremc/server/data/EquipmentSlot.java index ce9377ec3..fbab31871 100644 --- a/server/src/main/java/com/soulfiremc/server/data/EquipmentSlot.java +++ b/server/src/main/java/com/soulfiremc/server/data/EquipmentSlot.java @@ -31,6 +31,8 @@ public enum EquipmentSlot { HEAD(Type.HUMANOID_ARMOR), BODY(Type.ANIMAL_ARMOR); + public static final EquipmentSlot[] VALUES = values(); + private final Type type; public static EquipmentSlot fromMCPl(org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot slot) { @@ -45,6 +47,10 @@ public static EquipmentSlot fromMCPl(org.geysermc.mcprotocollib.protocol.data.ga }; } + public boolean isHumanoidArmor() { + return type == Type.HUMANOID_ARMOR; + } + public boolean isArmor() { return type == Type.HUMANOID_ARMOR || type == Type.ANIMAL_ARMOR; }