Skip to content
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,7 @@ buildNumber.properties
.mvn/wrapper/maven-wrapper.jar

# End of https://www.gitignore.io/api/git,java,maven,intellij+all

cache
local-repo
generated
26 changes: 22 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,34 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.21.6-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<version>1.21.11-pre1-R0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.5</version>
<scope>provided</scope>
<version>9.6</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>9.6</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-tree</artifactId>
<version>9.6</version>
</dependency>
</dependencies>

Expand Down Expand Up @@ -177,5 +191,9 @@
<id>paper-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>paper-pr</id>
<url>https://maven-prs.papermc.io/Paper/pr13194/</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class Main extends JavaPlugin {

public static Set<Material> EXPERIMENTAL_MATERIALS = null;
public static Set<PotionType> EXPERIMENTAL_POTIONS = null;
public static Set<String> VALID_ITEMS = null;

@Override
public void onEnable() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package io.github.essentialsx.itemdbgenerator;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.bukkit.Material;
import org.bukkit.potion.PotionType;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;

public class StandaloneMain {

private static final String MANIFEST_URL = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json";
private static final Gson GSON = new Gson();

public static void main(String[] args) {
String targetVersion = args.length > 0 ? args[0] : null;

try {
if (targetVersion == null) {
JsonObject manifest = fetchJson(MANIFEST_URL).getAsJsonObject();
targetVersion = manifest.getAsJsonObject("latest").get("snapshot").getAsString();
System.out.println("Targeting latest release: " + targetVersion);
}

File cacheDir = new File("cache");
if (!cacheDir.exists()) cacheDir.mkdirs();

// Clean up libraries to avoid version conflicts
File libDir = new File(cacheDir, "libraries");
if (libDir.exists()) {
System.out.println("Cleaning up old libraries...");
deleteDirectory(libDir);
}

System.out.println("Downloading server jar for version " + targetVersion + "...");
File serverJar = new File(cacheDir, "server-" + targetVersion + ".jar");
if (!serverJar.exists()) {
downloadServerJar(targetVersion, serverJar);
}
System.out.println("Using server jar: " + serverJar.getAbsolutePath());

System.out.println("Running server jar to unpack libraries...");
new ProcessBuilder("java", "-jar", serverJar.getName(), "--help")
.directory(cacheDir)
.start()
.waitFor();

File versionsDir = new File(cacheDir, "versions");
File extractedServerJar = findServerJar(versionsDir);
if (extractedServerJar == null) {
throw new RuntimeException("Could not find unpacked server jar in " + versionsDir);
}
System.out.println("Found unpacked server jar: " + extractedServerJar);

String classpath = buildClasspath(cacheDir, extractedServerJar);

System.out.println("Running data generator...");
new ProcessBuilder("java", "-cp", classpath, "net.minecraft.data.Main", "--reports")
.directory(cacheDir)
.inheritIO()
.start()
.waitFor();

File reportFile = new File(cacheDir, "generated/reports/items.json");
if (!reportFile.exists()) {
throw new RuntimeException("Data generator failed to produce " + reportFile);
}

System.out.println("Parsing generated report...");
Set<String> items = parseItemsReport(reportFile);
System.out.println("Found " + items.size() + " valid items.");
Main.VALID_ITEMS = items;

System.out.println("Generating experimental JSONs...");
AnnotationGenerator.main(new String[0]);

System.out.println("Loading experimental data...");
Reader reader = new InputStreamReader(Objects.requireNonNull(Main.class.getResourceAsStream("/experimental_materials.json")));
Main.EXPERIMENTAL_MATERIALS = GSON.fromJson(reader, new TypeToken<Set<Material>>(){}.getType());
reader.close();
reader = new InputStreamReader(Objects.requireNonNull(Main.class.getResourceAsStream("/experimental_potions.json")));
Main.EXPERIMENTAL_POTIONS = GSON.fromJson(reader, new TypeToken<Set<PotionType>>(){}.getType());
reader.close();

System.out.println("Generating items.json...");
JsonObject itemMap = Main.generateItemMap();
Main.save(itemMap);

System.out.println("Generation complete!");

} catch (Exception e) {
e.printStackTrace();
}
}

private static void deleteDirectory(File file) {
File[] contents = file.listFiles();
if (contents != null) {
for (File f : contents) {
deleteDirectory(f);
}
}
file.delete();
}

private static void downloadServerJar(String version, File outputFile) throws IOException {
JsonObject manifest = fetchJson(MANIFEST_URL).getAsJsonObject();
com.google.gson.JsonArray versions = manifest.getAsJsonArray("versions");

String versionUrl = null;
for (com.google.gson.JsonElement v : versions) {
JsonObject vObj = v.getAsJsonObject();
if (vObj.get("id").getAsString().equals(version)) {
versionUrl = vObj.get("url").getAsString();
break;
}
}

if (versionUrl == null) {
throw new IllegalArgumentException("Version " + version + " not found in manifest.");
}

JsonObject versionMeta = fetchJson(versionUrl).getAsJsonObject();
JsonObject downloads = versionMeta.getAsJsonObject("downloads");
if (!downloads.has("server")) {
throw new IllegalArgumentException("No server download found for version " + version);
}

String serverUrl = downloads.getAsJsonObject("server").get("url").getAsString();
downloadFile(serverUrl, outputFile);
}

private static File findServerJar(File dir) throws IOException {
if (!dir.exists()) return null;
try (Stream<Path> stream = Files.walk(dir.toPath())) {
return stream
.map(Path::toFile)
.filter(f -> f.getName().endsWith(".jar") && !f.getName().contains("bundler"))
.findFirst()
.orElse(null);
}
}

private static String buildClasspath(File workingDir, File extractedServerJar) throws IOException {
StringBuilder cp = new StringBuilder();
cp.append(extractedServerJar.getAbsolutePath());

Path libPath = new File(workingDir, "libraries").toPath();
if (Files.exists(libPath)) {
Files.walk(libPath)
.filter(p -> p.toString().endsWith(".jar"))
.forEach(p -> {
cp.append(File.pathSeparator);
cp.append(p.toAbsolutePath().toString());
});
}
return cp.toString();
}

private static Set<String> parseItemsReport(File reportFile) throws IOException {
try (Reader reader = new java.io.FileReader(reportFile)) {
JsonObject json = GSON.fromJson(reader, JsonObject.class);
Set<String> items = new HashSet<>();
for (String key : json.keySet()) {
if (key.startsWith("minecraft:")) {
items.add(key.substring(10));
} else {
items.add(key);
}
}
return items;
}
}

private static com.google.gson.JsonElement fetchJson(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");

try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
return GSON.fromJson(reader, com.google.gson.JsonElement.class);
}
}

private static void downloadFile(String urlString, File outputFile) throws IOException {
URL url = new URL(urlString);
try (InputStream in = url.openStream();
FileOutputStream out = new FileOutputStream(outputFile)) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ private enum CopperBuildingBlock implements CompoundType {
TRAPDOOR(null, "%strapdoor", "%sdoortrap", "%shatch", "%stdoor", "%sdoort", "%strapd", "%sdtrap"),
BULB(null, "%sbulb"),
GRATE(null, "%sgrate"),
BLOCK("^[A-Z]+_(SLAB|STAIRS)", true, "%scopperblock", "%scopblock", "%scoblock"),
GOLEM_STATUE(null, "%sgolemstatue", "%sstatue", "%sgolem"),
LANTERN(null, "%slantern", "%slantern", "%slight"),
LIGHTNING_ROD(null, "%srod", "%slrod", "%slightrod"),
BARS(null, "%sbars", "%sbar", "%sbarsb", "%sbarsblock", "%sfence"),
CHAIN(null, "%schain", "%slink", "%schains", "%slinks"),
CHEST(null, "%schest", "%scontainer", "%sdrawer"),
BLOCK("^[A-Z_]+_(SLAB|STAIRS)", true, "%scopperblock", "%scopblock", "%scoblock"),
;

private final Pattern regex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private static void add(EntityType entity, String... aliases) {
add(Material.BOOKSHELF, "bshelf", "bookcase", "casebook", "shelfbook", "bookblock", "blockbook");
add(Material.CHISELED_BOOKSHELF, "cbshelf", "cbookcase", "cshelfbook", "cbookblock", "cblockbook", "chiseledshelf", "chiseledb");
add(Material.TORCH, "burningstick", "burnstick");
add(Material.COPPER_TORCH, "copperburningstick", "copperburnstick");
add(Material.GLOWSTONE, "glowingstoneblock", "lightstoneblock", "glowstoneblock", "blockglowingstone", "blocklightstone", "blockglowstone", "glowingstone", "lightstone", "glowingblock", "lightblock", "glowblock", "lstone");
add(Material.LILY_PAD, "waterlily", "lily", "swamppad", "lpad", "wlily");
add(Material.ANCIENT_DEBRIS, "debris");
Expand Down Expand Up @@ -214,6 +215,8 @@ private static void add(EntityType entity, String... aliases) {
add(Material.TURTLE_SCUTE, "scute", "minecraft:scute");
add(EntityType.MOOSHROOM, "mushroom_cow_spawner");
add(EntityType.SNOW_GOLEM, "snowman_spawner");
// == 1.21.9 Enum Renaming Manual Fixes
add(Material.IRON_CHAIN, "chain", "minecraft:chain");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private enum FoodType implements CompoundType {
COOKED("COOKED_[A-Z_]+", "%scooked", "%scook", "%sc", "%sgrilled", "%sgrill", "%sg", "%sroasted", "%sroast", "%sro", "%sbbq", "%stoasted", "cooked%s", "cook%s", "c%s", "grilled%s", "grill%s", "g%s", "roasted%s", "roast%s", "ro%s", "bbq%s", "toasted%s"),
HIDE("[A-Z_]+_HIDE", "%shide", "%sskin", "%scoat", "%sfur"),
STEW("[A-Z_]+_STEW", "%sstew", "%ssoup"),
RAW("^(?!COOKED_)[A-Z_]+(?!_STEW)$", "raw%s", "ra%s", "uncooked%s", "plain%s", "%s"),
RAW("^(?!COOKED_|MUSIC_DISC_)[A-Z_]+(?!_STEW)$", "raw%s", "ra%s", "uncooked%s", "plain%s", "%s"),
;

private final Pattern regex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public String[] getNames() {
*/
@SuppressWarnings("unused")
private enum MineableItemType implements CompoundType {
// The Copper Age
NUGGET(null, "%snugget", "%snug"),
// Caves and Cliffs
RAW_ORE_BLOCK("RAW_[A-Z]+_BLOCK", "raw%soreblock", "%sorechunkblock", "r%soreblock", "raw%sorebl", "%sorechunkbl", "r%sorebl"),
RAW_ORE("RAW_[A-Z]+_ORE", "raw%sore", "%sorechunk", "r%sore"),
Expand All @@ -104,6 +106,7 @@ private enum MineableItemType implements CompoundType {
SCRAP(null, "%sscrap"),
// Tools
SWORD(null, "%ssword"),
SPEAR(null, "%sspear"),
SHOVEL(null, "%sshovel", "%sspade"),
PICKAXE(null, "%spickaxe", "%spick"),
AXE("[A-Z_]+_(?<!PICK)AXE", "%saxe"),
Expand All @@ -114,6 +117,7 @@ private enum MineableItemType implements CompoundType {
LEGGINGS(null, "%sleggings", "%slegs", "%spants"),
BOOTS(null, "%sboots", "%sshoes"),
HORSE_ARMOR(null, "%shorsearmor", "%sharmor", "%sarmor"),
NAUTILUS_ARMOR(null, "%snautilusarmor", "%snautarmor", "%snarmor"),
// Doors
DOOR("[A-Z_]+_(?<!TRAP)DOOR", "%sdoor", "door%s"),
TRAPDOOR(null, "%strapdoor", "%sdoortrap", "%shatch", "%stdoor", "%sdoort", "%strapd", "%sdtrap");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private enum MobType implements CompoundModifier {
PIGLIN_BRUTE("pigbrute", "pigbr", "meaniepiglin", "piglinbr"),
PIGLIN("pigman", "pigmen", "pman", "pigm", "piglin"),
STRAY,
CAMEL_HUSK("deadcamel", "dcamel", "camelhusk", "chusk"),
HUSK,
ZOMBIE_VILLAGER("zvillager", "deadvillager", "dvillager", "zvill", "dvill"),
SKELETON_HORSE("skhorse", "shorse", "bonehorse"),
Expand Down Expand Up @@ -140,6 +141,10 @@ private enum MobType implements CompoundModifier {
BOGGED,
BREEZE,
CREAKING("creak"),
COPPER_GOLEM("coppergolem", "cgolem", "sorter"),
NAUTILUS("nautilus", "naut"),
ZOMBIE_NAUTILUS("zombienautilus", "zombnautilus", "znautilus", "znaut", "zombnaut"),
PARCHED("parched", "parch"),
PLAYER("steve"),
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Stream<String> get(ItemProvider.Item item) {
private enum DiscType implements CompoundType {
// this is scuffed beyond belief
DISC_FRAGMENT("^DISC_FRAGMENT_5", "discfrag", "fragment"),
MUSIC_DISC("^MUSIC_DISC_[A-Z0-9]+", generateFormatsFromNames(MUSIC_DISC_NAMES))
MUSIC_DISC("^MUSIC_DISC_[A-Z0-9_]+", generateFormatsFromNames(MUSIC_DISC_NAMES))
;

private final Pattern regex;
Expand Down Expand Up @@ -82,6 +82,7 @@ private enum Track implements CompoundModifier {
CREATOR_MUSIC_BOX("MUSIC_DISC_CREATOR_MUSIC_BOX", "creatormusicbox", "creatormusicb", "creatormbox", "creatormb"),
PRECIPICE("MUSIC_DISC_PRECIPICE", "precipice"),
TEARS("MUSIC_DISC_TEARS", "tears", "ghast"),
LAVA_CHICKEN("MUSIC_DISC_LAVA_CHICKEN", "lavachicken", "lava", "chicken", "lchicken", "lchick")
;

private final Pattern regex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private enum WoodItemType implements CompoundType {
PROPAGULE(null, "propagule%s", "%sprop", "%ssapling", "%streesapling", "%slogsapling", "%strunksapling", "%swoodsapling"),
MUDDY_ROOTS("^MUDDY_[A-Z_]+_ROOTS", "mud%sroots", "mud%sroot"),
ROOTS("^(?!MUDDY_)[A-Z_]+_ROOTS", "%sroots", "%sroot"),
SHELF(null, "%sshelf", "%srack", "%sledge", "%smantel")
;

private final Pattern regex;
Expand Down
Loading