From 5ad3b05dae1e5df9f799347700289a7e400cba88 Mon Sep 17 00:00:00 2001 From: TylerS1066 Date: Wed, 3 Jul 2024 17:41:44 -0500 Subject: [PATCH] WIP --- .../APTurrets/TracerManager.java | 4 ++-- .../APTurrets/TurretManager.java | 20 ++++++++----------- .../APTurrets/TurretsMain.java | 8 ++++---- .../APTurrets/utils/NMSUtils.java | 13 ------------ .../APTurrets/utils/NMUtils.java | 7 +++++++ 5 files changed, 21 insertions(+), 31 deletions(-) delete mode 100644 src/main/java/com/snowleopard1863/APTurrets/utils/NMSUtils.java create mode 100644 src/main/java/com/snowleopard1863/APTurrets/utils/NMUtils.java diff --git a/src/main/java/com/snowleopard1863/APTurrets/TracerManager.java b/src/main/java/com/snowleopard1863/APTurrets/TracerManager.java index 95e9af4..987d9c7 100644 --- a/src/main/java/com/snowleopard1863/APTurrets/TracerManager.java +++ b/src/main/java/com/snowleopard1863/APTurrets/TracerManager.java @@ -31,11 +31,11 @@ public void startTracing(Arrow arrow) { arrow.setCritical(false); try { - Object packet = TurretsMain.getInstance().getNMSUtils().getNMSClass("PacketPlayOutEntityDestroy").getConstructor(int[].class).newInstance(new int[]{arrow.getEntityId()}); + Object packet = TurretsMain.getInstance().getNMUtils().getNMClass("PacketPlayOutEntityDestroy").getConstructor(int[].class).newInstance(new int[]{arrow.getEntityId()}); for(Player p : TurretsMain.getInstance().getServer().getOnlinePlayers()) { Object nmsPlayer = p.getClass().getMethod("getHandle").invoke(p); Object pConn = nmsPlayer.getClass().getField("playerConnection").get(nmsPlayer); - pConn.getClass().getMethod("sendPacket", TurretsMain.getInstance().getNMSUtils().getNMSClass("Packet")).invoke(pConn, packet); + pConn.getClass().getMethod("sendPacket", TurretsMain.getInstance().getNMUtils().getNMClass("Packet")).invoke(pConn, packet); } } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/com/snowleopard1863/APTurrets/TurretManager.java b/src/main/java/com/snowleopard1863/APTurrets/TurretManager.java index 6f7f3fe..5b89e4c 100644 --- a/src/main/java/com/snowleopard1863/APTurrets/TurretManager.java +++ b/src/main/java/com/snowleopard1863/APTurrets/TurretManager.java @@ -7,6 +7,8 @@ import net.countercraft.movecraft.worldguard.MovecraftWorldGuard; import net.countercraft.movecraft.worldguard.utils.WorldGuardUtils.State; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.Location; @@ -19,7 +21,6 @@ import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Directional; import org.bukkit.block.data.Rotatable; -import org.bukkit.craftbukkit.v1_18_R2.entity.CraftTippedArrow; import org.bukkit.entity.Arrow; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -131,17 +132,12 @@ public void fire(Player player) { private Arrow launchArrow(Player player) { Arrow arrow; try { - Object nmsPlayer = player.getClass().getMethod("getHandle").invoke(player); - Object nmsWorld = nmsPlayer.getClass().getMethod("getLevel").invoke(nmsPlayer); - CraftTippedArrow - Object nmsArrow = TurretsMain.getInstance().getNMSUtils().getNMSClass("EntityTippedArrow") - .getConstructor(TurretsMain.getInstance().getNMSUtils().getNMSClass("level.ServerLevel"), - TurretsMain.getInstance().getNMSUtils().getNMSClass("EntityLiving")) - .newInstance(nmsWorld, nmsPlayer); - nmsArrow.getClass().getMethod("setNoGravity", boolean.class).invoke(nmsArrow, true); - nmsWorld.getClass().getMethod("addEntity", TurretsMain.getInstance().getNMSUtils().getNMSClass("Entity")) - .invoke(nmsWorld, nmsArrow); - arrow = (Arrow) nmsArrow.getClass().getMethod("getBukkitEntity").invoke(nmsArrow); + ServerPlayer nmsPlayer = (ServerPlayer) player.getClass().getMethod("getHandle").invoke(player); + ServerLevel nmsWorld = nmsPlayer.getLevel(); + net.minecraft.world.entity.projectile.Arrow nmsArrow = new net.minecraft.world.entity.projectile.Arrow(nmsWorld, nmsPlayer); + nmsArrow.setNoGravity(true); + nmsWorld.addFreshEntity(nmsArrow); + arrow = (Arrow) nmsArrow.getBukkitEntity(); } catch (Exception e) { throw new ArrowLaunchException("Something went wrong when trying to launch an arrow", e); } diff --git a/src/main/java/com/snowleopard1863/APTurrets/TurretsMain.java b/src/main/java/com/snowleopard1863/APTurrets/TurretsMain.java index fc70e4b..79f2b81 100644 --- a/src/main/java/com/snowleopard1863/APTurrets/TurretsMain.java +++ b/src/main/java/com/snowleopard1863/APTurrets/TurretsMain.java @@ -3,7 +3,7 @@ import com.snowleopard1863.APTurrets.config.Config; import com.snowleopard1863.APTurrets.listener.*; import com.snowleopard1863.APTurrets.task.ArrowTracerTask; -import com.snowleopard1863.APTurrets.utils.NMSUtils; +import com.snowleopard1863.APTurrets.utils.NMUtils; import net.milkbowl.vault.economy.Economy; import org.bukkit.ChatColor; import org.bukkit.event.Listener; @@ -20,7 +20,7 @@ public static TurretsMain getInstance() { return instance; } - private NMSUtils nmsUtils; + private NMUtils nmsUtils; private TurretManager turretManager; private TracerManager tracerManager; @@ -86,7 +86,7 @@ public void onEnable() { getServer().getPluginManager().registerEvents(new ProjectileHitListener(), this); getServer().getPluginManager().registerEvents(new SignChangeListener(), this); - nmsUtils = new NMSUtils(getServer().getClass().getPackage().getName()); + nmsUtils = new NMUtils(); turretManager = new TurretManager(); tracerManager = new TracerManager(); getLogger().info(getDescription().getName() + " v" + getDescription().getVersion() + " has been enabled."); @@ -103,7 +103,7 @@ public Economy getEconomy() { return economy; } - public NMSUtils getNMSUtils() { + public NMUtils getNMUtils() { return nmsUtils; } diff --git a/src/main/java/com/snowleopard1863/APTurrets/utils/NMSUtils.java b/src/main/java/com/snowleopard1863/APTurrets/utils/NMSUtils.java deleted file mode 100644 index e141bed..0000000 --- a/src/main/java/com/snowleopard1863/APTurrets/utils/NMSUtils.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.snowleopard1863.APTurrets.utils; - -public class NMSUtils { - private final String serverVersion; - - public NMSUtils(String packageName) { - serverVersion = packageName.substring(packageName.lastIndexOf(".") + 1); - } - - public Class getNMSClass(String name) throws ClassNotFoundException { - return Class.forName("net.minecraft.server." + serverVersion + "." + name); - } -} diff --git a/src/main/java/com/snowleopard1863/APTurrets/utils/NMUtils.java b/src/main/java/com/snowleopard1863/APTurrets/utils/NMUtils.java new file mode 100644 index 0000000..6c6d054 --- /dev/null +++ b/src/main/java/com/snowleopard1863/APTurrets/utils/NMUtils.java @@ -0,0 +1,7 @@ +package com.snowleopard1863.APTurrets.utils; + +public class NMUtils { + public Class getNMClass(String name) throws ClassNotFoundException { + return Class.forName("net.minecraft." + name); + } +}