From dc4b5b4d354014c9834c02ab54a96f98dc25d99c Mon Sep 17 00:00:00 2001 From: Mats Date: Tue, 12 Apr 2016 13:01:37 +0200 Subject: [PATCH] Possibility to show the new 1.9 death messages (#348) --- .../us/myles/ViaVersion/ViaVersionPlugin.java | 5 ++ .../ViaVersion/api/ViaVersionConfig.java | 7 ++ .../protocol1_9to1_8/Protocol1_9TO1_8.java | 6 +- .../listeners/DeathListener.java | 65 +++++++++++++++++++ src/main/resources/config.yml | 2 + 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/DeathListener.java diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 9ee9b79c8..0437c13af 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -360,6 +360,11 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe return getConfig().getBoolean("use-new-effect-indicator", true); } + @Override + public boolean isShowNewDeathMessages() { + return getConfig().getBoolean("use-new-deathmessages", false); + } + public boolean isSuppressMetadataErrors() { return getConfig().getBoolean("suppress-metadata-errors", false); } diff --git a/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java b/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java index 6f4055505..378d1ceef 100644 --- a/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java +++ b/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java @@ -30,6 +30,13 @@ public interface ViaVersionConfig { */ boolean isNewEffectIndicator(); + /** + * Get if 1.9 clients are shown the new death message on the death screen + * + * @return true if enabled + */ + boolean isShowNewDeathMessages(); + /** * Get if metadata errors will be suppressed * diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java index 31e20fb50..00c3208be 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java @@ -15,10 +15,7 @@ import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.base.ProtocolInfo; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners.ArmorListener; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners.BlockListener; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners.CommandBlockListener; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners.PaperPatch; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners.*; import us.myles.ViaVersion.protocols.protocol1_9to1_8.packets.*; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.ClientChunks; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; @@ -99,6 +96,7 @@ public class Protocol1_9TO1_8 extends Protocol { ViaVersionPlugin plugin = (ViaVersionPlugin) Bukkit.getPluginManager().getPlugin("ViaVersion"); Bukkit.getPluginManager().registerEvents(new ArmorListener(plugin), plugin); Bukkit.getPluginManager().registerEvents(new CommandBlockListener(plugin), plugin); + Bukkit.getPluginManager().registerEvents(new DeathListener(plugin), plugin); Bukkit.getPluginManager().registerEvents(new BlockListener(plugin), plugin); if (Bukkit.getVersion().toLowerCase().contains("paper")) { plugin.getLogger().info("Enabling PaperSpigot patch: Fixes block placement."); diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/DeathListener.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/DeathListener.java new file mode 100644 index 000000000..70d856c09 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/DeathListener.java @@ -0,0 +1,65 @@ +package us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners; + +import lombok.RequiredArgsConstructor; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.PlayerDeathEvent; +import us.myles.ViaVersion.ViaVersionPlugin; +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.base.ProtocolInfo; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; + +@RequiredArgsConstructor +public class DeathListener implements Listener { + private final ViaVersionPlugin plugin; + + @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) + public void onDeath(PlayerDeathEvent e) { + Player p = e.getEntity(); + if (plugin.isShowNewDeathMessages() && checkGamerule(p.getWorld()) && e.getDeathMessage() != null && checkPipeline(p)) { + sendPacket(p, e.getDeathMessage()); + } + } + + public boolean checkPipeline(Player p) { + UserConnection userConnection = plugin.getConnection(p); + return userConnection != null && userConnection.get(ProtocolInfo.class).getPipeline().contains(Protocol1_9TO1_8.class); + } + + private UserConnection getUserConnection(Player p) { + return plugin.getConnection(p); + } + + public boolean checkGamerule(World w) { + try { + return Boolean.parseBoolean(w.getGameRuleValue("showDeathMessage")); + } catch (Exception e) { + return false; + } + } + + private void sendPacket(final Player p, final String msg) { + Bukkit.getScheduler().runTask(plugin, new Runnable() { + @Override + public void run() { + PacketWrapper wrapper = new PacketWrapper(0x2C, null, getUserConnection(p)); + try { + wrapper.write(Type.VAR_INT, 2); + wrapper.write(Type.VAR_INT, p.getEntityId()); + wrapper.write(Type.INT, p.getEntityId()); + Protocol1_9TO1_8.FIX_JSON.write(wrapper, msg); + wrapper.send(); + } catch (Exception e) { + e.printStackTrace(); + wrapper.clearInputBuffer(); + } + } + }); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e8974881a..1a318b0c3 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -22,6 +22,8 @@ bossbar-patch: true bossbar-anti-flicker: false # This will show the new effect indicator in the top-right corner for 1.9 players. use-new-effect-indicator: true +# Show the new death messages for 1.9 on the death screen +use-new-deathmessages: false # This will suppress the following error: 'Unable to get entity for ID: xxxx' # This error message means one of you plugins is sending bad packets! suppress-entityid-errors: false \ No newline at end of file