diff --git a/.gitignore b/.gitignore index 01adc143..a7a5ee51 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,146 @@ +# Maven +target/ +dependency-reduced-pom.xml +pom.xml.releaseBackup +pom.xml.versionsBackup +release.properties + +# Vim +*.swp +*.swo + +# IntelliJ IDEA +.idea/ +*.iml +*.ipr +*.iws + # Eclipse +.settings/ +.classpath .project +/bin/ +*/bin/ + +# User-specific stuff +.idea/ + +*.iml +*.ipr +*.iws + +# IntelliJ +out/ + +# Eclipse .classpath +.project .settings/ -bin/ +plugin/bin/ +api/bin/ -# Maven -/target +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Package Files # +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +target/ + +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next + +release.properties dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +# Common working directory +run/ +build +.gradle +/*.jar diff --git a/adapter_api/.gitignore b/adapter_api/.gitignore new file mode 100644 index 00000000..4f035375 --- /dev/null +++ b/adapter_api/.gitignore @@ -0,0 +1,2 @@ +upload*.sh +.factorypath diff --git a/adapter_api/build.gradle.kts b/adapter_api/build.gradle.kts new file mode 100644 index 00000000..36c1645e --- /dev/null +++ b/adapter_api/build.gradle.kts @@ -0,0 +1,10 @@ +plugins { + id("com.goncalomb.bukkit.java-conventions") +} + +dependencies { + compileOnly("org.spigotmc:spigot-api:1.16.3-R0.1-SNAPSHOT") +} + +description = "adapter_api" +version = rootProject.version diff --git a/src/main/java/com/goncalomb/bukkit/mylib/namemaps/EnchantmentsMap.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/EnchantmentsMap.java similarity index 98% rename from src/main/java/com/goncalomb/bukkit/mylib/namemaps/EnchantmentsMap.java rename to adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/EnchantmentsMap.java index d4d3a5f4..9a330c08 100644 --- a/src/main/java/com/goncalomb/bukkit/mylib/namemaps/EnchantmentsMap.java +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/EnchantmentsMap.java @@ -70,6 +70,7 @@ public final class EnchantmentsMap { _enchantments.put("Multishot", Enchantment.MULTISHOT); _enchantments.put("Piercing", Enchantment.PIERCING); _enchantments.put("QuickCharge", Enchantment.QUICK_CHARGE); + _enchantments.put("SoulSpeed", Enchantment.SOUL_SPEED); List enchantmentNames = new ArrayList(_enchantments.names()); Collections.sort(enchantmentNames, String.CASE_INSENSITIVE_ORDER); diff --git a/src/main/java/com/goncalomb/bukkit/mylib/namemaps/EntityTypeMap.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/EntityTypeMap.java similarity index 99% rename from src/main/java/com/goncalomb/bukkit/mylib/namemaps/EntityTypeMap.java rename to adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/EntityTypeMap.java index c808eeae..78bbf881 100644 --- a/src/main/java/com/goncalomb/bukkit/mylib/namemaps/EntityTypeMap.java +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/EntityTypeMap.java @@ -71,7 +71,7 @@ public final class EntityTypeMap { _legacyNames.put("Zombie".toLowerCase(), EntityType.ZOMBIE); _legacyNames.put("Slime".toLowerCase(), EntityType.SLIME); _legacyNames.put("Ghast".toLowerCase(), EntityType.GHAST); - _legacyNames.put("PigZombie".toLowerCase(), EntityType.PIG_ZOMBIE); + _legacyNames.put("PigZombie".toLowerCase(), EntityType.ZOMBIFIED_PIGLIN); _legacyNames.put("Enderman".toLowerCase(), EntityType.ENDERMAN); _legacyNames.put("CaveSpider".toLowerCase(), EntityType.CAVE_SPIDER); _legacyNames.put("Silverfish".toLowerCase(), EntityType.SILVERFISH); diff --git a/src/main/java/com/goncalomb/bukkit/mylib/namemaps/MaterialMap.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/MaterialMap.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/mylib/namemaps/MaterialMap.java rename to adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/MaterialMap.java diff --git a/src/main/java/com/goncalomb/bukkit/mylib/namemaps/NamingMap.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/NamingMap.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/mylib/namemaps/NamingMap.java rename to adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/NamingMap.java diff --git a/src/main/java/com/goncalomb/bukkit/mylib/namemaps/PotionEffectsMap.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/PotionEffectsMap.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/mylib/namemaps/PotionEffectsMap.java rename to adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/PotionEffectsMap.java diff --git a/src/main/java/com/goncalomb/bukkit/mylib/namemaps/SpawnEggMap.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/SpawnEggMap.java similarity index 93% rename from src/main/java/com/goncalomb/bukkit/mylib/namemaps/SpawnEggMap.java rename to adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/SpawnEggMap.java index 9c919c08..4ecca3e8 100644 --- a/src/main/java/com/goncalomb/bukkit/mylib/namemaps/SpawnEggMap.java +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/namemaps/SpawnEggMap.java @@ -15,7 +15,7 @@ public class SpawnEggMap { if (entityName.equals("MOOSHROOM")) { _spawnEggs.put(EntityTypeMap.getName(EntityType.MUSHROOM_COW), mat); } else if (entityName.equals("ZOMBIE_PIGMAN")) { - _spawnEggs.put(EntityTypeMap.getName(EntityType.PIG_ZOMBIE), mat); + _spawnEggs.put(EntityTypeMap.getName(EntityType.ZOMBIFIED_PIGLIN), mat); } else { EntityType entityType = EntityType.valueOf(entityName); if (entityType != null) { diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflect.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflect.java new file mode 100644 index 00000000..7449d1ab --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflect.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.util.logging.Logger; + +import org.bukkit.command.SimpleCommandMap; + +public final class BukkitReflect { + + private static BukkitReflectAdapter adapter = null; + + public static void prepareReflection(Class serverClass, Logger logger) throws Exception { + String packageName = serverClass.getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + + Class clazz = Class.forName("com.goncalomb.bukkit.mylib.reflect.BukkitReflectAdapter_" + version); + adapter = (BukkitReflectAdapter) clazz.getConstructor().newInstance(); + logger.info("Loaded BukkitReflect adapter for " + version); + } + + + public static SimpleCommandMap getCommandMap() { + ensureAdapter(adapter); + return adapter.getCommandMap(); + } + + public static boolean isValidRawJSON(String text) { + ensureAdapter(adapter); + return adapter.isValidRawJSON(text); + } + + public static String textToRawJSON(String text) { + ensureAdapter(adapter); + return adapter.textToRawJSON(text); + } + + public static void ensureAdapter(Object adapter) throws RuntimeException { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + } +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter.java new file mode 100644 index 00000000..952d41d4 --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import org.bukkit.command.SimpleCommandMap; + +public interface BukkitReflectAdapter { + SimpleCommandMap getCommandMap(); + + boolean isValidRawJSON(String text); + + String textToRawJSON(String text); +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersion.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersion.java new file mode 100644 index 00000000..2c03d15c --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersion.java @@ -0,0 +1,37 @@ +package com.goncalomb.bukkit.mylib.reflect; + +import java.util.logging.Logger; + +public final class BukkitVersion { + + private static BukkitVersionAdapter adapter = null; + + public static void prepareReflection(Class serverClass, Logger logger) throws Exception { + String packageName = serverClass.getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + + Class clazz = Class.forName("com.goncalomb.bukkit.mylib.reflect.BukkitVersionAdapter_" + version); + adapter = (BukkitVersionAdapter) clazz.getConstructor().newInstance(); + logger.info("Loaded BukkitVersion adapter for " + version); + } + + public static void prepareForTest() throws Exception { + Class clazz = Class.forName("com.goncalomb.bukkit.mylib.reflect.BukkitVersionAdapter_v1_18_R2"); + adapter = (BukkitVersionAdapter) clazz.getConstructor().newInstance(); + } + + public static boolean isVersion(int minor) { + return isVersion(minor, 1); + } + + public static boolean isVersion(int minor, int major) { + ensureAdapter(adapter); + return adapter.isVersion(minor, major); + } + + private static void ensureAdapter(Object adapter) throws RuntimeException { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + } +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter.java new file mode 100644 index 00000000..597f73de --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +public interface BukkitVersionAdapter { + boolean isVersion(int minor, int major); +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBase.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBase.java new file mode 100644 index 00000000..7bebb824 --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBase.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.util.logging.Logger; + +public class NBTBase { + + private static NBTBaseAdapter adapter = null; + + public static void prepareReflection(Class serverClass, Logger logger) throws Exception { + String packageName = serverClass.getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + + Class clazz = Class.forName("com.goncalomb.bukkit.mylib.reflect.NBTBaseAdapter_" + version); + adapter = (NBTBaseAdapter) clazz.getConstructor().newInstance(); + logger.info("Loaded NBTBase adapter for " + version); + } + + final Object _handle; // The wrapped Minecraft NBTBase instance. + + // Wraps any Minecraft tags in MyLib tags. + // Primitives and strings are wrapped with NBTBase. + protected static final NBTBase wrap(Object object) { + ensureAdapter(adapter); + return adapter.wrap(object); + } + + // Helper method for NBTTagCompoundWrapper.merge(). + // Clones any internal Minecraft tags. + protected static final Object clone(Object nbtBaseObject) { + ensureAdapter(adapter); + return adapter.clone(nbtBaseObject); + } + + static byte getTypeId(Object handle) { + ensureAdapter(adapter); + return adapter.getTypeId(handle); + } + + protected NBTBase(Object handle) { + _handle = handle; + } + + public NBTBase clone() { + return wrap(clone(_handle)); + } + + @Override + public String toString() { + return _handle.toString(); + } + + private static void ensureAdapter(Object adapter) throws RuntimeException { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + } +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter.java new file mode 100644 index 00000000..086fa63b --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +public interface NBTBaseAdapter { + // Wraps any Minecraft tags in MyLib tags. + // Primitives and strings are wrapped with NBTBase. + NBTBase wrap(Object object); + + // Helper method for NBTTagCompoundWrapper.merge(). + // Clones any internal Minecraft tags. + Object clone(Object nbtBaseObject); + + byte getTypeId(Object handle); +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompound.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompound.java new file mode 100644 index 00000000..384c7d16 --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompound.java @@ -0,0 +1,244 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; +import java.util.Map.Entry; +import java.util.logging.Logger; + +public final class NBTTagCompound extends NBTBase { + + private static NBTTagCompoundAdapter adapter = null; + + public static void prepareReflection(Class serverClass, Logger logger) throws Exception { + String packageName = serverClass.getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + + Class clazz = Class.forName("com.goncalomb.bukkit.mylib.reflect.NBTTagCompoundAdapter_" + version); + adapter = (NBTTagCompoundAdapter) clazz.getConstructor().newInstance(); + logger.info("Loaded NBTTagCompound adapter for " + version); + } + + private final Map _map; + + public NBTTagCompound() { + this(adapter.newInternalInstance()); + } + + public NBTTagCompound(Object handle) { + super(handle); + _map = adapter.getMap(handle); + } + + public byte getByte(String key) { + return adapter.getByte(_handle, key); + } + + public short getShort(String key) { + ensureAdapter(adapter); + return adapter.getShort(_handle, key); + } + + public int getInt(String key) { + ensureAdapter(adapter); + return adapter.getInt(_handle, key); + } + + public long getLong(String key) { + ensureAdapter(adapter); + return adapter.getLong(_handle, key); + } + + public float getFloat(String key) { + ensureAdapter(adapter); + return adapter.getFloat(_handle, key); + } + + public Double getDouble(String key) { + ensureAdapter(adapter); + return adapter.getDouble(_handle, key); + } + + public String getString(String key) { + ensureAdapter(adapter); + return adapter.getString(_handle, key); + } + + public byte[] getByteArray(String key) { + ensureAdapter(adapter); + return adapter.getByteArray(_handle, key); + } + + public int[] getIntArray(String key) { + ensureAdapter(adapter); + return adapter.getIntArray(_handle, key); + } + + public long[] getLongArray(String key) { + ensureAdapter(adapter); + return adapter.getLongArray(_handle, key); + } + + public NBTTagCompound getCompound(String key) { + ensureAdapter(adapter); + return adapter.getCompound(_handle, key); + } + + public NBTTagList getList(String key) { + ensureAdapter(adapter); + return adapter.getList(_handle, key); + } + + public Object[] getListAsArray(String key) { + NBTTagList list = getList(key); + return (list == null ? null : list.getAsArray()); + } + + public void setByte(String key, byte value) { + set(key, value); + } + + public void setShort(String key, short value) { + set(key, value); + } + + public void setInt(String key, int value) { + set(key, value); + } + + public void setLong(String key, long value) { + set(key, value); + } + + public void setFloat(String key, float value) { + set(key, value); + } + + public void setDouble(String key, double value) { + set(key, value); + } + + public void setString(String key, String value) { + set(key, value); + } + + public void setCompound(String key, NBTTagCompound value) { + set(key, value); + } + + public void setList(String key, NBTTagList value) { + set(key, value); + } + + public void setList(String key, Object... objects) { + set(key, new NBTTagList(objects)); + } + + public void setByteArray(String key, byte[] value) { + set(key, value); + } + + public void setIntArray(String key, int[] value) { + set(key, value); + } + + public void setLongArray(String key, long[] value) { + set(key, value); + } + + private void set(String key, Object value) { + ensureAdapter(adapter); + adapter.set(_handle, key, value); + } + + public boolean hasKey(String key) { + return _map.containsKey(key); + } + + public void remove(String key) { + _map.remove(key); + } + + public int size() { + return _map.size(); + } + + public boolean isEmpty() { + return _map.isEmpty(); + } + + public void clear() { + _map.clear(); + } + + public byte[] serialize() throws IOException { + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + serialize(out); + return out.toByteArray(); + } + } + + public void serialize(OutputStream outputStream) throws IOException { + ensureAdapter(adapter); + adapter.serialize(_handle, outputStream); + } + + public static NBTTagCompound unserialize(byte[] data) throws IOException { + try (ByteArrayInputStream in = new ByteArrayInputStream(data)) { + return unserialize(in); + } + } + + public static NBTTagCompound unserialize(InputStream inputStream) throws IOException { + ensureAdapter(adapter); + return adapter.unserialize(inputStream); + } + + public void merge(NBTTagCompound other) { + for (Entry tag : other._map.entrySet()) { + _map.put(tag.getKey(), NBTBase.clone(tag.getValue())); + } + } + + @Override + public NBTTagCompound clone() { + return (NBTTagCompound) super.clone(); + } + + /* + * Converts a string-ified "mojangson" tag back into an NBTTagCompound + * This is the opposite of toString() + */ + public static NBTTagCompound fromString(String mojangson) throws Exception { + ensureAdapter(adapter); + return adapter.fromString(mojangson); + } + + private static void ensureAdapter(Object adapter) throws RuntimeException { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + } +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter.java new file mode 100644 index 00000000..fd4b8672 --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; + +public interface NBTTagCompoundAdapter { + Object newInternalInstance(); + + Map getMap(Object handle); + + byte getByte(Object handle, String key); + + short getShort(Object handle, String key); + + int getInt(Object handle, String key); + + long getLong(Object handle, String key); + + float getFloat(Object handle, String key); + + Double getDouble(Object handle, String key); + + String getString(Object handle, String key); + + byte[] getByteArray(Object handle, String key); + + int[] getIntArray(Object handle, String key); + + long[] getLongArray(Object handle, String key); + + NBTTagCompound getCompound(Object handle, String key); + + NBTTagList getList(Object handle, String key); + + void set(Object handle, String key, Object value); + + void serialize(Object handle, OutputStream outputStream) throws IOException; + + NBTTagCompound unserialize(InputStream inputStream) throws IOException; + + NBTTagCompound fromString(String mojangson) throws Exception; +} diff --git a/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagList.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagList.java similarity index 63% rename from src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagList.java rename to adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagList.java index 865fe0c7..7d61874c 100644 --- a/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagList.java +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagList.java @@ -19,34 +19,35 @@ package com.goncalomb.bukkit.mylib.reflect; -import java.lang.reflect.Field; import java.util.List; +import java.util.logging.Logger; public final class NBTTagList extends NBTBase { - private static Field _typeField; - private static Field _listField; - List _list; + private static NBTTagListAdapter adapter = null; - static void prepareReflectionz() throws SecurityException, NoSuchMethodException, NoSuchFieldException { - _typeField = _nbtTagListClass.getDeclaredField("type"); - _typeField.setAccessible(true); - _listField = _nbtTagListClass.getDeclaredField("list"); - _listField.setAccessible(true); + public static void prepareReflection(Class serverClass, Logger logger) throws Exception { + String packageName = serverClass.getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + + Class clazz = Class.forName("com.goncalomb.bukkit.mylib.reflect.NBTTagListAdapter_" + version); + adapter = (NBTTagListAdapter) clazz.getConstructor().newInstance(); + logger.info("Loaded NBTTagList adapter for " + version); } + private final List _list; + public NBTTagList() { - this(BukkitReflect.newInstance(_nbtTagListClass)); + this(adapter.newInternalInstance()); } - @SuppressWarnings("unchecked") NBTTagList(Object handle) { super(handle); - _list = (List) BukkitReflect.getFieldValue(handle, _listField); + _list = adapter.getList(handle); } public NBTTagList(Object... values) { - this(BukkitReflect.newInstance(_nbtTagListClass)); + this(adapter.newInternalInstance()); for (Object value : values) { add(value); } @@ -57,9 +58,8 @@ public Object get(int index) { } public void add(Object value) { - Object handle = NBTTypes.toInternal(value); - BukkitReflect.setFieldValue(_handle, _typeField, NBTBase.getTypeId(handle)); - _list.add(handle); + ensureAdapter(adapter); + adapter.add(_handle, value); } public Object remove(int index) { @@ -84,4 +84,9 @@ public NBTTagList clone() { return (NBTTagList) super.clone(); } + private static void ensureAdapter(Object adapter) throws RuntimeException { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + } } diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter.java new file mode 100644 index 00000000..6e980229 --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.util.List; + +public interface NBTTagListAdapter { + Object newInternalInstance(); + + List getList(Object handle); + + void add(Object handle, Object value); +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypes.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypes.java new file mode 100644 index 00000000..ebafef93 --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypes.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.util.logging.Logger; + +public final class NBTTypes { + + private static NBTTypesAdapter adapter = null; + + public static void prepareReflection(Class serverClass, Logger logger) throws Exception { + String packageName = serverClass.getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + + Class clazz = Class.forName("com.goncalomb.bukkit.mylib.reflect.NBTTypesAdapter_" + version); + adapter = (NBTTypesAdapter) clazz.getConstructor().newInstance(); + logger.info("Loaded NBTTypes adapter for " + version); + } + + // Converts from MyLib tags, primitives and strings to internal Minecraft tags. + public static Object toInternal(Object object) { + ensureAdapter(adapter); + return adapter.toInternal(object); + } + + // Converts internal Minecraft tags to MyLib tags, primitives and strings. + public static Object fromInternal(Object object) { + ensureAdapter(adapter); + return adapter.fromInternal(object); + } + + private static void ensureAdapter(Object adapter) throws RuntimeException { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + } +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter.java new file mode 100644 index 00000000..c86ac1ec --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +public interface NBTTypesAdapter { + // Converts from MyLib tags, primitives and strings to internal Minecraft tags. + public Object toInternal(Object object); + + // Converts internal Minecraft tags to MyLib tags, primitives and strings. + public Object fromInternal(Object object); +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtils.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtils.java new file mode 100644 index 00000000..e7072c92 --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtils.java @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.util.logging.Logger; + +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.entity.Entity; +import org.bukkit.inventory.ItemStack; + +public final class NBTUtils { + + private static NBTUtilsAdapter adapter = null; + + public static void prepareReflection(Class serverClass, Logger logger) throws Exception { + String packageName = serverClass.getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + + Class clazz = Class.forName("com.goncalomb.bukkit.mylib.reflect.NBTUtilsAdapter_" + version); + adapter = (NBTUtilsAdapter) clazz.getConstructor().newInstance(); + logger.info("Loaded NBTUtils adapter for " + version); + } + + public static ItemStack itemStackFromNBTData(NBTTagCompound data) { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + return adapter.itemStackFromNBTData(data); + } + + public static NBTTagCompound itemStackToNBTData(ItemStack stack) { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + return adapter.itemStackToNBTData(stack); + } + + public static Entity spawnEntity(NBTTagCompound data, Location location) { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + return adapter.spawnEntity(data, location); + } + + public static NBTTagCompound getEntityNBTData(Entity entity) { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + return adapter.getEntityNBTData(entity); + } + + public static NBTTagList potionToNBTEffectsList(ItemStack potion) { + NBTTagCompound tag = getItemStackTag(potion); + if (tag.hasKey("CustomPotionEffects")) { + return tag.getList("CustomPotionEffects").clone(); + } + // Fallback to default potion effect. + + // XXX: implement fallback + + // Finding the default potion effect is not trivial on 1.9. + // Wait until org.bukkit.craftbukkit.potion.CraftPotionUtil is available upstream. + // For now, display some alert messages on InventoryForMobs and InventoryForThrownPotion. + return new NBTTagList(); + } + + public static ItemStack potionFromNBTEffectsList(NBTTagList effects) { + NBTTagCompound tag = new NBTTagCompound(); + tag.setList("CustomPotionEffects", effects.clone()); + tag.setString("Potion", "minecraft:mundane"); + NBTTagCompound data = new NBTTagCompound(); + data.setString("id", "minecraft:potion"); + data.setByte("Count", (byte) 1); + data.setShort("Damage", (short) 0); + data.setCompound("tag", tag); + return itemStackFromNBTData(data); + } + + public static NBTTagCompound getTileEntityNBTData(Block block) { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + return adapter.getTileEntityNBTData(block); + } + + public static void setTileEntityNBTData(Block block, NBTTagCompound data) { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + adapter.setTileEntityNBTData(block, data); + } + + public static NBTTagCompound getItemStackTag(ItemStack item) { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + return adapter.getItemStackTag(item); + } + + public static void setItemStackTag(ItemStack item, NBTTagCompound tag) { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + adapter.setItemStackTag(item, tag); + } + + public static ItemStack itemStackToCraftItemStack(ItemStack item) { + if (adapter == null) { + throw new RuntimeException("Version adapter is not loaded"); + } + return adapter.itemStackToCraftItemStack(item); + } +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter.java b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter.java new file mode 100644 index 00000000..b50fbb91 --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.entity.Entity; +import org.bukkit.inventory.ItemStack; + +public interface NBTUtilsAdapter { + ItemStack itemStackFromNBTData(NBTTagCompound data); + + NBTTagCompound itemStackToNBTData(ItemStack stack); + + Entity spawnEntity(NBTTagCompound data, Location location); + + NBTTagCompound getEntityNBTData(Entity entity); + + NBTTagCompound getTileEntityNBTData(Block block); + + void setTileEntityNBTData(Block block, NBTTagCompound data); + + NBTTagCompound getItemStackTag(ItemStack item); + + void setItemStackTag(ItemStack item, NBTTagCompound tag); + + ItemStack itemStackToCraftItemStack(ItemStack item); +} diff --git a/adapter_api/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter.java b/adapter_api/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter.java new file mode 100644 index 00000000..ca1a04cb --- /dev/null +++ b/adapter_api/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.nbteditor.nbt; + +import java.util.List; + +import com.goncalomb.bukkit.mylib.reflect.NBTTagCompound; + +import org.bukkit.entity.EntityType; + +public interface SpawnerNBTWrapperAdapter { + public static class SpawnerAdapterWrappedEntity { + public NBTTagCompound data; + public int weight; + + public SpawnerAdapterWrappedEntity(NBTTagCompound data, int weight) { + this.data = data; + this.weight = weight; + } + } + + void addEntity(NBTTagCompound handle, SpawnerAdapterWrappedEntity entity); + + List getEntities(NBTTagCompound handle); + + void setEntities(NBTTagCompound handle, List entities); + + EntityType getCurrentEntity(NBTTagCompound handle); +} diff --git a/adapter_v1_16_R3/.gitignore b/adapter_v1_16_R3/.gitignore new file mode 100644 index 00000000..4f035375 --- /dev/null +++ b/adapter_v1_16_R3/.gitignore @@ -0,0 +1,2 @@ +upload*.sh +.factorypath diff --git a/adapter_v1_16_R3/build.gradle.kts b/adapter_v1_16_R3/build.gradle.kts new file mode 100644 index 00000000..1d457847 --- /dev/null +++ b/adapter_v1_16_R3/build.gradle.kts @@ -0,0 +1,11 @@ +plugins { + id("com.goncalomb.bukkit.java-conventions") +} + +dependencies { + implementation(project(":adapter_api")) + compileOnly("com.destroystokyo.paper:paper:1.16.5-R0.1-SNAPSHOT") +} + +description = "adapter_v1_16_R3" +version = rootProject.version diff --git a/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflect.java b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter_v1_16_R3.java similarity index 93% rename from src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflect.java rename to adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter_v1_16_R3.java index 6ca1e4b3..a5137765 100644 --- a/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflect.java +++ b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter_v1_16_R3.java @@ -25,12 +25,12 @@ import java.lang.reflect.Method; import java.util.HashMap; +import com.google.gson.JsonParseException; + import org.bukkit.Bukkit; import org.bukkit.command.SimpleCommandMap; -import com.google.gson.JsonParseException; - -public final class BukkitReflect { +public final class BukkitReflectAdapter_v1_16_R3 implements BukkitReflectAdapter { private final static class CachedPackage { @@ -53,7 +53,6 @@ public Class getClass(String className) { } return clazz; } - } private static boolean _isPrepared = false; @@ -66,7 +65,7 @@ public Class getClass(String className) { private static Method _ChatSerializer_a_unserialize; private static Constructor _ChatComponentTextClass_contructor; - public static void prepareReflection() { + public BukkitReflectAdapter_v1_16_R3() throws Exception { if (!_isPrepared) { Class craftServerClass = Bukkit.getServer().getClass(); _craftBukkitPackage = new CachedPackage(craftServerClass.getPackage().getName()); @@ -93,25 +92,19 @@ public static void prepareReflection() { } } - private BukkitReflect() { } - public static Class getCraftBukkitClass(String className) { - prepareReflection(); return _craftBukkitPackage.getClass(className); } public static Class getMinecraftClass(String className) { - prepareReflection(); return _minecraftPackage.getClass(className); } - public static SimpleCommandMap getCommandMap() { - prepareReflection(); + public SimpleCommandMap getCommandMap() { return (SimpleCommandMap) invokeMethod(Bukkit.getServer(), _getCommandMap); } - public static boolean isValidRawJSON(String text) { - prepareReflection(); + public boolean isValidRawJSON(String text) { try { _ChatSerializer_a_unserialize.invoke(null, text); return true; @@ -125,8 +118,7 @@ public static boolean isValidRawJSON(String text) { } } - public static String textToRawJSON(String text) { - prepareReflection(); + public String textToRawJSON(String text) { try { return (String) _ChatSerializer_a_serialize.invoke(null, _ChatComponentTextClass_contructor.newInstance(text)); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException diff --git a/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter_v1_16_R3.java b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter_v1_16_R3.java new file mode 100644 index 00000000..3682dd2c --- /dev/null +++ b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter_v1_16_R3.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.bukkit.Bukkit; + +public class BukkitVersionAdapter_v1_16_R3 implements BukkitVersionAdapter { + private final int _minecraftVersionMajor; + private final int _minecraftVersionMinor; + + public BukkitVersionAdapter_v1_16_R3() throws Exception { + Object server = Bukkit.getServer(); + if (server == null) { + // test environment, CraftBukkit / Minecraft not available + _minecraftVersionMajor = Integer.MAX_VALUE; + _minecraftVersionMinor = Integer.MAX_VALUE; + return; + } + try { + Object mcServer = BukkitReflectAdapter_v1_16_R3.invokeMethod(server, BukkitReflectAdapter_v1_16_R3.getCraftBukkitClass("CraftServer").getDeclaredMethod("getServer")); + String version = (String) BukkitReflectAdapter_v1_16_R3.invokeMethod(mcServer, BukkitReflectAdapter_v1_16_R3.getMinecraftClass("MinecraftServer").getDeclaredMethod("getVersion")); + Matcher matcher = Pattern.compile("^(\\d+)(?:\\.(\\d+)(?:\\.(\\d+))?)?$").matcher(version); + if (matcher.find()) { + _minecraftVersionMajor = Integer.parseInt(matcher.group(1)); + _minecraftVersionMinor = Integer.parseInt(matcher.group(2)); + } else { + throw new RuntimeException("Invalid Minecraft version: " + version); + } + } catch (Exception e) { + throw new RuntimeException("Error finding Minecraft version.", e); + } + } + + public boolean isVersion(int minor, int major) { + return _minecraftVersionMajor > major || (_minecraftVersionMajor == major && _minecraftVersionMinor >= minor); + } +} diff --git a/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter_v1_16_R3.java b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter_v1_16_R3.java new file mode 100644 index 00000000..08aab3f4 --- /dev/null +++ b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter_v1_16_R3.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.lang.reflect.Method; + +public class NBTBaseAdapter_v1_16_R3 implements NBTBaseAdapter { + + protected static Class _nbtBaseClass; + protected static Class _nbtTagCompoundClass; + protected static Class _nbtTagListClass; + protected static Class _nbtTagStringClass; + + private static Method _getTypeId; + private static Method _clone; + + public NBTBaseAdapter_v1_16_R3() { + _nbtBaseClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("NBTBase"); + _nbtTagCompoundClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("NBTTagCompound"); + _nbtTagListClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("NBTTagList"); + _nbtTagStringClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("NBTTagString"); + try { + _getTypeId = _nbtBaseClass.getMethod("getTypeId"); + _clone = _nbtBaseClass.getMethod("clone"); + } catch (Exception e) { + throw new RuntimeException("Error while preparing NBT wrapper classes.", e); + } + } + + @Override + public NBTBase wrap(Object object) { + if (_nbtTagCompoundClass.isInstance(object)) { + return new NBTTagCompound(object); + } else if (_nbtTagListClass.isInstance(object)) { + return new NBTTagList(object); + } else if (_nbtBaseClass.isInstance(object)) { + return new NBTBase(object); + } else { + throw new RuntimeException(object.getClass() + " is not a valid NBT tag type."); + } + } + + @Override + public Object clone(Object nbtBaseObject) { + return BukkitReflectAdapter_v1_16_R3.invokeMethod(nbtBaseObject, _clone); + } + + @Override + public byte getTypeId(Object handle) { + return (Byte) BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _getTypeId); + } +} diff --git a/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter_v1_16_R3.java b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter_v1_16_R3.java new file mode 100644 index 00000000..e6584a62 --- /dev/null +++ b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter_v1_16_R3.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Map; + +public final class NBTTagCompoundAdapter_v1_16_R3 implements NBTTagCompoundAdapter { + + protected static Class _nbtTagListClass; + protected static Class _nbtTagCompoundClass; + private static Method _getByte; + private static Method _getShort; + private static Method _getInt; + private static Method _getLong; + private static Method _getFloat; + private static Method _getDouble; + private static Method _getString; + private static Method _getByteArray; + private static Method _getIntArray; + private static Method _getLongArray; + private static Field _mapField; + + private static Method _tagSerializeStream; + private static Method _tagUnserializeStream; + private static Method _parseMojangson; + + public NBTTagCompoundAdapter_v1_16_R3() throws Exception { + _nbtTagCompoundClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("NBTTagCompound"); + _nbtTagListClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("NBTTagList"); + _getByte = _nbtTagCompoundClass.getMethod("getByte", String.class); + _getShort = _nbtTagCompoundClass.getMethod("getShort", String.class); + _getInt = _nbtTagCompoundClass.getMethod("getInt", String.class); + _getLong = _nbtTagCompoundClass.getMethod("getLong", String.class); + _getFloat = _nbtTagCompoundClass.getMethod("getFloat", String.class); + _getDouble = _nbtTagCompoundClass.getMethod("getDouble", String.class); + _getString = _nbtTagCompoundClass.getMethod("getString", String.class); + _getByteArray = _nbtTagCompoundClass.getMethod("getByteArray", String.class); + _getIntArray = _nbtTagCompoundClass.getMethod("getIntArray", String.class); + _getLongArray = _nbtTagCompoundClass.getMethod("getLongArray", String.class); + _mapField = _nbtTagCompoundClass.getDeclaredField("map"); + _mapField.setAccessible(true); + + Class_mojangsonParserClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("MojangsonParser"); + _parseMojangson = _mojangsonParserClass.getMethod("parse", String.class); + + Class nbtCompressedStreamToolsClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("NBTCompressedStreamTools"); + _tagSerializeStream = nbtCompressedStreamToolsClass.getMethod("a", _nbtTagCompoundClass, OutputStream.class); + _tagUnserializeStream = nbtCompressedStreamToolsClass.getMethod("a", InputStream.class); + } + + public Object newInternalInstance() { + return BukkitReflectAdapter_v1_16_R3.newInstance(_nbtTagCompoundClass); + } + + @SuppressWarnings("unchecked") + public Map getMap(Object handle) { + return (Map) BukkitReflectAdapter_v1_16_R3.getFieldValue(handle, _mapField); + } + + public byte getByte(Object handle, String key) { + return (Byte) BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _getByte, key); + } + + public short getShort(Object handle, String key) { + return (Short) BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _getShort, key); + } + + public int getInt(Object handle, String key) { + return (Integer) BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _getInt, key); + } + + public long getLong(Object handle, String key) { + return (Long) BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _getLong, key); + } + + public float getFloat(Object handle, String key) { + return (Float) BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _getFloat, key); + } + + public Double getDouble(Object handle, String key) { + return (Double) BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _getDouble, key); + } + + public String getString(Object handle, String key) { + return (String) BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _getString, key); + } + + public byte[] getByteArray(Object handle, String key) { + return (byte[]) BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _getByteArray, key); + } + + public int[] getIntArray(Object handle, String key) { + return (int[]) BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _getIntArray, key); + } + + public long[] getLongArray(Object handle, String key) { + return (long[]) BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _getLongArray, key); + } + + public NBTTagCompound getCompound(Object handle, String key) { + Object obj = getMap(handle).get(key); + if (obj != null && _nbtTagCompoundClass.isInstance(obj)) { + return new NBTTagCompound(obj); + } + return null; + } + + public NBTTagList getList(Object handle, String key) { + Object obj = getMap(handle).get(key); + if (obj != null && _nbtTagListClass.isInstance(obj)) { + return new NBTTagList(obj); + } + return null; + } + + public void set(Object handle, String key, Object value) { + getMap(handle).put(key, NBTTypes.toInternal(value)); + } + + public void serialize(Object handle, OutputStream outputStream) throws IOException { + BukkitReflectAdapter_v1_16_R3.invokeMethod(null, _tagSerializeStream, handle, outputStream); + } + + public NBTTagCompound unserialize(InputStream inputStream) throws IOException { + return new NBTTagCompound(BukkitReflectAdapter_v1_16_R3.invokeMethod(null, _tagUnserializeStream, inputStream)); + } + + public NBTTagCompound fromString(String mojangson) throws Exception { + return new NBTTagCompound(BukkitReflectAdapter_v1_16_R3.invokeMethod(null, _parseMojangson, mojangson)); + } +} diff --git a/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter_v1_16_R3.java b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter_v1_16_R3.java new file mode 100644 index 00000000..de0bfa44 --- /dev/null +++ b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter_v1_16_R3.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.lang.reflect.Field; +import java.util.List; + +public final class NBTTagListAdapter_v1_16_R3 implements NBTTagListAdapter { + + private static Field _typeField; + private static Field _listField; + protected static Class _nbtTagListClass; + + public NBTTagListAdapter_v1_16_R3() throws Exception { + _nbtTagListClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("NBTTagList"); + _typeField = _nbtTagListClass.getDeclaredField("type"); + _typeField.setAccessible(true); + _listField = _nbtTagListClass.getDeclaredField("list"); + _listField.setAccessible(true); + } + + public Object newInternalInstance() { + return BukkitReflectAdapter_v1_16_R3.newInstance(_nbtTagListClass); + } + + @SuppressWarnings("unchecked") + public List getList(Object handle) { + return (List) BukkitReflectAdapter_v1_16_R3.getFieldValue(handle, _listField); + } + + public void add(Object handle, Object value) { + Object addHandle = NBTTypes.toInternal(value); + BukkitReflectAdapter_v1_16_R3.setFieldValue(handle, _typeField, NBTBase.getTypeId(addHandle)); + getList(handle).add(addHandle); + } +} diff --git a/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypes.java b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter_v1_16_R3.java similarity index 55% rename from src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypes.java rename to adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter_v1_16_R3.java index 582cc024..38cb90a7 100644 --- a/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypes.java +++ b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter_v1_16_R3.java @@ -25,17 +25,38 @@ import org.apache.commons.lang.ClassUtils; -final class NBTTypes { +public final class NBTTypesAdapter_v1_16_R3 implements NBTTypesAdapter { - private static HashMap, NBTTypes> _innerTypeMap = new HashMap, NBTTypes>();; - private static HashMap, NBTTypes> _outerTypeMap = new HashMap, NBTTypes>();; + private static final class NBTTypes { + private Class _class; + private Constructor _constructor; + private Field _data; + private Class _dataType; - private Class _class; - private Constructor _constructor; - private Field _data; - private Class _dataType; + private NBTTypes(String tagClassName) throws SecurityException, NoSuchMethodException, NoSuchFieldException { + _class = BukkitReflectAdapter_v1_16_R3.getMinecraftClass(tagClassName); + _data = _class.getDeclaredField("data"); + _data.setAccessible(true); + _dataType = _data.getType(); + _constructor = _class.getDeclaredConstructor(_dataType); + _constructor.setAccessible(true); + } + + // Wraps primitives and strings with Minecraft tags. + private Object wrap(Object innerObject) { + return BukkitReflectAdapter_v1_16_R3.newInstance(_constructor, innerObject); + } + + // Unwraps primitives and strings from Minecraft tags. + private Object unwrap(Object tagObject) { + return BukkitReflectAdapter_v1_16_R3.getFieldValue(tagObject, _data); + } + } + + private HashMap, NBTTypes> _innerTypeMap = new HashMap, NBTTypes>();; + private HashMap, NBTTypes> _outerTypeMap = new HashMap, NBTTypes>();; - public static void prepareReflection() throws SecurityException, NoSuchMethodException, NoSuchFieldException { + public NBTTypesAdapter_v1_16_R3() throws Exception { registerNew("NBTTagByte"); registerNew("NBTTagShort"); registerNew("NBTTagInt"); @@ -43,16 +64,19 @@ public static void prepareReflection() throws SecurityException, NoSuchMethodExc registerNew("NBTTagFloat"); registerNew("NBTTagDouble"); registerNew("NBTTagString"); + registerNew("NBTTagByteArray"); + registerNew("NBTTagIntArray"); + // TagLongArray's internal field name is 'b' instead of 'data' for some absurd reason. + // Since it isn't used, don't bother working around this } - private static void registerNew(String tagClassName) throws SecurityException, NoSuchMethodException, NoSuchFieldException { + private void registerNew(String tagClassName) throws SecurityException, NoSuchMethodException, NoSuchFieldException { NBTTypes handler = new NBTTypes(tagClassName); _innerTypeMap.put((handler._dataType.isPrimitive() ? ClassUtils.primitiveToWrapper(handler._dataType) : handler._dataType), handler); _outerTypeMap.put(handler._class, handler); } - // Converts from MyLib tags, primitives and strings to internal Minecraft tags. - public static Object toInternal(Object object) { + public Object toInternal(Object object) { if (object instanceof NBTBase) { return ((NBTBase) object)._handle; } else { @@ -65,8 +89,7 @@ public static Object toInternal(Object object) { } } - // Converts internal Minecraft tags to MyLib tags, primitives and strings. - public static Object fromInternal(Object object) { + public Object fromInternal(Object object) { if (object == null) { return null; } @@ -77,24 +100,4 @@ public static Object fromInternal(Object object) { return NBTBase.wrap(object); } } - - private NBTTypes(String tagClassName) throws SecurityException, NoSuchMethodException, NoSuchFieldException { - _class = BukkitReflect.getMinecraftClass(tagClassName); - _data = _class.getDeclaredField("data"); - _data.setAccessible(true); - _dataType = _data.getType(); - _constructor = _class.getDeclaredConstructor(_dataType); - _constructor.setAccessible(true); - } - - // Wraps primitives and strings with Minecraft tags. - private Object wrap(Object innerObject) { - return BukkitReflect.newInstance(_constructor, innerObject); - } - - // Unwraps primitives and strings from Minecraft tags. - private Object unwrap(Object tagObject) { - return BukkitReflect.getFieldValue(tagObject, _data); - } - } diff --git a/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter_v1_16_R3.java b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter_v1_16_R3.java new file mode 100644 index 00000000..b0e13169 --- /dev/null +++ b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter_v1_16_R3.java @@ -0,0 +1,189 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.function.Function; + +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.entity.Entity; +import org.bukkit.inventory.ItemStack; + +public final class NBTUtilsAdapter_v1_16_R3 implements NBTUtilsAdapter { + + // Minecraft's ItemStack Class; + private Constructor _ItemStack_nbtConstructor; + private Method _ItemStack_save; + private Method _ItemStack_getTag; + private Method _ItemStack_setTag; + + // CraftItemStack Class; + private Method _CraftItemStack_asCraftMirror; + private Method _CraftItemStack_asCraftCopy; + private Field _CraftItemStack_handle; + + // Minecraft's Entity Class; + private Method _Entity_save; + private Method _Entity_getBukkitEntity; + private Method _Entity_setPosition; + + // CraftEntity Class + private Method _CraftEntity_getHandle; + + // Minecraft's TileEntity + private Method _TileEntity_load; // Load data from NBTTagCompound. + private Method _TileEntity_save; // Save data to NBTTagCompound. + + // CraftWorld + private Method _CraftWorld_getHandle; + + // Minecraft's BlockPosition + private Constructor _BlockPosition_constructor; + + // Minecraft's World + private Method _World_getTileEntity; + private Method _WorldServer_addAllEntitiesSafely; + + // Minecraft's EntityTypes Class + private Method _EntityTypes_a; // Spawn an entity from a NBTCompound. + + public NBTUtilsAdapter_v1_16_R3() throws SecurityException, NoSuchMethodException, NoSuchFieldException { + Class nbtTagCompoundClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("NBTTagCompound"); + + Class minecraftItemStackClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("ItemStack"); + _ItemStack_nbtConstructor = minecraftItemStackClass.getDeclaredConstructor(nbtTagCompoundClass); + _ItemStack_nbtConstructor.setAccessible(true); + _ItemStack_save = minecraftItemStackClass.getMethod("save", nbtTagCompoundClass); + _ItemStack_getTag = minecraftItemStackClass.getMethod("getTag"); + _ItemStack_setTag = minecraftItemStackClass.getMethod("setTag", nbtTagCompoundClass); + + Class craftItemStackClass = BukkitReflectAdapter_v1_16_R3.getCraftBukkitClass("inventory.CraftItemStack"); + _CraftItemStack_asCraftMirror = craftItemStackClass.getMethod("asCraftMirror", minecraftItemStackClass); + _CraftItemStack_asCraftCopy = craftItemStackClass.getMethod("asCraftCopy", ItemStack.class); + _CraftItemStack_handle = craftItemStackClass.getDeclaredField("handle"); + _CraftItemStack_handle.setAccessible(true); + + Class minecraftEntityClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("Entity"); + _Entity_save = minecraftEntityClass.getMethod("save", nbtTagCompoundClass); + _Entity_getBukkitEntity = minecraftEntityClass.getMethod("getBukkitEntity"); + _Entity_setPosition = minecraftEntityClass.getMethod("setPosition", double.class, double.class, double.class); + + Class craftEntityClass = BukkitReflectAdapter_v1_16_R3.getCraftBukkitClass("entity.CraftEntity"); + _CraftEntity_getHandle = craftEntityClass.getMethod("getHandle"); + + Class minecraftTileEntityClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("TileEntity"); + _TileEntity_save = minecraftTileEntityClass.getMethod("save", nbtTagCompoundClass); + Class minecraftIBlockDataClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("IBlockData"); + _TileEntity_load = minecraftTileEntityClass.getMethod("load", minecraftIBlockDataClass, nbtTagCompoundClass); + + Class craftWorldClass = BukkitReflectAdapter_v1_16_R3.getCraftBukkitClass("CraftWorld"); + _CraftWorld_getHandle = craftWorldClass.getMethod("getHandle"); + + Class minecraftBlockPositionClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("BlockPosition"); + _BlockPosition_constructor = minecraftBlockPositionClass.getConstructor(int.class, int.class, int.class); + + Class minecraftWorldClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("World"); + _World_getTileEntity = minecraftWorldClass.getMethod("getTileEntity", minecraftBlockPositionClass); + + Class minecraftWorldServerClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("WorldServer"); + _WorldServer_addAllEntitiesSafely = minecraftWorldServerClass.getMethod("addAllEntitiesSafely", minecraftEntityClass); + + Class minecraftEntityTypesClass = BukkitReflectAdapter_v1_16_R3.getMinecraftClass("EntityTypes"); + _EntityTypes_a = minecraftEntityTypesClass.getMethod("a", nbtTagCompoundClass, minecraftWorldClass, Function.class); + } + + public ItemStack itemStackFromNBTData(NBTTagCompound data) { + return (ItemStack) BukkitReflectAdapter_v1_16_R3.invokeMethod(null, _CraftItemStack_asCraftMirror, BukkitReflectAdapter_v1_16_R3.invokeConstuctor(_ItemStack_nbtConstructor, data._handle)); + } + + public NBTTagCompound itemStackToNBTData(ItemStack stack) { + NBTTagCompound data = new NBTTagCompound(); + Object handle = BukkitReflectAdapter_v1_16_R3.getFieldValue(stack, _CraftItemStack_handle); + BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _ItemStack_save, data._handle); + return data; + } + + public Entity spawnEntity(NBTTagCompound data, Location location) { + Object worldHandle = BukkitReflectAdapter_v1_16_R3.invokeMethod(location.getWorld(), _CraftWorld_getHandle); + // This function will be applied to each summoned entity (including passengers) to set their location + Function entityFunction = (Object entity) -> { + BukkitReflectAdapter_v1_16_R3.invokeMethod(entity, _Entity_setPosition, location.getX(), location.getY(), location.getZ()); + return entity; + }; + // Summon the entity, and for each entity summoned (including passengers) run the above function + Object entityHandle = BukkitReflectAdapter_v1_16_R3.invokeMethod(null, _EntityTypes_a, data._handle, worldHandle, entityFunction); + if (entityHandle == null) { + return null; + } + BukkitReflectAdapter_v1_16_R3.invokeMethod(worldHandle, _WorldServer_addAllEntitiesSafely, entityHandle); + return (Entity) BukkitReflectAdapter_v1_16_R3.invokeMethod(entityHandle, _Entity_getBukkitEntity); + } + + public NBTTagCompound getEntityNBTData(Entity entity) { + Object entityHandle = BukkitReflectAdapter_v1_16_R3.invokeMethod(entity, _CraftEntity_getHandle); + NBTTagCompound data = new NBTTagCompound(); + BukkitReflectAdapter_v1_16_R3.invokeMethod(entityHandle, _Entity_save, data._handle); + return data; + } + + private Object getTileEntity(Block block) { + Object worldHandle = BukkitReflectAdapter_v1_16_R3.invokeMethod(block.getWorld(), _CraftWorld_getHandle); + Object pos = BukkitReflectAdapter_v1_16_R3.invokeConstuctor(_BlockPosition_constructor, block.getX(), block.getY(), block.getZ()); + return BukkitReflectAdapter_v1_16_R3.invokeMethod(worldHandle, _World_getTileEntity, pos); + } + + public NBTTagCompound getTileEntityNBTData(Block block) { + Object tileEntity = getTileEntity(block); + if (tileEntity != null) { + NBTTagCompound data = new NBTTagCompound(); + BukkitReflectAdapter_v1_16_R3.invokeMethod(tileEntity, _TileEntity_save, data._handle); + return data; + } + return null; + } + + public void setTileEntityNBTData(Block block, NBTTagCompound data) { + Object tileEntity = getTileEntity(block); + if (tileEntity != null) { + BukkitReflectAdapter_v1_16_R3.invokeMethod(tileEntity, _TileEntity_load, null, data._handle); + } + } + + public NBTTagCompound getItemStackTag(ItemStack item) { + Object handle = BukkitReflectAdapter_v1_16_R3.getFieldValue(item, _CraftItemStack_handle); + Object tag = BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _ItemStack_getTag); + return (tag == null ? new NBTTagCompound() : new NBTTagCompound(tag)); + } + + public void setItemStackTag(ItemStack item, NBTTagCompound tag) { + Object handle = BukkitReflectAdapter_v1_16_R3.getFieldValue(item, _CraftItemStack_handle); + BukkitReflectAdapter_v1_16_R3.invokeMethod(handle, _ItemStack_setTag, tag._handle); + } + + public ItemStack itemStackToCraftItemStack(ItemStack item) { + if (!_CraftItemStack_asCraftCopy.getClass().isInstance(item)) { + return (ItemStack) BukkitReflectAdapter_v1_16_R3.invokeMethod(null, _CraftItemStack_asCraftCopy, item); + } + return item; + } +} diff --git a/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter_v1_16_R3.java b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter_v1_16_R3.java new file mode 100644 index 00000000..9f38ed87 --- /dev/null +++ b/adapter_v1_16_R3/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter_v1_16_R3.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.nbteditor.nbt; + +import java.util.ArrayList; +import java.util.List; + +import com.goncalomb.bukkit.mylib.namemaps.EntityTypeMap; +import com.goncalomb.bukkit.mylib.reflect.NBTTagCompound; +import com.goncalomb.bukkit.mylib.reflect.NBTTagList; + +import org.bukkit.entity.EntityType; + +// minecraft:spawner{Delay:462s,MaxNearbyEntities:6s,MaxSpawnDelay:600s,MinSpawnDelay:400s,RequiredPlayerRange:16s,SpawnCount:4s,SpawnData:{ArmorItems:[{Count:1b,id:"minecraft:leather_boots",tag:{display:{color:1908001}}},{Count:1b,id:"minecraft:leather_leggings",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_chestplate",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_helmet",tag:{display:{Name:'{"text":"§c§lSacrifice\'s Scalp"}',color:9468004},plain:{display:{Name:"Sacrifice's Scalp"}}}}],Attributes:[{Base:30.0d,Name:"minecraft:generic.max_health"}],CustomName:'{"text":"Desiccated Husk"}',HandItems:[{Count:1b,id:"minecraft:yellow_glazed_terracotta",tag:{AttributeModifiers:[{Amount:7.0d,AttributeName:"minecraft:generic.attack_damage",Name:"Modifier",Operation:0,Slot:"mainhand",UUID:[I;1255868359,-901623816,-1585118361,1237496632]},{Amount:0.12d,AttributeName:"minecraft:generic.movement_speed",Name:"Modifier",Operation:1,Slot:"mainhand",UUID:[I;-1540949385,586696667,-1422368804,222333757]}]}},{}],Health:30.0f,Profession:3,id:"minecraft:zombie_villager",plain:{CustomName:"Desiccated Husk"}},SpawnPotentials:[{Entity:{ArmorItems:[{Count:1b,id:"minecraft:leather_boots",tag:{display:{color:1908001}}},{Count:1b,id:"minecraft:leather_leggings",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_chestplate",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_helmet",tag:{display:{Name:'{"text":"§c§lSacrifice\'s Scalp"}',color:9468004},plain:{display:{Name:"Sacrifice's Scalp"}}}}],Attributes:[{Base:30.0d,Name:"minecraft:generic.max_health"}],CustomName:'{"text":"Desiccated Husk"}',HandItems:[{Count:1b,id:"minecraft:yellow_glazed_terracotta",tag:{AttributeModifiers:[{Amount:7.0d,AttributeName:"minecraft:generic.attack_damage",Name:"Modifier",Operation:0,Slot:"mainhand",UUID:[I;1255868359,-901623816,-1585118361,1237496632]},{Amount:0.12d,AttributeName:"minecraft:generic.movement_speed",Name:"Modifier",Operation:1,Slot:"mainhand",UUID:[I;-1540949385,586696667,-1422368804,222333757]}]}},{}],Health:30.0f,Profession:3,id:"minecraft:zombie_villager",plain:{CustomName:"Desiccated Husk"}},Weight:1}],SpawnRange:4s,id:"minecraft:mob_spawner",x:-699,y:16,z:-215} + +public class SpawnerNBTWrapperAdapter_v1_16_R3 implements SpawnerNBTWrapperAdapter { + public SpawnerNBTWrapperAdapter_v1_16_R3() { + // No setup needed but this constructor must exist + } + + public void addEntity(NBTTagCompound data, SpawnerAdapterWrappedEntity entity) { + List entities = getEntities(data); + entities.add(entity); + data.setCompound("SpawnData", entity.data); + setEntities(data, entities); + } + + public List getEntities(NBTTagCompound data) { + ArrayList entities = new ArrayList(); + if (data.hasKey("SpawnPotentials")) { + NBTTagList spawnPotentials = data.getList("SpawnPotentials"); + int l = spawnPotentials.size(); + for (int i = 0; i < l; ++i) { + NBTTagCompound potential = (NBTTagCompound) spawnPotentials.get(i); + NBTTagCompound entityNbt = potential.getCompound("Entity"); + if (entityNbt != null) { + entities.add(new SpawnerAdapterWrappedEntity(entityNbt, potential.getInt("Weight"))); + } + } + } + return entities; + } + + public void setEntities(NBTTagCompound data, List entities) { + if (entities != null && entities.size() > 0) { + NBTTagList spawnPotentials = new NBTTagList(); + for (SpawnerAdapterWrappedEntity entity : entities) { + NBTTagCompound newEntry = new NBTTagCompound(); + newEntry.setInt("Weight", entity.weight); + newEntry.setCompound("Entity", entity.data); + spawnPotentials.add(newEntry); + } + data.setList("SpawnPotentials", spawnPotentials); + } else { + NBTTagCompound simplePig = new NBTTagCompound(); + + data.setCompound("SpawnData", simplePig); + data.remove("SpawnPotentials"); + } + } + + public EntityType getCurrentEntity(NBTTagCompound data) { + NBTTagCompound spawnData = data.getCompound("SpawnData"); + if (spawnData != null) { + return EntityTypeMap.getByName(spawnData.getString("id")); + } + return null; + } +} diff --git a/adapter_v1_18_R1/.gitignore b/adapter_v1_18_R1/.gitignore new file mode 100644 index 00000000..4f035375 --- /dev/null +++ b/adapter_v1_18_R1/.gitignore @@ -0,0 +1,2 @@ +upload*.sh +.factorypath diff --git a/adapter_v1_18_R1/build.gradle.kts b/adapter_v1_18_R1/build.gradle.kts new file mode 100644 index 00000000..91db0d42 --- /dev/null +++ b/adapter_v1_18_R1/build.gradle.kts @@ -0,0 +1,33 @@ +plugins { + id("com.goncalomb.bukkit.java-conventions") + id("io.papermc.paperweight.userdev") version "1.3.3" +} + +dependencies { + implementation(project(":adapter_api")) + paperDevBundle("1.18.1-R0.1-SNAPSHOT") +} + +description = "adapter_v1_18_R1" +version = rootProject.version + +tasks { + // Configure reobfJar to run when invoking the build task + assemble { + dependsOn(reobfJar) + } + + compileJava { + options.encoding = Charsets.UTF_8.name() // We want UTF-8 for everything + + // Set the release flag. This configures what version bytecode the compiler will emit, as well as what JDK APIs are usable. + // See https://openjdk.java.net/jeps/247 for more information. + options.release.set(17) + } + javadoc { + options.encoding = Charsets.UTF_8.name() // We want UTF-8 for everything + } + processResources { + filteringCharset = Charsets.UTF_8.name() // We want UTF-8 for everything + } +} diff --git a/adapter_v1_18_R1/settings.gradle.kts b/adapter_v1_18_R1/settings.gradle.kts new file mode 100644 index 00000000..211d7e16 --- /dev/null +++ b/adapter_v1_18_R1/settings.gradle.kts @@ -0,0 +1,5 @@ +pluginManagement { + repositories { + maven("https://papermc.io/repo/repository/maven-public/") + } +} diff --git a/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter_v1_18_R1.java b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter_v1_18_R1.java new file mode 100644 index 00000000..e0a91794 --- /dev/null +++ b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter_v1_18_R1.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import org.bukkit.Bukkit; +import org.bukkit.command.SimpleCommandMap; +import org.bukkit.craftbukkit.v1_18_R1.CraftServer; + +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.TextComponent; + +public final class BukkitReflectAdapter_v1_18_R1 implements BukkitReflectAdapter { + public BukkitReflectAdapter_v1_18_R1() { + // No setup needed but this constructor must exist + } + + public SimpleCommandMap getCommandMap() { + return ((CraftServer) Bukkit.getServer()).getCommandMap(); + } + + public boolean isValidRawJSON(String text) { + try { + if (Component.Serializer.fromJson(text) != null) { + return true; + } + } catch (Exception ex) { + // Invalid, fallthrough to return false + } + return false; + } + + public String textToRawJSON(String text) { + return Component.Serializer.toJson(new TextComponent(text)); + } +} diff --git a/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter_v1_18_R1.java b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter_v1_18_R1.java new file mode 100644 index 00000000..75ca8c19 --- /dev/null +++ b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter_v1_18_R1.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.bukkit.Bukkit; + +import net.minecraft.SharedConstants; + +public class BukkitVersionAdapter_v1_18_R1 implements BukkitVersionAdapter { + private final int _minecraftVersionMajor; + private final int _minecraftVersionMinor; + + public BukkitVersionAdapter_v1_18_R1() throws Exception { + if (Bukkit.getServer() == null) { + // test environment, CraftBukkit / Minecraft not available + _minecraftVersionMajor = Integer.MAX_VALUE; + _minecraftVersionMinor = Integer.MAX_VALUE; + return; + } + + String version = SharedConstants.VERSION_STRING; + Matcher matcher = Pattern.compile("^(\\d+)(?:\\.(\\d+)(?:\\.(\\d+))?)?$").matcher(version); + if (matcher.find()) { + _minecraftVersionMajor = Integer.parseInt(matcher.group(1)); + _minecraftVersionMinor = Integer.parseInt(matcher.group(2)); + } else { + throw new RuntimeException("Invalid Minecraft version: " + version); + } + } + + public boolean isVersion(int minor, int major) { + return _minecraftVersionMajor > major || (_minecraftVersionMajor == major && _minecraftVersionMinor >= minor); + } +} diff --git a/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter_v1_18_R1.java b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter_v1_18_R1.java new file mode 100644 index 00000000..63373d4f --- /dev/null +++ b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter_v1_18_R1.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; + +public class NBTBaseAdapter_v1_18_R1 implements NBTBaseAdapter { + public NBTBaseAdapter_v1_18_R1() { + // No setup needed but this constructor must exist + } + + @Override + public NBTBase wrap(Object object) { + if (object instanceof CompoundTag) { + return new NBTTagCompound(object); + } else if (object instanceof ListTag) { + return new NBTTagList(object); + } else if (object instanceof Tag) { + return new NBTBase(object); + } else { + throw new RuntimeException(object.getClass() + " is not a valid NBT tag type."); + } + } + + @Override + public Object clone(Object nbtBaseObject) { + return ((Tag) nbtBaseObject).copy(); + } + + @Override + public byte getTypeId(Object handle) { + return ((Tag) handle).getId(); + } +} diff --git a/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter_v1_18_R1.java b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter_v1_18_R1.java new file mode 100644 index 00000000..3a8d6883 --- /dev/null +++ b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter_v1_18_R1.java @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; + +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.NbtIo; +import net.minecraft.nbt.TagParser; + +public final class NBTTagCompoundAdapter_v1_18_R1 implements NBTTagCompoundAdapter { + public NBTTagCompoundAdapter_v1_18_R1() { + // No setup needed but this constructor must exist + } + + public Object newInternalInstance() { + return new CompoundTag(); + } + + @SuppressWarnings("unchecked") + public Map getMap(Object handle) { + return (Map) ((Object)((CompoundTag) handle).tags); + } + + public byte getByte(Object handle, String key) { + return ((CompoundTag) handle).getByte(key); + } + + public short getShort(Object handle, String key) { + return ((CompoundTag) handle).getShort(key); + } + + public int getInt(Object handle, String key) { + return ((CompoundTag) handle).getInt(key); + } + + public long getLong(Object handle, String key) { + return ((CompoundTag) handle).getLong(key); + } + + public float getFloat(Object handle, String key) { + return ((CompoundTag) handle).getFloat(key); + } + + public Double getDouble(Object handle, String key) { + return ((CompoundTag) handle).getDouble(key); + } + + public String getString(Object handle, String key) { + return ((CompoundTag) handle).getString(key); + } + + public byte[] getByteArray(Object handle, String key) { + return ((CompoundTag) handle).getByteArray(key); + } + + public int[] getIntArray(Object handle, String key) { + return ((CompoundTag) handle).getIntArray(key); + } + + public long[] getLongArray(Object handle, String key) { + return ((CompoundTag) handle).getLongArray(key); + } + + public NBTTagCompound getCompound(Object handle, String key) { + CompoundTag obj = ((CompoundTag) handle).getCompound(key); + if (obj != null) { + return new NBTTagCompound(obj); + } + return null; + } + + public NBTTagList getList(Object handle, String key) { + Object obj = getMap(handle).get(key); + if (obj != null && obj instanceof ListTag) { + return new NBTTagList(obj); + } + return null; + } + + public void set(Object handle, String key, Object value) { + getMap(handle).put(key, NBTTypes.toInternal(value)); + } + + public void serialize(Object handle, OutputStream outputStream) throws IOException { + NbtIo.writeCompressed((CompoundTag) handle, outputStream); + } + + public NBTTagCompound unserialize(InputStream inputStream) throws IOException { + return new NBTTagCompound(NbtIo.readCompressed(inputStream)); + } + + public NBTTagCompound fromString(String mojangson) throws Exception { + return new NBTTagCompound(TagParser.parseTag(mojangson)); + } +} + diff --git a/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter_v1_18_R1.java b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter_v1_18_R1.java new file mode 100644 index 00000000..86512f22 --- /dev/null +++ b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter_v1_18_R1.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.lang.reflect.Field; +import java.util.List; + +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; + +public final class NBTTagListAdapter_v1_18_R1 implements NBTTagListAdapter { + private static Field _listField; + protected static Class _nbtTagListClass; + + public NBTTagListAdapter_v1_18_R1() throws Exception { + _nbtTagListClass = Class.forName("net.minecraft.nbt.NBTTagList"); + _listField = _nbtTagListClass.getDeclaredField("c"); + _listField.setAccessible(true); + } + + public Object newInternalInstance() { + return new ListTag(); + } + + @SuppressWarnings("unchecked") + public List getList(Object handle) { + try { + return (List) _listField.get(handle); + } catch (Exception e) { + throw new RuntimeException("Error while getting field value " + _listField.getName() + " of class " + _listField.getDeclaringClass().getName() + ".", e); + } + } + + public void add(Object handle, Object value) { + Tag addHandle = (Tag) NBTTypes.toInternal(value); + ListTag list = ((ListTag) handle); + list.add(addHandle); + } +} diff --git a/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter_v1_18_R1.java b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter_v1_18_R1.java new file mode 100644 index 00000000..62ff3ade --- /dev/null +++ b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter_v1_18_R1.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.util.HashMap; +import java.util.function.Function; + +import org.apache.commons.lang.ClassUtils; + +import net.minecraft.nbt.ByteArrayTag; +import net.minecraft.nbt.ByteTag; +import net.minecraft.nbt.DoubleTag; +import net.minecraft.nbt.FloatTag; +import net.minecraft.nbt.IntArrayTag; +import net.minecraft.nbt.IntTag; +import net.minecraft.nbt.LongArrayTag; +import net.minecraft.nbt.LongTag; +import net.minecraft.nbt.ShortTag; +import net.minecraft.nbt.StringTag; + +public final class NBTTypesAdapter_v1_18_R1 implements NBTTypesAdapter { + + private static final class NBTTypes { + private final Function wrapFunc; + private final Function unwrapFunc; + + private NBTTypes(Function wrapFunc, Function unwrapFunc) { + this.wrapFunc = wrapFunc; + this.unwrapFunc = unwrapFunc; + } + + // Wraps primitives and strings with Minecraft tags. + private Object wrap(Object innerObject) { + return wrapFunc.apply(innerObject); + } + + // Unwraps primitives and strings from Minecraft tags. + private Object unwrap(Object tagObject) { + return unwrapFunc.apply(tagObject); + } + } + + private HashMap, NBTTypes> _innerTypeMap = new HashMap, NBTTypes>();; + private HashMap, NBTTypes> _outerTypeMap = new HashMap, NBTTypes>();; + + public NBTTypesAdapter_v1_18_R1() throws Exception { + registerNew(byte.class, ByteTag.class, (toWrap) -> ByteTag.valueOf((Byte) toWrap), (toUnwrap) -> ((ByteTag) toUnwrap).getAsByte()); + registerNew(short.class, ShortTag.class, (toWrap) -> ShortTag.valueOf((Short) toWrap), (toUnwrap) -> ((ShortTag) toUnwrap).getAsShort()); + registerNew(int.class, IntTag.class, (toWrap) -> IntTag.valueOf((Integer) toWrap), (toUnwrap) -> ((IntTag) toUnwrap).getAsInt()); + registerNew(long.class, LongTag.class, (toWrap) -> LongTag.valueOf((Long) toWrap), (toUnwrap) -> ((LongTag) toUnwrap).getAsLong()); + registerNew(float.class, FloatTag.class, (toWrap) -> FloatTag.valueOf((Float) toWrap), (toUnwrap) -> ((FloatTag) toUnwrap).getAsFloat()); + registerNew(double.class, DoubleTag.class, (toWrap) -> DoubleTag.valueOf((Double) toWrap), (toUnwrap) -> ((DoubleTag) toUnwrap).getAsDouble()); + registerNew(String.class, StringTag.class, (toWrap) -> StringTag.valueOf((String) toWrap), (toUnwrap) -> ((StringTag) toUnwrap).getAsString()); + registerNew(byte[].class, ByteArrayTag.class, (toWrap) -> new ByteArrayTag((byte[]) toWrap), (toUnwrap) -> ((ByteArrayTag) toUnwrap).getAsByteArray()); + registerNew(int[].class, IntArrayTag.class, (toWrap) -> new IntArrayTag((int[]) toWrap), (toUnwrap) -> ((IntArrayTag) toUnwrap).getAsIntArray()); + registerNew(long[].class, LongArrayTag.class, (toWrap) -> new LongArrayTag((long[]) toWrap), (toUnwrap) -> ((LongArrayTag) toUnwrap).getAsLongArray()); + } + + private void registerNew(Class innerClass, Class wrapperClass, Function wrapFunc, Function unwrapFunc) { + NBTTypes handler = new NBTTypes(wrapFunc, unwrapFunc); + _innerTypeMap.put((innerClass.isPrimitive() ? ClassUtils.primitiveToWrapper(innerClass) : innerClass), handler); + _outerTypeMap.put(wrapperClass, handler); + } + + public Object toInternal(Object object) { + if (object instanceof NBTBase) { + return ((NBTBase) object)._handle; + } else { + NBTTypes handler = _innerTypeMap.get(object.getClass()); + if (handler != null) { + return handler.wrap(object); + } else { + throw new RuntimeException(object.getClass() + " is not a valid NBTTag type."); + } + } + } + + public Object fromInternal(Object object) { + if (object == null) { + return null; + } + NBTTypes handler = _outerTypeMap.get(object.getClass()); + if (handler != null) { + return handler.unwrap(object); + } else { + return NBTBase.wrap(object); + } + } +} diff --git a/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter_v1_18_R1.java b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter_v1_18_R1.java new file mode 100644 index 00000000..abc16860 --- /dev/null +++ b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter_v1_18_R1.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.craftbukkit.v1_18_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_18_R1.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack; +import org.bukkit.entity.Entity; +import org.bukkit.inventory.ItemStack; + +import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.level.block.entity.BlockEntity; + +public final class NBTUtilsAdapter_v1_18_R1 implements NBTUtilsAdapter { + public NBTUtilsAdapter_v1_18_R1() { + // No setup needed but this constructor must exist + } + + public ItemStack itemStackFromNBTData(NBTTagCompound data) { + return net.minecraft.world.item.ItemStack.of((CompoundTag) data._handle).asBukkitMirror(); + } + + public NBTTagCompound itemStackToNBTData(ItemStack stack) { + NBTTagCompound data = new NBTTagCompound(); + ((CraftItemStack) stack).handle.save((CompoundTag) (data._handle)); + return data; + } + + public Entity spawnEntity(NBTTagCompound data, Location location) { + ServerLevel world = ((CraftWorld) location.getWorld()).getHandle(); + + net.minecraft.world.entity.Entity entity = EntityType.loadEntityRecursive((CompoundTag) (data._handle), world, (spawnedEntity) -> { + spawnedEntity.setPos(location.getX(), location.getY(), location.getZ()); + return spawnedEntity; + }); + world.addFreshEntity(entity); + return entity.getBukkitEntity(); + }; + + public NBTTagCompound getEntityNBTData(Entity entity) { + NBTTagCompound data = new NBTTagCompound(); + ((CraftEntity) entity).getHandle().save((CompoundTag) (data._handle)); + return data; + } + + private BlockEntity getTileEntity(Block block) { + return ((CraftWorld) block.getWorld()).getHandle().getBlockEntity(new BlockPos(block.getX(), block.getY(), block.getZ())); + } + + public NBTTagCompound getTileEntityNBTData(Block block) { + BlockEntity tileEntity = getTileEntity(block); + if (tileEntity != null) { + return new NBTTagCompound(tileEntity.saveWithId()); + } + return null; + } + + public void setTileEntityNBTData(Block block, NBTTagCompound data) { + BlockEntity tileEntity = getTileEntity(block); + if (tileEntity != null) { + tileEntity.load((CompoundTag) data._handle); + } + } + + public NBTTagCompound getItemStackTag(ItemStack item) { + Object tag = ((CraftItemStack) item).handle.getTag(); + return (tag == null ? new NBTTagCompound() : new NBTTagCompound(tag)); + } + + public void setItemStackTag(ItemStack item, NBTTagCompound tag) { + ((CraftItemStack) item).handle.setTag((CompoundTag) (tag._handle)); + } + + public ItemStack itemStackToCraftItemStack(ItemStack item) { + if (!CraftItemStack.class.isInstance(item)) { + return CraftItemStack.asCraftCopy(item); + } + return item; + } +} diff --git a/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter_v1_18_R1.java b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter_v1_18_R1.java new file mode 100644 index 00000000..ce7efec7 --- /dev/null +++ b/adapter_v1_18_R1/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter_v1_18_R1.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.nbteditor.nbt; + +import java.util.ArrayList; +import java.util.List; + +import com.goncalomb.bukkit.mylib.namemaps.EntityTypeMap; +import com.goncalomb.bukkit.mylib.reflect.NBTTagCompound; +import com.goncalomb.bukkit.mylib.reflect.NBTTagList; + +import org.bukkit.entity.EntityType; + +// minecraft:spawner{Delay:0s,MaxNearbyEntities:6s,MaxSpawnDelay:600s,MinSpawnDelay:400s,RequiredPlayerRange:10s,SpawnCount:4s,SpawnData:{entity:{ArmorItems:[{Count:1b,id:"minecraft:leather_boots",tag:{display:{color:1908001}}},{Count:1b,id:"minecraft:leather_leggings",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_chestplate",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_helmet",tag:{display:{Name:'{"text":"clSacrifice\'s Scalp"}',color:9468004},plain:{display:{Name:"Sacrifice's Scalp"}}}}],Attributes:[{Base:30.0d,Name:"minecraft:generic.max_health"}],CustomName:'{"text":"Desiccated Husk"}',HandItems:[{Count:1b,id:"minecraft:yellow_glazed_terracotta",tag:{AttributeModifiers:[{Amount:7.0d,AttributeName:"minecraft:generic.attack_damage",Name:"Modifier",Operation:0,Slot:"mainhand",UUID:[I;1255868359,-901623816,-1585118361,1237496632]},{Amount:0.12d,AttributeName:"minecraft:generic.movement_speed",Name:"Modifier",Operation:1,Slot:"mainhand",UUID:[I;-1540949385,586696667,-1422368804,222333757]}]}},{}],Health:30.0f,Profession:3,id:"minecraft:zombie_villager",plain:{CustomName:"Desiccated Husk"}}},SpawnPotentials:[{data:{entity:{ArmorItems:[{Count:1b,id:"minecraft:leather_boots",tag:{display:{color:1908001}}},{Count:1b,id:"minecraft:leather_leggings",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_chestplate",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_helmet",tag:{display:{Name:'{"text":"clSacrifice\'s Scalp"}',color:9468004},plain:{display:{Name:"Sacrifice's Scalp"}}}}],Attributes:[{Base:30.0d,Name:"minecraft:generic.max_health"}],CustomName:'{"text":"Desiccated Husk"}',HandItems:[{Count:1b,id:"minecraft:yellow_glazed_terracotta",tag:{AttributeModifiers:[{Amount:7.0d,AttributeName:"minecraft:generic.attack_damage",Name:"Modifier",Operation:0,Slot:"mainhand",UUID:[I;1255868359,-901623816,-1585118361,1237496632]},{Amount:0.12d,AttributeName:"minecraft:generic.movement_speed",Name:"Modifier",Operation:1,Slot:"mainhand",UUID:[I;-1540949385,586696667,-1422368804,222333757]}]}},{}],Health:30.0f,Profession:3,id:"minecraft:zombie_villager",plain:{CustomName:"Desiccated Husk"}}},weight:1}],SpawnRange:4s} +public class SpawnerNBTWrapperAdapter_v1_18_R1 implements SpawnerNBTWrapperAdapter { + public SpawnerNBTWrapperAdapter_v1_18_R1() { + // No setup needed but this constructor must exist + } + + public void addEntity(NBTTagCompound data, SpawnerAdapterWrappedEntity entity) { + List entities = getEntities(data); + entities.add(entity); + NBTTagCompound spawnData = new NBTTagCompound(); + spawnData.setCompound("entity", entity.data); + data.setCompound("SpawnData", spawnData); + setEntities(data, entities); + } + + public List getEntities(NBTTagCompound data) { + ArrayList entities = new ArrayList(); + if (data.hasKey("SpawnPotentials")) { + NBTTagList spawnPotentials = data.getList("SpawnPotentials"); + int l = spawnPotentials.size(); + for (int i = 0; i < l; ++i) { + NBTTagCompound potential = (NBTTagCompound) spawnPotentials.get(i); + NBTTagCompound dataTag = potential.getCompound("data"); + if (dataTag != null) { + NBTTagCompound entityNbt = dataTag.getCompound("entity"); + if (entityNbt != null) { + entities.add(new SpawnerAdapterWrappedEntity(entityNbt, potential.getInt("weight"))); + } + } + } + } + return entities; + } + + public void setEntities(NBTTagCompound data, List entities) { + if (entities != null && entities.size() > 0) { + NBTTagList spawnPotentials = new NBTTagList(); + for (SpawnerAdapterWrappedEntity entity : entities) { + NBTTagCompound newEntry = new NBTTagCompound(); + newEntry.setInt("weight", entity.weight); + NBTTagCompound newEntryData = new NBTTagCompound(); + newEntryData.setCompound("entity", entity.data); + newEntry.setCompound("data", newEntryData); + spawnPotentials.add(newEntry); + } + data.setList("SpawnPotentials", spawnPotentials); + } else { + NBTTagCompound simplePigData = new NBTTagCompound(); + simplePigData.setCompound("entity", new NBTTagCompound()); + + data.setCompound("SpawnData", simplePigData); + data.remove("SpawnPotentials"); + } + } + + public EntityType getCurrentEntity(NBTTagCompound data) { + NBTTagCompound spawnData = data.getCompound("SpawnData"); + if (spawnData != null && spawnData.hasKey("entity")) { + return EntityTypeMap.getByName(spawnData.getCompound("entity").getString("id")); + } + return null; + } +} diff --git a/adapter_v1_18_R2/.gitignore b/adapter_v1_18_R2/.gitignore new file mode 100644 index 00000000..4f035375 --- /dev/null +++ b/adapter_v1_18_R2/.gitignore @@ -0,0 +1,2 @@ +upload*.sh +.factorypath diff --git a/adapter_v1_18_R2/build.gradle.kts b/adapter_v1_18_R2/build.gradle.kts new file mode 100644 index 00000000..72a53cba --- /dev/null +++ b/adapter_v1_18_R2/build.gradle.kts @@ -0,0 +1,33 @@ +plugins { + id("com.goncalomb.bukkit.java-conventions") + id("io.papermc.paperweight.userdev") version "1.3.3" +} + +dependencies { + implementation(project(":adapter_api")) + paperDevBundle("1.18.2-R0.1-SNAPSHOT") +} + +description = "adapter_v1_18_R2" +version = rootProject.version + +tasks { + // Configure reobfJar to run when invoking the build task + assemble { + dependsOn(reobfJar) + } + + compileJava { + options.encoding = Charsets.UTF_8.name() // We want UTF-8 for everything + + // Set the release flag. This configures what version bytecode the compiler will emit, as well as what JDK APIs are usable. + // See https://openjdk.java.net/jeps/247 for more information. + options.release.set(17) + } + javadoc { + options.encoding = Charsets.UTF_8.name() // We want UTF-8 for everything + } + processResources { + filteringCharset = Charsets.UTF_8.name() // We want UTF-8 for everything + } +} diff --git a/adapter_v1_18_R2/settings.gradle.kts b/adapter_v1_18_R2/settings.gradle.kts new file mode 100644 index 00000000..211d7e16 --- /dev/null +++ b/adapter_v1_18_R2/settings.gradle.kts @@ -0,0 +1,5 @@ +pluginManagement { + repositories { + maven("https://papermc.io/repo/repository/maven-public/") + } +} diff --git a/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter_v1_18_R2.java b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter_v1_18_R2.java new file mode 100644 index 00000000..283865f5 --- /dev/null +++ b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitReflectAdapter_v1_18_R2.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import org.bukkit.Bukkit; +import org.bukkit.command.SimpleCommandMap; +import org.bukkit.craftbukkit.v1_18_R2.CraftServer; + +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.TextComponent; + +public final class BukkitReflectAdapter_v1_18_R2 implements BukkitReflectAdapter { + public BukkitReflectAdapter_v1_18_R2() { + // No setup needed but this constructor must exist + } + + public SimpleCommandMap getCommandMap() { + return ((CraftServer) Bukkit.getServer()).getCommandMap(); + } + + public boolean isValidRawJSON(String text) { + try { + if (Component.Serializer.fromJson(text) != null) { + return true; + } + } catch (Exception ex) { + // Invalid, fallthrough to return false + } + return false; + } + + public String textToRawJSON(String text) { + return Component.Serializer.toJson(new TextComponent(text)); + } +} diff --git a/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter_v1_18_R2.java b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter_v1_18_R2.java new file mode 100644 index 00000000..c5f6968a --- /dev/null +++ b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersionAdapter_v1_18_R2.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.bukkit.Bukkit; + +import net.minecraft.SharedConstants; + +public class BukkitVersionAdapter_v1_18_R2 implements BukkitVersionAdapter { + private final int _minecraftVersionMajor; + private final int _minecraftVersionMinor; + + public BukkitVersionAdapter_v1_18_R2() throws Exception { + if (Bukkit.getServer() == null) { + // test environment, CraftBukkit / Minecraft not available + _minecraftVersionMajor = Integer.MAX_VALUE; + _minecraftVersionMinor = Integer.MAX_VALUE; + return; + } + + String version = SharedConstants.VERSION_STRING; + Matcher matcher = Pattern.compile("^(\\d+)(?:\\.(\\d+)(?:\\.(\\d+))?)?$").matcher(version); + if (matcher.find()) { + _minecraftVersionMajor = Integer.parseInt(matcher.group(1)); + _minecraftVersionMinor = Integer.parseInt(matcher.group(2)); + } else { + throw new RuntimeException("Invalid Minecraft version: " + version); + } + } + + public boolean isVersion(int minor, int major) { + return _minecraftVersionMajor > major || (_minecraftVersionMajor == major && _minecraftVersionMinor >= minor); + } +} diff --git a/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter_v1_18_R2.java b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter_v1_18_R2.java new file mode 100644 index 00000000..3fcddd13 --- /dev/null +++ b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBaseAdapter_v1_18_R2.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; + +public class NBTBaseAdapter_v1_18_R2 implements NBTBaseAdapter { + public NBTBaseAdapter_v1_18_R2() { + // No setup needed but this constructor must exist + } + + @Override + public NBTBase wrap(Object object) { + if (object instanceof CompoundTag) { + return new NBTTagCompound(object); + } else if (object instanceof ListTag) { + return new NBTTagList(object); + } else if (object instanceof Tag) { + return new NBTBase(object); + } else { + throw new RuntimeException(object.getClass() + " is not a valid NBT tag type."); + } + } + + @Override + public Object clone(Object nbtBaseObject) { + return ((Tag) nbtBaseObject).copy(); + } + + @Override + public byte getTypeId(Object handle) { + return ((Tag) handle).getId(); + } +} diff --git a/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter_v1_18_R2.java b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter_v1_18_R2.java new file mode 100644 index 00000000..098f7479 --- /dev/null +++ b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompoundAdapter_v1_18_R2.java @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.NbtIo; +import net.minecraft.nbt.TagParser; + +public final class NBTTagCompoundAdapter_v1_18_R2 implements NBTTagCompoundAdapter { + public NBTTagCompoundAdapter_v1_18_R2() { + // No setup needed but this constructor must exist + } + + public Object newInternalInstance() { + return new CompoundTag(); + } + + @SuppressWarnings("unchecked") + public Map getMap(Object handle) { + return (Map) ((Object)((CompoundTag) handle).tags); + } + + public byte getByte(Object handle, String key) { + return ((CompoundTag) handle).getByte(key); + } + + public short getShort(Object handle, String key) { + return ((CompoundTag) handle).getShort(key); + } + + public int getInt(Object handle, String key) { + return ((CompoundTag) handle).getInt(key); + } + + public long getLong(Object handle, String key) { + return ((CompoundTag) handle).getLong(key); + } + + public float getFloat(Object handle, String key) { + return ((CompoundTag) handle).getFloat(key); + } + + public Double getDouble(Object handle, String key) { + return ((CompoundTag) handle).getDouble(key); + } + + public String getString(Object handle, String key) { + if (!((CompoundTag) handle).contains(key)) { + return null; + } + return ((CompoundTag) handle).getString(key); + } + + public byte[] getByteArray(Object handle, String key) { + if (!((CompoundTag) handle).contains(key)) { + return null; + } + return ((CompoundTag) handle).getByteArray(key); + } + + public int[] getIntArray(Object handle, String key) { + if (!((CompoundTag) handle).contains(key)) { + return null; + } + return ((CompoundTag) handle).getIntArray(key); + } + + public long[] getLongArray(Object handle, String key) { + if (!((CompoundTag) handle).contains(key)) { + return null; + } + return ((CompoundTag) handle).getLongArray(key); + } + + public NBTTagCompound getCompound(Object handle, String key) { + if (!((CompoundTag) handle).contains(key)) { + return null; + } + CompoundTag obj = ((CompoundTag) handle).getCompound(key); + if (obj != null) { + return new NBTTagCompound(obj); + } + return null; + } + + public NBTTagList getList(Object handle, String key) { + Object obj = getMap(handle).get(key); + if (obj != null && obj instanceof ListTag) { + return new NBTTagList(obj); + } + return null; + } + + public void set(Object handle, String key, Object value) { + getMap(handle).put(key, NBTTypes.toInternal(value)); + } + + public void serialize(Object handle, OutputStream outputStream) throws IOException { + NbtIo.writeCompressed((CompoundTag) handle, outputStream); + } + + public NBTTagCompound unserialize(InputStream inputStream) throws IOException { + return new NBTTagCompound(NbtIo.readCompressed(inputStream)); + } + + public NBTTagCompound fromString(String mojangson) throws Exception { + return new NBTTagCompound(TagParser.parseTag(mojangson)); + } +} + diff --git a/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter_v1_18_R2.java b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter_v1_18_R2.java new file mode 100644 index 00000000..5c69b6ab --- /dev/null +++ b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagListAdapter_v1_18_R2.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.lang.reflect.Field; +import java.util.List; + +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; + +public final class NBTTagListAdapter_v1_18_R2 implements NBTTagListAdapter { + private static Field _listField; + protected static Class _nbtTagListClass; + + public NBTTagListAdapter_v1_18_R2() throws Exception { + _nbtTagListClass = Class.forName("net.minecraft.nbt.NBTTagList"); + _listField = _nbtTagListClass.getDeclaredField("c"); + _listField.setAccessible(true); + } + + public Object newInternalInstance() { + return new ListTag(); + } + + @SuppressWarnings("unchecked") + public List getList(Object handle) { + try { + return (List) _listField.get(handle); + } catch (Exception e) { + throw new RuntimeException("Error while getting field value " + _listField.getName() + " of class " + _listField.getDeclaringClass().getName() + ".", e); + } + } + + public void add(Object handle, Object value) { + Tag addHandle = (Tag) NBTTypes.toInternal(value); + ListTag list = ((ListTag) handle); + list.add(addHandle); + } +} diff --git a/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter_v1_18_R2.java b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter_v1_18_R2.java new file mode 100644 index 00000000..7fdd3234 --- /dev/null +++ b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTypesAdapter_v1_18_R2.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import java.util.HashMap; +import java.util.function.Function; + +import org.apache.commons.lang.ClassUtils; + +import net.minecraft.nbt.ByteArrayTag; +import net.minecraft.nbt.ByteTag; +import net.minecraft.nbt.DoubleTag; +import net.minecraft.nbt.FloatTag; +import net.minecraft.nbt.IntArrayTag; +import net.minecraft.nbt.IntTag; +import net.minecraft.nbt.LongArrayTag; +import net.minecraft.nbt.LongTag; +import net.minecraft.nbt.ShortTag; +import net.minecraft.nbt.StringTag; + +public final class NBTTypesAdapter_v1_18_R2 implements NBTTypesAdapter { + + private static final class NBTTypes { + private final Function wrapFunc; + private final Function unwrapFunc; + + private NBTTypes(Function wrapFunc, Function unwrapFunc) { + this.wrapFunc = wrapFunc; + this.unwrapFunc = unwrapFunc; + } + + // Wraps primitives and strings with Minecraft tags. + private Object wrap(Object innerObject) { + return wrapFunc.apply(innerObject); + } + + // Unwraps primitives and strings from Minecraft tags. + private Object unwrap(Object tagObject) { + return unwrapFunc.apply(tagObject); + } + } + + private HashMap, NBTTypes> _innerTypeMap = new HashMap, NBTTypes>();; + private HashMap, NBTTypes> _outerTypeMap = new HashMap, NBTTypes>();; + + public NBTTypesAdapter_v1_18_R2() throws Exception { + registerNew(byte.class, ByteTag.class, (toWrap) -> ByteTag.valueOf((Byte) toWrap), (toUnwrap) -> ((ByteTag) toUnwrap).getAsByte()); + registerNew(short.class, ShortTag.class, (toWrap) -> ShortTag.valueOf((Short) toWrap), (toUnwrap) -> ((ShortTag) toUnwrap).getAsShort()); + registerNew(int.class, IntTag.class, (toWrap) -> IntTag.valueOf((Integer) toWrap), (toUnwrap) -> ((IntTag) toUnwrap).getAsInt()); + registerNew(long.class, LongTag.class, (toWrap) -> LongTag.valueOf((Long) toWrap), (toUnwrap) -> ((LongTag) toUnwrap).getAsLong()); + registerNew(float.class, FloatTag.class, (toWrap) -> FloatTag.valueOf((Float) toWrap), (toUnwrap) -> ((FloatTag) toUnwrap).getAsFloat()); + registerNew(double.class, DoubleTag.class, (toWrap) -> DoubleTag.valueOf((Double) toWrap), (toUnwrap) -> ((DoubleTag) toUnwrap).getAsDouble()); + registerNew(String.class, StringTag.class, (toWrap) -> StringTag.valueOf((String) toWrap), (toUnwrap) -> ((StringTag) toUnwrap).getAsString()); + registerNew(byte[].class, ByteArrayTag.class, (toWrap) -> new ByteArrayTag((byte[]) toWrap), (toUnwrap) -> ((ByteArrayTag) toUnwrap).getAsByteArray()); + registerNew(int[].class, IntArrayTag.class, (toWrap) -> new IntArrayTag((int[]) toWrap), (toUnwrap) -> ((IntArrayTag) toUnwrap).getAsIntArray()); + registerNew(long[].class, LongArrayTag.class, (toWrap) -> new LongArrayTag((long[]) toWrap), (toUnwrap) -> ((LongArrayTag) toUnwrap).getAsLongArray()); + } + + private void registerNew(Class innerClass, Class wrapperClass, Function wrapFunc, Function unwrapFunc) { + NBTTypes handler = new NBTTypes(wrapFunc, unwrapFunc); + _innerTypeMap.put((innerClass.isPrimitive() ? ClassUtils.primitiveToWrapper(innerClass) : innerClass), handler); + _outerTypeMap.put(wrapperClass, handler); + } + + public Object toInternal(Object object) { + if (object instanceof NBTBase) { + return ((NBTBase) object)._handle; + } else { + NBTTypes handler = _innerTypeMap.get(object.getClass()); + if (handler != null) { + return handler.wrap(object); + } else { + throw new RuntimeException(object.getClass() + " is not a valid NBTTag type."); + } + } + } + + public Object fromInternal(Object object) { + if (object == null) { + return null; + } + NBTTypes handler = _outerTypeMap.get(object.getClass()); + if (handler != null) { + return handler.unwrap(object); + } else { + return NBTBase.wrap(object); + } + } +} diff --git a/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter_v1_18_R2.java b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter_v1_18_R2.java new file mode 100644 index 00000000..1cd4f4a6 --- /dev/null +++ b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtilsAdapter_v1_18_R2.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.mylib.reflect; + +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.craftbukkit.v1_18_R2.CraftWorld; +import org.bukkit.craftbukkit.v1_18_R2.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack; +import org.bukkit.entity.Entity; +import org.bukkit.inventory.ItemStack; + +import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.level.block.entity.BlockEntity; + +public final class NBTUtilsAdapter_v1_18_R2 implements NBTUtilsAdapter { + public NBTUtilsAdapter_v1_18_R2() { + // No setup needed but this constructor must exist + } + + public ItemStack itemStackFromNBTData(NBTTagCompound data) { + return net.minecraft.world.item.ItemStack.of((CompoundTag) data._handle).asBukkitMirror(); + } + + public NBTTagCompound itemStackToNBTData(ItemStack stack) { + NBTTagCompound data = new NBTTagCompound(); + ((CraftItemStack) stack).handle.save((CompoundTag) (data._handle)); + return data; + } + + public Entity spawnEntity(NBTTagCompound data, Location location) { + ServerLevel world = ((CraftWorld) location.getWorld()).getHandle(); + + net.minecraft.world.entity.Entity entity = EntityType.loadEntityRecursive((CompoundTag) (data._handle), world, (spawnedEntity) -> { + spawnedEntity.setPos(location.getX(), location.getY(), location.getZ()); + return spawnedEntity; + }); + entity.getSelfAndPassengers().forEach((e) -> world.addFreshEntity(e)); + return entity.getBukkitEntity(); + }; + + public NBTTagCompound getEntityNBTData(Entity entity) { + NBTTagCompound data = new NBTTagCompound(); + ((CraftEntity) entity).getHandle().save((CompoundTag) (data._handle)); + return data; + } + + private BlockEntity getTileEntity(Block block) { + return ((CraftWorld) block.getWorld()).getHandle().getBlockEntity(new BlockPos(block.getX(), block.getY(), block.getZ())); + } + + public NBTTagCompound getTileEntityNBTData(Block block) { + BlockEntity tileEntity = getTileEntity(block); + if (tileEntity != null) { + return new NBTTagCompound(tileEntity.saveWithId()); + } + return null; + } + + public void setTileEntityNBTData(Block block, NBTTagCompound data) { + BlockEntity tileEntity = getTileEntity(block); + if (tileEntity != null) { + tileEntity.load((CompoundTag) data._handle); + } + } + + public NBTTagCompound getItemStackTag(ItemStack item) { + Object tag = ((CraftItemStack) item).handle.getTag(); + return (tag == null ? new NBTTagCompound() : new NBTTagCompound(tag)); + } + + public void setItemStackTag(ItemStack item, NBTTagCompound tag) { + ((CraftItemStack) item).handle.setTag((CompoundTag) (tag._handle)); + } + + public ItemStack itemStackToCraftItemStack(ItemStack item) { + if (!CraftItemStack.class.isInstance(item)) { + return CraftItemStack.asCraftCopy(item); + } + return item; + } +} diff --git a/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter_v1_18_R2.java b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter_v1_18_R2.java new file mode 100644 index 00000000..fb18e510 --- /dev/null +++ b/adapter_v1_18_R2/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapperAdapter_v1_18_R2.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.nbteditor.nbt; + +import java.util.ArrayList; +import java.util.List; + +import com.goncalomb.bukkit.mylib.namemaps.EntityTypeMap; +import com.goncalomb.bukkit.mylib.reflect.NBTTagCompound; +import com.goncalomb.bukkit.mylib.reflect.NBTTagList; + +import org.bukkit.entity.EntityType; + +// minecraft:spawner{Delay:0s,MaxNearbyEntities:6s,MaxSpawnDelay:600s,MinSpawnDelay:400s,RequiredPlayerRange:10s,SpawnCount:4s,SpawnData:{entity:{ArmorItems:[{Count:1b,id:"minecraft:leather_boots",tag:{display:{color:1908001}}},{Count:1b,id:"minecraft:leather_leggings",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_chestplate",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_helmet",tag:{display:{Name:'{"text":"clSacrifice\'s Scalp"}',color:9468004},plain:{display:{Name:"Sacrifice's Scalp"}}}}],Attributes:[{Base:30.0d,Name:"minecraft:generic.max_health"}],CustomName:'{"text":"Desiccated Husk"}',HandItems:[{Count:1b,id:"minecraft:yellow_glazed_terracotta",tag:{AttributeModifiers:[{Amount:7.0d,AttributeName:"minecraft:generic.attack_damage",Name:"Modifier",Operation:0,Slot:"mainhand",UUID:[I;1255868359,-901623816,-1585118361,1237496632]},{Amount:0.12d,AttributeName:"minecraft:generic.movement_speed",Name:"Modifier",Operation:1,Slot:"mainhand",UUID:[I;-1540949385,586696667,-1422368804,222333757]}]}},{}],Health:30.0f,Profession:3,id:"minecraft:zombie_villager",plain:{CustomName:"Desiccated Husk"}}},SpawnPotentials:[{data:{entity:{ArmorItems:[{Count:1b,id:"minecraft:leather_boots",tag:{display:{color:1908001}}},{Count:1b,id:"minecraft:leather_leggings",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_chestplate",tag:{display:{color:7567221}}},{Count:1b,id:"minecraft:leather_helmet",tag:{display:{Name:'{"text":"clSacrifice\'s Scalp"}',color:9468004},plain:{display:{Name:"Sacrifice's Scalp"}}}}],Attributes:[{Base:30.0d,Name:"minecraft:generic.max_health"}],CustomName:'{"text":"Desiccated Husk"}',HandItems:[{Count:1b,id:"minecraft:yellow_glazed_terracotta",tag:{AttributeModifiers:[{Amount:7.0d,AttributeName:"minecraft:generic.attack_damage",Name:"Modifier",Operation:0,Slot:"mainhand",UUID:[I;1255868359,-901623816,-1585118361,1237496632]},{Amount:0.12d,AttributeName:"minecraft:generic.movement_speed",Name:"Modifier",Operation:1,Slot:"mainhand",UUID:[I;-1540949385,586696667,-1422368804,222333757]}]}},{}],Health:30.0f,Profession:3,id:"minecraft:zombie_villager",plain:{CustomName:"Desiccated Husk"}}},weight:1}],SpawnRange:4s} +public class SpawnerNBTWrapperAdapter_v1_18_R2 implements SpawnerNBTWrapperAdapter { + public SpawnerNBTWrapperAdapter_v1_18_R2() { + // No setup needed but this constructor must exist + } + + public void addEntity(NBTTagCompound data, SpawnerAdapterWrappedEntity entity) { + List entities = getEntities(data); + entities.add(entity); + NBTTagCompound spawnData = new NBTTagCompound(); + spawnData.setCompound("entity", entity.data); + data.setCompound("SpawnData", spawnData); + setEntities(data, entities); + } + + public List getEntities(NBTTagCompound data) { + ArrayList entities = new ArrayList(); + if (data.hasKey("SpawnPotentials")) { + NBTTagList spawnPotentials = data.getList("SpawnPotentials"); + int l = spawnPotentials.size(); + for (int i = 0; i < l; ++i) { + NBTTagCompound potential = (NBTTagCompound) spawnPotentials.get(i); + NBTTagCompound dataTag = potential.getCompound("data"); + if (dataTag != null) { + NBTTagCompound entityNbt = dataTag.getCompound("entity"); + if (entityNbt != null) { + entities.add(new SpawnerAdapterWrappedEntity(entityNbt, potential.getInt("weight"))); + } + } + } + } + return entities; + } + + public void setEntities(NBTTagCompound data, List entities) { + if (entities != null && entities.size() > 0) { + NBTTagList spawnPotentials = new NBTTagList(); + for (SpawnerAdapterWrappedEntity entity : entities) { + NBTTagCompound newEntry = new NBTTagCompound(); + newEntry.setInt("weight", entity.weight); + NBTTagCompound newEntryData = new NBTTagCompound(); + newEntryData.setCompound("entity", entity.data); + newEntry.setCompound("data", newEntryData); + spawnPotentials.add(newEntry); + } + data.setList("SpawnPotentials", spawnPotentials); + } else { + NBTTagCompound simplePigData = new NBTTagCompound(); + simplePigData.setCompound("entity", new NBTTagCompound()); + + data.setCompound("SpawnData", simplePigData); + data.remove("SpawnPotentials"); + } + } + + public EntityType getCurrentEntity(NBTTagCompound data) { + NBTTagCompound spawnData = data.getCompound("SpawnData"); + if (spawnData != null && spawnData.hasKey("entity")) { + return EntityTypeMap.getByName(spawnData.getCompound("entity").getString("id")); + } + return null; + } +} diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 00000000..ae50b9b8 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,2 @@ +version = "3.3-SNAPSHOT" +description = "NBTEditor" diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts new file mode 100644 index 00000000..50cec874 --- /dev/null +++ b/buildSrc/build.gradle.kts @@ -0,0 +1,9 @@ +plugins { + // Support convention plugins written in Kotlin. Convention plugins are build scripts in 'src/main' that automatically become available as plugins in the main build. + `kotlin-dsl` +} + +repositories { + // Use the plugin portal to apply community plugins in convention plugins. + gradlePluginPortal() +} diff --git a/buildSrc/src/main/kotlin/com.goncalomb.bukkit.java-conventions.gradle.kts b/buildSrc/src/main/kotlin/com.goncalomb.bukkit.java-conventions.gradle.kts new file mode 100644 index 00000000..9ea8eb80 --- /dev/null +++ b/buildSrc/src/main/kotlin/com.goncalomb.bukkit.java-conventions.gradle.kts @@ -0,0 +1,27 @@ +plugins { + `java-library` + `maven-publish` +} + +repositories { + mavenLocal() + maven { + url = uri("https://hub.spigotmc.org/nexus/content/groups/public/") + } + + maven { + url = uri("https://repo.codemc.org/repository/maven-public") + } + + maven { + url = uri("https://repo.maven.apache.org/maven2/") + } +} + +group = "com.goncalomb.bukkit" +java.sourceCompatibility = JavaVersion.VERSION_1_8 +java.targetCompatibility = JavaVersion.VERSION_1_8 + +tasks.withType() { + options.encoding = "UTF-8" +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..7454180f Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..2e6e5897 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 00000000..1b6c7873 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 00000000..ac1b06f9 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts new file mode 100644 index 00000000..c00357c7 --- /dev/null +++ b/plugin/build.gradle.kts @@ -0,0 +1,201 @@ +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import net.minecrell.pluginyml.bukkit.BukkitPluginDescription +import org.hidetake.groovy.ssh.core.Remote +import org.hidetake.groovy.ssh.core.RunHandler +import org.hidetake.groovy.ssh.core.Service +import org.hidetake.groovy.ssh.session.SessionHandler + +plugins { + id("com.github.johnrengelman.shadow") version "7.1.2" + id("net.minecrell.plugin-yml.bukkit") version "0.5.1" // Generates plugin.yml + id("org.hidetake.ssh") version "2.10.1" + id("com.goncalomb.bukkit.java-conventions") + id("java") +} + +dependencies { + implementation(project(":adapter_api")) + implementation(project(":adapter_v1_16_R3")) + implementation(project(":adapter_v1_18_R1")) + implementation(project(":adapter_v1_18_R2")) + implementation("org.bstats:bstats-bukkit:1.5") + compileOnly("org.spigotmc:spigot-api:1.16.3-R0.1-SNAPSHOT") + + testImplementation("junit:junit:4.13.1") + testImplementation("org.spigotmc:spigot-api:1.16.3-R0.1-SNAPSHOT") +} + +group = "com.goncalomb.bukkit" +description = "NBTEditor" +version = rootProject.version + +// Configure plugin.yml generation +bukkit { + load = BukkitPluginDescription.PluginLoadOrder.POSTWORLD + main = "com.goncalomb.bukkit.nbteditor.NBTEditor" + apiVersion = "1.13" + name = "NBTEditor" + authors = listOf("goncalomb", "Combustible") + depend = listOf() + softDepend = listOf() +} + +tasks.withType().configureEach { + options.compilerArgs.add("-Xmaxwarns") + options.compilerArgs.add("10000") + options.compilerArgs.add("-Xlint:deprecation") +} + +val basicssh = remotes.create("basicssh") { + host = "admin-eu.playmonumenta.com" + port = 8822 + user = "epic" + knownHosts = allowAnyHosts + agent = System.getenv("IDENTITY_FILE") == null + identity = if (System.getenv("IDENTITY_FILE") == null) null else file(System.getenv("IDENTITY_FILE")) +} + +val adminssh = remotes.create("adminssh") { + host = "admin-eu.playmonumenta.com" + port = 9922 + user = "epic" + knownHosts = allowAnyHosts + agent = System.getenv("IDENTITY_FILE") == null + identity = if (System.getenv("IDENTITY_FILE") == null) null else file(System.getenv("IDENTITY_FILE")) +} + +// Relocation / shading +tasks { + shadowJar { + relocate("org.bstats", "com.goncalomb.bukkit.bstats") + } +} + +tasks.create("dev1-deploy") { + val shadowJar by tasks.named("shadowJar") + dependsOn(shadowJar) + doLast { + ssh.runSessions { + session(basicssh) { + execute("cd /home/epic/dev1_shard_plugins && rm -f nbteditor*.jar") + put(shadowJar.archiveFile.get().getAsFile(), "/home/epic/dev1_shard_plugins") + } + } + } +} + +tasks.create("dev2-deploy") { + val shadowJar by tasks.named("shadowJar") + dependsOn(shadowJar) + doLast { + ssh.runSessions { + session(basicssh) { + execute("cd /home/epic/dev2_shard_plugins && rm -f nbteditor*.jar") + put(shadowJar.archiveFile.get().getAsFile(), "/home/epic/dev2_shard_plugins") + } + } + } +} + +tasks.create("dev3-deploy") { + val shadowJar by tasks.named("shadowJar") + dependsOn(shadowJar) + doLast { + ssh.runSessions { + session(basicssh) { + execute("cd /home/epic/dev3_shard_plugins && rm -f nbteditor*.jar") + put(shadowJar.archiveFile.get().getAsFile(), "/home/epic/dev3_shard_plugins") + } + } + } +} + +tasks.create("dev4-deploy") { + val shadowJar by tasks.named("shadowJar") + dependsOn(shadowJar) + doLast { + ssh.runSessions { + session(basicssh) { + execute("cd /home/epic/dev4_shard_plugins && rm -f nbteditor*.jar") + put(shadowJar.archiveFile.get().getAsFile(), "/home/epic/dev4_shard_plugins") + } + } + } +} + +tasks.create("futurama-deploy") { + val shadowJar by tasks.named("shadowJar") + dependsOn(shadowJar) + doLast { + ssh.runSessions { + session(basicssh) { + execute("cd /home/epic/futurama_shard_plugins && rm -f nbteditor*.jar") + put(shadowJar.archiveFile.get().getAsFile(), "/home/epic/futurama_shard_plugins") + } + } + } +} + +tasks.create("mobs-deploy") { + val shadowJar by tasks.named("shadowJar") + dependsOn(shadowJar) + doLast { + ssh.runSessions { + session(basicssh) { + execute("cd /home/epic/mob_shard_plugins && rm -f nbteditor*.jar") + put(shadowJar.archiveFile.get().getAsFile(), "/home/epic/mob_shard_plugins") + } + } + } +} + +tasks.create("stage-deploy") { + val shadowJar by tasks.named("shadowJar") + dependsOn(shadowJar) + doLast { + ssh.runSessions { + session(basicssh) { + put(shadowJar.archiveFile.get().getAsFile(), "/home/epic/stage/m12/server_config/plugins") + execute("cd /home/epic/stage/m12/server_config/plugins && rm -f nbteditor.jar && ln -s " + shadowJar.archiveFileName.get() + " nbteditor.jar") + } + } + } +} + +tasks.create("build-deploy") { + val shadowJar by tasks.named("shadowJar") + dependsOn(shadowJar) + doLast { + ssh.runSessions { + session(adminssh) { + put(shadowJar.archiveFile.get().getAsFile(), "/home/epic/project_epic/server_config/plugins") + execute("cd /home/epic/project_epic/server_config/plugins && rm -f nbteditor.jar && ln -s " + shadowJar.archiveFileName.get() + " nbteditor.jar") + execute("cd /home/epic/project_epic/mobs/plugins && rm -f nbteditor.jar && ln -s ../../server_config/plugins/nbteditor.jar") + } + } + } +} + +tasks.create("play-deploy") { + val shadowJar by tasks.named("shadowJar") + dependsOn(shadowJar) + doLast { + ssh.runSessions { + session(adminssh) { + put(shadowJar.archiveFile.get().getAsFile(), "/home/epic/play/m8/server_config/plugins") + put(shadowJar.archiveFile.get().getAsFile(), "/home/epic/play/m11/server_config/plugins") + execute("cd /home/epic/play/m8/server_config/plugins && rm -f nbteditor.jar && ln -s " + shadowJar.archiveFileName.get() + " nbteditor.jar") + execute("cd /home/epic/play/m11/server_config/plugins && rm -f nbteditor.jar && ln -s " + shadowJar.archiveFileName.get() + " nbteditor.jar") + } + } + } +} + +fun Service.runSessions(action: RunHandler.() -> Unit) = + run(delegateClosureOf(action)) + +fun RunHandler.session(vararg remotes: Remote, action: SessionHandler.() -> Unit) = + session(*remotes, delegateClosureOf(action)) + +fun SessionHandler.put(from: Any, into: Any) = + put(hashMapOf("from" to from, "into" to into)) diff --git a/src/main/assembly/default.xml b/plugin/src/main/assembly/default.xml similarity index 100% rename from src/main/assembly/default.xml rename to plugin/src/main/assembly/default.xml diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/CustomBow.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomBow.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/CustomBow.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomBow.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/CustomFirework.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomFirework.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/CustomFirework.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomFirework.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItem.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItem.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/CustomItem.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItem.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemConfig.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemConfig.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemConfig.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemConfig.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemContainer.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemContainer.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemContainer.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemContainer.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemListener.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemListener.java similarity index 95% rename from src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemListener.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemListener.java index 00b85700..80a8cf99 100644 --- a/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemListener.java +++ b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemListener.java @@ -22,6 +22,9 @@ import java.util.Arrays; import java.util.HashSet; +import com.goncalomb.bukkit.mylib.utils.Utils; +import com.goncalomb.bukkit.nbteditor.NBTEditor; + import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.World; @@ -73,7 +76,10 @@ private static boolean verifyCustomItem(CustomItem customItem, Player player, bo @EventHandler private void playerInteract(PlayerInteractEvent event) { Action action = event.getAction(); - if (action != Action.PHYSICAL) { + + // Only check the first interaction each tick per player + // This is needed because 1.15+ generate an extra left click event when right clicking with a book + if (action != Action.PHYSICAL && Utils.checkOnceThisTick(NBTEditor.getInstance(), event.getPlayer(), "NBTEditorInteractEvent")) { if (action == Action.RIGHT_CLICK_BLOCK && _interationMaterials.contains(event.getClickedBlock().getType())) { return; } diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemManager.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemManager.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemManager.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/CustomItemManager.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/DelayedPlayerDetails.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/DelayedPlayerDetails.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/DelayedPlayerDetails.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/DelayedPlayerDetails.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/DispenserDetails.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/DispenserDetails.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/DispenserDetails.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/DispenserDetails.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/FireworkPlayerDetails.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/FireworkPlayerDetails.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/FireworkPlayerDetails.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/FireworkPlayerDetails.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/IConsumableDetails.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/IConsumableDetails.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/IConsumableDetails.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/IConsumableDetails.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/ItemDetails.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/ItemDetails.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/ItemDetails.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/ItemDetails.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/api/PlayerDetails.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/api/PlayerDetails.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/api/PlayerDetails.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/api/PlayerDetails.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/commands/CommandCustomItems.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/commands/CommandCustomItems.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/commands/CommandCustomItems.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/commands/CommandCustomItems.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/AntiMatterBomb.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/AntiMatterBomb.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/AntiMatterBomb.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/AntiMatterBomb.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/BatBomb.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/BatBomb.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/BatBomb.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/BatBomb.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/EnderBow.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/EnderBow.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/EnderBow.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/EnderBow.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/EscapePlan.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/EscapePlan.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/EscapePlan.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/EscapePlan.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/FireBomb.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/FireBomb.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/FireBomb.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/FireBomb.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/GenericBomb.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/GenericBomb.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/GenericBomb.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/GenericBomb.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/GenericSuperAxe.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/GenericSuperAxe.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/GenericSuperAxe.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/GenericSuperAxe.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/GravitationalAxe.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/GravitationalAxe.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/GravitationalAxe.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/GravitationalAxe.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/KingsCrown.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/KingsCrown.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/KingsCrown.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/KingsCrown.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/LightningRod.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/LightningRod.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/LightningRod.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/LightningRod.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/MoonStick.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/MoonStick.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/MoonStick.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/MoonStick.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/RadiusBomb.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/RadiusBomb.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/RadiusBomb.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/RadiusBomb.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/RepulsionBomb.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/RepulsionBomb.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/RepulsionBomb.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/RepulsionBomb.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/SimpleMine.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/SimpleMine.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/SimpleMine.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/SimpleMine.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/SunStick.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/SunStick.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/SunStick.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/SunStick.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/TimeFirework.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/TimeFirework.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/TimeFirework.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/TimeFirework.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/TorchBow.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/TorchBow.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/TorchBow.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/TorchBow.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/TreeVaporizer.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/TreeVaporizer.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/TreeVaporizer.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/TreeVaporizer.java diff --git a/src/main/java/com/goncalomb/bukkit/customitems/items/WitherBow.java b/plugin/src/main/java/com/goncalomb/bukkit/customitems/items/WitherBow.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/customitems/items/WitherBow.java rename to plugin/src/main/java/com/goncalomb/bukkit/customitems/items/WitherBow.java diff --git a/src/main/java/com/goncalomb/bukkit/mylib/command/InternalCommand.java b/plugin/src/main/java/com/goncalomb/bukkit/mylib/command/InternalCommand.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/mylib/command/InternalCommand.java rename to plugin/src/main/java/com/goncalomb/bukkit/mylib/command/InternalCommand.java diff --git a/src/main/java/com/goncalomb/bukkit/mylib/command/MyCommand.java b/plugin/src/main/java/com/goncalomb/bukkit/mylib/command/MyCommand.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/mylib/command/MyCommand.java rename to plugin/src/main/java/com/goncalomb/bukkit/mylib/command/MyCommand.java diff --git a/src/main/java/com/goncalomb/bukkit/mylib/command/MyCommandException.java b/plugin/src/main/java/com/goncalomb/bukkit/mylib/command/MyCommandException.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/mylib/command/MyCommandException.java rename to plugin/src/main/java/com/goncalomb/bukkit/mylib/command/MyCommandException.java diff --git a/src/main/java/com/goncalomb/bukkit/mylib/command/MyCommandManager.java b/plugin/src/main/java/com/goncalomb/bukkit/mylib/command/MyCommandManager.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/mylib/command/MyCommandManager.java rename to plugin/src/main/java/com/goncalomb/bukkit/mylib/command/MyCommandManager.java diff --git a/src/main/java/com/goncalomb/bukkit/mylib/command/MySubCommand.java b/plugin/src/main/java/com/goncalomb/bukkit/mylib/command/MySubCommand.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/mylib/command/MySubCommand.java rename to plugin/src/main/java/com/goncalomb/bukkit/mylib/command/MySubCommand.java diff --git a/src/main/java/com/goncalomb/bukkit/mylib/utils/BookSerialize.java b/plugin/src/main/java/com/goncalomb/bukkit/mylib/utils/BookSerialize.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/mylib/utils/BookSerialize.java rename to plugin/src/main/java/com/goncalomb/bukkit/mylib/utils/BookSerialize.java diff --git a/src/main/java/com/goncalomb/bukkit/mylib/utils/CustomInventory.java b/plugin/src/main/java/com/goncalomb/bukkit/mylib/utils/CustomInventory.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/mylib/utils/CustomInventory.java rename to plugin/src/main/java/com/goncalomb/bukkit/mylib/utils/CustomInventory.java diff --git a/src/main/java/com/goncalomb/bukkit/mylib/utils/Utils.java b/plugin/src/main/java/com/goncalomb/bukkit/mylib/utils/Utils.java similarity index 80% rename from src/main/java/com/goncalomb/bukkit/mylib/utils/Utils.java rename to plugin/src/main/java/com/goncalomb/bukkit/mylib/utils/Utils.java index fe5ce90e..ed4d9938 100644 --- a/src/main/java/com/goncalomb/bukkit/mylib/utils/Utils.java +++ b/plugin/src/main/java/com/goncalomb/bukkit/mylib/utils/Utils.java @@ -26,6 +26,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.bukkit.entity.Entity; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.plugin.Plugin; import org.bukkit.util.StringUtil; public final class Utils { @@ -109,4 +112,23 @@ public static List getElementsWithPrefixGeneric(Collection values, St return Collections.unmodifiableList(result); } + /* This method can be used to wrap code that should only execute once per tick + * for a given metadata object (block, entity, etc.). + * + * If this function returns true, the program should proceed - this is the + * first time it has been invoked on this particular tick + * + * If this function returns false, then it has been called already this tick + * for this entity / metakey pair. When returning false, the code should not + * be executed. + */ + public static boolean checkOnceThisTick(Plugin plugin, Entity entity, String metakey) { + if (entity.hasMetadata(metakey) + && entity.getMetadata(metakey).get(0).asInt() == entity.getTicksLived()) { + return false; + } + + entity.setMetadata(metakey, new FixedMetadataValue(plugin, entity.getTicksLived())); + return true; + } } diff --git a/src/main/java/com/goncalomb/bukkit/mylib/utils/UtilsMc.java b/plugin/src/main/java/com/goncalomb/bukkit/mylib/utils/UtilsMc.java similarity index 90% rename from src/main/java/com/goncalomb/bukkit/mylib/utils/UtilsMc.java rename to plugin/src/main/java/com/goncalomb/bukkit/mylib/utils/UtilsMc.java index 1fe36abf..82e13fe5 100644 --- a/src/main/java/com/goncalomb/bukkit/mylib/utils/UtilsMc.java +++ b/plugin/src/main/java/com/goncalomb/bukkit/mylib/utils/UtilsMc.java @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -155,4 +156,16 @@ public static Permission getRootPermission(Plugin plugin) { return perm; } + public static UUID convertFromUUIDInts(int[] uuidData) { + return new UUID(((long) uuidData[0] << 32) | uuidData[1], ((long) uuidData[2] << 32) | uuidData[3]); + } + + public static int[] convertToUUIDInts(UUID uuid) { + return new int[] { + (int)((uuid.getMostSignificantBits() >> 32) & 0xFFFFFFFF), + (int)(uuid.getMostSignificantBits() & 0xFFFFFFFF), + (int)((uuid.getLeastSignificantBits() >> 32) & 0xFFFFFFFF), + (int)(uuid.getLeastSignificantBits() & 0xFFFFFFFF) + }; + } } diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/ItemStorage.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/ItemStorage.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/ItemStorage.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/ItemStorage.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/NBTEditor.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/NBTEditor.java similarity index 85% rename from src/main/java/com/goncalomb/bukkit/nbteditor/NBTEditor.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/NBTEditor.java index f3a314eb..461279f5 100644 --- a/src/main/java/com/goncalomb/bukkit/nbteditor/NBTEditor.java +++ b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/NBTEditor.java @@ -45,7 +45,12 @@ import com.goncalomb.bukkit.customitems.items.WitherBow; import com.goncalomb.bukkit.mylib.command.MyCommandManager; import com.goncalomb.bukkit.mylib.reflect.BukkitReflect; +import com.goncalomb.bukkit.mylib.reflect.BukkitVersion; import com.goncalomb.bukkit.mylib.reflect.NBTBase; +import com.goncalomb.bukkit.mylib.reflect.NBTTagCompound; +import com.goncalomb.bukkit.mylib.reflect.NBTTagList; +import com.goncalomb.bukkit.mylib.reflect.NBTUtils; +import com.goncalomb.bukkit.mylib.reflect.NBTTypes; import com.goncalomb.bukkit.nbteditor.bos.BookOfSouls; import com.goncalomb.bukkit.nbteditor.commands.CommandBOS; import com.goncalomb.bukkit.nbteditor.commands.CommandItemStorage; @@ -54,6 +59,7 @@ import com.goncalomb.bukkit.nbteditor.commands.CommandNBTPotion; import com.goncalomb.bukkit.nbteditor.commands.CommandNBTSpawner; import com.goncalomb.bukkit.nbteditor.commands.CommandNBTTile; +import com.goncalomb.bukkit.nbteditor.nbt.SpawnerNBTWrapper; import com.goncalomb.bukkit.nbteditor.tools.EntityInspectorTool; import com.goncalomb.bukkit.nbteditor.tools.EntityRemoverTool; import com.goncalomb.bukkit.nbteditor.tools.SuperLeadTool; @@ -66,8 +72,14 @@ public final class NBTEditor extends JavaPlugin { @Override public void onEnable() { try { - BukkitReflect.prepareReflection(); - NBTBase.prepareReflection(); + BukkitReflect.prepareReflection(this.getServer().getClass(), getLogger()); + BukkitVersion.prepareReflection(this.getServer().getClass(), getLogger()); + NBTBase.prepareReflection(this.getServer().getClass(), getLogger()); + NBTTagCompound.prepareReflection(this.getServer().getClass(), getLogger()); + NBTTagList.prepareReflection(this.getServer().getClass(), getLogger()); + NBTTypes.prepareReflection(this.getServer().getClass(), getLogger()); + NBTUtils.prepareReflection(this.getServer().getClass(), getLogger()); + SpawnerNBTWrapper.prepareReflection(this.getServer().getClass(), getLogger()); } catch (Throwable e) { getLogger().log(Level.SEVERE, "Error preparing reflection objects", e); getLogger().severe("This version of NBTEditor is not compatible with this version of Bukkit"); diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSouls.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSouls.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSouls.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSouls.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSoulsCI.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSoulsCI.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSoulsCI.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSoulsCI.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSoulsEmptyCI.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSoulsEmptyCI.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSoulsEmptyCI.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/bos/BookOfSoulsEmptyCI.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/AbstractNBTCommand.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/AbstractNBTCommand.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/AbstractNBTCommand.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/AbstractNBTCommand.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandBOS.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandBOS.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandBOS.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandBOS.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandItemStorage.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandItemStorage.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandItemStorage.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandItemStorage.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTEnchant.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTEnchant.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTEnchant.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTEnchant.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTItem.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTItem.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTItem.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTItem.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTPotion.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTPotion.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTPotion.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTPotion.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTSpawner.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTSpawner.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTSpawner.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTSpawner.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTTile.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTTile.java similarity index 99% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTTile.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTTile.java index 28f16a88..79b72258 100644 --- a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTTile.java +++ b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/CommandNBTTile.java @@ -59,6 +59,7 @@ protected TileNBTWrapper getWrapper(Player player) throws MyCommandException { } return new TileNBTWrapper(block); } catch (RuntimeException e) { + e.printStackTrace(); throw new MyCommandException("§cCannot edit that tile!"); } } diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/HandItemWrapper.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/HandItemWrapper.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/HandItemWrapper.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/HandItemWrapper.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/InventoryForItemStorage.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/InventoryForItemStorage.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/InventoryForItemStorage.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/InventoryForItemStorage.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/InventoryForSpawnerEntities.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/InventoryForSpawnerEntities.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/InventoryForSpawnerEntities.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/InventoryForSpawnerEntities.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/ItemUtils.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/ItemUtils.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/ItemUtils.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/ItemUtils.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForContainer.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForContainer.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForContainer.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForContainer.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForItems.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForItems.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForItems.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForItems.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForPassengers.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForPassengers.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForPassengers.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForPassengers.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForSingleItem.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForSingleItem.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForSingleItem.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForSingleItem.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForSpecialVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForSpecialVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForSpecialVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForSpecialVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForVillagerOffers.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForVillagerOffers.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForVillagerOffers.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/commands/inventories/InventoryForVillagerOffers.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/BaseNBT.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/BaseNBT.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/BaseNBT.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/BaseNBT.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBT.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBT.java similarity index 87% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBT.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBT.java index f2b08f0e..f5a72005 100644 --- a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBT.java +++ b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBT.java @@ -19,13 +19,6 @@ package com.goncalomb.bukkit.nbteditor.nbt; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; - -import org.bukkit.entity.EntityType; - import com.goncalomb.bukkit.mylib.namemaps.EntityTypeMap; import com.goncalomb.bukkit.mylib.reflect.BukkitVersion; import com.goncalomb.bukkit.mylib.reflect.NBTTagCompound; @@ -57,6 +50,11 @@ import com.goncalomb.bukkit.nbteditor.nbt.variables.VectorVariable; import com.goncalomb.bukkit.nbteditor.nbt.variables.VillagerCareerVariable; import com.goncalomb.bukkit.nbteditor.nbt.variables.VillagerOffersVariable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import org.bukkit.entity.EntityType; public class EntityNBT extends EntityNBTBase { @@ -125,9 +123,12 @@ public class EntityNBT extends EntityNBTBase { cCreeper.add("Ignited", new BooleanVariable("ignited")); NBTUnboundVariableContainer cDolphin = new NBTUnboundVariableContainer("Dolphin", cMob); + cDolphin.add("CanFindTreasure", new BooleanVariable("CanFindTreasure")); cDolphin.add("GotFish", new BooleanVariable("GotFish")); cDolphin.add("Moistness", new IntegerVariable("Moistness")); - // TODO: add missing variables + cDolphin.add("TreasurePosX", new IntegerVariable("TreasurePosX")); + cDolphin.add("TreasurePosY", new IntegerVariable("TreasurePosY")); + cDolphin.add("TreasurePosZ", new IntegerVariable("TreasurePosZ")); NBTUnboundVariableContainer cEnderDragon = new NBTUnboundVariableContainer("EnderDragon", cMob); cEnderDragon.add("DragonPhase", new IntegerVariable("DragonPhase", 0, 10)); @@ -159,7 +160,12 @@ public class EntityNBT extends EntityNBTBase { NBTUnboundVariableContainer cTurtle = new NBTUnboundVariableContainer("Turtle", cBreed); cTurtle.add("HasEgg", new BooleanVariable("HasEgg")); - // TODO: add missing variables + cTurtle.add("HomePosX", new IntegerVariable("HomePosX")); + cTurtle.add("HomePosY", new IntegerVariable("HomePosY")); + cTurtle.add("HomePosZ", new IntegerVariable("HomePosZ")); + cTurtle.add("TravelPosX", new IntegerVariable("TravelPosX")); + cTurtle.add("TravelPosY", new IntegerVariable("TravelPosY")); + cTurtle.add("TravelPosZ", new IntegerVariable("TravelPosZ")); NBTUnboundVariableContainer cVillagerGolem = new NBTUnboundVariableContainer("VillagerGolem", cMob); cVillagerGolem.add("PlayerCreated", new BooleanVariable("PlayerCreated")); @@ -176,16 +182,32 @@ public class EntityNBT extends EntityNBTBase { cZombie.add("InWaterTime", new IntegerVariable("InWaterTime")); cZombie.add("DrownedConversionTime", new IntegerVariable("DrownedConversionTime")); + // 1.15 and below NBTUnboundVariableContainer cZombiePigman = new NBTUnboundVariableContainer("ZombiePigman", cZombie); cZombiePigman.add("Anger", new ShortVariable("Anger")); + NBTUnboundVariableContainer cZoglin = new NBTUnboundVariableContainer("Zoglin", cMob); + cZoglin.add("IsBaby", new BooleanVariable("IsBaby")); + + NBTUnboundVariableContainer cPiglin = new NBTUnboundVariableContainer("Piglin", cMob); + cPiglin.add("IsBaby", new BooleanVariable("IsBaby")); + cPiglin.add("IsImmuneToZombification", new BooleanVariable("IsImmuneToZombification")); + cPiglin.add("CannotHunt", new BooleanVariable("CannotHunt")); + cPiglin.add("TimeInOverworld", new IntegerVariable("TimeInOverworld")); + // TODO: add villager inventory + + NBTUnboundVariableContainer cZombifiedPiglin = new NBTUnboundVariableContainer("ZombifiedPilgin", cZombie); + cZombifiedPiglin.add("AngerTime", new IntegerVariable("AngerTime")); + NBTUnboundVariableContainer cZombieVillager = new NBTUnboundVariableContainer("ZombieVillager", cZombie); cZombieVillager.add("Profession", new IntegerVariable("Profession", 0, 5)); cZombieVillager.add("ConversionTime", new IntegerVariable("ConversionTime", -1)); NBTUnboundVariableContainer cPhantom = new NBTUnboundVariableContainer("Phantom", cMob); + cPhantom.add("AX", new IntegerVariable("AX")); + cPhantom.add("AY", new IntegerVariable("AY")); + cPhantom.add("AZ", new IntegerVariable("AZ")); cPhantom.add("Size", new IntegerVariable("Size")); - // TODO: add missing variables // Breed SubTypes @@ -236,6 +258,31 @@ public class EntityNBT extends EntityNBTBase { cFox.add("Sitting", new BooleanVariable("Sitting")); cFox.add("Crouching", new BooleanVariable("Crouching")); + NBTUnboundVariableContainer cBee = new NBTUnboundVariableContainer("Bee", cBreed); + cBee.add("AngerTime", new IntegerVariable("AngerTime")); + cBee.add("CannotEnterHiveTicks", new IntegerVariable("CannotEnterHiveTicks")); + cBee.add("CropsGrownSincePollination", new IntegerVariable("CropsGrownSincePollination")); + cBee.add("TicksSincePollination", new IntegerVariable("TicksSincePollination")); + cBee.add("HasNectar", new BooleanVariable("HasNectar")); + cBee.add("HasStung", new BooleanVariable("HasStung")); + // TODO: add FlowerPos / HivePos + + NBTUnboundVariableContainer cHoglin = new NBTUnboundVariableContainer("Hoglin", cBreed); + cHoglin.add("IsImmuneToZombification", new BooleanVariable("IsImmuneToZombification")); + cHoglin.add("CannotBeHunted", new BooleanVariable("CannotBeHunted")); + cHoglin.add("TimeInOverworld", new IntegerVariable("TimeInOverworld")); + + NBTUnboundVariableContainer cAxolotl = new NBTUnboundVariableContainer("Axolotl", cBreed); + cAxolotl.add("FromBucket", new BooleanVariable("FromBucket")); + cAxolotl.add("Variant", new IntegerVariable("Variant", 0, 4)); + + NBTUnboundVariableContainer cGoat = new NBTUnboundVariableContainer("Goat", cBreed); + cGoat.add("IsScreamingGoat", new BooleanVariable("IsScreamingGoat")); + if (BukkitVersion.isVersion(19)) { + cGoat.add("HasLeftHorn", new BooleanVariable("HasLeftHorn")); + cGoat.add("HasRightHorn", new BooleanVariable("HasRightHorn")); + } + // Tameable SubTypes NBTUnboundVariableContainer cOcelot = new NBTUnboundVariableContainer("Ocelot", cTameable); @@ -245,7 +292,7 @@ public class EntityNBT extends EntityNBTBase { cParrot.add("Variant", new IntegerVariable("Variant", 0, 4)); NBTUnboundVariableContainer cWolf = new NBTUnboundVariableContainer("Wolf", cTameable); - cWolf.add("Angry", new BooleanVariable("Angry")); + cWolf.add("AngerTime", new IntegerVariable("AngerTime")); cWolf.add("CollarColor", new ByteVariable("CollarColor", (byte) 0, (byte) 15)); NBTUnboundVariableContainer cCat = new NBTUnboundVariableContainer("Cat", cTameable); @@ -313,7 +360,9 @@ public class EntityNBT extends EntityNBTBase { ENTITY_VARIABLES.put("minecraft:wolf", cWolf); ENTITY_VARIABLES.put("minecraft:zombie", cZombie); ENTITY_VARIABLES.put("minecraft:zombie_horse", cHorse); - ENTITY_VARIABLES.put("minecraft:zombie_pigman", cZombiePigman); + if (!BukkitVersion.isVersion(16)) { + ENTITY_VARIABLES.put("minecraft:zombie_pigman", cZombiePigman); + } ENTITY_VARIABLES.put("minecraft:zombie_villager", cZombieVillager); ENTITY_VARIABLES.put("minecraft:cod", cFish); ENTITY_VARIABLES.put("minecraft:salmon", cFish); @@ -321,17 +370,33 @@ public class EntityNBT extends EntityNBTBase { ENTITY_VARIABLES.put("minecraft:tropical_fish", cTropicalFish); // TODO: add variables for 1.14 mobs - ENTITY_VARIABLES.put("minecraft:pillager", cMob); - ENTITY_VARIABLES.put("minecraft:panda", cMob); - ENTITY_VARIABLES.put("minecraft:wandering_trader", cMob); - ENTITY_VARIABLES.put("minecraft:trader_llama", cMob); - ENTITY_VARIABLES.put("minecraft:fox", cFox); - ENTITY_VARIABLES.put("minecraft:cat", cCat); - ENTITY_VARIABLES.put("minecraft:ravager", cMob); + if (BukkitVersion.isVersion(14)) { + ENTITY_VARIABLES.put("minecraft:pillager", cMob); + ENTITY_VARIABLES.put("minecraft:panda", cMob); + ENTITY_VARIABLES.put("minecraft:wandering_trader", cMob); + ENTITY_VARIABLES.put("minecraft:trader_llama", cMob); + ENTITY_VARIABLES.put("minecraft:fox", cFox); + ENTITY_VARIABLES.put("minecraft:cat", cCat); + ENTITY_VARIABLES.put("minecraft:ravager", cMob); + } if (BukkitVersion.isVersion(15)) { - // TODO: add variables for 1.15 mobs - ENTITY_VARIABLES.put("minecraft:bee", cMob); + ENTITY_VARIABLES.put("minecraft:bee", cBee); + } + + if (BukkitVersion.isVersion(16)) { + ENTITY_VARIABLES.put("minecraft:piglin", cPiglin); + ENTITY_VARIABLES.put("minecraft:piglin_brute", cMob); + ENTITY_VARIABLES.put("minecraft:zombified_piglin", cZombifiedPiglin); + ENTITY_VARIABLES.put("minecraft:zoglin", cZoglin); + ENTITY_VARIABLES.put("minecraft:hoglin", cHoglin); + ENTITY_VARIABLES.put("minecraft:strider", cPig); + } + + if (BukkitVersion.isVersion(17)) { + ENTITY_VARIABLES.put("minecraft:axolotl", cAxolotl); + ENTITY_VARIABLES.put("minecraft:goat", cGoat); + ENTITY_VARIABLES.put("minecraft:glow_squid", cMob); } // Projectile Entities @@ -362,7 +427,7 @@ public class EntityNBT extends EntityNBTBase { cEnderPearl.add("Owner", new StringVariable("ownerName")); NBTUnboundVariableContainer cPotion = new NBTUnboundVariableContainer("Potion", cEntity); - cPotion.add("Potion", new PotionVariable("Potion")); + cPotion.add("Item", new PotionVariable("Item")); NBTUnboundVariableContainer cTrident = new NBTUnboundVariableContainer("Trident", cArrow); cTrident.add("Trident", new SingleItemVariable("Trident")); diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBTBase.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBTBase.java similarity index 99% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBTBase.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBTBase.java index 62603bc7..ebf03083 100644 --- a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBTBase.java +++ b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/EntityNBTBase.java @@ -129,6 +129,7 @@ public static EntityNBT fromEntity(Entity entity) { // When cloning, remove the UUID to force all entities to have a unique one. entityNbt._data.remove("UUIDMost"); entityNbt._data.remove("UUIDLeast"); + entityNbt._data.remove("UUID"); return entityNbt; } diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/FallingBlockNBT.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/FallingBlockNBT.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/FallingBlockNBT.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/FallingBlockNBT.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/ItemNBT.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/ItemNBT.java similarity index 97% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/ItemNBT.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/ItemNBT.java index bf4cb65e..15d97c09 100644 --- a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/ItemNBT.java +++ b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/ItemNBT.java @@ -25,6 +25,7 @@ public class ItemNBT extends BaseNBT { cItem.add("Lore", new RawJsonListVariable("display/Lore")); cItem.add("Damage", new IntegerVariable("Damage")); cItem.add("Unbreakable", new BooleanVariable("Unbreakable")); + cItem.add("CustomModelData", new IntegerVariable("CustomModelData")); cItem.add("CanDestroy", new MaterialListVariable("CanDestroy")); cItem.add("CanPlaceOn", new MaterialListVariable("CanPlaceOn")); cItem.add("HideFlags", new IntegerVariable("HideFlags")); diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/ItemStackNBTWrapper.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/ItemStackNBTWrapper.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/ItemStackNBTWrapper.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/ItemStackNBTWrapper.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/MinecartSpawnerNBT.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/MinecartSpawnerNBT.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/MinecartSpawnerNBT.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/MinecartSpawnerNBT.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/MobNBT.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/MobNBT.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/MobNBT.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/MobNBT.java diff --git a/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapper.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapper.java new file mode 100644 index 00000000..c157245a --- /dev/null +++ b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapper.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2013-2018 Gonçalo Baltazar + * + * This file is part of NBTEditor. + * + * NBTEditor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NBTEditor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NBTEditor. If not, see . + */ + +package com.goncalomb.bukkit.nbteditor.nbt; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; +import java.util.stream.Collectors; + +import com.goncalomb.bukkit.mylib.reflect.BukkitReflect; +import com.goncalomb.bukkit.nbteditor.nbt.SpawnerNBTWrapperAdapter.SpawnerAdapterWrappedEntity; + +import org.bukkit.block.Block; +import org.bukkit.entity.EntityType; + +public final class SpawnerNBTWrapper extends TileNBTWrapper { + + private static SpawnerNBTWrapperAdapter adapter = null; + + public static void prepareReflection(Class serverClass, Logger logger) throws Exception { + String packageName = serverClass.getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + + Class clazz = Class.forName("com.goncalomb.bukkit.nbteditor.nbt.SpawnerNBTWrapperAdapter_" + version); + adapter = (SpawnerNBTWrapperAdapter) clazz.getConstructor().newInstance(); + logger.info("Loaded SpawnerNBTWrapper adapter for " + version); + } + + public static class SpawnerEntity { + + public final EntityNBT entityNBT; + public final int weight; + + public SpawnerEntity(EntityNBT entityNBT, int weight) { + this.entityNBT = entityNBT; + this.weight = (weight < 1 ? 1 : weight); + } + + public SpawnerEntity(EntityType entityType, int weight) { + this(EntityNBT.fromEntityType(entityType), weight); + } + + private SpawnerAdapterWrappedEntity wrapForAdapter() { + return new SpawnerAdapterWrappedEntity(entityNBT._data, weight); + } + + private static SpawnerEntity fromAdapterWrapped(SpawnerAdapterWrappedEntity wrapped) { + return new SpawnerEntity(EntityNBT.fromEntityData(wrapped.data), wrapped.weight); + } + } + + public SpawnerNBTWrapper(Block block) { + super(block); + } + + public void addEntity(SpawnerEntity entity) { + BukkitReflect.ensureAdapter(adapter); + adapter.addEntity(_data, entity.wrapForAdapter()); + } + + public List getEntities() { + BukkitReflect.ensureAdapter(adapter); + List entities = adapter.getEntities(_data); + if (entities == null) { + return null; + } + return new ArrayList<>(entities.stream().map((entity) -> SpawnerEntity.fromAdapterWrapped(entity)).collect(Collectors.toList())); + } + + public void setEntities(List entities) { + BukkitReflect.ensureAdapter(adapter); + if (entities == null) { + adapter.setEntities(_data, null); + } else { + adapter.setEntities(_data, entities.stream().map((entity) -> entity.wrapForAdapter()).collect(Collectors.toList())); + } + } + + public void clearEntities() { + setEntities(null); + } + + public EntityType getCurrentEntity() { + BukkitReflect.ensureAdapter(adapter); + return adapter.getCurrentEntity(_data); + } +} diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/TileNBT.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/TileNBT.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/TileNBT.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/TileNBT.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/TileNBTWrapper.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/TileNBTWrapper.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/TileNBTWrapper.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/TileNBTWrapper.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/Attribute.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/Attribute.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/Attribute.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/Attribute.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/AttributeContainer.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/AttributeContainer.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/AttributeContainer.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/AttributeContainer.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/AttributeType.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/AttributeType.java similarity index 65% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/AttributeType.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/AttributeType.java index 76fdb3af..030cdb13 100644 --- a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/AttributeType.java +++ b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/AttributeType.java @@ -23,22 +23,22 @@ public enum AttributeType { // Mobs - MAX_HEALTH("MaxHealth", "generic.maxHealth", 0.0, Double.MAX_VALUE), - FOLLOW_RANGE("FollowRange", "generic.followRange", 0.0, 2048), - KNOCKBACK_RESISTANCE("KnockbackResistance", "generic.knockbackResistance", 0.0, 1.0), - MOVEMENT_SPEED("MovementSpeed", "generic.movementSpeed", 0.0, Double.MAX_VALUE), - ATTACK_DAMAGE("AttackDamage", "generic.attackDamage", 0.0, Double.MAX_VALUE), - ATTACK_KNOCKBACK("AttackKnockback", "generic.attackKnockback", 0.0, 5.0), - ARMOR("Armor", "generic.armor", 0.0, 30.0), - ARMOR_TOUGHNESS("ArmorToughness", "generic.armorToughness", 0.0, 20.0), - FLYING_SPEED("FlyingSpeed", "generic.flyingSpeed", 0.0, 1024.0), + MAX_HEALTH("MaxHealth", "minecraft:generic.max_health", 0.0, Double.MAX_VALUE), + FOLLOW_RANGE("FollowRange", "minecraft:generic.follow_range", 0.0, 2048), + KNOCKBACK_RESISTANCE("KnockbackResistance", "minecraft:generic.knockback_resistance", 0.0, 1.0), + MOVEMENT_SPEED("MovementSpeed", "minecraft:generic.movement_speed", 0.0, Double.MAX_VALUE), + ATTACK_DAMAGE("AttackDamage", "minecraft:generic.attack_damage", 0.0, Double.MAX_VALUE), + ATTACK_KNOCKBACK("AttackKnockback", "minecraft:generic.attack_knockback", 0.0, 5.0), + ARMOR("Armor", "minecraft:generic.armor", 0.0, 30.0), + ARMOR_TOUGHNESS("ArmorToughness", "minecraft:generic.armor_toughness", 0.0, 20.0), + FLYING_SPEED("FlyingSpeed", "minecraft:generic.flying_speed", 0.0, 1024.0), // Player - ATTACK_SPEED("AttackSpeed", "generic.attackSpeed", 0.0, 1024.0), - LUCK("Luck", "generic.luck", -1024.0, 1024.0), + ATTACK_SPEED("AttackSpeed", "minecraft:generic.attack_speed", 0.0, 1024.0), + LUCK("Luck", "minecraft:generic.luck", -1024.0, 1024.0), // Horses - JUMP_STRENGTH("JumpStrength", "horse.jumpStrength", 0.0, 2), + JUMP_STRENGTH("JumpStrength", "minecraft:horse.jump_strength", 0.0, 2), // Zombies - SPAWN_REINFORCEMENTS("SpawnReinforcements", "zombie.spawnReinforcements", 0.0, 1.0); + SPAWN_REINFORCEMENTS("SpawnReinforcements", "minecraft:zombie.spawn_reinforcements", 0.0, 1.0); private static final HashMap _attributes = new HashMap(); private static final HashMap _attributesInternal = new HashMap(); diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/ItemModifier.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/ItemModifier.java similarity index 96% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/ItemModifier.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/ItemModifier.java index 39da3263..d5b0187d 100644 --- a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/ItemModifier.java +++ b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/ItemModifier.java @@ -28,6 +28,7 @@ import com.goncalomb.bukkit.mylib.reflect.NBTTagCompound; import com.goncalomb.bukkit.mylib.reflect.NBTTagList; import com.goncalomb.bukkit.mylib.reflect.NBTUtils; +import com.goncalomb.bukkit.mylib.utils.UtilsMc; public final class ItemModifier extends Modifier { @@ -41,7 +42,7 @@ public static ItemModifier fromNBT(NBTTagCompound data) { data.getDouble("Amount"), data.getInt("Operation"), (data.hasKey("Slot") ? data.getString("Slot") : null), - new UUID(data.getLong("UUIDMost"), data.getLong("UUIDLeast"))); + UtilsMc.convertFromUUIDInts(data.getIntArray("UUID"))); } public static List getItemStackModifiers(ItemStack item) { diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/Modifier.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/Modifier.java similarity index 89% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/Modifier.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/Modifier.java index b689c0f9..e800223e 100644 --- a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/Modifier.java +++ b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/attributes/Modifier.java @@ -22,6 +22,7 @@ import java.util.UUID; import com.goncalomb.bukkit.mylib.reflect.NBTTagCompound; +import com.goncalomb.bukkit.mylib.utils.UtilsMc; public class Modifier { @@ -31,7 +32,7 @@ public class Modifier { private UUID _uuid; public static Modifier fromNBT(NBTTagCompound data) { - return new Modifier(data.getString("Name"), data.getDouble("Amount"), data.getInt("Operation"), new UUID(data.getLong("UUIDMost"), data.getLong("UUIDLeast"))); + return new Modifier(data.getString("Name"), data.getDouble("Amount"), data.getInt("Operation"), UtilsMc.convertFromUUIDInts(data.getIntArray("UUID"))); } public Modifier(String name, double amount, int operation) { @@ -66,8 +67,7 @@ public NBTTagCompound toNBT() { data.setString("Name", _name); data.setDouble("Amount", _amount); data.setInt("Operation", _operation); - data.setLong("UUIDMost", _uuid.getMostSignificantBits()); - data.setLong("UUIDLeast", _uuid.getLeastSignificantBits()); + data.setIntArray("UUID", UtilsMc.convertToUUIDInts(_uuid)); return data; } diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/BlockStateVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/BlockStateVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/BlockStateVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/BlockStateVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/BooleanVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/BooleanVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/BooleanVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/BooleanVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ByteVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ByteVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ByteVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ByteVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ColorVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ColorVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ColorVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ColorVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ContainerVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ContainerVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ContainerVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ContainerVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/DoubleVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/DoubleVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/DoubleVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/DoubleVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/EffectsVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/EffectsVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/EffectsVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/EffectsVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FireworksItemVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FireworksItemVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FireworksItemVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FireworksItemVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FloatArrayVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FloatArrayVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FloatArrayVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FloatArrayVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FloatVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FloatVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FloatVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/FloatVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/HorseVariantVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/HorseVariantVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/HorseVariantVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/HorseVariantVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/IntegerVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/IntegerVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/IntegerVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/IntegerVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/IntergerPositionVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/IntergerPositionVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/IntergerPositionVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/IntergerPositionVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ItemsVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ItemsVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ItemsVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ItemsVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ListVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ListVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ListVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ListVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/LongVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/LongVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/LongVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/LongVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/MaterialListVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/MaterialListVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/MaterialListVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/MaterialListVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTUnboundVariableContainer.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTUnboundVariableContainer.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTUnboundVariableContainer.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTUnboundVariableContainer.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariableContainer.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariableContainer.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariableContainer.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariableContainer.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariableDouble.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariableDouble.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariableDouble.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NBTVariableDouble.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NumericVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NumericVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NumericVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/NumericVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ParticleVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ParticleVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ParticleVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ParticleVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/PassengersVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/PassengersVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/PassengersVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/PassengersVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/PotionVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/PotionVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/PotionVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/PotionVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RawJsonListVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RawJsonListVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RawJsonListVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RawJsonListVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RawJsonVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RawJsonVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RawJsonVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RawJsonVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RotationVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RotationVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RotationVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/RotationVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ShortVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ShortVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ShortVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/ShortVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/SingleItemVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/SingleItemVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/SingleItemVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/SingleItemVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/SpecialVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/SpecialVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/SpecialVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/SpecialVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/StringListVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/StringListVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/StringListVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/StringListVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/StringVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/StringVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/StringVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/StringVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VectorVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VectorVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VectorVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VectorVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VillagerCareerVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VillagerCareerVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VillagerCareerVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VillagerCareerVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VillagerOffersVariable.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VillagerOffersVariable.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VillagerOffersVariable.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/variables/VillagerOffersVariable.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/tools/EntityInspectorTool.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/tools/EntityInspectorTool.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/tools/EntityInspectorTool.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/tools/EntityInspectorTool.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/tools/EntityRemoverTool.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/tools/EntityRemoverTool.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/tools/EntityRemoverTool.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/tools/EntityRemoverTool.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/tools/SuperLeadTool.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/tools/SuperLeadTool.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/tools/SuperLeadTool.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/tools/SuperLeadTool.java diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/tools/TropicalGeneratorTool.java b/plugin/src/main/java/com/goncalomb/bukkit/nbteditor/tools/TropicalGeneratorTool.java similarity index 100% rename from src/main/java/com/goncalomb/bukkit/nbteditor/tools/TropicalGeneratorTool.java rename to plugin/src/main/java/com/goncalomb/bukkit/nbteditor/tools/TropicalGeneratorTool.java diff --git a/src/main/java/net/iharder/Base64.java b/plugin/src/main/java/net/iharder/Base64.java similarity index 100% rename from src/main/java/net/iharder/Base64.java rename to plugin/src/main/java/net/iharder/Base64.java diff --git a/src/main/resources/README.txt b/plugin/src/main/resources/README.txt similarity index 100% rename from src/main/resources/README.txt rename to plugin/src/main/resources/README.txt diff --git a/src/main/resources/config.yml b/plugin/src/main/resources/config.yml similarity index 100% rename from src/main/resources/config.yml rename to plugin/src/main/resources/config.yml diff --git a/src/test/java/com/goncalomb/bukkit/mylib/TestNameMaps.java b/plugin/src/test/java/com/goncalomb/bukkit/mylib/TestNameMaps.java similarity index 100% rename from src/test/java/com/goncalomb/bukkit/mylib/TestNameMaps.java rename to plugin/src/test/java/com/goncalomb/bukkit/mylib/TestNameMaps.java diff --git a/src/test/java/com/goncalomb/bukkit/nbteditor/TestBosEntities.java b/plugin/src/test/java/com/goncalomb/bukkit/nbteditor/TestBosEntities.java similarity index 82% rename from src/test/java/com/goncalomb/bukkit/nbteditor/TestBosEntities.java rename to plugin/src/test/java/com/goncalomb/bukkit/nbteditor/TestBosEntities.java index 87e286c2..a6185b1e 100644 --- a/src/test/java/com/goncalomb/bukkit/nbteditor/TestBosEntities.java +++ b/plugin/src/test/java/com/goncalomb/bukkit/nbteditor/TestBosEntities.java @@ -19,10 +19,13 @@ package com.goncalomb.bukkit.nbteditor; +import com.goncalomb.bukkit.mylib.reflect.BukkitVersion; import java.util.Arrays; import java.util.HashSet; +import java.util.logging.Logger; import org.apache.commons.lang.StringUtils; +import org.bukkit.Server; import org.bukkit.entity.EntityType; import org.junit.Test; @@ -32,9 +35,9 @@ public class TestBosEntities { @Test - public void testBosEntities() { - HashSet entityTypes = new HashSet(); - entityTypes.addAll(Arrays.asList(EntityType.values())); + public void testBosEntities() throws Exception { + BukkitVersion.prepareForTest(); + HashSet entityTypes = new HashSet<>(Arrays.asList(EntityType.values())); for (String name : EntityNBT.getValidTypeNames()) { entityTypes.remove(EntityTypeMap.getByName(name)); } diff --git a/pom.xml b/pom.xml deleted file mode 100644 index 417507d9..00000000 --- a/pom.xml +++ /dev/null @@ -1,215 +0,0 @@ - - - 4.0.0 - - com.goncalomb.bukkit - nbteditor - 3.2-SNAPSHOT - jar - - NBTEditor - http://dev.bukkit.org/bukkit-plugins/nbteditor/ - 2013 - - - 1.15-R0.1-SNAPSHOT - UTF-8 - ${maven.build.timestamp} - yyyy-MM-dd'T'HH:mm:ss'Z' - - - - - spigot-repo - https://hub.spigotmc.org/nexus/content/groups/public/ - - - bstats-repo - https://repo.codemc.org/repository/maven-public - - - - - - junit - junit - 4.13.1 - test - - - org.bukkit - bukkit - ${bukkit.version} - provided - - - org.bstats - bstats-bukkit - 1.5 - - - - - - nexus-releases - https://nexus.goncalomb.com/repository/maven-releases - - - nexus-snapshots - https://nexus.goncalomb.com/repository/maven-snapshots - - - - - clean package - - - - ${project.basedir}/src/main/resources - true - - - ${project.basedir} - - LICENSE.txt - - - - - - - org.apache.maven.plugins - maven-enforcer-plugin - 3.0.0-M2 - - - enforce-maven - - enforce - - - - - 3.0 - - - - - - - - - org.apache.maven.plugins - maven-clean-plugin - 2.6.1 - - - - ${project.basedir} - - dependency-reduced-pom.xml - - - - - - - - org.apache.maven.plugins - maven-resources-plugin - 2.7 - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.3 - - 1.8 - 1.8 - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.18.1 - - - - org.apache.maven.plugins - maven-jar-plugin - 2.6 - - - - ${project.name} - ${project.version} - goncalomb - ${project.build.timestamp} - ${bukkit.version} - - - - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.1 - - - - org.bstats:* - - - - - org.bstats.bukkit - com.goncalomb.bukkit.bstats - - - - - - package - - shade - - - - - - - maven-assembly-plugin - 2.5.5 - - - package - - single - - - false - src/main/assembly/default.xml - - - - - - - org.apache.maven.plugins - maven-install-plugin - 2.5.2 - - - - org.apache.maven.plugins - maven-deploy-plugin - 2.8.2 - - - - - diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 00000000..98c008a3 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,14 @@ +rootProject.name = "nbteditor" +include(":adapter_api") +include(":adapter_v1_16_R3") +include(":adapter_v1_18_R1") +include(":adapter_v1_18_R2") +include(":NBTEditor") +project(":NBTEditor").projectDir = file("plugin") + +pluginManagement { + repositories { + gradlePluginPortal() + maven("https://papermc.io/repo/repository/maven-public/") + } +} diff --git a/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersion.java b/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersion.java deleted file mode 100644 index bc6d3cf0..00000000 --- a/src/main/java/com/goncalomb/bukkit/mylib/reflect/BukkitVersion.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.goncalomb.bukkit.mylib.reflect; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.bukkit.Bukkit; - -public final class BukkitVersion { - - private static int _minecraftVersionMajor; - private static int _minecraftVersionMinor; - - private static void getVersion() { - if (_minecraftVersionMajor == 0) { - Object server = Bukkit.getServer(); - if (server == null) { - // test environment, CraftBukkit / Minecraft not available - _minecraftVersionMajor = Integer.MAX_VALUE; - _minecraftVersionMinor = Integer.MAX_VALUE; - return; - } - try { - Object mcServer = BukkitReflect.invokeMethod(server, BukkitReflect.getCraftBukkitClass("CraftServer").getDeclaredMethod("getServer")); - String version = (String) BukkitReflect.invokeMethod(mcServer, BukkitReflect.getMinecraftClass("MinecraftServer").getDeclaredMethod("getVersion")); - Matcher matcher = Pattern.compile("^(\\d+)(?:\\.(\\d+)(?:\\.(\\d+))?)?$").matcher(version); - if (matcher.find()) { - _minecraftVersionMajor = Integer.parseInt(matcher.group(1)); - _minecraftVersionMinor = Integer.parseInt(matcher.group(2)); - } else { - throw new RuntimeException("Invalid Minecraft version."); - } - } catch (Exception e) { - throw new RuntimeException("Error finding Minecraft version.", e); - } - } - } - - private BukkitVersion() { } - - public static boolean isVersion(int minor) { - return isVersion(minor, 1); - } - - public static boolean isVersion(int minor, int major) { - getVersion(); - return _minecraftVersionMajor > major || (_minecraftVersionMajor == major && _minecraftVersionMinor >= minor); - } - -} diff --git a/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBase.java b/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBase.java deleted file mode 100644 index 661e054e..00000000 --- a/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTBase.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (C) 2013-2018 Gonçalo Baltazar - * - * This file is part of NBTEditor. - * - * NBTEditor is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NBTEditor is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NBTEditor. If not, see . - */ - -package com.goncalomb.bukkit.mylib.reflect; - -import java.lang.reflect.Method; - -public class NBTBase { - - private static boolean _isPrepared = false; - - protected static Class _nbtBaseClass; - protected static Class _nbtTagCompoundClass; - protected static Class _nbtTagListClass; - protected static Class _nbtTagStringClass; - - private static Method _getTypeId; - private static Method _clone; - - final Object _handle; // The wrapped Minecraft NBTBase instance. - - public static final void prepareReflection() { - if (!_isPrepared) { - _nbtBaseClass = BukkitReflect.getMinecraftClass("NBTBase"); - _nbtTagCompoundClass = BukkitReflect.getMinecraftClass("NBTTagCompound"); - _nbtTagListClass = BukkitReflect.getMinecraftClass("NBTTagList"); - _nbtTagStringClass = BukkitReflect.getMinecraftClass("NBTTagString"); - try { - _getTypeId = _nbtBaseClass.getMethod("getTypeId"); - _clone = _nbtBaseClass.getMethod("clone"); - NBTTagCompound.prepareReflectionz(); - NBTTagList.prepareReflectionz(); - NBTTypes.prepareReflection(); - NBTUtils.prepareReflection(); - } catch (Exception e) { - throw new RuntimeException("Error while preparing NBT wrapper classes.", e); - } - _isPrepared = true; - } - } - - // Wraps any Minecraft tags in MyLib tags. - // Primitives and strings are wrapped with NBTBase. - protected static final NBTBase wrap(Object object) { - if (_nbtTagCompoundClass.isInstance(object)) { - return new NBTTagCompound(object); - } else if (_nbtTagListClass.isInstance(object)) { - return new NBTTagList(object); - } else if (_nbtBaseClass.isInstance(object)) { - return new NBTBase(object); - } else { - throw new RuntimeException(object.getClass() + " is not a valid NBT tag type."); - } - } - - // Helper method for NBTTagCompoundWrapper.merge(). - // Clones any internal Minecraft tags. - protected static final Object clone(Object nbtBaseObject) { - return BukkitReflect.invokeMethod(nbtBaseObject, _clone); - } - - protected NBTBase(Object handle) { - _handle = handle; - } - - protected final Object invokeMethod(Method method, Object... args) { - return BukkitReflect.invokeMethod(_handle, method, args); - } - - static byte getTypeId(Object handle) { - return (Byte) BukkitReflect.invokeMethod(handle, _getTypeId); - } - - public NBTBase clone() { - return wrap(invokeMethod(_clone)); - } - - @Override - public String toString() { - return _handle.toString(); - } - -} diff --git a/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompound.java b/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompound.java deleted file mode 100644 index fad4e051..00000000 --- a/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTTagCompound.java +++ /dev/null @@ -1,232 +0,0 @@ -/* - * Copyright (C) 2013-2018 Gonçalo Baltazar - * - * This file is part of NBTEditor. - * - * NBTEditor is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NBTEditor is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NBTEditor. If not, see . - */ - -package com.goncalomb.bukkit.mylib.reflect; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.Map; -import java.util.Map.Entry; - -public final class NBTTagCompound extends NBTBase { - - private static Method _getByte; - private static Method _getShort; - private static Method _getInt; - private static Method _getLong; - private static Method _getFloat; - private static Method _getDouble; - private static Method _getString; - private static Field _mapField; - - private static Method _tagSerializeStream; - private static Method _tagUnserializeStream; - private static Method _parseMojangson; - - static void prepareReflectionz() throws SecurityException, NoSuchMethodException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { - _getByte = _nbtTagCompoundClass.getMethod("getByte", String.class); - _getShort = _nbtTagCompoundClass.getMethod("getShort", String.class); - _getInt = _nbtTagCompoundClass.getMethod("getInt", String.class); - _getLong = _nbtTagCompoundClass.getMethod("getLong", String.class); - _getFloat = _nbtTagCompoundClass.getMethod("getFloat", String.class); - _getDouble = _nbtTagCompoundClass.getMethod("getDouble", String.class); - _getString = _nbtTagCompoundClass.getMethod("getString", String.class); - _mapField = _nbtTagCompoundClass.getDeclaredField("map"); - _mapField.setAccessible(true); - - Class_mojangsonParserClass = BukkitReflect.getMinecraftClass("MojangsonParser"); - _parseMojangson = _mojangsonParserClass.getMethod("parse", String.class); - - Class nbtCompressedStreamToolsClass = BukkitReflect.getMinecraftClass("NBTCompressedStreamTools"); - _tagSerializeStream = nbtCompressedStreamToolsClass.getMethod("a", _nbtTagCompoundClass, OutputStream.class); - _tagUnserializeStream = nbtCompressedStreamToolsClass.getMethod("a", InputStream.class); - } - - Map _map; - - public NBTTagCompound() { - this(BukkitReflect.newInstance(_nbtTagCompoundClass)); - } - - @SuppressWarnings("unchecked") - NBTTagCompound(Object handle) { - super(handle); - _map = (Map) BukkitReflect.getFieldValue(handle, _mapField); - } - - public byte getByte(String key) { - return (Byte) invokeMethod(_getByte, key); - } - - public short getShort(String key) { - return (Short) invokeMethod(_getShort, key); - } - - public int getInt(String key) { - return (Integer) invokeMethod(_getInt, key); - } - - public long getLong(String key) { - return (Long) invokeMethod(_getLong, key); - } - - public float getFloat(String key) { - return (Float) invokeMethod(_getFloat, key); - } - - public Double getDouble(String key) { - return (Double) invokeMethod(_getDouble, key); - } - - public String getString(String key) { - return (String) invokeMethod(_getString, key); - } - - public NBTTagCompound getCompound(String key) { - Object obj = _map.get(key); - if (obj != null && _nbtTagCompoundClass.isInstance(obj)) { - return new NBTTagCompound(obj); - } - return null; - } - - public NBTTagList getList(String key) { - Object obj = _map.get(key); - if (obj != null && _nbtTagListClass.isInstance(obj)) { - return new NBTTagList(obj); - } - return null; - } - - public Object[] getListAsArray(String key) { - NBTTagList list = getList(key); - return (list == null ? null : list.getAsArray()); - } - - public void setByte(String key, byte value) { - set(key, value); - } - - public void setShort(String key, short value) { - set(key, value); - } - - public void setInt(String key, int value) { - set(key, value); - } - - public void setLong(String key, long value) { - set(key, value); - } - - public void setFloat(String key, float value) { - set(key, value); - } - - public void setDouble(String key, double value) { - set(key, value); - } - - public void setString(String key, String value) { - set(key, value); - } - - public void setCompound(String key, NBTTagCompound value) { - set(key, value); - } - - public void setList(String key, NBTTagList value) { - set(key, value); - } - - public void setList(String key, Object... objects) { - set(key, new NBTTagList(objects)); - } - - private void set(String key, Object value) { - _map.put(key, NBTTypes.toInternal(value)); - } - - public boolean hasKey(String key) { - return _map.containsKey(key); - } - - public void remove(String key) { - _map.remove(key); - } - - public int size() { - return _map.size(); - } - - public boolean isEmpty() { - return _map.isEmpty(); - } - - public void clear() { - _map.clear(); - } - - public byte[] serialize() { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - serialize(out); - return out.toByteArray(); - } - - public void serialize(OutputStream outputStream) { - BukkitReflect.invokeMethod(null, _tagSerializeStream, _handle, outputStream); - } - - public static NBTTagCompound unserialize(byte[] data) { - ByteArrayInputStream in = new ByteArrayInputStream(data); - NBTTagCompound tag = unserialize(in); - try { - in.close(); - } catch (IOException e) { } - return tag; - } - - public static NBTTagCompound unserialize(InputStream inputStream) { - return new NBTTagCompound(BukkitReflect.invokeMethod(null, _tagUnserializeStream, inputStream)); - } - - public void merge(NBTTagCompound other) { - for (Entry tag : other._map.entrySet()) { - _map.put(tag.getKey(), NBTBase.clone(tag.getValue())); - } - } - - @Override - public NBTTagCompound clone() { - return (NBTTagCompound) super.clone(); - } - - /* - * Converts a string-ified "mojangson" tag back into an NBTTagCompound - * This is the opposite of toString() - */ - public static NBTTagCompound fromString(String mojangson) { - return new NBTTagCompound(BukkitReflect.invokeMethod(null, _parseMojangson, mojangson)); - } -} diff --git a/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtils.java b/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtils.java deleted file mode 100644 index 921fa21d..00000000 --- a/src/main/java/com/goncalomb/bukkit/mylib/reflect/NBTUtils.java +++ /dev/null @@ -1,222 +0,0 @@ -/* - * Copyright (C) 2013-2018 Gonçalo Baltazar - * - * This file is part of NBTEditor. - * - * NBTEditor is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NBTEditor is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NBTEditor. If not, see . - */ - -package com.goncalomb.bukkit.mylib.reflect; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.Optional; - -import org.bukkit.Location; -import org.bukkit.block.Block; -import org.bukkit.entity.Entity; -import org.bukkit.inventory.ItemStack; - -public final class NBTUtils { - - // Minecraft's ItemStack Class; - private static Constructor _ItemStack_nbtConstructor; - private static Method _ItemStack_save; - private static Method _ItemStack_getTag; - private static Method _ItemStack_setTag; - - // CraftItemStack Class; - private static Method _CraftItemStack_asCraftMirror; - private static Method _CraftItemStack_asCraftCopy; - private static Field _CraftItemStack_handle; - - // Minecraft's Entity Class; - private static Method _Entity_save; - private static Method _Entity_getBukkitEntity; - private static Method _Entity_setPosition; - - // CraftEntity Class - private static Method _CraftEntity_getHandle; - - // Minecraft's TileEntity - private static Method _TileEntity_load; // Load data from NBTTagCompound. - private static Method _TileEntity_save; // Save data to NBTTagCompound. - - // CraftWorld - private static Method _CraftWorld_getHandle; - - // Minecraft's BlockPosition - private static Constructor _BlockPosition_constructor; - - // Minecraft's World - private static Method _World_getTileEntity; - private static Method _World_addEntity; - - // Minecraft's EntityTypes Class - private static Method _EntityTypes_a; // Spawn an entity from a NBTCompound. - - static void prepareReflection() throws SecurityException, NoSuchMethodException, NoSuchFieldException { - Class nbtTagCompoundClass = BukkitReflect.getMinecraftClass("NBTTagCompound"); - - Class minecraftItemStackClass = BukkitReflect.getMinecraftClass("ItemStack"); - _ItemStack_nbtConstructor = minecraftItemStackClass.getDeclaredConstructor(nbtTagCompoundClass); - _ItemStack_nbtConstructor.setAccessible(true); - _ItemStack_save = minecraftItemStackClass.getMethod("save", nbtTagCompoundClass); - _ItemStack_getTag = minecraftItemStackClass.getMethod("getTag"); - _ItemStack_setTag = minecraftItemStackClass.getMethod("setTag", nbtTagCompoundClass); - - Class craftItemStackClass = BukkitReflect.getCraftBukkitClass("inventory.CraftItemStack"); - _CraftItemStack_asCraftMirror = craftItemStackClass.getMethod("asCraftMirror", minecraftItemStackClass); - _CraftItemStack_asCraftCopy = craftItemStackClass.getMethod("asCraftCopy", ItemStack.class); - _CraftItemStack_handle = craftItemStackClass.getDeclaredField("handle"); - _CraftItemStack_handle.setAccessible(true); - - Class minecraftEntityClass = BukkitReflect.getMinecraftClass("Entity"); - _Entity_save = minecraftEntityClass.getMethod("save", nbtTagCompoundClass); - _Entity_getBukkitEntity = minecraftEntityClass.getMethod("getBukkitEntity"); - _Entity_setPosition = minecraftEntityClass.getMethod("setPosition", double.class, double.class, double.class); - - Class craftEntityClass = BukkitReflect.getCraftBukkitClass("entity.CraftEntity"); - _CraftEntity_getHandle = craftEntityClass.getMethod("getHandle"); - - Class minecraftTileEntityClass = BukkitReflect.getMinecraftClass("TileEntity"); - _TileEntity_save = minecraftTileEntityClass.getMethod("save", nbtTagCompoundClass); - try { - // Bukkit 1.12.1+ - _TileEntity_load = minecraftTileEntityClass.getMethod("load", nbtTagCompoundClass); - } catch (NoSuchMethodException e) { - // Bukkit 1.12 - // XXX: remove fallback on next version - _TileEntity_load = minecraftTileEntityClass.getMethod("a", nbtTagCompoundClass); - } - - Class craftWorldClass = BukkitReflect.getCraftBukkitClass("CraftWorld"); - _CraftWorld_getHandle = craftWorldClass.getMethod("getHandle"); - - Class minecraftBlockPositionClass = BukkitReflect.getMinecraftClass("BlockPosition"); - _BlockPosition_constructor = minecraftBlockPositionClass.getConstructor(int.class, int.class, int.class); - - Class minecraftWorldClass = BukkitReflect.getMinecraftClass("World"); - _World_getTileEntity = minecraftWorldClass.getMethod("getTileEntity", minecraftBlockPositionClass); - _World_addEntity = minecraftWorldClass.getMethod("addEntity", minecraftEntityClass); - - Class minecraftEntityTypesClass = BukkitReflect.getMinecraftClass("EntityTypes"); - _EntityTypes_a = minecraftEntityTypesClass.getMethod("a", nbtTagCompoundClass, minecraftWorldClass); - } - - private NBTUtils() { } - - public static ItemStack itemStackFromNBTData(NBTTagCompound data) { - return (ItemStack) BukkitReflect.invokeMethod(null, _CraftItemStack_asCraftMirror, BukkitReflect.invokeConstuctor(_ItemStack_nbtConstructor, data._handle)); - } - - public static NBTTagCompound itemStackToNBTData(ItemStack stack) { - NBTTagCompound data = new NBTTagCompound(); - Object handle = BukkitReflect.getFieldValue(stack, _CraftItemStack_handle); - BukkitReflect.invokeMethod(handle, _ItemStack_save, data._handle); - return data; - } - - @SuppressWarnings("unchecked") - public static Entity spawnEntity(NBTTagCompound data, Location location) { - Object worldHandle = BukkitReflect.invokeMethod(location.getWorld(), _CraftWorld_getHandle); - Optional entityHandleOp = (Optional) BukkitReflect.invokeMethod(null, _EntityTypes_a, data._handle, worldHandle); - if (!entityHandleOp.isPresent()) { - return null; - } - Object entityHandle = entityHandleOp.get(); - BukkitReflect.invokeMethod(entityHandle, _Entity_setPosition, location.getX(), location.getY(), location.getZ()); - BukkitReflect.invokeMethod(worldHandle, _World_addEntity, entityHandle); - return (Entity) BukkitReflect.invokeMethod(entityHandle, _Entity_getBukkitEntity); - } - - public static NBTTagCompound getEntityNBTData(Entity entity) { - Object entityHandle = BukkitReflect.invokeMethod(entity, _CraftEntity_getHandle); - NBTTagCompound data = new NBTTagCompound(); - BukkitReflect.invokeMethod(entityHandle, _Entity_save, data._handle); - return data; - } - - public static NBTTagList potionToNBTEffectsList(ItemStack potion) { - NBTTagCompound tag = getItemStackTag(potion); - if (tag.hasKey("CustomPotionEffects")) { - return tag.getList("CustomPotionEffects").clone(); - } - // Fallback to default potion effect. - - // XXX: implement fallback - - // Finding the default potion effect is not trivial on 1.9. - // Wait until org.bukkit.craftbukkit.potion.CraftPotionUtil is available upstream. - // For now, display some alert messages on InventoryForMobs and InventoryForThrownPotion. - return new NBTTagList(); - } - - public static ItemStack potionFromNBTEffectsList(NBTTagList effects) { - NBTTagCompound tag = new NBTTagCompound(); - tag.setList("CustomPotionEffects", effects.clone()); - tag.setString("Potion", "minecraft:empty"); - NBTTagCompound data = new NBTTagCompound(); - data.setString("id", "minecraft:potion"); - data.setByte("Count", (byte) 1); - data.setShort("Damage", (short) 0); - data.setCompound("tag", tag); - return itemStackFromNBTData(data); - } - - private static Object getTileEntity(Block block) { - Object worldHandle = BukkitReflect.invokeMethod(block.getWorld(), _CraftWorld_getHandle); - Object pos = BukkitReflect.invokeConstuctor(_BlockPosition_constructor, block.getX(), block.getY(), block.getZ()); - return BukkitReflect.invokeMethod(worldHandle, _World_getTileEntity, pos); - } - - public static NBTTagCompound getTileEntityNBTData(Block block) { - NBTBase.prepareReflection(); - Object tileEntity = getTileEntity(block); - if (tileEntity != null) { - NBTTagCompound data = new NBTTagCompound(); - BukkitReflect.invokeMethod(tileEntity, _TileEntity_save, data._handle); - return data; - } - return null; - } - - public static void setTileEntityNBTData(Block block, NBTTagCompound data) { - NBTBase.prepareReflection(); - Object tileEntity = getTileEntity(block); - if (tileEntity != null) { - BukkitReflect.invokeMethod(tileEntity, _TileEntity_load, data._handle); - } - } - - public static NBTTagCompound getItemStackTag(ItemStack item) { - Object handle = BukkitReflect.getFieldValue(item, _CraftItemStack_handle); - Object tag = BukkitReflect.invokeMethod(handle, _ItemStack_getTag); - return (tag == null ? new NBTTagCompound() : new NBTTagCompound(tag)); - } - - public static void setItemStackTag(ItemStack item, NBTTagCompound tag) { - Object handle = BukkitReflect.getFieldValue(item, _CraftItemStack_handle); - BukkitReflect.invokeMethod(handle, _ItemStack_setTag, tag._handle); - } - - public static ItemStack itemStackToCraftItemStack(ItemStack item) { - if (!_CraftItemStack_asCraftCopy.getClass().isInstance(item)) { - return (ItemStack) BukkitReflect.invokeMethod(null, _CraftItemStack_asCraftCopy, item); - } - return item; - } - -} diff --git a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapper.java b/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapper.java deleted file mode 100644 index 0fde8acf..00000000 --- a/src/main/java/com/goncalomb/bukkit/nbteditor/nbt/SpawnerNBTWrapper.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2013-2018 Gonçalo Baltazar - * - * This file is part of NBTEditor. - * - * NBTEditor is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NBTEditor is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NBTEditor. If not, see . - */ - -package com.goncalomb.bukkit.nbteditor.nbt; - -import java.util.ArrayList; -import java.util.List; - -import org.bukkit.block.Block; -import org.bukkit.entity.EntityType; - -import com.goncalomb.bukkit.mylib.namemaps.EntityTypeMap; -import com.goncalomb.bukkit.mylib.reflect.NBTTagCompound; -import com.goncalomb.bukkit.mylib.reflect.NBTTagList; - -public final class SpawnerNBTWrapper extends TileNBTWrapper { - - public static class SpawnerEntity { - - public final EntityNBT entityNBT; - public final int weight; - - public SpawnerEntity(EntityNBT entityNBT, int weight) { - this.entityNBT = entityNBT; - this.weight = (weight < 1 ? 1 : weight); - } - - public SpawnerEntity(EntityType entityType, int weight) { - this(EntityNBT.fromEntityType(entityType), weight); - } - - NBTTagCompound getCompound() { - NBTTagCompound data = new NBTTagCompound(); - data.setInt("Weight", weight); - data.setCompound("Entity", entityNBT._data); - return data; - } - - } - - public SpawnerNBTWrapper(Block block) { - super(block); - } - - public void addEntity(SpawnerEntity entity) { - List entities = getEntities(); - entities.add(entity); - _data.setCompound("SpawnData", entity.entityNBT._data); - setEntities(entities); - } - - public List getEntities() { - ArrayList entities = new ArrayList(); - if (_data.hasKey("SpawnPotentials")) { - NBTTagList spawnPotentials = _data.getList("SpawnPotentials"); - int l = spawnPotentials.size(); - for (int i = 0; i < l; ++i) { - NBTTagCompound potential = (NBTTagCompound) spawnPotentials.get(i); - EntityNBT entityNbt = EntityNBT.fromEntityData(potential.getCompound("Entity")); - if (entityNbt != null) { - entities.add(new SpawnerEntity(entityNbt, potential.getInt("Weight"))); - } - } - _data.remove("SpawnPotentials"); - } - return entities; - } - - public void setEntities(List entities) { - if (entities != null && entities.size() > 0) { - NBTTagList spawnPotentials = new NBTTagList(); - for (SpawnerEntity entity : entities) { - spawnPotentials.add(entity.getCompound()); - } - _data.setList("SpawnPotentials", spawnPotentials); - } else { - NBTTagCompound simplePig = new NBTTagCompound(); - - _data.setCompound("SpawnData", simplePig); - _data.remove("SpawnPotentials"); - } - - } - - public void clearEntities() { - setEntities(null); - } - - public EntityType getCurrentEntity() { - NBTTagCompound spawnData = _data.getCompound("SpawnData"); - if (spawnData != null) { - return EntityTypeMap.getByName(spawnData.getString("id")); - } - return null; - } - -} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml deleted file mode 100644 index b1c7c314..00000000 --- a/src/main/resources/plugin.yml +++ /dev/null @@ -1,5 +0,0 @@ -name: NBTEditor -main: com.goncalomb.bukkit.nbteditor.NBTEditor -version: ${project.version} -author: goncalomb -api-version: 1.13