From 31546ddf8e77e538c19ceb0750cced1abba5b5fb Mon Sep 17 00:00:00 2001
From: CraftBukkit/Spigot <noreply+git-craftbukkit@papermc.io>
Date: Sat, 8 Jan 2011 01:22:17 +0000
Subject: [PATCH] Implemented BlockState.update(boolean), signs should now
 work, cleaned up some code a little

By: Dinnerbone <dinnerbone@dinnerbone.com>
---
 .../org/bukkit/craftbukkit/CraftBlock.java    | 12 ++++++-
 .../org/bukkit/craftbukkit/CraftBoat.java     | 36 +++++++++----------
 .../org/bukkit/craftbukkit/CraftMappable.java | 32 ++++++++---------
 .../org/bukkit/craftbukkit/CraftWorld.java    |  5 +++
 .../craftbukkit/block/CraftBlockState.java    | 26 ++++++++------
 .../bukkit/craftbukkit/block/CraftSign.java   | 31 +++++++++++-----
 6 files changed, 89 insertions(+), 53 deletions(-)

diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBlock.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBlock.java
index 204a25ebb9..1650e972b6 100644
--- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBlock.java
+++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBlock.java
@@ -4,6 +4,7 @@ package org.bukkit.craftbukkit;
 import org.bukkit.*;
 import org.bukkit.block.BlockState;
 import org.bukkit.craftbukkit.block.CraftBlockState;
+import org.bukkit.craftbukkit.block.CraftSign;
 
 public class CraftBlock implements Block {
     private final CraftWorld world;
@@ -253,6 +254,15 @@ public class CraftBlock implements Block {
     }
 
     public BlockState getState() {
-        return new CraftBlockState(world, x, y, z, type, data);
+        Material material = getType();
+
+        switch (material) {
+            case Sign:
+            case SignPost:
+            case WallSign:
+                return new CraftSign(this);
+            default:
+                return new CraftBlockState(this);
+        }
     }
 }
diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBoat.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBoat.java
index dd3acb62df..cad1e40779 100644
--- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBoat.java
+++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBoat.java
@@ -1,18 +1,18 @@
-package org.bukkit.craftbukkit;
-
-import net.minecraft.server.EntityBoat;
-import org.bukkit.Boat;
-
-/**
- * A minecart.
- * 
- * @author sk89q
- */
-public class CraftBoat extends CraftVehicle implements Boat {
-    protected EntityBoat boat;
-
-    public CraftBoat(CraftServer server, EntityBoat entity) {
-        super(server, entity);
-        boat = entity;
-    }
-}
+package org.bukkit.craftbukkit;
+
+import net.minecraft.server.EntityBoat;
+import org.bukkit.Boat;
+
+/**
+ * A minecart.
+ * 
+ * @author sk89q
+ */
+public class CraftBoat extends CraftVehicle implements Boat {
+    protected EntityBoat boat;
+
+    public CraftBoat(CraftServer server, EntityBoat entity) {
+        super(server, entity);
+        boat = entity;
+    }
+}
diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftMappable.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftMappable.java
index 9bcc4b9936..8e411846bd 100644
--- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftMappable.java
+++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftMappable.java
@@ -1,16 +1,16 @@
-package org.bukkit.craftbukkit;
-
-/**
- * Indicates that an object has a method to get its CraftBukkit-equivalent
- * CraftEntity object from its Minecraft net.minecraft.server.Entity object.
- * 
- * @author sk89q
- */
-public interface CraftMappable {
-    /**
-     * Gets the CraftEntity version.
-     * 
-     * @return
-     */
-    public CraftEntity getCraftEntity();
-}
+package org.bukkit.craftbukkit;
+
+/**
+ * Indicates that an object has a method to get its CraftBukkit-equivalent
+ * CraftEntity object from its Minecraft net.minecraft.server.Entity object.
+ * 
+ * @author sk89q
+ */
+public interface CraftMappable {
+    /**
+     * Gets the CraftEntity version.
+     * 
+     * @return
+     */
+    public CraftEntity getCraftEntity();
+}
diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index fdfcaea5d3..3631d13294 100644
--- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -14,6 +14,7 @@ import net.minecraft.server.EntityPlayerMP;
 import net.minecraft.server.EntitySnowball;
 import net.minecraft.server.EntityArrow;
 import net.minecraft.server.EntityPlayer;
