diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 714e7e44..2748dc2c 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -14,22 +14,21 @@ jobs: name: Build runs-on: ubuntu-latest steps: + # Checkout project files + - uses: actions/checkout@v2 + # Setup Java - uses: actions/setup-java@v2 with: java-version: 17 distribution: adopt + cache: maven - # Checkout project files - - uses: actions/checkout@v2 - - # Caches - - name: 'Cache: Maven' - uses: actions/cache@v2 + # Build Spigot 1.18.2 (remapped) + - uses: SpraxDev/Action-SpigotMC@v4 with: - path: ~/.m2/repository - key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-maven- + versions: 1.18.2 + remapped: true # Build project - name: Build with Maven diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 5bd42b4a..8eed787a 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -13,33 +13,35 @@ jobs: env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} steps: - # Setup Java - - uses: actions/setup-java@v2 - if: ${{ env.SONAR_TOKEN != null }} - with: - java-version: 17 - distribution: adopt - # Checkout project files - uses: actions/checkout@v2 if: ${{ env.SONAR_TOKEN != null }} with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - # Caches - - name: 'Cache: Maven' - uses: actions/cache@v2 + # Setup Java + - uses: actions/setup-java@v2 if: ${{ env.SONAR_TOKEN != null }} with: - path: ~/.m2/repository - key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-maven- + java-version: 17 + distribution: adopt + cache: maven + + # Cache - name: 'Cache: SonarCloud' + if: ${{ env.SONAR_TOKEN != null }} uses: actions/cache@v2 with: path: ~/.sonar/cache key: ${{ runner.os }}-sonar + # Build Spigot 1.18.2 (remapped) + - uses: SpraxDev/Action-SpigotMC@v4 + if: ${{ env.SONAR_TOKEN != null }} + with: + versions: 1.18.2 + remapped: true + - name: Analyze project if: ${{ env.SONAR_TOKEN != null }} run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=songoda_SongodaCore diff --git a/Compatibility/pom.xml b/Compatibility/pom.xml index f35808a0..f3846668 100644 --- a/Compatibility/pom.xml +++ b/Compatibility/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../pom.xml diff --git a/Compatibility/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java b/Compatibility/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java index 4fb19f42..48ad72f4 100644 --- a/Compatibility/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java +++ b/Compatibility/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java @@ -32,6 +32,9 @@ public enum CompatibleMaterial { DARK_OAK_DOOR_ITEM(431), */ + /* 1.18 */ + MUSIC_DISC_OTHERSIDE(), + /* 1.17 */ AMETHYST_BLOCK(), AMETHYST_CLUSTER(), @@ -1274,7 +1277,7 @@ public enum CompatibleMaterial { YELLOW_WOOL("WOOL", (byte) 4), ZOMBIE_HEAD("SKULL_ITEM", (byte) 2), ZOMBIE_HORSE_SPAWN_EGG("MONSTER_EGG", (byte) 29), - ZOMBIE_PIGMAN_SPAWN_EGG("MONSTER_EGG", (byte) 57), + ZOMBIE_PIGMAN_SPAWN_EGG("MONSTER_EGG", (byte) 57), // FIXME: The name changed in newer versions (nether update maybe?) ZOMBIE_SPAWN_EGG("MONSTER_EGG", (byte) 54), ZOMBIE_VILLAGER_SPAWN_EGG("MONSTER_EGG", (byte) 27), ZOMBIE_WALL_HEAD("SKULL", (byte) 2), diff --git a/Compatibility/src/main/java/com/songoda/core/compatibility/MethodMapping.java b/Compatibility/src/main/java/com/songoda/core/compatibility/MethodMapping.java index 26890834..abb97765 100644 --- a/Compatibility/src/main/java/com/songoda/core/compatibility/MethodMapping.java +++ b/Compatibility/src/main/java/com/songoda/core/compatibility/MethodMapping.java @@ -6,21 +6,21 @@ import org.bukkit.inventory.ItemStack; import java.lang.reflect.Method; public enum MethodMapping { - MC_ITEM_STACK__GET_TAG("getTag", "getTag", "s"), - MC_ITEM_STACK__SET_TAG("setTag", "setTag", "c", ClassMapping.NBT_TAG_COMPOUND.getClazz()), + MC_ITEM_STACK__GET_TAG("getTag", "getTag", "s", "t"), + MC_ITEM_STACK__SET_TAG("setTag", "setTag", "c", "c", ClassMapping.NBT_TAG_COMPOUND.getClazz()), - MC_NBT_TAG_COMPOUND__SET("set", "set", "a", String.class, ClassMapping.NBT_BASE.getClazz()), - MC_NBT_TAG_COMPOUND__SET_SHORT("setShort", "setShort", "a", String.class, short.class), - MC_NBT_TAG_COMPOUND__SET_STRING("setString", "setString", "a", String.class, String.class), - MC_NBT_TAG_COMPOUND__REMOVE("remove", "remove", "r", String.class), + MC_NBT_TAG_COMPOUND__SET("set", "set", "a", "a", String.class, ClassMapping.NBT_BASE.getClazz()), + MC_NBT_TAG_COMPOUND__SET_SHORT("setShort", "setShort", "a", "a", String.class, short.class), + MC_NBT_TAG_COMPOUND__SET_STRING("setString", "setString", "a", "a", String.class, String.class), + MC_NBT_TAG_COMPOUND__REMOVE("remove", "remove", "r", "r", String.class), - MC_NBT_TAG_LIST__ADD("add", "a", "add", "a", ClassMapping.NBT_BASE.getClazz()), + MC_NBT_TAG_LIST__ADD("add", "a", "add", "a", "add", ClassMapping.NBT_BASE.getClazz()), - MC_CHUNK__GET_WORLD("getWorld", "D"), + MC_CHUNK__GET_WORLD("getWorld", "getWorld", "D"), CB_GENERIC__GET_HANDLE("getHandle"), - CB_BLOCK__GET_NMS("getNMSBlock", "getNMS"), + CB_BLOCK__GET_NMS("getNMSBlock", "getNMS", "getNMS"), CB_BLOCK__GET_POSITION("getPosition"), CB_BLOCK_DATA__GET_STATE("getState"), @@ -30,67 +30,72 @@ public enum MethodMapping { CRAFT_MAGIC_NUMBERS__GET_BLOCK__MATERIAL("getBlock", Material.class), - I_BLOCK_DATA__GET_BLOCK("getBlock", "b"), + I_BLOCK_DATA__GET_BLOCK("getBlock", "b", "b"), - BLOCK__GET_BLOCK_DATA("getBlockData", "n"), + BLOCK__GET_BLOCK_DATA("getBlockData", "n", "n"), - CHUNK__SET_BLOCK_STATE("setType", "setBlockState", ClassMapping.BLOCK_POSITION.getClazz(), ClassMapping.I_BLOCK_DATA.getClazz(), boolean.class, boolean.class), + CHUNK__SET_BLOCK_STATE("setType", "setBlockState", "a", ClassMapping.BLOCK_POSITION.getClazz(), ClassMapping.I_BLOCK_DATA.getClazz(), boolean.class, boolean.class), - ITEM_STACK__SAVE("save", "b", ClassMapping.NBT_TAG_COMPOUND.getClazz()), - ITEM_STACK__GET_ITEM("getItem", "c"), - ITEM_STACK__GET_MAX_STACK_SIZE("getMaxStackSize", "l"), + ITEM_STACK__SAVE("save", "b", "b", ClassMapping.NBT_TAG_COMPOUND.getClazz()), + ITEM_STACK__GET_ITEM("getItem", "c", "c"), + ITEM_STACK__GET_MAX_STACK_SIZE("getMaxStackSize", "l", "e"), - WORLD__UPDATE_ADJACENT_COMPARATORS("updateAdjacentComparators", "c", ClassMapping.BLOCK_POSITION.getClazz(), ClassMapping.BLOCK.getClazz()), - WORLD__GET_CHUNK_AT("getChunkAt", "d", int.class, int.class), + WORLD__UPDATE_ADJACENT_COMPARATORS("updateAdjacentComparators", "c", "c", ClassMapping.BLOCK_POSITION.getClazz(), ClassMapping.BLOCK.getClazz()), + WORLD__GET_CHUNK_AT("getChunkAt", "d", "a", int.class, int.class), - WORLD_BOARDER__SET_CENTER("setCenter", "setCenter", "setCenter", "c", double.class, double.class), - WORLD_BOARDER__SET_SIZE("setSize", "setSize", "setSize", "a", double.class), - WORLD_BOARDER__SET_WARNING_TIME("setWarningTime", "setWarningTime", "setWarningTime", "b", int.class), - WORLD_BOARDER__SET_WARNING_DISTANCE("setWarningDistance", "setWarningDistance", "setWarningDistance", "c", int.class), - WORLD_BOARDER__TRANSITION_SIZE_BETWEEN("transitionSizeBetween", "transitionSizeBetween", "transitionSizeBetween", "a", double.class, double.class, long.class), + WORLD_BOARDER__SET_CENTER("setCenter", "setCenter", "setCenter", "c", "c", double.class, double.class), + WORLD_BOARDER__SET_SIZE("setSize", "setSize", "setSize", "a", "a", double.class), + WORLD_BOARDER__SET_WARNING_TIME("setWarningTime", "setWarningTime", "setWarningTime", "b", "b", int.class), + WORLD_BOARDER__SET_WARNING_DISTANCE("setWarningDistance", "setWarningDistance", "setWarningDistance", "c", "c", int.class), + WORLD_BOARDER__TRANSITION_SIZE_BETWEEN("transitionSizeBetween", "transitionSizeBetween", "transitionSizeBetween", "a", "a", double.class, double.class, long.class), - MOJANGSON_PARSER__PARSE("parse", "a", String.class); + MOJANGSON_PARSER__PARSE("parse", "a", "a", String.class); private final String saneFallback; private final String _1_14; private final String _1_17; private final String _1_18; - private final Class[] paramaters; + private final String _1_18_2; + private final Class[] parameters; - MethodMapping(String saneFallback, String _1_14, String _1_17, String _1_18, Class... paramaters) { + MethodMapping(String saneFallback, String _1_14, String _1_17, String _1_18, String _1_18_2, Class... parameters) { this.saneFallback = saneFallback; this._1_14 = _1_14; this._1_17 = _1_17; this._1_18 = _1_18; - this.paramaters = paramaters; + this._1_18_2 = _1_18_2; + this.parameters = parameters; } - MethodMapping(String saneFallback, String _1_17, String _1_18, Class... paramaters) { + MethodMapping(String saneFallback, String _1_17, String _1_18, String _1_18_2, Class... parameters) { this.saneFallback = saneFallback; this._1_14 = null; this._1_17 = _1_17; this._1_18 = _1_18; - this.paramaters = paramaters; + this._1_18_2 = _1_18_2; + this.parameters = parameters; } - MethodMapping(String saneFallback, String _1_18, Class... paramaters) { + MethodMapping(String saneFallback, String _1_18, String _1_18_2, Class... parameters) { this.saneFallback = saneFallback; this._1_14 = null; this._1_17 = null; this._1_18 = _1_18; - this.paramaters = paramaters; + this._1_18_2 = _1_18_2; + this.parameters = parameters; } - MethodMapping(String saneFallback, Class... paramaters) { + MethodMapping(String saneFallback, Class... parameters) { this.saneFallback = saneFallback; this._1_14 = null; this._1_17 = null; this._1_18 = null; - this.paramaters = paramaters; + this._1_18_2 = null; + this.parameters = parameters; } public Method getMethod(Class clazz) { @@ -109,17 +114,21 @@ public enum MethodMapping { } break; + case V1_18: + if (_1_18_2 != null) { + methodName = _1_18_2; + } } try { - Method method = clazz.getMethod(methodName, paramaters); + Method method = clazz.getMethod(methodName, parameters); method.setAccessible(true); return method; } catch (NullPointerException | NoSuchMethodException ex) { if (saneFallback != null && !saneFallback.equals(methodName)) { try { - Method method = clazz.getMethod(saneFallback, paramaters); + Method method = clazz.getMethod(saneFallback, parameters); method.setAccessible(true); return method; diff --git a/Core/pom.xml b/Core/pom.xml index fb16a09f..1ffe1cec 100644 --- a/Core/pom.xml +++ b/Core/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../pom.xml @@ -69,6 +69,7 @@ com.songoda:* com.zaxxer:HikariCP + de.tr7zw:item-nbt-api @@ -77,6 +78,11 @@ com.zaxxer com.songoda.core.third_party.com.zaxxer + + + de.tr7zw.changeme.nbtapi + com.songoda.core.third_party.de.tr7zw.nbtapi + @@ -85,6 +91,7 @@ com/ + de/tr7zw/ META-INF/MANIFEST.MF META-INF/maven/com.songoda/SongodaCore/ @@ -112,6 +119,13 @@ compile + + de.tr7zw + item-nbt-api + 2.9.2 + compile + + @@ -223,6 +237,12 @@ ${project.version} compile + + ${project.groupId} + SongodaCore-NMS-v1_18_R2 + ${project.version} + compile + diff --git a/Core/src/main/java/com/songoda/core/SongodaCore.java b/Core/src/main/java/com/songoda/core/SongodaCore.java index b1f8b23e..9190b321 100644 --- a/Core/src/main/java/com/songoda/core/SongodaCore.java +++ b/Core/src/main/java/com/songoda/core/SongodaCore.java @@ -53,7 +53,7 @@ public class SongodaCore { /** * @since coreRevision 6 */ - private final static String coreVersion = "2.6.11"; + private final static String coreVersion = "2.6.12"; /** * This is specific to the website api diff --git a/Core/src/main/java/com/songoda/core/SongodaPlugin.java b/Core/src/main/java/com/songoda/core/SongodaPlugin.java index 7f163c39..8b700be9 100644 --- a/Core/src/main/java/com/songoda/core/SongodaPlugin.java +++ b/Core/src/main/java/com/songoda/core/SongodaPlugin.java @@ -4,6 +4,7 @@ import com.songoda.core.configuration.Config; import com.songoda.core.database.DataManagerAbstract; import com.songoda.core.locale.Locale; import com.songoda.core.utils.Metrics; +import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.ConsoleCommandSender; @@ -26,6 +27,12 @@ public abstract class SongodaPlugin extends JavaPlugin { protected ConsoleCommandSender console = Bukkit.getConsoleSender(); private boolean emergencyStop = false; + static { + /* NBT-API */ + MinecraftVersion.getLogger().setLevel(Level.WARNING); + MinecraftVersion.disableUpdateCheck(); + } + public abstract void onPluginLoad(); public abstract void onPluginEnable(); @@ -218,7 +225,7 @@ public abstract class SongodaPlugin extends JavaPlugin { * * @param th The error(s) that occurred */ - protected void criticalErrorOnPluginStartup(Throwable... th) { + protected void criticalErrorOnPluginStartup(Throwable th) { Bukkit.getLogger().log(Level.SEVERE, String.format( "Unexpected error while loading %s v%s c%s: Disabling plugin!", diff --git a/Core/src/main/java/com/songoda/core/configuration/editor/ConfigEditorGui.java b/Core/src/main/java/com/songoda/core/configuration/editor/ConfigEditorGui.java index d3649a7a..9ab71e8c 100644 --- a/Core/src/main/java/com/songoda/core/configuration/editor/ConfigEditorGui.java +++ b/Core/src/main/java/com/songoda/core/configuration/editor/ConfigEditorGui.java @@ -131,10 +131,17 @@ public class ConfigEditorGui extends SimplePagedGui { paged.setItem(4, configItem(CompatibleMaterial.FILLED_MAP, settingKey, node, settingKey, "Choose an item to change this value to")); int i = 9; for (CompatibleMaterial mat : CompatibleMaterial.getAllValidItemMaterials()) { - paged.setButton(i++, GuiUtils.createButtonItem(mat, mat.name()), ClickType.LEFT, (matEvent) -> { - setMaterial(event.slot, settingKey, matEvent.clickedItem); - matEvent.player.closeInventory(); - }); + try { + ItemStack buttonItem = GuiUtils.createButtonItem(mat, mat.name()); + if (!buttonItem.getType().isItem()) continue; + + paged.setButton(i++, buttonItem, ClickType.LEFT, (matEvent) -> { + setMaterial(event.slot, settingKey, matEvent.clickedItem); + matEvent.player.closeInventory(); + }); + } catch (IllegalArgumentException ex) { + // FIXME: CompatibleMaterial is not working properly for 'ZOMBIE_PIGMAN_SPAWN_EGG' + } } event.manager.showGUI(event.player, paged); }); diff --git a/Core/src/main/java/com/songoda/core/nms/NmsManager.java b/Core/src/main/java/com/songoda/core/nms/NmsManager.java index f98a7058..16ea6c07 100644 --- a/Core/src/main/java/com/songoda/core/nms/NmsManager.java +++ b/Core/src/main/java/com/songoda/core/nms/NmsManager.java @@ -121,6 +121,12 @@ public class NmsManager { nbt = new com.songoda.core.nms.v1_18_R1.nbt.NBTCoreImpl(); world = new com.songoda.core.nms.v1_18_R1.world.WorldCoreImpl(); break; + case "v1_18_R2": + player = new com.songoda.core.nms.v1_18_R2.entity.NMSPlayerImpl(); + anvil = new com.songoda.core.nms.v1_18_R2.anvil.AnvilCore(); + nbt = new com.songoda.core.nms.v1_18_R2.nbt.NBTCoreImpl(); + world = new com.songoda.core.nms.v1_18_R2.world.WorldCoreImpl(); + break; default: Logger.getLogger(NmsManager.class.getName()).log(Level.SEVERE, "Failed to load NMS for this server version: version {0} not found", serverPackageVersion); diff --git a/NMS/NMS-API/pom.xml b/NMS/NMS-API/pom.xml index 5206db10..37cba01b 100644 --- a/NMS/NMS-API/pom.xml +++ b/NMS/NMS-API/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-API/src/main/java/com/songoda/core/nms/nbt/NBTCore.java b/NMS/NMS-API/src/main/java/com/songoda/core/nms/nbt/NBTCore.java index fca20e34..b7cbfcaa 100644 --- a/NMS/NMS-API/src/main/java/com/songoda/core/nms/nbt/NBTCore.java +++ b/NMS/NMS-API/src/main/java/com/songoda/core/nms/nbt/NBTCore.java @@ -4,8 +4,16 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public interface NBTCore { + /** + * @deprecated Use {@link de.tr7zw.nbtapi.NBTItem} instead. + */ + @Deprecated NBTItem of(ItemStack item); + /** + * @deprecated Use {@link de.tr7zw.nbtapi.NBTItem} instead. + */ + @Deprecated NBTItem newItem(); NBTEntity of(Entity entity); diff --git a/NMS/NMS-API/src/main/java/com/songoda/core/nms/nbt/NBTItem.java b/NMS/NMS-API/src/main/java/com/songoda/core/nms/nbt/NBTItem.java index 968df307..cf7cdfad 100644 --- a/NMS/NMS-API/src/main/java/com/songoda/core/nms/nbt/NBTItem.java +++ b/NMS/NMS-API/src/main/java/com/songoda/core/nms/nbt/NBTItem.java @@ -2,6 +2,10 @@ package com.songoda.core.nms.nbt; import org.bukkit.inventory.ItemStack; +/** + * @deprecated Use {@link de.tr7zw.nbtapi.NBTItem} instead. + */ +@Deprecated public interface NBTItem extends NBTCompound { ItemStack finish(); } diff --git a/NMS/NMS-v1_10_R1/pom.xml b/NMS/NMS-v1_10_R1/pom.xml index 4f52ba58..e9be7266 100644 --- a/NMS/NMS-v1_10_R1/pom.xml +++ b/NMS/NMS-v1_10_R1/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_10_R1/src/main/java/com/songoda/core/nms/v1_10_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_10_R1/src/main/java/com/songoda/core/nms/v1_10_R1/nbt/NBTCoreImpl.java index 7d44721d..2340ef0a 100644 --- a/NMS/NMS-v1_10_R1/src/main/java/com/songoda/core/nms/v1_10_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_10_R1/src/main/java/com/songoda/core/nms/v1_10_R1/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_11_R1/pom.xml b/NMS/NMS-v1_11_R1/pom.xml index a36c3a9c..e17b0b7f 100644 --- a/NMS/NMS-v1_11_R1/pom.xml +++ b/NMS/NMS-v1_11_R1/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_11_R1/src/main/java/com/songoda/core/nms/v1_11_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_11_R1/src/main/java/com/songoda/core/nms/v1_11_R1/nbt/NBTCoreImpl.java index fe6a9d51..9308342f 100644 --- a/NMS/NMS-v1_11_R1/src/main/java/com/songoda/core/nms/v1_11_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_11_R1/src/main/java/com/songoda/core/nms/v1_11_R1/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_12_R1/pom.xml b/NMS/NMS-v1_12_R1/pom.xml index e6c6f718..97edd38c 100644 --- a/NMS/NMS-v1_12_R1/pom.xml +++ b/NMS/NMS-v1_12_R1/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_12_R1/src/main/java/com/songoda/core/nms/v1_12_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_12_R1/src/main/java/com/songoda/core/nms/v1_12_R1/nbt/NBTCoreImpl.java index 2451f26c..a12c9dac 100644 --- a/NMS/NMS-v1_12_R1/src/main/java/com/songoda/core/nms/v1_12_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_12_R1/src/main/java/com/songoda/core/nms/v1_12_R1/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_13_R1/pom.xml b/NMS/NMS-v1_13_R1/pom.xml index df8832af..bad9cad9 100644 --- a/NMS/NMS-v1_13_R1/pom.xml +++ b/NMS/NMS-v1_13_R1/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_13_R1/src/main/java/com/songoda/core/nms/v1_13_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_13_R1/src/main/java/com/songoda/core/nms/v1_13_R1/nbt/NBTCoreImpl.java index 761ebdcb..da771d8a 100644 --- a/NMS/NMS-v1_13_R1/src/main/java/com/songoda/core/nms/v1_13_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_13_R1/src/main/java/com/songoda/core/nms/v1_13_R1/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_13_R2/pom.xml b/NMS/NMS-v1_13_R2/pom.xml index 0ae5dd51..8631e133 100644 --- a/NMS/NMS-v1_13_R2/pom.xml +++ b/NMS/NMS-v1_13_R2/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_13_R2/src/main/java/com/songoda/core/nms/v1_13_R2/nbt/NBTCoreImpl.java b/NMS/NMS-v1_13_R2/src/main/java/com/songoda/core/nms/v1_13_R2/nbt/NBTCoreImpl.java index ed25b138..95f0f69f 100644 --- a/NMS/NMS-v1_13_R2/src/main/java/com/songoda/core/nms/v1_13_R2/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_13_R2/src/main/java/com/songoda/core/nms/v1_13_R2/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_14_R1/pom.xml b/NMS/NMS-v1_14_R1/pom.xml index c7970c52..ae699ccd 100644 --- a/NMS/NMS-v1_14_R1/pom.xml +++ b/NMS/NMS-v1_14_R1/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_14_R1/src/main/java/com/songoda/core/nms/v1_14_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_14_R1/src/main/java/com/songoda/core/nms/v1_14_R1/nbt/NBTCoreImpl.java index 955daaf8..cc171c83 100644 --- a/NMS/NMS-v1_14_R1/src/main/java/com/songoda/core/nms/v1_14_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_14_R1/src/main/java/com/songoda/core/nms/v1_14_R1/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_15_R1/pom.xml b/NMS/NMS-v1_15_R1/pom.xml index ba13f158..4ed1b5db 100644 --- a/NMS/NMS-v1_15_R1/pom.xml +++ b/NMS/NMS-v1_15_R1/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_15_R1/src/main/java/com/songoda/core/nms/v1_15_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_15_R1/src/main/java/com/songoda/core/nms/v1_15_R1/nbt/NBTCoreImpl.java index d0f1bd7c..70ecfa48 100644 --- a/NMS/NMS-v1_15_R1/src/main/java/com/songoda/core/nms/v1_15_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_15_R1/src/main/java/com/songoda/core/nms/v1_15_R1/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_16_R1/pom.xml b/NMS/NMS-v1_16_R1/pom.xml index ee9e00ec..51c9b936 100644 --- a/NMS/NMS-v1_16_R1/pom.xml +++ b/NMS/NMS-v1_16_R1/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_16_R1/src/main/java/com/songoda/core/nms/v1_16_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_16_R1/src/main/java/com/songoda/core/nms/v1_16_R1/nbt/NBTCoreImpl.java index 90b43fa1..e5d5dfc7 100644 --- a/NMS/NMS-v1_16_R1/src/main/java/com/songoda/core/nms/v1_16_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_16_R1/src/main/java/com/songoda/core/nms/v1_16_R1/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_16_R2/pom.xml b/NMS/NMS-v1_16_R2/pom.xml index 6b9f0f69..3b0c8aa2 100644 --- a/NMS/NMS-v1_16_R2/pom.xml +++ b/NMS/NMS-v1_16_R2/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_16_R2/src/main/java/com/songoda/core/nms/v1_16_R2/nbt/NBTCoreImpl.java b/NMS/NMS-v1_16_R2/src/main/java/com/songoda/core/nms/v1_16_R2/nbt/NBTCoreImpl.java index 72cba80f..229cd875 100644 --- a/NMS/NMS-v1_16_R2/src/main/java/com/songoda/core/nms/v1_16_R2/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_16_R2/src/main/java/com/songoda/core/nms/v1_16_R2/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_16_R3/pom.xml b/NMS/NMS-v1_16_R3/pom.xml index 76b7e442..0def2c60 100644 --- a/NMS/NMS-v1_16_R3/pom.xml +++ b/NMS/NMS-v1_16_R3/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_16_R3/src/main/java/com/songoda/core/nms/v1_16_R3/nbt/NBTCoreImpl.java b/NMS/NMS-v1_16_R3/src/main/java/com/songoda/core/nms/v1_16_R3/nbt/NBTCoreImpl.java index 8961999d..00530bb2 100644 --- a/NMS/NMS-v1_16_R3/src/main/java/com/songoda/core/nms/v1_16_R3/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_16_R3/src/main/java/com/songoda/core/nms/v1_16_R3/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_17_R1/pom.xml b/NMS/NMS-v1_17_R1/pom.xml index 945074ce..9fd7fc44 100644 --- a/NMS/NMS-v1_17_R1/pom.xml +++ b/NMS/NMS-v1_17_R1/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_17_R1/src/main/java/com/songoda/core/nms/v1_17_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_17_R1/src/main/java/com/songoda/core/nms/v1_17_R1/nbt/NBTCoreImpl.java index d10727d2..9f9c35ba 100644 --- a/NMS/NMS-v1_17_R1/src/main/java/com/songoda/core/nms/v1_17_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_17_R1/src/main/java/com/songoda/core/nms/v1_17_R1/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_18_R1/pom.xml b/NMS/NMS-v1_18_R1/pom.xml index a091c190..866e870c 100644 --- a/NMS/NMS-v1_18_R1/pom.xml +++ b/NMS/NMS-v1_18_R1/pom.xml @@ -19,7 +19,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_18_R1/src/main/java/com/songoda/core/nms/v1_18_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_18_R1/src/main/java/com/songoda/core/nms/v1_18_R1/nbt/NBTCoreImpl.java index 37017153..a1cf48fe 100644 --- a/NMS/NMS-v1_18_R1/src/main/java/com/songoda/core/nms/v1_18_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_18_R1/src/main/java/com/songoda/core/nms/v1_18_R1/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_18_R2/pom.xml b/NMS/NMS-v1_18_R2/pom.xml new file mode 100644 index 00000000..14b892b7 --- /dev/null +++ b/NMS/NMS-v1_18_R2/pom.xml @@ -0,0 +1,105 @@ + + + 4.0.0 + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + + ${java.release} + + + + + net.md-5 + specialsource-maven-plugin + 1.2.4 + + + + remap-obf + package + + remap + + + + org.spigotmc:minecraft-server:${nms.ver}:txt:maps-mojang + true + org.spigotmc:spigot:${nms.ver}:jar:remapped-mojang + true + remapped-obf + + + + + remap-spigot + package + + remap + + + + ${project.build.directory}/${project.artifactId}-${project.version}-remapped-obf.jar + org.spigotmc:minecraft-server:${nms.ver}:csrg:maps-spigot + org.spigotmc:spigot:${nms.ver}:jar:remapped-obf + + + + + + + + + com.songoda + SongodaCore-Modules + 2.6.12 + ../../pom.xml + + + + 17 + 17 + + 1.18.2-R0.1-SNAPSHOT + + + SongodaCore-NMS-v1_18_R2 + jar + + + + org.spigotmc + spigot-api + ${nms.ver} + provided + + + + org.spigotmc + spigot + ${nms.ver} + provided + remapped-mojang + + + + ${project.groupId} + SongodaCore-NMS-API + ${project.version} + + + + ${project.groupId} + SongodaCore-Compatibility + ${project.version} + + + diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/anvil/AnvilCore.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/anvil/AnvilCore.java new file mode 100644 index 00000000..a4842c23 --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/anvil/AnvilCore.java @@ -0,0 +1,21 @@ +package com.songoda.core.nms.v1_18_R2.anvil; + +import com.songoda.core.nms.anvil.CustomAnvil; +import net.minecraft.server.level.ServerPlayer; +import org.bukkit.craftbukkit.v1_18_R2.entity.CraftPlayer; +import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryHolder; + +public class AnvilCore implements com.songoda.core.nms.anvil.AnvilCore { + @Override + public CustomAnvil createAnvil(Player player) { + ServerPlayer p = ((CraftPlayer) player).getHandle(); + return new AnvilView(p.nextContainerCounter(), p, null); + } + + @Override + public CustomAnvil createAnvil(Player player, InventoryHolder holder) { + ServerPlayer p = ((CraftPlayer) player).getHandle(); + return new AnvilView(p.nextContainerCounter(), p, holder); + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/anvil/AnvilInventoryCustom.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/anvil/AnvilInventoryCustom.java new file mode 100644 index 00000000..d569c900 --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/anvil/AnvilInventoryCustom.java @@ -0,0 +1,22 @@ +package com.songoda.core.nms.v1_18_R2.anvil; + +import net.minecraft.world.Container; +import net.minecraft.world.inventory.AnvilMenu; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventoryAnvil; +import org.bukkit.inventory.InventoryHolder; + +public class AnvilInventoryCustom extends CraftInventoryAnvil { + final InventoryHolder holder; + + public AnvilInventoryCustom(InventoryHolder holder, Location location, Container inventory, Container resultInventory, AnvilMenu container) { + super(location, inventory, resultInventory, container); + + this.holder = holder; + } + + @Override + public InventoryHolder getHolder() { + return holder; + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/anvil/AnvilView.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/anvil/AnvilView.java new file mode 100644 index 00000000..695832ff --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/anvil/AnvilView.java @@ -0,0 +1,214 @@ +package com.songoda.core.nms.v1_18_R2.anvil; + +import com.songoda.core.nms.anvil.CustomAnvil; +import com.songoda.core.nms.anvil.methods.AnvilTextChange; +import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.Container; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.inventory.AnvilMenu; +import net.minecraft.world.inventory.ContainerLevelAccess; +import net.minecraft.world.inventory.ItemCombinerMenu; +import net.minecraft.world.inventory.MenuType; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventoryView; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; + +import java.lang.reflect.Field; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class AnvilView extends AnvilMenu implements CustomAnvil { + private final ServerPlayer entity; + private final Inventory inventory; + private String customTitle = "Repairing"; + private int cost = -1; + private boolean canUse = true; + private AnvilTextChange textChange; + + // used for setting custom inventory + static Field mc_ContainerAnvil_repairInventory; // subcontainer with only the result + static Field mc_ContainerAnvil_resultInventory; // full inventory + static Field mc_ContainerAnvil_bukkitEntity; + + static { + try { + mc_ContainerAnvil_repairInventory = ItemCombinerMenu.class.getDeclaredField("p"); + mc_ContainerAnvil_repairInventory.setAccessible(true); + + mc_ContainerAnvil_resultInventory = ItemCombinerMenu.class.getDeclaredField("o"); + mc_ContainerAnvil_resultInventory.setAccessible(true); + + mc_ContainerAnvil_bukkitEntity = AnvilMenu.class.getDeclaredField("bukkitEntity"); + mc_ContainerAnvil_bukkitEntity.setAccessible(true); + } catch (Exception ex) { + Logger.getLogger(AnvilView.class.getName()).log(Level.SEVERE, "Anvil Setup Error", ex); + } + } + + // 1.14 also introduced a title field, also private, which can only be set once and can't be checked + static Field mc_Container_title; + + static { + try { + mc_Container_title = AbstractContainerMenu.class.getDeclaredField("title"); + mc_Container_title.setAccessible(true); + } catch (Exception ex) { + Logger.getLogger(AnvilView.class.getName()).log(Level.SEVERE, "Anvil Setup Error", ex); + } + } + + public AnvilView(int id, ServerPlayer entity, InventoryHolder holder) { + super(entity.nextContainerCounter(), entity.getInventory(), ContainerLevelAccess.create(entity.level, new BlockPos(0, 0, 0))); + + this.setTitle(new TranslatableComponent(customTitle != null ? customTitle : "")); + this.checkReachable = false; + this.entity = entity; + + if (holder != null) { + this.inventory = getBukkitView(entity, holder).getTopInventory(); + } else { + this.inventory = getBukkitView().getTopInventory(); + } + } + + public CraftInventoryView getBukkitView(Player player, InventoryHolder holder) { + try { + AnvilInventoryCustom craftInventory = new AnvilInventoryCustom(holder, + new Location(entity.level.getWorld(), 0, 0, 0), + (Container) mc_ContainerAnvil_repairInventory.get(this), + (Container) mc_ContainerAnvil_resultInventory.get(this), this); + CraftInventoryView view = new CraftInventoryView(player.getBukkitEntity(), craftInventory, this); + mc_ContainerAnvil_bukkitEntity.set(this, view); + + return view; + } catch (Exception ex) { + Logger.getLogger(AnvilView.class.getName()).log(Level.SEVERE, "Anvil Setup Error", ex); + } + + return getBukkitView(); + } + + @Override + public boolean stillValid(Player entityHuman) { + return canUse; + } + + @Override + public void broadcastFullState() { + super.broadcastFullState(); + + if (cost >= 0) { + this.setLevelCost(cost); + } + + textChange.onChange(); + } + + @Override + public void update() { + broadcastFullState(); + } + + @Override + public String getRenameText() { + return this.itemName; + } + + @Override + public void setRenameText(String text) { + this.setItemName(text); + } + + @Override + public void setOnChange(AnvilTextChange handler) { + textChange = handler; + } + + @Override + public String getCustomTitle() { + return customTitle; + } + + @Override + public void setCustomTitle(String title) { + this.customTitle = title; + + try { + mc_Container_title.set(this, new TranslatableComponent(customTitle != null ? customTitle : "")); + } catch (Exception ex) { + Logger.getLogger(AnvilView.class.getName()).log(Level.SEVERE, "Anvil Error", ex); + } + } + + @Override + public void setLevelCost(int cost) { + this.cost = cost; + } + + @Override + public int getLevelCost() { + if (cost >= 0) { + return cost; + } + + return this.getLevelCost(); + } + + @Override + public void setCanUse(boolean bool) { + this.canUse = bool; + } + + @Override + public ItemStack getLeftInput() { + return inventory.getItem(0); + } + + @Override + public ItemStack getRightInput() { + return inventory.getItem(1); + } + + @Override + public ItemStack getOutput() { + return inventory.getItem(2); + } + + @Override + public void setLeftInput(ItemStack item) { + inventory.setItem(0, item); + } + + @Override + public void setRightInput(ItemStack item) { + inventory.setItem(1, item); + } + + @Override + public void setOutput(ItemStack item) { + inventory.setItem(2, item); + } + + @Override + public Inventory getInventory() { + return inventory; + } + + @Override + public void open() { + // Send the packet + entity.connection.send(new ClientboundOpenScreenPacket(super.containerId, MenuType.ANVIL, new TranslatableComponent(customTitle != null ? customTitle : ""))); + + // Set their active container to this anvil + entity.containerMenu = this; + + // Add the slot listener + entity.initMenu(entity.containerMenu); + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/entity/NMSPlayerImpl.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/entity/NMSPlayerImpl.java new file mode 100644 index 00000000..ab84303a --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/entity/NMSPlayerImpl.java @@ -0,0 +1,13 @@ +package com.songoda.core.nms.v1_18_R2.entity; + +import com.songoda.core.nms.entity.NMSPlayer; +import net.minecraft.network.protocol.Packet; +import org.bukkit.craftbukkit.v1_18_R2.entity.CraftPlayer; +import org.bukkit.entity.Player; + +public class NMSPlayerImpl implements NMSPlayer { + @Override + public void sendPacket(Player p, Object packet) { + ((CraftPlayer) p).getHandle().connection.send((Packet) packet); + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTCompoundImpl.java new file mode 100644 index 00000000..49d3e549 --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTCompoundImpl.java @@ -0,0 +1,208 @@ +package com.songoda.core.nms.v1_18_R2.nbt; + +import com.songoda.core.nms.nbt.NBTCompound; +import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.NbtIo; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Set; +import java.util.UUID; + +public class NBTCompoundImpl implements NBTCompound { + protected CompoundTag compound; + + protected NBTCompoundImpl(CompoundTag compound) { + this.compound = compound; + } + + public NBTCompoundImpl() { + this.compound = new CompoundTag(); + } + + @Override + public NBTCompound set(String tag, String s) { + compound.putString(tag, s); + return this; + } + + @Override + public NBTCompound set(String tag, boolean b) { + compound.putBoolean(tag, b); + return this; + } + + @Override + public NBTCompound set(String tag, int i) { + compound.putInt(tag, i); + return this; + } + + @Override + public NBTCompound set(String tag, double i) { + compound.putDouble(tag, i); + return this; + } + + @Override + public NBTCompound set(String tag, long l) { + compound.putLong(tag, l); + return this; + } + + @Override + public NBTCompound set(String tag, short s) { + compound.putShort(tag, s); + return this; + } + + @Override + public NBTCompound set(String tag, byte b) { + compound.putByte(tag, b); + return this; + } + + @Override + public NBTCompound set(String tag, int[] i) { + compound.putIntArray(tag, i); + return this; + } + + @Override + public NBTCompound set(String tag, byte[] b) { + compound.putByteArray(tag, b); + return this; + } + + @Override + public NBTCompound set(String tag, UUID u) { + compound.putUUID(tag, u); + return this; + } + + @Override + public NBTCompound remove(String tag) { + compound.remove(tag); + return this; + } + + @Override + public boolean has(String tag) { + return compound.contains(tag); + } + + @Override + public NBTObject getNBTObject(String tag) { + return new NBTObjectImpl(compound, tag); + } + + @Override + public String getString(String tag) { + return getNBTObject(tag).asString(); + } + + @Override + public boolean getBoolean(String tag) { + return getNBTObject(tag).asBoolean(); + } + + @Override + public int getInt(String tag) { + return getNBTObject(tag).asInt(); + } + + @Override + public double getDouble(String tag) { + return getNBTObject(tag).asDouble(); + } + + @Override + public long getLong(String tag) { + return getNBTObject(tag).asLong(); + } + + @Override + public short getShort(String tag) { + return getNBTObject(tag).asShort(); + } + + @Override + public byte getByte(String tag) { + return getNBTObject(tag).asByte(); + } + + @Override + public int[] getIntArray(String tag) { + return getNBTObject(tag).asIntArray(); + } + + @Override + public byte[] getByteArray(String tag) { + return getNBTObject(tag).asByteArray(); + } + + @Override + public NBTCompound getCompound(String tag) { + if (has(tag)) { + return getNBTObject(tag).asCompound(); + } + + CompoundTag newCompound = new CompoundTag(); + compound.put(tag, newCompound); + return new NBTCompoundImpl(newCompound); + } + + @Override + public Set getKeys() { + return compound.getAllKeys(); + } + + @Override + public Set getKeys(String tag) { + return compound.getCompound(tag).getAllKeys(); + } + + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + CompoundTag compound = this.compound.copy(); + + for (String exclusion : exclusions) { + compound.remove(exclusion); + } + + NbtIo.writeCompressed(compound, dataOutput); + + return outputStream.toByteArray(); + } catch (Exception ex) { + ex.printStackTrace(); + } + + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NbtIo.readCompressed(dataInput); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + @Override + public void addExtras() { + // None + } + + @Override + public String toString() { + return compound.toString(); + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTCoreImpl.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTCoreImpl.java new file mode 100644 index 00000000..01441e4c --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTCoreImpl.java @@ -0,0 +1,38 @@ +package com.songoda.core.nms.v1_18_R2.nbt; + +import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; +import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.nbt.CompoundTag; +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; + +public class NBTCoreImpl implements NBTCore { + @Deprecated + @Override + public NBTItem of(ItemStack item) { + return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); + } + + @Deprecated + @Override + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.world.entity.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + CompoundTag nbt = new CompoundTag(); + nmsEntity.saveWithoutId(nbt); + + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new CompoundTag(), null); + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTEntityImpl.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..cbbca061 --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTEntityImpl.java @@ -0,0 +1,66 @@ +package com.songoda.core.nms.v1_18_R2.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Registry; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.MobSpawnType; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_18_R2.CraftWorld; + +import java.util.Optional; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + private Entity nmsEntity; + + public NBTEntityImpl(CompoundTag entityNBT, Entity nmsEntity) { + super(entityNBT); + + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Optional> optionalEntity = EntityType.byString(entityType); + if (optionalEntity.isPresent()) { + assert location.getWorld() != null; + + Entity spawned = optionalEntity.get().spawn( + ((CraftWorld) location.getWorld()).getHandle(), + compound, + null, + null, + new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()), + MobSpawnType.COMMAND, + true, + false + ); + + if (spawned != null) { + spawned.load(compound); + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + entity.teleport(location); + nmsEntity = spawned; + + return entity; + } + } + + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.discard(); + return spawn(location); + } + + @Override + public void addExtras() { + compound.putString("entity_type", Registry.ENTITY_TYPE.getKey(nmsEntity.getType()).toString()); + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTItemImpl.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTItemImpl.java new file mode 100644 index 00000000..94d7ef55 --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTItemImpl.java @@ -0,0 +1,24 @@ +package com.songoda.core.nms.v1_18_R2.nbt; + +import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.item.ItemStack; +import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack; + +public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { + private final ItemStack nmsItem; + + public NBTItemImpl(ItemStack nmsItem) { + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new CompoundTag()); + + this.nmsItem = nmsItem; + } + + public org.bukkit.inventory.ItemStack finish() { + if (nmsItem == null) { + return CraftItemStack.asBukkitCopy(ItemStack.of(compound)); + } + + return CraftItemStack.asBukkitCopy(nmsItem); + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTObjectImpl.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTObjectImpl.java new file mode 100644 index 00000000..3e657dfb --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/nbt/NBTObjectImpl.java @@ -0,0 +1,72 @@ +package com.songoda.core.nms.v1_18_R2.nbt; + +import com.songoda.core.nms.nbt.NBTCompound; +import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.nbt.CompoundTag; + +import java.util.Set; + +public class NBTObjectImpl implements NBTObject { + private final CompoundTag compound; + private final String tag; + + public NBTObjectImpl(CompoundTag compound, String tag) { + this.compound = compound; + this.tag = tag; + } + + @Override + public String asString() { + return compound.getString(tag); + } + + @Override + public boolean asBoolean() { + return compound.getBoolean(tag); + } + + @Override + public int asInt() { + return compound.getInt(tag); + } + + @Override + public double asDouble() { + return compound.getDouble(tag); + } + + @Override + public long asLong() { + return compound.getLong(tag); + } + + @Override + public short asShort() { + return compound.getShort(tag); + } + + @Override + public byte asByte() { + return compound.getByte(tag); + } + + @Override + public int[] asIntArray() { + return compound.getIntArray(tag); + } + + @Override + public byte[] asByteArray() { + return compound.getByteArray(tag); + } + + @Override + public NBTCompound asCompound() { + return new NBTCompoundImpl(compound.getCompound(tag)); + } + + @Override + public Set getKeys() { + return compound.getAllKeys(); + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/SItemStackImpl.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/SItemStackImpl.java new file mode 100644 index 00000000..b6ffb494 --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/SItemStackImpl.java @@ -0,0 +1,39 @@ +package com.songoda.core.nms.v1_18_R2.world; + +import com.songoda.core.nms.world.SItemStack; +import net.minecraft.core.particles.ItemParticleOption; +import net.minecraft.core.particles.ParticleTypes; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.phys.Vec3; +import org.bukkit.craftbukkit.v1_18_R2.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +public class SItemStackImpl implements SItemStack { + private final ItemStack item; + + public SItemStackImpl(ItemStack item) { + this.item = item; + } + + @Override + public void breakItem(Player player, int amount) { + ServerPlayer entityPlayer = ((CraftPlayer) player).getHandle(); + + for (int i = 0; i < amount; ++i) { + Vec3 vec3d = new Vec3(((double) random.nextFloat() - 0.5D) * 0.1D, Math.random() * 0.1D + 0.1D, 0.0D); + vec3d = vec3d.xRot(-entityPlayer.getXRot() * 0.017453292F); + vec3d = vec3d.yRot(-entityPlayer.getYRot() * 0.017453292F); + + double d0 = (double) (-random.nextFloat()) * 0.6D - 0.3D; + + Vec3 vec3d1 = new Vec3(((double) random.nextFloat() - 0.5D) * 0.3D, d0, 0.6D); + vec3d1 = vec3d1.xRot(-entityPlayer.getXRot() * 0.017453292F); + vec3d1 = vec3d1.yRot(-entityPlayer.getYRot() * 0.017453292F); + vec3d1 = vec3d1.add(entityPlayer.getX(), entityPlayer.getEyeY(), entityPlayer.getZ()); + + entityPlayer.level.addParticle(new ItemParticleOption(ParticleTypes.ITEM, CraftItemStack.asNMSCopy(item)), vec3d1.x, vec3d1.y, vec3d1.z, vec3d.x, vec3d.y + 0.05D, vec3d.z); + } + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/SSpawnerImpl.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/SSpawnerImpl.java new file mode 100644 index 00000000..5af38e9c --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/SSpawnerImpl.java @@ -0,0 +1,132 @@ +package com.songoda.core.nms.v1_18_R2.world; + +import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.compatibility.CompatibleParticleHandler; +import com.songoda.core.nms.world.SSpawner; +import com.songoda.core.nms.world.SpawnedEntity; +import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.DifficultyInstance; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.MobSpawnType; +import net.minecraft.world.level.SpawnData; +import org.bukkit.Location; +import org.bukkit.block.BlockFace; +import org.bukkit.craftbukkit.v1_18_R2.CraftWorld; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.entity.CreatureSpawnEvent; + +import java.util.Optional; +import java.util.Random; +import java.util.Set; + +public class SSpawnerImpl implements SSpawner { + private final Location spawnerLocation; + + public SSpawnerImpl(Location location) { + this.spawnerLocation = location; + } + + @Override + public LivingEntity spawnEntity(EntityType type, Location spawnerLocation) { + return spawnEntity(type, "EXPLOSION_NORMAL", null, null); + } + + @Override + public LivingEntity spawnEntity(EntityType type, String particleType, SpawnedEntity spawned, Set canSpawnOn) { + SpawnData data = new SpawnData(); + CompoundTag compound = data.getEntityToSpawn(); + + String name = type.name().toLowerCase().replace("snowman", "snow_golem") + .replace("mushroom_cow", "mooshroom"); + compound.putString("id", "minecraft:" + name); + + short spawnRange = 4; + for (int i = 0; i < 50; i++) { + assert spawnerLocation.getWorld() != null; + ServerLevel world = ((CraftWorld) spawnerLocation.getWorld()).getHandle(); + + Random random = world.getRandom(); + double x = spawnerLocation.getX() + (random.nextDouble() - random.nextDouble()) * (double) spawnRange + 0.5D; + double y = spawnerLocation.getY() + random.nextInt(3) - 1; + double z = spawnerLocation.getZ() + (random.nextDouble() - random.nextDouble()) * (double) spawnRange + 0.5D; + + Optional optionalEntity = net.minecraft.world.entity.EntityType.create(compound, world); + if (optionalEntity.isEmpty()) continue; + + Entity entity = optionalEntity.get(); + entity.setPos(x, y, z); + + BlockPos position = entity.blockPosition(); + DifficultyInstance damageScaler = world.getCurrentDifficultyAt(position); + + if (!(entity instanceof Mob entityInsentient)) { + continue; + } + + Location spot = new Location(spawnerLocation.getWorld(), x, y, z); + + if (!canSpawn(world, entityInsentient, spot, canSpawnOn)) { + continue; + } + + entityInsentient.finalizeSpawn(world, damageScaler, MobSpawnType.SPAWNER, null, null); + + LivingEntity craftEntity = (LivingEntity) entity.getBukkitEntity(); + + if (spawned != null && !spawned.onSpawn(craftEntity)) { + return null; + } + + if (particleType != null) { + float xx = (float) (0 + (Math.random() * 1)); + float yy = (float) (0 + (Math.random() * 2)); + float zz = (float) (0 + (Math.random() * 1)); + + CompatibleParticleHandler.spawnParticles(CompatibleParticleHandler.ParticleType.getParticle(particleType), spot, 5, xx, yy, zz, 0); + } + + world.addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.SPAWNER); + + spot.setYaw(random.nextFloat() * 360.0F); + craftEntity.teleport(spot); + + return craftEntity; + } + + return null; + } + + private boolean canSpawn(ServerLevel world, Mob entityInsentient, Location location, Set canSpawnOn) { + if (!world.noCollision(entityInsentient, entityInsentient.getBoundingBox())) { + return false; + } + + CompatibleMaterial spawnedIn = CompatibleMaterial.getMaterial(location.getBlock()); + CompatibleMaterial spawnedOn = CompatibleMaterial.getMaterial(location.getBlock().getRelative(BlockFace.DOWN)); + + if (spawnedIn == null || spawnedOn == null) { + return false; + } + + if (!spawnedIn.isAir() && + spawnedIn != CompatibleMaterial.WATER && + !spawnedIn.name().contains("PRESSURE") && + !spawnedIn.name().contains("SLAB")) { + return false; + } + + for (CompatibleMaterial material : canSpawnOn) { + if (material == null) continue; + + if (spawnedOn.equals(material) || material.isAir()) { + return true; + } + } + + return false; + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/SWorldImpl.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/SWorldImpl.java new file mode 100644 index 00000000..a3ac885c --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/SWorldImpl.java @@ -0,0 +1,38 @@ +package com.songoda.core.nms.v1_18_R2.world; + +import com.songoda.core.nms.world.SWorld; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.entity.LevelEntityGetter; +import org.bukkit.World; +import org.bukkit.craftbukkit.v1_18_R2.CraftWorld; +import org.bukkit.entity.LivingEntity; + +import java.util.ArrayList; +import java.util.List; + +public class SWorldImpl implements SWorld { + private final World world; + + public SWorldImpl(World world) { + this.world = world; + } + + @Override + public List getLivingEntities() { + List result = new ArrayList<>(); + + ServerLevel worldServer = ((CraftWorld) world).getHandle(); + LevelEntityGetter entities = worldServer.entityManager.getEntityGetter(); + + entities.getAll().forEach((mcEnt) -> { + org.bukkit.entity.Entity bukkitEntity = mcEnt.getBukkitEntity(); + + if (bukkitEntity instanceof LivingEntity && bukkitEntity.isValid()) { + result.add((LivingEntity) bukkitEntity); + } + }); + + return result; + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/WorldCoreImpl.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/WorldCoreImpl.java new file mode 100644 index 00000000..fc0198eb --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/WorldCoreImpl.java @@ -0,0 +1,95 @@ +package com.songoda.core.nms.v1_18_R2.world; + +import com.songoda.core.nms.ReflectionUtils; +import com.songoda.core.nms.v1_18_R2.world.spawner.BBaseSpawnerImpl; +import com.songoda.core.nms.world.BBaseSpawner; +import com.songoda.core.nms.world.SItemStack; +import com.songoda.core.nms.world.SSpawner; +import com.songoda.core.nms.world.SWorld; +import com.songoda.core.nms.world.WorldCore; +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.util.profiling.ProfilerFiller; +import net.minecraft.world.level.BaseSpawner; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.chunk.LevelChunk; +import net.minecraft.world.level.chunk.LevelChunkSection; +import net.minecraft.world.level.material.FluidState; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.CreatureSpawner; +import org.bukkit.craftbukkit.v1_18_R2.CraftChunk; +import org.bukkit.inventory.ItemStack; + +public class WorldCoreImpl implements WorldCore { + @Override + public SSpawner getSpawner(CreatureSpawner spawner) { + return new SSpawnerImpl(spawner.getLocation()); + } + + @Override + public SSpawner getSpawner(Location location) { + return new SSpawnerImpl(location); + } + + @Override + public SItemStack getItemStack(ItemStack item) { + return new SItemStackImpl(item); + } + + @Override + public SWorld getWorld(World world) { + return new SWorldImpl(world); + } + + @Override + public BBaseSpawner getBaseSpawner(CreatureSpawner spawner) throws NoSuchFieldException, IllegalAccessException { + Object cTileEntity = ReflectionUtils.getFieldValue(spawner, "tileEntity"); + + return new BBaseSpawnerImpl(spawner, (BaseSpawner) ReflectionUtils.getFieldValue(cTileEntity, "a")); + } + + /** + * Method is based on {@link ServerLevel#tickChunk(LevelChunk, int)}. + */ + @Override + public void randomTickChunk(org.bukkit.Chunk bukkitChunk, int tickAmount) { + LevelChunk chunk = ((CraftChunk) bukkitChunk).getHandle(); + ServerLevel world = chunk.q; + + ChunkPos chunkCoordIntPair = chunk.getPos(); + int j = chunkCoordIntPair.getMinBlockX(); + int k = chunkCoordIntPair.getMinBlockZ(); + + ProfilerFiller gameProfilerFiller = world.getProfiler(); + gameProfilerFiller.popPush("tickBlocks"); + if (tickAmount > 0) { + LevelChunkSection[] aChunkSection = chunk.getSections(); + + for (LevelChunkSection chunkSection : aChunkSection) { + if (chunkSection.isRandomlyTicking()) { + int j1 = chunkSection.bottomBlockY(); + + for (int k1 = 0; k1 < tickAmount; ++k1) { + BlockPos blockposition2 = world.getBlockRandomPos(j, j1, k, 15); + gameProfilerFiller.push("randomTick"); + BlockState iBlockData1 = chunkSection.getBlockState(blockposition2.getX() - j, blockposition2.getY() - j1, blockposition2.getZ() - k); + if (iBlockData1.isRandomlyTicking()) { + iBlockData1.randomTick(world, blockposition2, world.random); + } + + FluidState fluid = iBlockData1.getFluidState(); + if (fluid.isRandomlyTicking()) { + fluid.randomTick(world, blockposition2, world.random); + } + + gameProfilerFiller.pop(); + } + } + } + } + + gameProfilerFiller.pop(); + } +} diff --git a/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/spawner/BBaseSpawnerImpl.java b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/spawner/BBaseSpawnerImpl.java new file mode 100644 index 00000000..5a35d71b --- /dev/null +++ b/NMS/NMS-v1_18_R2/src/main/java/com/songoda/core/nms/v1_18_R2/world/spawner/BBaseSpawnerImpl.java @@ -0,0 +1,194 @@ +package com.songoda.core.nms.v1_18_R2.world.spawner; + +import com.songoda.core.nms.world.BBaseSpawner; +import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.Difficulty; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.MobSpawnType; +import net.minecraft.world.entity.SpawnPlacements; +import net.minecraft.world.level.BaseSpawner; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.LightLayer; +import net.minecraft.world.level.SpawnData; +import net.minecraft.world.phys.AABB; +import org.bukkit.block.CreatureSpawner; +import org.bukkit.craftbukkit.v1_18_R2.CraftWorld; +import org.bukkit.craftbukkit.v1_18_R2.block.CraftCreatureSpawner; +import org.bukkit.craftbukkit.v1_18_R2.event.CraftEventFactory; +import org.bukkit.event.entity.CreatureSpawnEvent; + +import java.util.Optional; +import java.util.Random; + +public class BBaseSpawnerImpl implements BBaseSpawner { + private final CreatureSpawner bukkitSpawner; + private final BaseSpawner spawner; + + private static final Random spawnerRandom = new Random(); // Field random in BaseSpawner is private - We use one random for *all* our spawners (should be fine, right?) + + public BBaseSpawnerImpl(CreatureSpawner bukkitSpawner, BaseSpawner spawner) { + this.bukkitSpawner = bukkitSpawner; + this.spawner = spawner; + } + + /** + * This method is based on {@link BaseSpawner#isNearPlayer(Level, BlockPos)}. + */ + @SuppressWarnings("JavadocReference") + @Override + public boolean isNearPlayer() { + BlockPos bPos = getBlockPosition(); + + return getWorld().hasNearbyAlivePlayer( + (double) bPos.getX() + 0.5D, + (double) bPos.getY() + 0.5D, + (double) bPos.getZ() + 0.5D, + this.spawner.requiredPlayerRange + ); + } + + /** + * This method is based on {@link BaseSpawner#serverTick(ServerLevel, BlockPos)}. + */ + @Override + public void tick() { + ServerLevel world = getWorld(); + BlockPos bPos = getBlockPosition(); + + if (this.spawner.spawnDelay == -1) { + delay(world, bPos); + } + + if (this.spawner.spawnDelay > 0) { + --this.spawner.spawnDelay; + } else { + boolean flag = false; + int i = 0; + + while (true) { + if (i >= this.spawner.spawnCount) { + if (flag) { + delay(world, bPos); + } + + break; + } + + CompoundTag nbtTagCompound = this.spawner.nextSpawnData.getEntityToSpawn(); + Optional> optional = EntityType.by(nbtTagCompound); + if (optional.isEmpty()) { + delay(world, bPos); + return; + } + + ListTag nbtTagList = nbtTagCompound.getList("Pos", 6); + int j = nbtTagList.size(); + double d0 = j >= 1 ? nbtTagList.getDouble(0) : (double) bPos.getX() + (world.random.nextDouble() - world.random.nextDouble()) * (double) this.spawner.spawnRange + 0.5D; + double d1 = j >= 2 ? nbtTagList.getDouble(1) : (double) (bPos.getY() + world.random.nextInt(3) - 1); + double d2 = j >= 3 ? nbtTagList.getDouble(2) : (double) bPos.getZ() + (world.random.nextDouble() - world.random.nextDouble()) * (double) this.spawner.spawnRange + 0.5D; + if (world.noCollision(optional.get().getAABB(d0, d1, d2))) { + label128: + { + BlockPos blockPosition1 = new BlockPos(d0, d1, d2); + if (this.spawner.nextSpawnData.getCustomSpawnRules().isPresent()) { + if (!optional.get().getCategory().isFriendly() && world.getDifficulty() == Difficulty.PEACEFUL) { + break label128; + } + + SpawnData.CustomSpawnRules mobSpawnerDataA = this.spawner.nextSpawnData.getCustomSpawnRules().get(); + if (!mobSpawnerDataA.blockLightLimit().isValueInRange(world.getBrightness(LightLayer.BLOCK, blockPosition1)) || + !mobSpawnerDataA.skyLightLimit().isValueInRange(world.getBrightness(LightLayer.SKY, blockPosition1))) { + break label128; + } + } else if (!SpawnPlacements.checkSpawnRules(optional.get(), world, MobSpawnType.SPAWNER, blockPosition1, world.getRandom())) { + break label128; + } + + Entity entity = EntityType.loadEntityRecursive(nbtTagCompound, world, (entity1) -> { + entity1.moveTo(d0, d1, d2, entity1.getYRot(), entity1.getXRot()); + return entity1; + }); + if (entity == null) { + delay(world, bPos); + return; + } + + int k = world.getEntitiesOfClass(entity.getClass(), (new AABB(bPos.getX(), bPos.getY(), bPos.getZ(), bPos.getX() + 1, bPos.getY() + 1, bPos.getZ() + 1)).inflate(this.spawner.spawnRange)).size(); + if (k >= this.spawner.maxNearbyEntities) { + delay(world, bPos); + return; + } + + entity.moveTo(entity.getX(), entity.getY(), entity.getZ(), world.random.nextFloat() * 360.0F, 0.0F); + if (entity instanceof Mob entityInsentient) { + if (this.spawner.nextSpawnData.getCustomSpawnRules().isEmpty() && !entityInsentient.checkSpawnRules(world, MobSpawnType.SPAWNER) || !entityInsentient.checkSpawnObstruction(world)) { + break label128; + } + + if (this.spawner.nextSpawnData.getEntityToSpawn().size() == 1 && this.spawner.nextSpawnData.getEntityToSpawn().contains("id", 8)) { + ((Mob) entity).finalizeSpawn(world, world.getCurrentDifficultyAt(entity.blockPosition()), MobSpawnType.SPAWNER, null, null); + } + + if (entityInsentient.level.spigotConfig.nerfSpawnerMobs) { + entityInsentient.aware = false; + } + } + + if (CraftEventFactory.callSpawnerSpawnEvent(entity, bPos).isCancelled()) { + Entity vehicle = entity.getVehicle(); + if (vehicle != null) { + vehicle.discard(); + } + + for (Entity passenger : entity.getIndirectPassengers()) { + passenger.discard(); + } + } else { + if (!world.tryAddFreshEntityWithPassengers(entity, CreatureSpawnEvent.SpawnReason.SPAWNER)) { + delay(world, bPos); + return; + } + + world.levelEvent(2004, bPos, 0); + if (entity instanceof Mob) { + ((Mob) entity).spawnAnim(); + } + + flag = true; + } + } + } + + ++i; + } + } + } + + /** + * This method is based on {@link BaseSpawner#delay(Level, BlockPos)}. + */ + @SuppressWarnings("JavadocReference") + private void delay(ServerLevel world, BlockPos bPos) { + if (this.spawner.maxSpawnDelay <= this.spawner.minSpawnDelay) { + this.spawner.spawnDelay = this.spawner.minSpawnDelay; + } else { + this.spawner.spawnDelay = this.spawner.minSpawnDelay + spawnerRandom.nextInt(this.spawner.maxSpawnDelay - this.spawner.minSpawnDelay); + } + + this.spawner.spawnPotentials.getRandom(spawnerRandom).ifPresent((weightedEntryB) -> this.spawner.setNextSpawnData(world, bPos, weightedEntryB.getData())); + this.spawner.broadcastEvent(world, bPos, 1); + } + + private ServerLevel getWorld() { + return ((CraftWorld) this.bukkitSpawner.getWorld()).getHandle(); + } + + private BlockPos getBlockPosition() { + return ((CraftCreatureSpawner) this.bukkitSpawner).getPosition(); + } +} diff --git a/NMS/NMS-v1_8_R1/pom.xml b/NMS/NMS-v1_8_R1/pom.xml index c1a5e1b9..5b6f8b34 100644 --- a/NMS/NMS-v1_8_R1/pom.xml +++ b/NMS/NMS-v1_8_R1/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_8_R1/src/main/java/com/songoda/core/nms/v1_8_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_8_R1/src/main/java/com/songoda/core/nms/v1_8_R1/nbt/NBTCoreImpl.java index d13f7785..f159a8f9 100644 --- a/NMS/NMS-v1_8_R1/src/main/java/com/songoda/core/nms/v1_8_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_8_R1/src/main/java/com/songoda/core/nms/v1_8_R1/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_8_R2/pom.xml b/NMS/NMS-v1_8_R2/pom.xml index 68428cc9..d9286f42 100644 --- a/NMS/NMS-v1_8_R2/pom.xml +++ b/NMS/NMS-v1_8_R2/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_8_R2/src/main/java/com/songoda/core/nms/v1_8_R2/nbt/NBTCoreImpl.java b/NMS/NMS-v1_8_R2/src/main/java/com/songoda/core/nms/v1_8_R2/nbt/NBTCoreImpl.java index 0d9785b8..b07a7693 100644 --- a/NMS/NMS-v1_8_R2/src/main/java/com/songoda/core/nms/v1_8_R2/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_8_R2/src/main/java/com/songoda/core/nms/v1_8_R2/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_8_R3/pom.xml b/NMS/NMS-v1_8_R3/pom.xml index 871f96a5..8f723d4b 100644 --- a/NMS/NMS-v1_8_R3/pom.xml +++ b/NMS/NMS-v1_8_R3/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_8_R3/src/main/java/com/songoda/core/nms/v1_8_R3/nbt/NBTCoreImpl.java b/NMS/NMS-v1_8_R3/src/main/java/com/songoda/core/nms/v1_8_R3/nbt/NBTCoreImpl.java index 8af4de92..5475722d 100644 --- a/NMS/NMS-v1_8_R3/src/main/java/com/songoda/core/nms/v1_8_R3/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_8_R3/src/main/java/com/songoda/core/nms/v1_8_R3/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_9_R1/pom.xml b/NMS/NMS-v1_9_R1/pom.xml index 862170a2..1a2a5a03 100644 --- a/NMS/NMS-v1_9_R1/pom.xml +++ b/NMS/NMS-v1_9_R1/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_9_R1/src/main/java/com/songoda/core/nms/v1_9_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_9_R1/src/main/java/com/songoda/core/nms/v1_9_R1/nbt/NBTCoreImpl.java index 6d07a9a8..f4b7e48a 100644 --- a/NMS/NMS-v1_9_R1/src/main/java/com/songoda/core/nms/v1_9_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_9_R1/src/main/java/com/songoda/core/nms/v1_9_R1/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/NMS/NMS-v1_9_R2/pom.xml b/NMS/NMS-v1_9_R2/pom.xml index 610a82e8..54998a41 100644 --- a/NMS/NMS-v1_9_R2/pom.xml +++ b/NMS/NMS-v1_9_R2/pom.xml @@ -7,7 +7,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 ../../pom.xml diff --git a/NMS/NMS-v1_9_R2/src/main/java/com/songoda/core/nms/v1_9_R2/nbt/NBTCoreImpl.java b/NMS/NMS-v1_9_R2/src/main/java/com/songoda/core/nms/v1_9_R2/nbt/NBTCoreImpl.java index 2541d993..7c61513c 100644 --- a/NMS/NMS-v1_9_R2/src/main/java/com/songoda/core/nms/v1_9_R2/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_9_R2/src/main/java/com/songoda/core/nms/v1_9_R2/nbt/NBTCoreImpl.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { + @Deprecated @Override public NBTItem of(ItemStack item) { return new NBTItemImpl(CraftItemStack.asNMSCopy(item)); } + @Deprecated @Override public NBTItem newItem() { return new NBTItemImpl(null); diff --git a/pom.xml b/pom.xml index b44967d9..a33c1214 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.songoda SongodaCore-Modules - 2.6.11 + 2.6.12 pom @@ -45,6 +45,7 @@ NMS/NMS-v1_16_R3 NMS/NMS-v1_17_R1 NMS/NMS-v1_18_R1 + NMS/NMS-v1_18_R2