From 1e873c123c469cefdd8808db85ebcdd8a797d43c Mon Sep 17 00:00:00 2001 From: fillefilip8 Date: Wed, 2 Mar 2016 10:37:43 +0100 Subject: [PATCH 01/10] Added that fancy message when 1.9 clients try to enter a Minecart :) --- .../us/myles/ViaVersion/ViaVersionPlugin.java | 2 ++ .../listeners/MinecartListener.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/main/java/us/myles/ViaVersion/listeners/MinecartListener.java diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 0bdaf7820..83e620424 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -16,6 +16,7 @@ import org.bukkit.plugin.java.JavaPlugin; import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.ViaVersionAPI; import us.myles.ViaVersion.handlers.ViaVersionInitializer; +import us.myles.ViaVersion.listeners.MinecartListener; import us.myles.ViaVersion.util.ReflectionUtil; import java.util.Collections; @@ -52,6 +53,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI { setPorted(e.getPlayer().getUniqueId(), false); } }, this); + Bukkit.getPluginManager().registerEvents(new MinecartListener(),this); } public void injectPacketHandler() throws Exception { diff --git a/src/main/java/us/myles/ViaVersion/listeners/MinecartListener.java b/src/main/java/us/myles/ViaVersion/listeners/MinecartListener.java new file mode 100644 index 000000000..3f95ca6fc --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/listeners/MinecartListener.java @@ -0,0 +1,25 @@ +package us.myles.ViaVersion.listeners; + +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.vehicle.VehicleEnterEvent; +import us.myles.ViaVersion.api.ViaVersion; + +/** + * Created by fillefilip8 on 2016-03-02. + */ +public class MinecartListener implements Listener { + @EventHandler + public void onMinecartEnter(VehicleEnterEvent e){ + if(e.getEntered() instanceof Player) { + Player player = (Player) e.getEntered(); + if(ViaVersion.getInstance().isPorted(player)){ + e.setCancelled(true); + e.getEntered().sendMessage(ChatColor.RED + "Minecarts are not supported. Due to changes on minecarts in 1.9"); + } + } + + } +} From 121bc9de6c3143de80a5670916066a8e54c1b230 Mon Sep 17 00:00:00 2001 From: fillefilip8 Date: Wed, 2 Mar 2016 11:32:57 +0100 Subject: [PATCH 02/10] * Modifyed that fancy message when 1.9 clients try to enter a Minecart or a Boat :) * Renamed the listener to something better. --- .../us/myles/ViaVersion/ViaVersionPlugin.java | 4 +- .../listeners/MinecartListener.java | 25 ------------ .../ViaVersion/listeners/VehicleListener.java | 39 +++++++++++++++++++ 3 files changed, 41 insertions(+), 27 deletions(-) delete mode 100644 src/main/java/us/myles/ViaVersion/listeners/MinecartListener.java create mode 100644 src/main/java/us/myles/ViaVersion/listeners/VehicleListener.java diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 83e620424..f9e2b5fb0 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -16,7 +16,7 @@ import org.bukkit.plugin.java.JavaPlugin; import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.ViaVersionAPI; import us.myles.ViaVersion.handlers.ViaVersionInitializer; -import us.myles.ViaVersion.listeners.MinecartListener; +import us.myles.ViaVersion.listeners.VehicleListener; import us.myles.ViaVersion.util.ReflectionUtil; import java.util.Collections; @@ -53,7 +53,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI { setPorted(e.getPlayer().getUniqueId(), false); } }, this); - Bukkit.getPluginManager().registerEvents(new MinecartListener(),this); + Bukkit.getPluginManager().registerEvents(new VehicleListener(),this); } public void injectPacketHandler() throws Exception { diff --git a/src/main/java/us/myles/ViaVersion/listeners/MinecartListener.java b/src/main/java/us/myles/ViaVersion/listeners/MinecartListener.java deleted file mode 100644 index 3f95ca6fc..000000000 --- a/src/main/java/us/myles/ViaVersion/listeners/MinecartListener.java +++ /dev/null @@ -1,25 +0,0 @@ -package us.myles.ViaVersion.listeners; - -import org.bukkit.ChatColor; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.vehicle.VehicleEnterEvent; -import us.myles.ViaVersion.api.ViaVersion; - -/** - * Created by fillefilip8 on 2016-03-02. - */ -public class MinecartListener implements Listener { - @EventHandler - public void onMinecartEnter(VehicleEnterEvent e){ - if(e.getEntered() instanceof Player) { - Player player = (Player) e.getEntered(); - if(ViaVersion.getInstance().isPorted(player)){ - e.setCancelled(true); - e.getEntered().sendMessage(ChatColor.RED + "Minecarts are not supported. Due to changes on minecarts in 1.9"); - } - } - - } -} diff --git a/src/main/java/us/myles/ViaVersion/listeners/VehicleListener.java b/src/main/java/us/myles/ViaVersion/listeners/VehicleListener.java new file mode 100644 index 000000000..537dde557 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/listeners/VehicleListener.java @@ -0,0 +1,39 @@ +package us.myles.ViaVersion.listeners; + +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.entity.Boat; +import org.bukkit.entity.Minecart; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerToggleSneakEvent; +import org.bukkit.event.vehicle.VehicleEnterEvent; +import org.bukkit.event.vehicle.VehicleExitEvent; +import us.myles.ViaVersion.api.ViaVersion; + +/** + * Created by fillefilip8 on 2016-03-02. + */ +public class VehicleListener implements Listener { + @EventHandler + public void onVehicleEnter(VehicleEnterEvent e){ + if(e.getEntered() instanceof Player) { + Player player = (Player) e.getEntered(); + if(ViaVersion.getInstance().isPorted(player)){ + + if(e.getVehicle() instanceof Minecart){ + e.getEntered().sendMessage(ChatColor.RED + "Minecarts are not supported. Due to changes on minecarts in 1.9"); + e.setCancelled(true); + }else if(e.getVehicle() instanceof Boat){ + e.getEntered().sendMessage(ChatColor.RED + "Boats are not supported. Due to changes on boats in 1.9"); + e.setCancelled(true); + } + + + } + } + + } + +} From bb42cda4a8b716d721dd1a1626052e7be03845f5 Mon Sep 17 00:00:00 2001 From: fillefilip8 Date: Thu, 3 Mar 2016 13:26:52 +0100 Subject: [PATCH 03/10] . --- .../ViaVersion/listeners/InteractListener.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/main/java/us/myles/ViaVersion/listeners/InteractListener.java diff --git a/src/main/java/us/myles/ViaVersion/listeners/InteractListener.java b/src/main/java/us/myles/ViaVersion/listeners/InteractListener.java new file mode 100644 index 000000000..ad8a6686e --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/listeners/InteractListener.java @@ -0,0 +1,15 @@ +package us.myles.ViaVersion.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; + +/** + * Created by fillefilip8 on 2016-03-03. + */ +public class InteractListener implements Listener { + @EventHandler + public void onSwordBlock(PlayerInteractEvent e){ + + } +} From ef118a3a642180bcdf658547c685bdb61086f345 Mon Sep 17 00:00:00 2001 From: fillefilip8 Date: Thu, 3 Mar 2016 14:28:26 +0100 Subject: [PATCH 04/10] * One JavaDoc to the api XD * Added faster way to change version number. --- pom.xml | 8 +++- .../us/myles/ViaVersion/ViaVersionPlugin.java | 4 +- .../myles/ViaVersion/api/ViaVersionAPI.java | 6 ++- .../listeners/InteractListener.java | 15 ------- .../ViaVersion/listeners/VehicleListener.java | 39 ------------------- src/main/resources/plugin.yml | 2 +- 6 files changed, 14 insertions(+), 60 deletions(-) delete mode 100644 src/main/java/us/myles/ViaVersion/listeners/InteractListener.java delete mode 100644 src/main/java/us/myles/ViaVersion/listeners/VehicleListener.java diff --git a/pom.xml b/pom.xml index 7ab46f1b7..28e6acfee 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ us.myles ViaVersion - 1.0-SNAPSHOT + 0.4.6 @@ -18,6 +18,12 @@ + + + src/main/resources + true + + diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index f9e2b5fb0..2dfe7bcfc 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -16,7 +16,6 @@ import org.bukkit.plugin.java.JavaPlugin; import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.ViaVersionAPI; import us.myles.ViaVersion.handlers.ViaVersionInitializer; -import us.myles.ViaVersion.listeners.VehicleListener; import us.myles.ViaVersion.util.ReflectionUtil; import java.util.Collections; @@ -39,7 +38,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI { return; } - getLogger().info("ViaVersion enabled, injecting. (Allows 1.8 to be accessed via 1.9)"); + getLogger().info("ViaVersion " + getDescription().getVersion() + " is now enabled, injecting. (Allows 1.8 to be accessed via 1.9)"); try { injectPacketHandler(); System.setProperty("ViaVersion", getDescription().getVersion()); @@ -53,7 +52,6 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI { setPorted(e.getPlayer().getUniqueId(), false); } }, this); - Bukkit.getPluginManager().registerEvents(new VehicleListener(),this); } public void injectPacketHandler() throws Exception { diff --git a/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java index cdccc80ce..2d053b69f 100644 --- a/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java +++ b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java @@ -3,6 +3,10 @@ package us.myles.ViaVersion.api; import org.bukkit.entity.Player; public interface ViaVersionAPI { - + /** + * Is player using 1.9? + * @param player + * @return + */ boolean isPorted(Player player); } diff --git a/src/main/java/us/myles/ViaVersion/listeners/InteractListener.java b/src/main/java/us/myles/ViaVersion/listeners/InteractListener.java deleted file mode 100644 index ad8a6686e..000000000 --- a/src/main/java/us/myles/ViaVersion/listeners/InteractListener.java +++ /dev/null @@ -1,15 +0,0 @@ -package us.myles.ViaVersion.listeners; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerInteractEvent; - -/** - * Created by fillefilip8 on 2016-03-03. - */ -public class InteractListener implements Listener { - @EventHandler - public void onSwordBlock(PlayerInteractEvent e){ - - } -} diff --git a/src/main/java/us/myles/ViaVersion/listeners/VehicleListener.java b/src/main/java/us/myles/ViaVersion/listeners/VehicleListener.java deleted file mode 100644 index 537dde557..000000000 --- a/src/main/java/us/myles/ViaVersion/listeners/VehicleListener.java +++ /dev/null @@ -1,39 +0,0 @@ -package us.myles.ViaVersion.listeners; - -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.entity.Boat; -import org.bukkit.entity.Minecart; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerToggleSneakEvent; -import org.bukkit.event.vehicle.VehicleEnterEvent; -import org.bukkit.event.vehicle.VehicleExitEvent; -import us.myles.ViaVersion.api.ViaVersion; - -/** - * Created by fillefilip8 on 2016-03-02. - */ -public class VehicleListener implements Listener { - @EventHandler - public void onVehicleEnter(VehicleEnterEvent e){ - if(e.getEntered() instanceof Player) { - Player player = (Player) e.getEntered(); - if(ViaVersion.getInstance().isPorted(player)){ - - if(e.getVehicle() instanceof Minecart){ - e.getEntered().sendMessage(ChatColor.RED + "Minecarts are not supported. Due to changes on minecarts in 1.9"); - e.setCancelled(true); - }else if(e.getVehicle() instanceof Boat){ - e.getEntered().sendMessage(ChatColor.RED + "Boats are not supported. Due to changes on boats in 1.9"); - e.setCancelled(true); - } - - - } - } - - } - -} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 3f3eaa8a2..012f63e08 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: ViaVersion main: us.myles.ViaVersion.ViaVersionPlugin author: _MylesC -version: 0.4.6 +version: ${version} load: startup loadbefore: [ProtocolLib, ProxyPipe] \ No newline at end of file From 5930a8376510b59848c5bf2a3ff43e69649f03c3 Mon Sep 17 00:00:00 2001 From: fillefilip8 Date: Thu, 3 Mar 2016 14:57:04 +0100 Subject: [PATCH 05/10] Added a command and a new API function "getVersion()" --- .../myles/ViaVersion/ViaVersionCommand.java | 48 +++++++++++++++++++ .../us/myles/ViaVersion/ViaVersionPlugin.java | 6 +++ .../myles/ViaVersion/api/ViaVersionAPI.java | 1 + src/main/resources/plugin.yml | 4 +- 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/main/java/us/myles/ViaVersion/ViaVersionCommand.java diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionCommand.java b/src/main/java/us/myles/ViaVersion/ViaVersionCommand.java new file mode 100644 index 000000000..d02098112 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/ViaVersionCommand.java @@ -0,0 +1,48 @@ +package us.myles.ViaVersion; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import us.myles.ViaVersion.api.ViaVersion; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by fillefilip8 on 2016-03-03. + */ +public class ViaVersionCommand implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender.hasPermission("viaversion.admin")) { + if (args.length == 0) { + sender.sendMessage(color("&aViaVersion &c" + ViaVersion.getInstance().getVersion() + "&b by &6_MylesC")); + sender.sendMessage(color("&6Commands:")); + sender.sendMessage(color("&2/viaversion list &7- &6Shows lists of all 1.9 clients and 1.8 clients.")); + } else if (args.length == 1) { + if (args[0].equalsIgnoreCase("list")) { + List portedPlayers = new ArrayList(); + List normalPlayers = new ArrayList(); + for (Player p : Bukkit.getOnlinePlayers()) { + if (ViaVersion.getInstance().isPorted(p)) { + portedPlayers.add(p.getName()); + } else { + normalPlayers.add(p.getName()); + } + } + + sender.sendMessage(color("&8[&61.9&8]: &b" + portedPlayers.toString())); + sender.sendMessage(color("&8[&61.8&8]: &b" + normalPlayers.toString())); + } + } + + } + return false; + } + public String color(String string){ + return string.replace("&", "ยง"); + } +} diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 2dfe7bcfc..6cf14b4f6 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -52,6 +52,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI { setPorted(e.getPlayer().getUniqueId(), false); } }, this); + getCommand("viaversion").setExecutor(new ViaVersionCommand()); } public void injectPacketHandler() throws Exception { @@ -78,6 +79,11 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI { return portedPlayers.contains(player.getUniqueId()); } + @Override + public String getVersion() { + return getDescription().getVersion(); + } + public void setPorted(UUID id, boolean value) { if (value) { portedPlayers.add(id); diff --git a/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java index 2d053b69f..ba3fe0b0e 100644 --- a/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java +++ b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java @@ -9,4 +9,5 @@ public interface ViaVersionAPI { * @return */ boolean isPorted(Player player); + String getVersion(); } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 012f63e08..e570f35b9 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,4 +3,6 @@ main: us.myles.ViaVersion.ViaVersionPlugin author: _MylesC version: ${version} load: startup -loadbefore: [ProtocolLib, ProxyPipe] \ No newline at end of file +loadbefore: [ProtocolLib, ProxyPipe] +commands: + viaversion: \ No newline at end of file From 1b75526892a71fe41d7d6614b5fa34696cc360b3 Mon Sep 17 00:00:00 2001 From: Myles Date: Thu, 3 Mar 2016 15:17:31 +0000 Subject: [PATCH 06/10] Implement changes to entity effect --- .../transformers/OutgoingTransformer.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java index 667d50be3..9fcc91970 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java @@ -428,6 +428,20 @@ public class OutgoingTransformer { output.writeBytes(input); return; } + if (packet == PacketType.PLAY_ENTITY_EFFECT) { + int id = PacketUtil.readVarInt(input); + PacketUtil.writeVarInt(id, output); + byte effectID = input.readByte(); + output.writeByte(effectID); + byte amplifier = input.readByte(); + output.writeByte(amplifier); + int duration = PacketUtil.readVarInt(input); + PacketUtil.writeVarInt(duration, output); + // we need to write as a byte instead of boolean + boolean hideParticles = input.readBoolean(); + output.writeByte(hideParticles ? 1 : 0); + return; + } if (packet == PacketType.PLAY_TEAM) { String teamName = PacketUtil.readString(input); PacketUtil.writeString(teamName, output); From 9108e78491d3893f0f279039eab156994770e373 Mon Sep 17 00:00:00 2001 From: fillefilip8 Date: Thu, 3 Mar 2016 17:42:38 +0100 Subject: [PATCH 07/10] Added more yml info and remove author name from the message in the command. --- src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java | 1 + .../myles/ViaVersion/{ => commands}/ViaVersionCommand.java | 4 ++-- src/main/resources/plugin.yml | 5 ++++- 3 files changed, 7 insertions(+), 3 deletions(-) rename src/main/java/us/myles/ViaVersion/{ => commands}/ViaVersionCommand.java (95%) diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 6cf14b4f6..3f8d24e03 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -15,6 +15,7 @@ import org.bukkit.plugin.java.JavaPlugin; import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.ViaVersionAPI; +import us.myles.ViaVersion.commands.ViaVersionCommand; import us.myles.ViaVersion.handlers.ViaVersionInitializer; import us.myles.ViaVersion.util.ReflectionUtil; diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionCommand.java b/src/main/java/us/myles/ViaVersion/commands/ViaVersionCommand.java similarity index 95% rename from src/main/java/us/myles/ViaVersion/ViaVersionCommand.java rename to src/main/java/us/myles/ViaVersion/commands/ViaVersionCommand.java index d02098112..11872a831 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionCommand.java +++ b/src/main/java/us/myles/ViaVersion/commands/ViaVersionCommand.java @@ -1,4 +1,4 @@ -package us.myles.ViaVersion; +package us.myles.ViaVersion.commands; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -19,7 +19,7 @@ public class ViaVersionCommand implements CommandExecutor { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (sender.hasPermission("viaversion.admin")) { if (args.length == 0) { - sender.sendMessage(color("&aViaVersion &c" + ViaVersion.getInstance().getVersion() + "&b by &6_MylesC")); + sender.sendMessage(color("&aViaVersion &c" + ViaVersion.getInstance().getVersion())); sender.sendMessage(color("&6Commands:")); sender.sendMessage(color("&2/viaversion list &7- &6Shows lists of all 1.9 clients and 1.8 clients.")); } else if (args.length == 1) { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e570f35b9..18a0b5f85 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -5,4 +5,7 @@ version: ${version} load: startup loadbefore: [ProtocolLib, ProxyPipe] commands: - viaversion: \ No newline at end of file + viaversion: + description: Shows ViaVersion Version and more. + permission: viaversion.admin + aliases: [viaver] \ No newline at end of file From 64c2ba243f94b63623a31264d8d58c927dbb2cba Mon Sep 17 00:00:00 2001 From: Paulomart Date: Thu, 3 Mar 2016 18:34:17 +0100 Subject: [PATCH 08/10] Basic support for rewriting SpawnEggs and Potions, still needs more work --- pom.xml | 7 +- .../ViaVersion/api/slot/ItemSlotRewriter.java | 168 ++++++++++++++++++ .../ViaVersion/metadata/MetadataRewriter.java | 13 +- .../transformers/IncomingTransformer.java | 28 ++- .../transformers/OutgoingTransformer.java | 43 ++++- .../us/myles/ViaVersion/util/PacketUtil.java | 39 ---- .../myles/ViaVersion/util/ReflectionUtil.java | 12 ++ 7 files changed, 254 insertions(+), 56 deletions(-) create mode 100644 src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java diff --git a/pom.xml b/pom.xml index 28e6acfee..e85a36124 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,12 @@ - + + org.spigotmc + spigot + 1.8.8-R0.1-SNAPSHOT + + org.bukkit bukkit 1.8.8-R0.1-SNAPSHOT diff --git a/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java b/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java new file mode 100644 index 000000000..0c94f4ac3 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java @@ -0,0 +1,168 @@ +package us.myles.ViaVersion.api.slot; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.bukkit.Material; + +import net.minecraft.server.v1_8_R3.EntityTypes; +import net.minecraft.server.v1_8_R3.Item; +import net.minecraft.server.v1_8_R3.ItemStack; +import net.minecraft.server.v1_8_R3.MobEffect; +import net.minecraft.server.v1_8_R3.MobEffectList; +import net.minecraft.server.v1_8_R3.NBTTagCompound; +import net.minecraft.server.v1_8_R3.PotionBrewer; + +import io.netty.buffer.ByteBuf; + +import us.myles.ViaVersion.CancelException; +import us.myles.ViaVersion.util.ReflectionUtil; + +public class ItemSlotRewriter { + + public static void rewrite1_9To1_8(ByteBuf input, ByteBuf output) throws CancelException { + try { + Object item = readItemStack(input); + fixIdsFrom1_9To1_8(item); + writeItemStack(item, output); + } catch (Exception e) { + System.out.println("Error while rewriting an item slot."); + e.printStackTrace(); + throw new CancelException(); + } + } + + public static void rewrite1_8To1_9(ByteBuf input, ByteBuf output) throws CancelException { + try { + Object item = readItemStack(input); + fixIdsFrom1_8To1_9(item); + writeItemStack(item, output); + } catch (Exception e) { + System.out.println("Error while rewriting an item slot."); + e.printStackTrace(); + throw new CancelException(); + } + } + + public static void fixIdsFrom1_9To1_8(Object itemstack) throws NoSuchFieldException, IllegalAccessException { + if (itemstack != null) { + ItemStack stack = (ItemStack) itemstack; + int itemId = Item.getId(stack.getItem()); + if (itemId == Material.MONSTER_EGG.getId() && stack.getData() == 0) { + NBTTagCompound tag = stack.getTag(); + int data = 0; + if (tag != null && tag.hasKeyOfType("EntityTag", 10)) { + NBTTagCompound entityTag = tag.getCompound("EntityTag"); + if (entityTag.hasKeyOfType("id", 8)) { + String id = entityTag.getString("id"); + Map g = (Map) ReflectionUtil.getStatic(EntityTypes.class, "g", Map.class); + data = g.get(id); + } + } + stack.setTag(null); + stack.setData(data); + } else if (itemId == Material.POTION.getId() && stack.getData() == 0) { + NBTTagCompound tag = stack.getTag(); + if (tag != null) { + System.out.println("in: " + tag); + } + } + } + } + + public static void fixIdsFrom1_8To1_9(Object itemstack) { + if (itemstack != null) { + ItemStack stack = (ItemStack) itemstack; + int itemId = Item.getId(stack.getItem()); + if (itemId == Material.MONSTER_EGG.getId() && stack.getData() != 0) { + NBTTagCompound tag = stack.getTag(); + if (tag == null) { + tag = new NBTTagCompound(); + } + NBTTagCompound entityTag = new NBTTagCompound(); + entityTag.setString("id", EntityTypes.b(stack.getData())); + tag.set("EntityTag", entityTag); + stack.setTag(tag); + stack.setData(0); + } else if (itemId == Material.POTION.getId() && stack.getData() != 0) { + NBTTagCompound tag = stack.getTag(); + if (tag == null) { + tag = new NBTTagCompound(); + stack.setTag(tag); + } + try { + List effects = PotionBrewer.getEffects(stack.getData(), true); + if (effects != null && effects.size() >= 1) { + MobEffect effect = effects.get(0); + MobEffectList type = MobEffectList.byId[effect.getEffectId()]; + StringBuilder name = new StringBuilder(); + System.out.println(effect.getDuration() + " ?>? " +type.k()); + if (effect.getAmplifier() > 0) { + name.append("strong_"); + } else if (effect.getDuration() > type.k()) { + name.append("long_"); + } + + name.append(POTION_TYPE_TO_KEY.get(effect.getEffectId())); + System.out.println("Rewriting to: " + name.toString()); + tag.setString("Potion", name.toString()); + } else { + System.out.println("Falling back to water for subId: " + stack.getData()); + tag.setString("Potion", "water"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + + public static Object readItemStack(ByteBuf input) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Object serializer = getPacketDataSerializer(input); + return READ_ITEM.invoke(serializer); + } + + public static void writeItemStack(Object itemstack, ByteBuf output) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Object serializer = getPacketDataSerializer(output); + WRITE_ITEM.invoke(serializer, itemstack); + } + + private static final Map POTION_TYPE_TO_KEY = new HashMap<>(); + private static Constructor SERIALIZER_CONSTRUCTOR; + private static Method WRITE_ITEM; + private static Method READ_ITEM; + + static { + try { + Class list = ReflectionUtil.nms("MobEffectList"); + Map map = ReflectionUtil.getStatic(list, "I", Map.class); + for (Entry e : map.entrySet()) { + System.out.println(e.getValue()); + System.out.println(e.getValue().getClass()); + int id = ReflectionUtil.get(e.getValue(), list, "id", int.class); + String type = ReflectionUtil.get(e.getKey(), "b", String.class); + POTION_TYPE_TO_KEY.put(id, type); + } + } catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException e1) { + e1.printStackTrace(); + } + try { + Class serializer = ReflectionUtil.nms("PacketDataSerializer"); + Class itemStack = ReflectionUtil.nms("ItemStack"); + SERIALIZER_CONSTRUCTOR = serializer.getDeclaredConstructor(ByteBuf.class); + WRITE_ITEM = serializer.getDeclaredMethod("a", itemStack); + READ_ITEM = serializer.getDeclaredMethod("i"); + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + } + + private static Object getPacketDataSerializer(ByteBuf buf) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + return SERIALIZER_CONSTRUCTOR.newInstance(buf); + } +} diff --git a/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java b/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java index 1dad62f05..fd2467a33 100644 --- a/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java +++ b/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java @@ -11,6 +11,7 @@ import org.bukkit.util.Vector; import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.slot.ItemSlotRewriter; import us.myles.ViaVersion.util.PacketUtil; public class MetadataRewriter { @@ -92,7 +93,8 @@ public class MetadataRewriter { output.writeBoolean(((Byte) value).byteValue() != 0); break; case Slot: - PacketUtil.writeItem(value, output); + ItemSlotRewriter.fixIdsFrom1_8To1_9(value); + ItemSlotRewriter.writeItemStack(value, output); break; case Position: Vector vector = (Vector) value; @@ -148,8 +150,13 @@ public class MetadataRewriter { case String: entries.add(new Entry(index, PacketUtil.readString(buf))); break; - case Slot: - entries.add(new Entry(index, PacketUtil.readItem(buf))); + case Slot: { + try { + entries.add(new Entry(index, ItemSlotRewriter.readItemStack(buf))); + } catch (Exception e) { + e.printStackTrace(); + } + } break; case Position: { int x = buf.readInt(); diff --git a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java index b7c99669d..5d1d0a522 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java @@ -5,6 +5,7 @@ import org.bukkit.inventory.ItemStack; import us.myles.ViaVersion.CancelException; import us.myles.ViaVersion.ConnectionInfo; import us.myles.ViaVersion.ViaVersionPlugin; +import us.myles.ViaVersion.api.slot.ItemSlotRewriter; import us.myles.ViaVersion.packets.PacketType; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.util.PacketUtil; @@ -121,7 +122,7 @@ public class IncomingTransformer { output.writeByte(button); output.writeShort(action); output.writeByte(mode); - output.writeBytes(input); + ItemSlotRewriter.rewrite1_9To1_8(input, output); return; } if (packet == PacketType.PLAY_CLIENT_SETTINGS) { @@ -178,6 +179,8 @@ public class IncomingTransformer { try { Method m = ReflectionUtil.obc("inventory.CraftItemStack").getDeclaredMethod("asNMSCopy", ItemStack.class); item = m.invoke(null, inHand); + ItemSlotRewriter.fixIdsFrom1_9To1_8(item); + ItemSlotRewriter.writeItemStack(item, output); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { @@ -186,10 +189,14 @@ public class IncomingTransformer { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); } - PacketUtil.writeItem(item, output); - short curX = input.readUnsignedByte(); output.writeByte(curX); short curY = input.readUnsignedByte(); @@ -210,6 +217,8 @@ public class IncomingTransformer { try { Method m = ReflectionUtil.obc("inventory.CraftItemStack").getDeclaredMethod("asNMSCopy", ItemStack.class); item = m.invoke(null, inHand); + ItemSlotRewriter.fixIdsFrom1_9To1_8(item); + ItemSlotRewriter.writeItemStack(item, output); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { @@ -218,14 +227,25 @@ public class IncomingTransformer { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); } - PacketUtil.writeItem(item, output); output.writeByte(-1); output.writeByte(-1); output.writeByte(-1); return; } + if (packet == PacketType.PLAY_CREATIVE_INVENTORY_ACTION) { + short slot = input.readShort(); + output.writeShort(slot); + + ItemSlotRewriter.rewrite1_9To1_8(input, output); + } output.writeBytes(input); } } diff --git a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java index 9fcc91970..7c5bdc958 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java @@ -14,6 +14,7 @@ import us.myles.ViaVersion.CancelException; import us.myles.ViaVersion.ConnectionInfo; import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.api.ViaVersion; +import us.myles.ViaVersion.api.slot.ItemSlotRewriter; import us.myles.ViaVersion.metadata.MetadataRewriter; import us.myles.ViaVersion.packets.PacketType; import us.myles.ViaVersion.packets.State; @@ -64,11 +65,11 @@ public class OutgoingTransformer { int catid = 0; String newname = name; if (effect != null) { - if(effect.isBreakPlaceSound()) { - input.readBytes(input.readableBytes()); - output.clear(); - return; - } + if(effect.isBreakPlaceSound()) { + input.readBytes(input.readableBytes()); + output.clear(); + return; + } catid = effect.getCategory().getId(); newname = effect.getNewName(); } @@ -221,7 +222,9 @@ public class OutgoingTransformer { slot += 1; // add 1 so it's now 2-5 } PacketUtil.writeVarInt(slot, output); - output.writeBytes(input); + + ItemSlotRewriter.rewrite1_8To1_9(input, output); + return; } if (packet == PacketType.PLAY_ENTITY_METADATA) { int id = PacketUtil.readVarInt(input); @@ -338,6 +341,28 @@ public class OutgoingTransformer { output.writeBytes(input); return; } + if (packet == PacketType.PLAY_SET_SLOT) { + int windowId = input.readUnsignedByte(); + output.writeByte(windowId); + + short slot = input.readShort(); + output.writeShort(slot); + + ItemSlotRewriter.rewrite1_8To1_9(input, output); + return; + } + if (packet == PacketType.PLAY_WINDOW_ITEMS) { + int windowId = input.readUnsignedByte(); + output.writeByte(windowId); + + short count = input.readShort(); + output.writeShort(count); + + for (int i = 0; i < count; i++) { + ItemSlotRewriter.rewrite1_8To1_9(input, output); + } + return; + } if (packet == PacketType.PLAY_SPAWN_MOB) { int id = PacketUtil.readVarInt(input); PacketUtil.writeVarInt(id, output); @@ -521,11 +546,11 @@ public class OutgoingTransformer { line = "{\"text\":" + line + "}"; } try { - new JSONParser().parse(line); + new JSONParser().parse(line); } catch (org.json.simple.parser.ParseException e) { - System.out.println("Invalid JSON String: \"" + line + "\" Please report this issue to the ViaVersion Github!"); - return "{\"text\":\"\"}"; + System.out.println("Invalid JSON String: \"" + line + "\" Please report this issue to the ViaVersion Github!"); + return "{\"text\":\"\"}"; } return line; } diff --git a/src/main/java/us/myles/ViaVersion/util/PacketUtil.java b/src/main/java/us/myles/ViaVersion/util/PacketUtil.java index 1c2dde784..5d703b4fe 100644 --- a/src/main/java/us/myles/ViaVersion/util/PacketUtil.java +++ b/src/main/java/us/myles/ViaVersion/util/PacketUtil.java @@ -354,45 +354,6 @@ public class PacketUtil { return output; } - public static void writeItem(Object value, ByteBuf output) { - try { - Class serializer = ReflectionUtil.nms("PacketDataSerializer"); - Object init = serializer.getDeclaredConstructor(ByteBuf.class).newInstance(output); - Method toCall = init.getClass().getDeclaredMethod("a", ReflectionUtil.nms("ItemStack")); - toCall.invoke(init, value); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - } - - public static Object readItem(ByteBuf output) { - try { - Class serializer = ReflectionUtil.nms("PacketDataSerializer"); - Object init = serializer.getDeclaredConstructor(ByteBuf.class).newInstance(output); - Method toCall = init.getClass().getDeclaredMethod("i"); - return toCall.invoke(init); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - return null; - } - public static long[] readBlockPosition(ByteBuf buf) { long val = buf.readLong(); long x = (val >> 38); // signed diff --git a/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java b/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java index e5f19187e..9b7829d3e 100644 --- a/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java +++ b/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java @@ -27,6 +27,18 @@ public class ReflectionUtil { return m.invoke(o); } + public static T getStatic(Class clazz, String f, Class t) throws NoSuchFieldException, IllegalAccessException { + Field field = clazz.getDeclaredField(f); + field.setAccessible(true); + return (T) field.get(null); + } + + public static T get(Object instance, Class clazz, String f, Class t) throws NoSuchFieldException, IllegalAccessException { + Field field = clazz.getDeclaredField(f); + field.setAccessible(true); + return (T) field.get(instance); + } + public static T get(Object o, String f, Class t) throws NoSuchFieldException, IllegalAccessException { Field field = o.getClass().getDeclaredField(f); field.setAccessible(true); From 00df80e6fd21c54a25ab7d27dc13db0d001bb5f7 Mon Sep 17 00:00:00 2001 From: Paulomart Date: Thu, 3 Mar 2016 21:13:49 +0100 Subject: [PATCH 09/10] Replace nms code with fancy library and own code. --- pom.xml | 39 ++- .../ViaVersion/api/slot/ItemSlotRewriter.java | 243 ++++++++++-------- .../ViaVersion/metadata/MetadataRewriter.java | 6 +- .../transformers/IncomingTransformer.java | 36 +-- .../us/myles/ViaVersion/util/PacketUtil.java | 27 ++ 5 files changed, 197 insertions(+), 154 deletions(-) diff --git a/pom.xml b/pom.xml index e85a36124..a8e32f12c 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,6 @@ - + 4.0.0 us.myles @@ -9,6 +8,22 @@ 0.4.6 + + org.apache.maven.plugins + maven-shade-plugin + 1.4 + + false + + + + package + + shade + + + + org.apache.maven.plugins maven-compiler-plugin @@ -26,12 +41,12 @@ - - org.spigotmc - spigot - 1.8.8-R0.1-SNAPSHOT - - + + org.spacehq + opennbt + 1.0 + + org.bukkit bukkit 1.8.8-R0.1-SNAPSHOT @@ -61,5 +76,9 @@ spigot-repo https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - + + spacehq-repo + https://repo.spacehq.org/content/repositories/releases/ + + diff --git a/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java b/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java index 0c94f4ac3..a25be1362 100644 --- a/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java +++ b/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java @@ -1,33 +1,23 @@ package us.myles.ViaVersion.api.slot; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; +import java.io.IOException; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.Map.Entry; import org.bukkit.Material; - -import net.minecraft.server.v1_8_R3.EntityTypes; -import net.minecraft.server.v1_8_R3.Item; -import net.minecraft.server.v1_8_R3.ItemStack; -import net.minecraft.server.v1_8_R3.MobEffect; -import net.minecraft.server.v1_8_R3.MobEffectList; -import net.minecraft.server.v1_8_R3.NBTTagCompound; -import net.minecraft.server.v1_8_R3.PotionBrewer; +import org.spacehq.opennbt.tag.builtin.CompoundTag; +import org.spacehq.opennbt.tag.builtin.StringTag; import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.CancelException; -import us.myles.ViaVersion.util.ReflectionUtil; +import us.myles.ViaVersion.util.PacketUtil; public class ItemSlotRewriter { public static void rewrite1_9To1_8(ByteBuf input, ByteBuf output) throws CancelException { try { - Object item = readItemStack(input); + ItemStack item = readItemStack(input); fixIdsFrom1_9To1_8(item); writeItemStack(item, output); } catch (Exception e) { @@ -39,7 +29,7 @@ public class ItemSlotRewriter { public static void rewrite1_8To1_9(ByteBuf input, ByteBuf output) throws CancelException { try { - Object item = readItemStack(input); + ItemStack item = readItemStack(input); fixIdsFrom1_8To1_9(item); writeItemStack(item, output); } catch (Exception e) { @@ -49,120 +39,153 @@ public class ItemSlotRewriter { } } - public static void fixIdsFrom1_9To1_8(Object itemstack) throws NoSuchFieldException, IllegalAccessException { - if (itemstack != null) { - ItemStack stack = (ItemStack) itemstack; - int itemId = Item.getId(stack.getItem()); - if (itemId == Material.MONSTER_EGG.getId() && stack.getData() == 0) { - NBTTagCompound tag = stack.getTag(); + public static void fixIdsFrom1_9To1_8(ItemStack item) { + if (item != null) { + if (item.id == Material.MONSTER_EGG.getId() && item.data == 0) { + CompoundTag tag = item.tag; int data = 0; - if (tag != null && tag.hasKeyOfType("EntityTag", 10)) { - NBTTagCompound entityTag = tag.getCompound("EntityTag"); - if (entityTag.hasKeyOfType("id", 8)) { - String id = entityTag.getString("id"); - Map g = (Map) ReflectionUtil.getStatic(EntityTypes.class, "g", Map.class); - data = g.get(id); + if (tag != null && tag.get("EntityTag") instanceof CompoundTag) { + CompoundTag entityTag = tag.get("EntityTag"); + if (entityTag.get("id") instanceof StringTag) { + StringTag id = entityTag.get("id"); + data = ENTTIY_NAME_TO_ID.get(id.getValue()); } } - stack.setTag(null); - stack.setData(data); - } else if (itemId == Material.POTION.getId() && stack.getData() == 0) { - NBTTagCompound tag = stack.getTag(); - if (tag != null) { - System.out.println("in: " + tag); - } + item.tag = null; + item.data = (short) data; } } } - public static void fixIdsFrom1_8To1_9(Object itemstack) { - if (itemstack != null) { - ItemStack stack = (ItemStack) itemstack; - int itemId = Item.getId(stack.getItem()); - if (itemId == Material.MONSTER_EGG.getId() && stack.getData() != 0) { - NBTTagCompound tag = stack.getTag(); + public static void fixIdsFrom1_8To1_9(ItemStack item) { + if (item != null) { + if (item.id == Material.MONSTER_EGG.getId() && item.data != 0) { + CompoundTag tag = item.tag; if (tag == null) { - tag = new NBTTagCompound(); - } - NBTTagCompound entityTag = new NBTTagCompound(); - entityTag.setString("id", EntityTypes.b(stack.getData())); - tag.set("EntityTag", entityTag); - stack.setTag(tag); - stack.setData(0); - } else if (itemId == Material.POTION.getId() && stack.getData() != 0) { - NBTTagCompound tag = stack.getTag(); - if (tag == null) { - tag = new NBTTagCompound(); - stack.setTag(tag); - } - try { - List effects = PotionBrewer.getEffects(stack.getData(), true); - if (effects != null && effects.size() >= 1) { - MobEffect effect = effects.get(0); - MobEffectList type = MobEffectList.byId[effect.getEffectId()]; - StringBuilder name = new StringBuilder(); - System.out.println(effect.getDuration() + " ?>? " +type.k()); - if (effect.getAmplifier() > 0) { - name.append("strong_"); - } else if (effect.getDuration() > type.k()) { - name.append("long_"); - } - - name.append(POTION_TYPE_TO_KEY.get(effect.getEffectId())); - System.out.println("Rewriting to: " + name.toString()); - tag.setString("Potion", name.toString()); - } else { - System.out.println("Falling back to water for subId: " + stack.getData()); - tag.setString("Potion", "water"); - } - } catch (Exception e) { - e.printStackTrace(); + tag = new CompoundTag("tag"); } + CompoundTag entityTag = new CompoundTag("EntityTag"); + StringTag id = new StringTag("id", ENTTIY_ID_TO_NAME.get(item.data)); + entityTag.put(id); + tag.put(entityTag); + item.tag = tag; + item.data = 0; } } } - public static Object readItemStack(ByteBuf input) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Object serializer = getPacketDataSerializer(input); - return READ_ITEM.invoke(serializer); + public static ItemStack readItemStack(ByteBuf input) throws IOException { + short id = input.readShort(); + if (id < 0) { + return null; + } else { + ItemStack item = new ItemStack(); + item.id = id; + item.amount = input.readByte(); + item.data = input.readShort(); + item.tag = PacketUtil.readNBT(input); + return item; + } } - public static void writeItemStack(Object itemstack, ByteBuf output) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Object serializer = getPacketDataSerializer(output); - WRITE_ITEM.invoke(serializer, itemstack); + public static void writeItemStack(ItemStack item, ByteBuf output) throws IOException { + if (item == null) { + output.writeShort(-1); + } else { + output.writeShort(item.id); + output.writeByte(item.amount); + output.writeShort(item.data); + PacketUtil.writeNBT(output, item.tag); + } } - private static final Map POTION_TYPE_TO_KEY = new HashMap<>(); - private static Constructor SERIALIZER_CONSTRUCTOR; - private static Method WRITE_ITEM; - private static Method READ_ITEM; + public static class ItemStack { + + private short id; + private byte amount; + private short data; + private CompoundTag tag; + + public static ItemStack fromBukkit(org.bukkit.inventory.ItemStack stack) { + ItemStack item = new ItemStack(); + item.id = (short) stack.getTypeId(); + item.amount = (byte) stack.getAmount(); + item.data = stack.getData().getData(); + // TODO: nbt + return item; + } + } + + private static Map ENTTIY_NAME_TO_ID = new HashMap<>(); + private static Map ENTTIY_ID_TO_NAME = new HashMap<>(); static { - try { - Class list = ReflectionUtil.nms("MobEffectList"); - Map map = ReflectionUtil.getStatic(list, "I", Map.class); - for (Entry e : map.entrySet()) { - System.out.println(e.getValue()); - System.out.println(e.getValue().getClass()); - int id = ReflectionUtil.get(e.getValue(), list, "id", int.class); - String type = ReflectionUtil.get(e.getKey(), "b", String.class); - POTION_TYPE_TO_KEY.put(id, type); - } - } catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException e1) { - e1.printStackTrace(); - } - try { - Class serializer = ReflectionUtil.nms("PacketDataSerializer"); - Class itemStack = ReflectionUtil.nms("ItemStack"); - SERIALIZER_CONSTRUCTOR = serializer.getDeclaredConstructor(ByteBuf.class); - WRITE_ITEM = serializer.getDeclaredMethod("a", itemStack); - READ_ITEM = serializer.getDeclaredMethod("i"); - } catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) { - e.printStackTrace(); - } + register(1, "Item"); + register(2, "XPOrb"); + register(7, "ThrownEgg"); + register(8, "LeashKnot"); + register(9, "Painting"); + register(10, "Arrow"); + register(11, "Snowball"); + register(12, "Fireball"); + register(13, "SmallFireball"); + register(14, "ThrownEnderpearl"); + register(15, "EyeOfEnderSignal"); + register(16, "ThrownPotion"); + register(17, "ThrownExpBottle"); + register(18, "ItemFrame"); + register(19, "WitherSkull"); + register(20, "PrimedTnt"); + register(21, "FallingSand"); + register(22, "FireworksRocketEntity"); + register(30, "ArmorStand"); + register(40, "MinecartCommandBlock"); + register(41, "Boat"); + register(42, "MinecartRideable"); + register(43, "MinecartChest"); + register(44, "MinecartFurnace"); + register(45, "MinecartTNT"); + register(46, "MinecartHopper"); + register(47, "MinecartSpawner"); + register(48, "Mob"); + register(49, "Monster"); + register(50, "Creeper"); + register(51, "Skeleton"); + register(52, "Spider"); + register(53, "Giant"); + register(54, "Zombie"); + register(55, "Slime"); + register(56, "Ghast"); + register(57, "PigZombie"); + register(58, "Enderman"); + register(59, "CaveSpider"); + register(60, "Silverfish"); + register(61, "Blaze"); + register(62, "LavaSlime"); + register(63, "EnderDragon"); + register(64, "WitherBoss"); + register(65, "Bat"); + register(66, "Witch"); + register(67, "Endermite"); + register(68, "Guardian"); + register(90, "Pig"); + register(91, "Sheep"); + register(92, "Cow"); + register(93, "Chicken"); + register(94, "Squid"); + register(95, "Wolf"); + register(96, "MushroomCow"); + register(97, "SnowMan"); + register(98, "Ozelot"); + register(99, "VillagerGolem"); + register(100, "EntityHorse"); + register(101, "Rabbit"); + register(120, "Villager"); + register(200, "EnderCrystal"); } - private static Object getPacketDataSerializer(ByteBuf buf) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - return SERIALIZER_CONSTRUCTOR.newInstance(buf); + private static void register(int id, String name) { + ENTTIY_ID_TO_NAME.put(id, name); + ENTTIY_NAME_TO_ID.put(name, id); } } diff --git a/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java b/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java index fd2467a33..a983a41d3 100644 --- a/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java +++ b/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java @@ -12,6 +12,7 @@ import org.bukkit.util.Vector; import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.slot.ItemSlotRewriter; +import us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack; import us.myles.ViaVersion.util.PacketUtil; public class MetadataRewriter { @@ -93,8 +94,9 @@ public class MetadataRewriter { output.writeBoolean(((Byte) value).byteValue() != 0); break; case Slot: - ItemSlotRewriter.fixIdsFrom1_8To1_9(value); - ItemSlotRewriter.writeItemStack(value, output); + ItemStack item = (ItemStack) value; + ItemSlotRewriter.fixIdsFrom1_8To1_9(item); + ItemSlotRewriter.writeItemStack(item, output); break; case Position: Vector vector = (Vector) value; diff --git a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java index 5d1d0a522..e102a4214 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java @@ -175,25 +175,11 @@ public class IncomingTransformer { int hand = PacketUtil.readVarInt(input); ItemStack inHand = ViaVersionPlugin.getHandItem(info); - Object item = null; try { - Method m = ReflectionUtil.obc("inventory.CraftItemStack").getDeclaredMethod("asNMSCopy", ItemStack.class); - item = m.invoke(null, inHand); + us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack item = us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack.fromBukkit(inHand); ItemSlotRewriter.fixIdsFrom1_9To1_8(item); ItemSlotRewriter.writeItemStack(item, output); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { + } catch (Exception e) { e.printStackTrace(); } @@ -213,25 +199,11 @@ public class IncomingTransformer { output.writeByte(255); // write item in hand ItemStack inHand = ViaVersionPlugin.getHandItem(info); - Object item = null; try { - Method m = ReflectionUtil.obc("inventory.CraftItemStack").getDeclaredMethod("asNMSCopy", ItemStack.class); - item = m.invoke(null, inHand); + us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack item = us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack.fromBukkit(inHand); ItemSlotRewriter.fixIdsFrom1_9To1_8(item); ItemSlotRewriter.writeItemStack(item, output); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { + } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/us/myles/ViaVersion/util/PacketUtil.java b/src/main/java/us/myles/ViaVersion/util/PacketUtil.java index 5d703b4fe..8129e008f 100644 --- a/src/main/java/us/myles/ViaVersion/util/PacketUtil.java +++ b/src/main/java/us/myles/ViaVersion/util/PacketUtil.java @@ -3,6 +3,8 @@ package us.myles.ViaVersion.util; import com.google.common.base.Charsets; import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufInputStream; +import io.netty.buffer.ByteBufOutputStream; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.MessageToByteEncoder; @@ -11,6 +13,9 @@ import us.myles.ViaVersion.chunks.PacketChunk; import us.myles.ViaVersion.chunks.PacketChunkData; import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.ByteBuffer; @@ -21,6 +26,9 @@ import java.util.BitSet; import java.util.List; import java.util.UUID; +import org.spacehq.opennbt.NBTIO; +import org.spacehq.opennbt.tag.builtin.CompoundTag; + public class PacketUtil { private static Method DECODE_METHOD; private static Method ENCODE_METHOD; @@ -42,6 +50,25 @@ public class PacketUtil { } } + public static CompoundTag readNBT(ByteBuf input) throws IOException { + int readerIndex = input.readerIndex(); + byte b = input.readByte(); + if (b == 0) { + return null; + } else { + input.readerIndex(readerIndex); + return (CompoundTag) NBTIO.readTag(new DataInputStream(new ByteBufInputStream(input))); + } + } + + public static void writeNBT(ByteBuf output, CompoundTag tag) throws IOException { + if (tag == null) { + output.writeByte(0); + } else { + NBTIO.writeTag(new DataOutputStream(new ByteBufOutputStream(output)), tag); + } + } + public static List callDecode(ByteToMessageDecoder decoder, ChannelHandlerContext ctx, Object input) { List output = new ArrayList(); try { From c97b443cf24e19985e6fc936c49b66fd1a2e8d9b Mon Sep 17 00:00:00 2001 From: Paulomart Date: Thu, 3 Mar 2016 21:24:19 +0100 Subject: [PATCH 10/10] I dont even Java. --- .../java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java b/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java index a25be1362..fa126972c 100644 --- a/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java +++ b/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java @@ -65,7 +65,7 @@ public class ItemSlotRewriter { tag = new CompoundTag("tag"); } CompoundTag entityTag = new CompoundTag("EntityTag"); - StringTag id = new StringTag("id", ENTTIY_ID_TO_NAME.get(item.data)); + StringTag id = new StringTag("id", ENTTIY_ID_TO_NAME.get(Integer.valueOf(item.data))); entityTag.put(id); tag.put(entityTag); item.tag = tag; @@ -184,7 +184,7 @@ public class ItemSlotRewriter { register(200, "EnderCrystal"); } - private static void register(int id, String name) { + private static void register(Integer id, String name) { ENTTIY_ID_TO_NAME.put(id, name); ENTTIY_NAME_TO_ID.put(name, id); }