+import net.minecraft.server.TileEntity;
 import net.minecraft.server.WorldGenBigTree;
 import net.minecraft.server.WorldServer;
 import net.minecraft.server.WorldGenTrees;
@@ -185,6 +186,10 @@ public class CraftWorld implements World {
         }
     }
 
+    public TileEntity getTileEntityAt(final int x, final int y, final int z) {
+        return world.l(x, y, z);
+    }
+
     @Override
     public String toString() {
         return "CraftWorld";
diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java
index 0b216be149..f2a0c2756d 100644
--- a/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java
+++ b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java
@@ -19,15 +19,15 @@ public class CraftBlockState implements BlockState {
     protected byte data;
     protected byte light;
 
-    public CraftBlockState(final CraftWorld world, final int x, final int y, final int z, final int type, final byte data) {
-        this.world = world;
-        this.x = x;
-        this.y = y;
-        this.z = z;
-        this.type = type;
-        this.data = data;
-        this.light = (byte)world.getHandle().i(x, y, z);
-        this.chunk = (CraftChunk)world.getChunkAt(x << 4, z << 4);
+    public CraftBlockState(final Block block) {
+        this.world = (CraftWorld)block.getWorld();
+        this.x = block.getX();
+        this.y = block.getY();
+        this.z = block.getZ();
+        this.type = block.getTypeID();
+        this.data = block.getData();
+        this.light = block.getLightLevel();
+        this.chunk = (CraftChunk)block.getChunk();
     }
 
     /**
@@ -153,8 +153,14 @@ public class CraftBlockState implements BlockState {
 
         synchronized (block) {
             if (block.getType() != this.getType()) {
-                return false;
+                if (force) {
+                    block.setTypeID(this.getTypeID());
+                } else {
+                    return false;
+                }
             }
+
+            block.setData(data);
         }
 
         return true;
diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java
index ec606a7572..a3a8649b93 100644
--- a/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java
+++ b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java
@@ -1,27 +1,42 @@
 
 package org.bukkit.craftbukkit.block;
 
+import net.minecraft.server.TileEntitySign;
 import org.bukkit.Block;
-import org.bukkit.Chunk;
-import org.bukkit.Material;
-import org.bukkit.World;
 import org.bukkit.block.Sign;
 import org.bukkit.craftbukkit.CraftWorld;
 
 public class CraftSign extends CraftBlockState implements Sign {
-    public CraftSign(final CraftWorld world, final int x, final int y, final int z, final int type, final byte data) {
-        super(world, x, y, z, type, data);
+    private final CraftWorld world;
+    private final TileEntitySign sign;
+
+    public CraftSign(final Block block) {
+        super(block);
+
+        world = (CraftWorld)block.getWorld();
+        sign = (TileEntitySign)world.getTileEntityAt(getX(), getY(), getZ());
     }
 
     public String[] getLines() {
-        throw new UnsupportedOperationException("Not supported yet.");
+        return sign.e;
     }
 
     public String getLine(int index) throws IndexOutOfBoundsException {
-        throw new UnsupportedOperationException("Not supported yet.");
+        return sign.e[index];
     }
 
     public void setLine(int index, String line) throws IndexOutOfBoundsException {
-        throw new UnsupportedOperationException("Not supported yet.");
+        sign.e[index] = line;
+    }
+
+    @Override
+    public boolean update(boolean force) {
+        boolean result = super.update(force);
+
+        if (result) {
+            sign.d();
+        }
+
+        return result;
     }
 }