diff --git a/TODOLIST b/TODOLIST index f3b52ace6..6f03b3481 100644 --- a/TODOLIST +++ b/TODOLIST @@ -2,6 +2,5 @@ PORT STUFF TO GUAVA :D (so we dont need to include commons) Stop using new Gson() everywhere Java docs (for platforms etc) Add comments to sponge api -Port bukkit listeners to sponge maybe Fix task ids, methods for sponge Find all the TODO's and check they're done. \ No newline at end of file diff --git a/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 75a5aea71..51389604e 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -18,7 +18,6 @@ import us.myles.ViaVersion.bukkit.*; import us.myles.ViaVersion.classgenerator.ClassGenerator; import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.util.NMSUtil; -import us.myles.ViaVersion.util.ReflectionUtil; import java.util.ArrayList; import java.util.List; diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/BukkitViaMovementTransmitter.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/BukkitViaMovementTransmitter.java index 316783853..f5da163d5 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/BukkitViaMovementTransmitter.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/BukkitViaMovementTransmitter.java @@ -8,7 +8,6 @@ import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.MovementTracker; import us.myles.ViaVersion.util.NMSUtil; -import us.myles.ViaVersion.util.ReflectionUtil; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; diff --git a/bukkit/src/main/java/us/myles/ViaVersion/classgenerator/ClassGenerator.java b/bukkit/src/main/java/us/myles/ViaVersion/classgenerator/ClassGenerator.java index f9daaa86f..310390da5 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/classgenerator/ClassGenerator.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/classgenerator/ClassGenerator.java @@ -9,7 +9,6 @@ import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.handlers.ViaDecodeHandler; import us.myles.ViaVersion.handlers.ViaEncodeHandler; import us.myles.ViaVersion.util.NMSUtil; -import us.myles.ViaVersion.util.ReflectionUtil; public class ClassGenerator { private static HandlerConstructor constructor = new BasicHandlerConstructor(); diff --git a/bukkit/src/main/java/us/myles/ViaVersion/listeners/ViaBukkitListener.java b/bukkit/src/main/java/us/myles/ViaVersion/listeners/ViaBukkitListener.java new file mode 100644 index 000000000..f76f31b71 --- /dev/null +++ b/bukkit/src/main/java/us/myles/ViaVersion/listeners/ViaBukkitListener.java @@ -0,0 +1,53 @@ +package us.myles.ViaVersion.listeners; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NonNull; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; +import us.myles.ViaVersion.ViaVersionPlugin; +import us.myles.ViaVersion.api.ViaListener; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.protocol.Protocol; + +@Getter(AccessLevel.PROTECTED) +public class ViaBukkitListener extends ViaListener implements Listener { + private final Plugin plugin; + + public ViaBukkitListener(ViaVersionPlugin plugin, Class requiredPipeline) { + super(requiredPipeline); + this.plugin = plugin; + } + + /** + * Get the UserConnection from a player + * + * @param player Player object + * @return The UserConnection + */ + protected UserConnection getUserConnection(@NonNull Player player) { + return getUserConnection(player.getUniqueId()); + } + + /** + * Checks if the player is on the selected pipe + * + * @param player Player Object + * @return True if on pipe + */ + protected boolean isOnPipe(Player player) { + return isOnPipe(player.getUniqueId()); + } + + /** + * Register as Bukkit event + */ + @Override + public void register() { + if (isRegistered()) return; + + plugin.getServer().getPluginManager().registerEvents(this, plugin); + setRegistered(true); + } +} diff --git a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/ArmorListener.java b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/ArmorListener.java index 38f8a5dad..1d55fa8d8 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/ArmorListener.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/ArmorListener.java @@ -15,14 +15,15 @@ import org.bukkit.inventory.CraftingInventory; import org.bukkit.inventory.ItemStack; import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.ViaListener; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.listeners.ViaBukkitListener; import us.myles.ViaVersion.protocols.protocol1_9to1_8.ArmorType; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; import java.util.UUID; -public class ArmorListener extends ViaListener { +public class ArmorListener extends ViaBukkitListener { private static final UUID ARMOR_ATTRIBUTE = UUID.fromString("2AD3F246-FEE1-4E67-B886-69FD380BB150"); @@ -107,7 +108,7 @@ public class ArmorListener extends ViaListener { public void sendDelayedArmorUpdate(final Player player) { if (!isOnPipe(player)) return; // Don't start a task if the player is not on the pipe - Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable() { + Via.getPlatform().runSync(new Runnable() { @Override public void run() { sendArmorUpdate(player); diff --git a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/BlockListener.java b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/BlockListener.java index 1c4319021..964bad178 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/BlockListener.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/BlockListener.java @@ -5,12 +5,12 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.block.BlockPlaceEvent; import us.myles.ViaVersion.ViaVersionPlugin; -import us.myles.ViaVersion.api.ViaListener; import us.myles.ViaVersion.api.minecraft.Position; +import us.myles.ViaVersion.listeners.ViaBukkitListener; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; -public class BlockListener extends ViaListener { +public class BlockListener extends ViaBukkitListener { public BlockListener(ViaVersionPlugin plugin) { super(plugin, Protocol1_9TO1_8.class); diff --git a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/CommandBlockListener.java b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/CommandBlockListener.java index 43b49b392..b12a99f7e 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/CommandBlockListener.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/CommandBlockListener.java @@ -18,10 +18,10 @@ import org.spacehq.opennbt.tag.builtin.ByteTag; import org.spacehq.opennbt.tag.builtin.CompoundTag; import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.ViaListener; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.listeners.ViaBukkitListener; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; import us.myles.ViaVersion.util.NMSUtil; import us.myles.ViaVersion.util.ReflectionUtil; @@ -30,7 +30,7 @@ import java.io.DataOutput; import java.io.DataOutputStream; import java.lang.reflect.Method; -public class CommandBlockListener extends ViaListener { +public class CommandBlockListener extends ViaBukkitListener { public CommandBlockListener(ViaVersionPlugin plugin) { super(plugin, Protocol1_9TO1_8.class); diff --git a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/DeathListener.java b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/DeathListener.java index 07760f598..9cb82857e 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/DeathListener.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/DeathListener.java @@ -1,6 +1,5 @@ package us.myles.ViaVersion.listeners.protocol1_9to1_8; -import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -8,12 +7,13 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.entity.PlayerDeathEvent; import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.ViaListener; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.listeners.ViaBukkitListener; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; -public class DeathListener extends ViaListener { +public class DeathListener extends ViaBukkitListener { public DeathListener(ViaVersionPlugin plugin) { super(plugin, Protocol1_9TO1_8.class); } @@ -34,7 +34,7 @@ public class DeathListener extends ViaListener { } private void sendPacket(final Player p, final String msg) { - Bukkit.getScheduler().runTask(getPlugin(), new Runnable() { + Via.getPlatform().runSync(new Runnable() { @Override public void run() { PacketWrapper wrapper = new PacketWrapper(0x2C, null, getUserConnection(p)); diff --git a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/PaperPatch.java b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/PaperPatch.java index 5a1e36371..38e24701e 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/PaperPatch.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/PaperPatch.java @@ -6,10 +6,10 @@ import org.bukkit.block.BlockFace; import org.bukkit.event.EventHandler; import org.bukkit.event.block.BlockPlaceEvent; import us.myles.ViaVersion.ViaVersionPlugin; -import us.myles.ViaVersion.api.ViaListener; +import us.myles.ViaVersion.listeners.ViaBukkitListener; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; -public class PaperPatch extends ViaListener { +public class PaperPatch extends ViaBukkitListener { public PaperPatch(ViaVersionPlugin plugin) { super(plugin, Protocol1_9TO1_8.class); diff --git a/bungee/src/main/java/us/myles/ViaVersion/Bungee.java b/bungee/src/main/java/us/myles/ViaVersion/Bungee.java index 71c4af280..f3a0c5e3c 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/Bungee.java +++ b/bungee/src/main/java/us/myles/ViaVersion/Bungee.java @@ -11,11 +11,11 @@ import us.myles.ViaVersion.api.ViaVersionConfig; import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.api.platform.ViaPlatform; -import us.myles.ViaVersion.bungee.command.BungeeCommand; -import us.myles.ViaVersion.bungee.command.BungeeCommandHandler; import us.myles.ViaVersion.bungee.BungeeViaAPI; import us.myles.ViaVersion.bungee.BungeeViaInjector; import us.myles.ViaVersion.bungee.BungeeViaLoader; +import us.myles.ViaVersion.bungee.command.BungeeCommand; +import us.myles.ViaVersion.bungee.command.BungeeCommandHandler; import us.myles.ViaVersion.bungee.command.BungeeCommandSender; import us.myles.ViaVersion.bungee.config.BungeeConfigAPI; diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/BungeeViaInjector.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/BungeeViaInjector.java index 2c9de6455..8b3f04d3c 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/BungeeViaInjector.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/BungeeViaInjector.java @@ -26,6 +26,7 @@ public class BungeeViaInjector implements ViaInjector { throw e; } } + @Override public void uninject() { // TODO: Uninject from players currently online diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaDecodeHandler.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaDecodeHandler.java index cb7073118..cde90c1ec 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaDecodeHandler.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaDecodeHandler.java @@ -2,8 +2,6 @@ package us.myles.ViaVersion.bungee.handlers; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; -import io.netty.handler.codec.MessageToMessageDecoder; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.protocol.MinecraftDecoder; import net.md_5.bungee.protocol.Protocol; diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaEncodeHandler.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaEncodeHandler.java index b6cdb8767..dbe0b01e3 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaEncodeHandler.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaEncodeHandler.java @@ -2,7 +2,6 @@ package us.myles.ViaVersion.bungee.handlers; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToByteEncoder; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.MinecraftEncoder; diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaVersionInitializer.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaVersionInitializer.java index bf1be261f..e15899e4c 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaVersionInitializer.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaVersionInitializer.java @@ -3,9 +3,6 @@ package us.myles.ViaVersion.bungee.handlers; import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.SocketChannel; -import io.netty.handler.codec.ByteToMessageDecoder; -import io.netty.handler.codec.MessageToByteEncoder; -import io.netty.handler.codec.MessageToMessageDecoder; import net.md_5.bungee.protocol.MinecraftDecoder; import net.md_5.bungee.protocol.MinecraftEncoder; import us.myles.ViaVersion.api.data.UserConnection; diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java index 0b8934680..f6f3aa22e 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java @@ -2,14 +2,11 @@ package us.myles.ViaVersion.bungee.util; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToMessageDecoder; import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.MinecraftDecoder; import net.md_5.bungee.protocol.MinecraftEncoder; -import us.myles.ViaVersion.Bungee; -import us.myles.ViaVersion.util.PipelineUtil; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -36,6 +33,7 @@ public class BungeePipelineUtil { System.out.println("Netty issue?"); } } + public static List callDecode(MessageToMessageDecoder decoder, ChannelHandlerContext ctx, Object input) throws InvocationTargetException { List output = new ArrayList<>(); try { diff --git a/bukkit/src/main/java/us/myles/ViaVersion/api/ViaListener.java b/common/src/main/java/us/myles/ViaVersion/api/ViaListener.java similarity index 51% rename from bukkit/src/main/java/us/myles/ViaVersion/api/ViaListener.java rename to common/src/main/java/us/myles/ViaVersion/api/ViaListener.java index e7953a31a..8b661ce5d 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/api/ViaListener.java +++ b/common/src/main/java/us/myles/ViaVersion/api/ViaListener.java @@ -1,12 +1,6 @@ package us.myles.ViaVersion.api; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import org.bukkit.entity.Player; -import org.bukkit.event.Listener; -import us.myles.ViaVersion.ViaVersionPlugin; +import lombok.*; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.protocols.base.ProtocolInfo; @@ -14,22 +8,12 @@ import us.myles.ViaVersion.protocols.base.ProtocolInfo; import java.util.UUID; @Getter(AccessLevel.PROTECTED) +@Setter(AccessLevel.PROTECTED) @RequiredArgsConstructor -public abstract class ViaListener implements Listener { - private final ViaVersionPlugin plugin; +public abstract class ViaListener { private final Class requiredPipeline; private boolean registered = false; - /** - * Get the UserConnection from a player - * - * @param player Player object - * @return The UserConnection - */ - protected UserConnection getUserConnection(@NonNull Player player) { - return getUserConnection(player.getUniqueId()); - } - /** * Get the UserConnection from an UUID * @@ -41,16 +25,6 @@ public abstract class ViaListener implements Listener { return Via.getManager().getConnection(uuid); } - /** - * Checks if the player is on the selected pipe - * - * @param player Player Object - * @return True if on pipe - */ - protected boolean isOnPipe(Player player) { - return isOnPipe(player.getUniqueId()); - } - /** * Checks if the UUID is on the selected pipe * @@ -64,12 +38,7 @@ public abstract class ViaListener implements Listener { } /** - * Register as Bukkit event + * Register the event */ - public void register() { - if (registered) return; - - plugin.getServer().getPluginManager().registerEvents(this, plugin); - registered = true; - } + public abstract void register(); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ArmorType.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ArmorType.java index a9cd642cc..b99a8e433 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ArmorType.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ArmorType.java @@ -9,27 +9,27 @@ import java.util.HashMap; @Getter public enum ArmorType { - LEATHER_HELMET(1, 298), - LEATHER_CHESTPLATE(3, 299), - LEATHER_LEGGINGS(2, 300), - LEATHER_BOOTS(1, 301), - CHAINMAIL_HELMET(2, 302), - CHAINMAIL_CHESTPLATE(5, 303), - CHAINMAIL_LEGGINGS(4, 304), - CHAINMAIL_BOOTS(1, 305), - IRON_HELMET(2, 306), - IRON_CHESTPLATE(6, 307), - IRON_LEGGINGS(5, 308), - IRON_BOOTS(2, 309), - DIAMOND_HELMET(3, 310), - DIAMOND_CHESTPLATE(8, 311), - DIAMOND_LEGGINGS(6, 312), - DIAMOND_BOOTS(3, 313), - GOLD_HELMET(2, 314), - GOLD_CHESTPLATE(5, 315), - GOLD_LEGGINGS(3, 316), - GOLD_BOOTS(1, 317), - NONE(0, 0); + LEATHER_HELMET(1, 298, "minecraft:leather_helmet"), + LEATHER_CHESTPLATE(3, 299, "minecraft:leather_chestplate"), + LEATHER_LEGGINGS(2, 300, "minecraft:leather_leggings"), + LEATHER_BOOTS(1, 301, "minecraft:leather_boots"), + CHAINMAIL_HELMET(2, 302, "minecraft:chainmail_helmet"), + CHAINMAIL_CHESTPLATE(5, 303, "minecraft:chainmail_chestplate"), + CHAINMAIL_LEGGINGS(4, 304, "minecraft:chainmail_leggings"), + CHAINMAIL_BOOTS(1, 305, "minecraft:chainmail_boots"), + IRON_HELMET(2, 306, "minecraft:iron_helmet"), + IRON_CHESTPLATE(6, 307, "minecraft:iron_chestplate"), + IRON_LEGGINGS(5, 308, "minecraft:iron_leggings"), + IRON_BOOTS(2, 309, "minecraft:iron_boots"), + DIAMOND_HELMET(3, 310, "minecraft:diamond_helmet"), + DIAMOND_CHESTPLATE(8, 311, "minecraft:diamond_chestplate"), + DIAMOND_LEGGINGS(6, 312, "minecraft:diamond_leggings"), + DIAMOND_BOOTS(3, 313, "minecraft:diamond_boots"), + GOLD_HELMET(2, 314, "minecraft:gold_helmet"), + GOLD_CHESTPLATE(5, 315, "minecraft:gold_chestplate"), + GOLD_LEGGINGS(3, 316, "minecraft:gold_leggings"), + GOLD_BOOTS(1, 317, "minecraft:gold_boots"), + NONE(0, 0, "none"); private static HashMap armor; @@ -42,6 +42,7 @@ public enum ArmorType { private final int armorPoints; private final int id; + private final String type; public static ArmorType findById(int id) { for (ArmorType a : ArmorType.values()) @@ -50,6 +51,13 @@ public enum ArmorType { return ArmorType.NONE; } + public static ArmorType findByType(String type) { + for (ArmorType a : ArmorType.values()) + if (a.getType().equals(type)) + return a; + return NONE; + } + public static boolean isArmor(int id) { for (ArmorType a : ArmorType.values()) if (a.getId() == id) @@ -57,13 +65,11 @@ public enum ArmorType { return false; } - public static int calculateArmorPoints(int[] armor) { - int total = 0; - for (int anArmor : armor) { - if (anArmor != -1) - total += findById(anArmor).getArmorPoints(); - } - return total; + public static boolean isArmor(String type) { + for (ArmorType a : ArmorType.values()) + if (a.getType().equals(type)) + return true; + return false; } public int getId() { diff --git a/sponge/pom.xml b/sponge/pom.xml index 115e29b0f..fb41d0443 100644 --- a/sponge/pom.xml +++ b/sponge/pom.xml @@ -64,7 +64,7 @@ org.spongepowered spongeapi - 4.1.0 + LATEST provided diff --git a/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java b/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java index 0e4ba8f88..11854ee83 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java +++ b/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java @@ -3,15 +3,11 @@ package us.myles.ViaVersion; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.inject.Inject; -import ninja.leaping.configurate.commented.CommentedConfigurationNode; -import ninja.leaping.configurate.loader.ConfigurationLoader; -import ninja.leaping.configurate.yaml.YAMLConfigurationLoader; import org.spongepowered.api.Game; import org.spongepowered.api.config.DefaultConfig; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.game.state.GameAboutToStartServerEvent; -import org.spongepowered.api.event.game.state.GameStartedServerEvent; import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.scheduler.SpongeExecutorService; diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeCommandSender.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeCommandSender.java index b4c29a260..68e66cb37 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeCommandSender.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeCommandSender.java @@ -3,7 +3,6 @@ package us.myles.ViaVersion.sponge; import lombok.AllArgsConstructor; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.entity.living.player.Player; -import org.spongepowered.api.text.serializer.TextSerializer; import org.spongepowered.api.text.serializer.TextSerializers; import us.myles.ViaVersion.api.command.ViaCommandSender; diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeConfigAPI.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeConfigAPI.java index d5f85d6fb..e94282226 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeConfigAPI.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeConfigAPI.java @@ -6,7 +6,10 @@ import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import java.io.*; import java.net.URL; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider { private final File defaultConfig; diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaAPI.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaAPI.java index f0628a7f1..769081be2 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaAPI.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaAPI.java @@ -1,10 +1,8 @@ package us.myles.ViaVersion.sponge; import io.netty.buffer.ByteBuf; -import lombok.AllArgsConstructor; import lombok.NonNull; import org.spongepowered.api.entity.living.player.Player; -import us.myles.ViaVersion.SpongePlugin; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.ViaAPI; import us.myles.ViaVersion.api.boss.BossBar; diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaInjector.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaInjector.java index 7524023b3..cfedde3a3 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaInjector.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaInjector.java @@ -10,8 +10,8 @@ import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.platform.ViaInjector; import us.myles.ViaVersion.sponge.handlers.ViaVersionInitializer; -import us.myles.ViaVersion.util.ReflectionUtil; import us.myles.ViaVersion.util.ListWrapper; +import us.myles.ViaVersion.util.ReflectionUtil; import java.lang.reflect.Field; import java.lang.reflect.Method; diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaLoader.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaLoader.java index b11eb5948..deb5e97de 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaLoader.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeViaLoader.java @@ -9,6 +9,10 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BulkChunkTransla import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; import us.myles.ViaVersion.sponge.listeners.ClientLeaveListener; import us.myles.ViaVersion.sponge.listeners.UpdateListener; +import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.ArmorListener; +import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.BlockListener; +import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.CommandBlockListener; +import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.DeathListener; import us.myles.ViaVersion.sponge.providers.SpongeViaBulkChunkTranslator; import us.myles.ViaVersion.sponge.providers.SpongeViaMovementTransmitter; @@ -25,15 +29,11 @@ public class SpongeViaLoader implements ViaPlatformLoader { Sponge.getEventManager().registerListeners(plugin, new ClientLeaveListener()); // /* 1.9 client to 1.8 server */ // -// new ArmorListener(plugin).register(); -// new CommandBlockListener(plugin).register(); -// new DeathListener(plugin).register(); -// new BlockListener(plugin).register(); -// -// if (Bukkit.getVersion().toLowerCase().contains("paper") || Bukkit.getVersion().toLowerCase().contains("taco")) { -// plugin.getLogger().info("Enabling PaperSpigot/TacoSpigot patch: Fixes block placement."); -// new PaperPatch(plugin).register(); -// } + new ArmorListener(plugin).register(); + new CommandBlockListener(plugin).register(); + new DeathListener(plugin).register(); + new BlockListener(plugin).register(); + // if (plugin.getConf().isItemCache()) { // new HandItemCache().runTaskTimerAsynchronously(plugin, 2L, 2L); // Updates player's items :) // HandItemCache.CACHE = true; diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/ViaDecodeHandler.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/ViaDecodeHandler.java index bd42d7f60..943d87094 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/ViaDecodeHandler.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/ViaDecodeHandler.java @@ -4,7 +4,6 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.exception.CancelException; diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/ViaEncodeHandler.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/ViaEncodeHandler.java index ce6faef49..06e51f05f 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/ViaEncodeHandler.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/ViaEncodeHandler.java @@ -4,7 +4,6 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.exception.CancelException; @@ -12,7 +11,6 @@ import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.util.PipelineUtil; -import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; public class ViaEncodeHandler extends MessageToByteEncoder { diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/ViaSpongeListener.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/ViaSpongeListener.java new file mode 100644 index 000000000..b000b7ee0 --- /dev/null +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/ViaSpongeListener.java @@ -0,0 +1,49 @@ +package us.myles.ViaVersion.sponge.listeners; + +import lombok.Getter; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.entity.living.player.Player; +import us.myles.ViaVersion.SpongePlugin; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.ViaListener; +import us.myles.ViaVersion.api.protocol.Protocol; + +import java.lang.reflect.Field; + +public class ViaSpongeListener extends ViaListener { + private static Field entityIdField; + + @Getter + private final SpongePlugin plugin; + + public ViaSpongeListener(SpongePlugin plugin, Class requiredPipeline) { + super(requiredPipeline); + this.plugin = plugin; + } + + @Override + public void register() { + if (isRegistered()) return; + + Sponge.getEventManager().registerListeners(plugin, this); + setRegistered(true); + } + + // Hey sponge, please create a getEntityId method :'( + protected int getEntityId(Player p) { + try { + if (entityIdField == null) { + entityIdField = p.getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("field_145783_c"); + entityIdField.setAccessible(true); + } + + return entityIdField.getInt(p); + } catch (Exception e) { + Via.getPlatform().getLogger().severe("Could not get the entity id, please report this on our Github"); + e.printStackTrace(); + } + + Via.getPlatform().getLogger().severe("Could not get the entity id, please report this on our Github"); + return -1; + } +} diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/ArmorListener.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/ArmorListener.java new file mode 100644 index 000000000..5c8924e00 --- /dev/null +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/ArmorListener.java @@ -0,0 +1,114 @@ +package us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8; + +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.action.InteractEvent; +import org.spongepowered.api.event.entity.living.humanoid.player.RespawnPlayerEvent; +import org.spongepowered.api.event.filter.cause.Root; +import org.spongepowered.api.event.item.inventory.ClickInventoryEvent; +import org.spongepowered.api.event.network.ClientConnectionEvent; +import org.spongepowered.api.item.inventory.ItemStack; +import org.spongepowered.api.item.inventory.transaction.SlotTransaction; +import us.myles.ViaVersion.SpongePlugin; +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.ArmorType; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; +import us.myles.ViaVersion.sponge.listeners.ViaSpongeListener; + +import java.util.Optional; +import java.util.UUID; + +public class ArmorListener extends ViaSpongeListener { + private static final UUID ARMOR_ATTRIBUTE = UUID.fromString("2AD3F246-FEE1-4E67-B886-69FD380BB150"); + + public ArmorListener(SpongePlugin plugin) { + super(plugin, Protocol1_9TO1_8.class); + } + + // + public void sendArmorUpdate(Player player) { + // Ensure that the player is on our pipe + if (!isOnPipe(player.getUniqueId())) return; + + + int armor = 0; + + // TODO is there a method like getArmorContents? + armor += calculate(player.getHelmet()); + armor += calculate(player.getChestplate()); + armor += calculate(player.getLeggings()); + armor += calculate(player.getBoots()); + + PacketWrapper wrapper = new PacketWrapper(0x4B, null, getUserConnection(player.getUniqueId())); + try { + wrapper.write(Type.VAR_INT, getEntityId(player)); // Player ID + wrapper.write(Type.INT, 1); // only 1 property + wrapper.write(Type.STRING, "generic.armor"); + wrapper.write(Type.DOUBLE, 0D); //default 0 armor + wrapper.write(Type.VAR_INT, 1); // 1 modifier + wrapper.write(Type.UUID, ARMOR_ATTRIBUTE); // armor modifier uuid + wrapper.write(Type.DOUBLE, (double) armor); // the modifier value + wrapper.write(Type.BYTE, (byte) 0);// the modifier operation, 0 is add number + + wrapper.send(Protocol1_9TO1_8.class); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private int calculate(Optional itemStack) { + if (itemStack.isPresent()) + return ArmorType.findByType(itemStack.get().getItem().getType().getId()).getArmorPoints(); + + return 0; + } + + @Listener + public void onInventoryClick(ClickInventoryEvent e, @Root Player player) { + for (SlotTransaction transaction : e.getTransactions()) { + if (ArmorType.isArmor(transaction.getFinal().getType().getId()) || + ArmorType.isArmor(e.getCursorTransaction().getFinal().getType().getId())) { + sendDelayedArmorUpdate(player); + break; + } + } + } + + @Listener + public void onInteract(InteractEvent event, @Root Player player) { + if (player.getItemInHand().isPresent()) { + if (ArmorType.isArmor(player.getItemInHand().get().getItem().getId())) + sendDelayedArmorUpdate(player); + } + } + + @Listener + public void onJoin(ClientConnectionEvent.Join e) { + sendArmorUpdate(e.getTargetEntity()); + } + + @Listener + public void onRespawn(RespawnPlayerEvent e) { + if (!isOnPipe(e.getTargetEntity().getUniqueId())) return; + + sendDelayedArmorUpdate(e.getTargetEntity()); + } + + // TODO find world change event +// @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) +// public void onWorldChange(PlayerChangedWorldEvent e) { +// sendArmorUpdate(e.getPlayer()); +// } +// + public void sendDelayedArmorUpdate(final Player player) { + if (!isOnPipe(player.getUniqueId())) return; // Don't start a task if the player is not on the pipe + Via.getPlatform().runSync(new Runnable() { + @Override + public void run() { + sendArmorUpdate(player); + } + }); + } +} diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/BlockListener.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/BlockListener.java new file mode 100644 index 000000000..5019e32d4 --- /dev/null +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/BlockListener.java @@ -0,0 +1,29 @@ +package us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8; + +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.block.ChangeBlockEvent; +import org.spongepowered.api.event.filter.cause.Root; +import org.spongepowered.api.world.Location; +import us.myles.ViaVersion.SpongePlugin; +import us.myles.ViaVersion.api.minecraft.Position; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.sponge.listeners.ViaSpongeListener; + +public class BlockListener extends ViaSpongeListener { + + public BlockListener(SpongePlugin plugin) { + super(plugin, Protocol1_9TO1_8.class); + } + + @Listener // TODO is there better way to do this? + public void placeBlock(ChangeBlockEvent.Place e, @Root Player player) { + if (isOnPipe(player.getUniqueId())) { + Location loc = e.getTransactions().get(0).getFinal().getLocation().get(); + getUserConnection(player.getUniqueId()) + .get(EntityTracker.class) + .addBlockInteraction(new Position((long) loc.getX(), (long) loc.getY(), (long) loc.getZ())); + } + } +} diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/CommandBlockListener.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/CommandBlockListener.java new file mode 100644 index 000000000..610385503 --- /dev/null +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/CommandBlockListener.java @@ -0,0 +1,156 @@ +package us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufOutputStream; +import io.netty.buffer.Unpooled; +import org.spacehq.opennbt.tag.builtin.ByteTag; +import org.spacehq.opennbt.tag.builtin.CompoundTag; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.block.tileentity.CommandBlock; +import org.spongepowered.api.block.tileentity.TileEntity; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.block.InteractBlockEvent; +import org.spongepowered.api.event.entity.living.humanoid.player.RespawnPlayerEvent; +import org.spongepowered.api.event.filter.cause.Root; +import org.spongepowered.api.event.network.ClientConnectionEvent; +import org.spongepowered.api.world.Location; +import org.spongepowered.api.world.World; +import us.myles.ViaVersion.SpongePlugin; +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.minecraft.Position; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; +import us.myles.ViaVersion.sponge.listeners.ViaSpongeListener; +import us.myles.ViaVersion.util.ReflectionUtil; + +import java.io.DataOutput; +import java.io.DataOutputStream; +import java.lang.reflect.Method; +import java.util.Optional; + +// TODO Change to bytebuf to not use reflection bullshit? +public class CommandBlockListener extends ViaSpongeListener { + public CommandBlockListener(SpongePlugin plugin) { + super(plugin, Protocol1_9TO1_8.class); + } + + @Listener + public void onJoin(ClientConnectionEvent.Join e) { + sendOp(e.getTargetEntity()); + } + + @Listener + public void onRespawn(RespawnPlayerEvent e) { + if (!isOnPipe(e.getTargetEntity().getUniqueId())) return; + + Sponge.getScheduler().createTaskBuilder().delayTicks(1).execute(new Runnable() { + @Override + public void run() { + sendOp(e.getTargetEntity()); + } + }).submit(getPlugin()); + } + + @Listener + public void onInteract(InteractBlockEvent e, @Root Player player) { + Optional> location = e.getTargetBlock().getLocation(); + if (!location.isPresent()) return; + Optional optTile = location.get().getTileEntity(); + if (!optTile.isPresent()) return; + TileEntity block = optTile.get(); + if (block instanceof CommandBlock) { + CommandBlock cmd = (CommandBlock) block; + try { + sendCommandBlockPacket(cmd, player); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } + +// TODO Change world +// @EventHandler(priority = EventPriority.MONITOR) +// public void onWorldChange(PlayerChangedWorldEvent e) { +// sendOp(e.getPlayer()); +// } + +// @EventHandler(ignoreCancelled = true) +// public void onInteract(PlayerInteractEvent e) { +// if (e.getAction() == Action.RIGHT_CLICK_BLOCK && isOnPipe(e.getPlayer()) && e.getPlayer().isOp()) { +// try { +// sendCommandBlockPacket(e.getClickedBlock(), e.getPlayer()); +// } catch (Exception ex) { +// ex.printStackTrace(); +// } +// } +// } + + private void sendOp(Player p) { + // TODO Is there an isOp check? + if (p.hasPermission("viaversion.commandblocks") && isOnPipe(p.getUniqueId())) { + try { + PacketWrapper wrapper = new PacketWrapper(0x1B, null, getUserConnection(p.getUniqueId())); // Entity status + + wrapper.write(Type.INT, getEntityId(p)); // Entity ID + wrapper.write(Type.BYTE, (byte) 26); //Hardcoded op permission level + + wrapper.send(Protocol1_9TO1_8.class); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private void sendCommandBlockPacket(CommandBlock b, Player player) throws Exception { + Method m = b.getClass().getDeclaredMethod("func_145844_m"); + m.setAccessible(true); + + Object updatePacket = m.invoke(b); + + PacketWrapper wrapper = generatePacket(updatePacket, getUserConnection(player.getUniqueId())); + wrapper.send(Protocol1_9TO1_8.class); + } + + // + private PacketWrapper generatePacket(Object updatePacket, UserConnection usr) throws Exception { + PacketWrapper wrapper = new PacketWrapper(0x09, null, usr); // Update block entity + + long[] pos = getPosition(ReflectionUtil.get(updatePacket, "field_179824_a", Class.forName("net.minecraft.util.BlockPos"))); + + wrapper.write(Type.POSITION, new Position(pos[0], pos[1], pos[2])); //Block position + wrapper.write(Type.BYTE, (byte) 2); // Action id always 2 + + CompoundTag nbt = getNBT(ReflectionUtil.get(updatePacket, "field_148860_e", Class.forName("net.minecraft.nbt.NBTTagCompound"))); + if (nbt == null) { + wrapper.write(Type.BYTE, (byte) 0); //If nbt is null. Use 0 as nbt + return wrapper; + } + nbt.put(new ByteTag("powered", (byte) 0)); + nbt.put(new ByteTag("auto", (byte) 0)); + nbt.put(new ByteTag("conditionMet", (byte) 0)); + + wrapper.write(Type.NBT, nbt); // NBT TAG + return wrapper; + } + + private long[] getPosition(Object obj) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { + return new long[]{ + (long) ReflectionUtil.getSuper(obj, "field_177962_a", int.class), //X + (long) ReflectionUtil.getSuper(obj, "field_177960_b", int.class), //Y + (long) ReflectionUtil.getSuper(obj, "field_177961_c", int.class) //Z + }; + } + + private CompoundTag getNBT(Object obj) throws Exception { + ByteBuf buf = Unpooled.buffer(); + Method m = Class.forName("net.minecraft.nbt.CompressedStreamTools").getMethod("func_74800_a", Class.forName("net.minecraft.nbt.NBTTagCompound"), DataOutput.class); + m.invoke(null, obj, new DataOutputStream(new ByteBufOutputStream(buf))); + try { + return Type.NBT.read(buf); + } finally { + buf.release(); + } + } +} diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/DeathListener.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/DeathListener.java new file mode 100644 index 000000000..eece82ce0 --- /dev/null +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/DeathListener.java @@ -0,0 +1,65 @@ +package us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8; + +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.Order; +import org.spongepowered.api.event.entity.DestructEntityEvent; +import org.spongepowered.api.world.World; +import us.myles.ViaVersion.SpongePlugin; +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; +import us.myles.ViaVersion.sponge.listeners.ViaSpongeListener; + +import java.util.Optional; + +public class DeathListener extends ViaSpongeListener { + public DeathListener(SpongePlugin plugin) { + super(plugin, Protocol1_9TO1_8.class); + } + + @Listener(order = Order.LAST) + public void onDeath(DestructEntityEvent.Death e) { + if (!(e.getTargetEntity() instanceof Player)) + return; + + Player p = (Player) e.getTargetEntity(); + if (isOnPipe(p.getUniqueId()) && Via.getConfig().isShowNewDeathMessages() && checkGamerule(p.getWorld())) { + sendPacket(p, e.getMessage().toPlain()); + } + } + + public boolean checkGamerule(World w) { + Optional gamerule = w.getGameRule("showDeathMessages"); + + if (gamerule.isPresent()) { + try { + return Boolean.parseBoolean(gamerule.get()); + } catch (Exception e) { + return false; + } + } + return false; + } + + private void sendPacket(final Player p, final String msg) { + Via.getPlatform().runSync(new Runnable() { + @Override + public void run() { + PacketWrapper wrapper = new PacketWrapper(0x2C, null, getUserConnection(p.getUniqueId())); + try { + int entityId = getEntityId(p); + wrapper.write(Type.VAR_INT, 2); // Event - Entity dead + wrapper.write(Type.VAR_INT, entityId); // Player ID + wrapper.write(Type.INT, entityId); // Entity ID + Protocol1_9TO1_8.FIX_JSON.write(wrapper, msg); // Message + + wrapper.send(Protocol1_9TO1_8.class); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } +}