From 0df7555cecff17ffab68b8280dcf8471b10f020c Mon Sep 17 00:00:00 2001 From: Patrick Seidel Date: Thu, 21 Mar 2013 16:51:35 -0400 Subject: [PATCH] Add method to send fake sign updates to players. Adds BUKKIT-2300 --- .../minecraft/server/PlayerConnection.java | 7 +---- .../net/minecraft/server/TileEntitySign.java | 26 +++++++++++-------- .../bukkit/craftbukkit/block/CraftSign.java | 22 +++++++++++----- .../craftbukkit/entity/CraftPlayer.java | 22 ++++++++++++++++ 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java index ca6b33c949..be2bca23e8 100644 --- a/src/main/java/net/minecraft/server/PlayerConnection.java +++ b/src/main/java/net/minecraft/server/PlayerConnection.java @@ -1583,12 +1583,7 @@ public class PlayerConnection implements PacketPlayInListener { this.server.getPluginManager().callEvent(event); if (!event.isCancelled()) { - for (int l = 0; l < 4; ++l) { - tileentitysign1.lines[l] = event.getLine(l); - if(tileentitysign1.lines[l] == null) { - tileentitysign1.lines[l] = ""; - } - } + tileentitysign1.lines = org.bukkit.craftbukkit.block.CraftSign.sanitizeLines(event.getLines()); tileentitysign1.isEditable = false; } // System.arraycopy(packetplayinupdatesign.f(), 0, tileentitysign1.lines, 0, 4); diff --git a/src/main/java/net/minecraft/server/TileEntitySign.java b/src/main/java/net/minecraft/server/TileEntitySign.java index c504dbe641..552919c4c5 100644 --- a/src/main/java/net/minecraft/server/TileEntitySign.java +++ b/src/main/java/net/minecraft/server/TileEntitySign.java @@ -30,17 +30,7 @@ public class TileEntitySign extends TileEntity { } public Packet getUpdatePacket() { - String[] astring = new String[4]; - - // CraftBukkit start - Limit sign text to 15 chars per line - for (int i = 0; i < 4; ++i) { - astring[i] = this.lines[i]; - - if (this.lines[i].length() > 15) { - astring[i] = this.lines[i].substring(0, 15); - } - } - // CraftBukkit end + String[] astring = sanitizeLines(this.lines); // CraftBukkit - call sign line sanitizer to limit line length return new PacketPlayOutUpdateSign(this.x, this.y, this.z, astring); } @@ -56,4 +46,18 @@ public class TileEntitySign extends TileEntity { public EntityHuman b() { return this.k; } + + // CraftBukkit start - central method to limit sign text to 15 chars per line + public static String[] sanitizeLines(String[] lines) { + String[] astring = new String[4]; + for (int i = 0; i < 4; ++i) { + astring[i] = lines[i]; + + if (lines[i].length() > 15) { + astring[i] = lines[i].substring(0, 15); + } + } + return astring; + } + // CraftBukkit end } diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java b/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java index 16471009fa..77717d5b2b 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java @@ -35,16 +35,24 @@ public class CraftSign extends CraftBlockState implements Sign { boolean result = super.update(force, applyPhysics); if (result) { - for(int i = 0; i < 4; i++) { - if(lines[i] != null) { - sign.lines[i] = lines[i]; - } else { - sign.lines[i] = ""; - } - } + sign.lines = sanitizeLines(lines); sign.update(); } return result; } + + public static String[] sanitizeLines(String[] lines) { + String[] astring = new String[4]; + + for (int i = 0; i < 4; i++) { + if (i < lines.length && lines[i] != null) { + astring[i] = lines[i]; + } else { + astring[i] = ""; + } + } + + return TileEntitySign.sanitizeLines(astring); + } } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 4c19370980..17c16dc692 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -31,6 +31,7 @@ import org.bukkit.configuration.serialization.DelegateDeserialization; import org.bukkit.conversations.Conversation; import org.bukkit.conversations.ConversationAbandonedEvent; import org.bukkit.conversations.ManuallyAbandonedConversationCanceller; +import org.bukkit.craftbukkit.block.CraftSign; import org.bukkit.craftbukkit.conversations.ConversationTracker; import org.bukkit.craftbukkit.CraftEffect; import org.bukkit.craftbukkit.CraftOfflinePlayer; @@ -368,6 +369,27 @@ public class CraftPlayer extends CraftHumanEntity implements Player { getHandle().playerConnection.sendPacket(packet); } + @Override + public void sendSignChange(Location loc, String[] lines) { + if (getHandle().playerConnection == null) { + return; + } + + if (lines == null) { + lines = new String[4]; + } + + Validate.notNull(loc, "Location can not be null"); + if (lines.length < 4) { + throw new IllegalArgumentException("Must have at least 4 lines"); + } + + // Limit to 15 chars per line and set null lines to blank + String[] astring = CraftSign.sanitizeLines(lines); + + getHandle().playerConnection.sendPacket(new PacketPlayOutUpdateSign(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), astring)); + } + @Override public boolean sendChunkChange(Location loc, int sx, int sy, int sz, byte[] data) { if (getHandle().playerConnection == null) return false;