Skip to content

Commit

Permalink
Generate possible drops data roughly
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Dec 21, 2024
1 parent 8361135 commit 1b42c0f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
1 change: 1 addition & 0 deletions data-generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LootPoolEntryContainer> fromComposite(CompositeEntryBase compositeEntryBase) {
return RStream.of(compositeEntryBase).withSuper().fields().by("children").get();
}

private static void insertNested(LootPoolEntryContainer container, Set<ResourceLocation> 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").<Holder<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();

Expand Down Expand Up @@ -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<ResourceLocation>();
var pools = RStream.of(lootTable).fields().by("pools").<List<LootPool>>get();
for (var pool : pools) {
var entries = RStream.of(pool).fields().by("entries").<List<LootPoolEntryContainer>>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").<Float>invoke());
offsetData.addProperty("maxVerticalOffset", RStream.of(block).withSuper().methods().by("getMaxVerticalOffset").<Float>invoke());

var blockSettings = ((BlockAccessor) block).properties();
var offsetType = ((BlockSettingsAccessor) blockSettings).soulfire$getOffsetType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -43,8 +43,6 @@ public String getDataName() {
return "data/default_tags.json.zip";
}

@SuppressWarnings("unchecked")
@SneakyThrows
@Override
public byte[] generateDataJson() {
var byteOutputStream = new ByteArrayOutputStream();
Expand All @@ -62,9 +60,7 @@ public byte[] generateDataJson() {

var registryObj = new JsonObject();

var tagsField = TagNetworkSerialization.NetworkPayload.class.getDeclaredField("tags");
tagsField.setAccessible(true);
var tags = (Map<ResourceLocation, IntList>) tagsField.get(payload);
var tags = RStream.of(payload).fields().by("tags").<Map<ResourceLocation, IntList>>get();
for (var tag : tags.entrySet()) {
var tagObj = new JsonArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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").<MapColor[]>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").<Integer>get();
colorArray[i] = "new MapColor(%d, %s)".formatted(i, col);
}

var base = ResourceHelper.getResourceAsString("/templates/MapColor.java");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 1b42c0f

Please sign in to comment.