Solved Check if Player is Line of Sign

Discussion in 'Plugin Help/Development/Requests' started by BrickBoy55, Apr 3, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    BrickBoy55

    So, I'm making a protection plugin, and people can protect land in a 25 radius using a sign. The problem is that people can still break in the 25 block radius from the sign. Help is appreciated!

    Code:
    @EventHandler
        public void onBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            int radius = 25;
            Location loc= player.getLocation();
           
            for(int x = (int) (loc.getX() - radius); x < loc.getX() + radius; x++ ) {
                 for(int y = (int) (loc.getY() - radius); y < loc.getY() + radius; y++ ) {
                     for (int z = (int) (loc.getZ() - radius); z < loc.getZ() + radius; z++ ) {
                         
                         Block block = player.getWorld().getBlockAt(x, y, z);
                         
                         if (block.getType().equals(Material.WALL_SIGN) || block.getType().equals(Material.WALL_SIGN) ) {
                             Sign sign = (Sign) player.getWorld().getBlockAt(x, y, z);
                             
                              if (sign.getLine(0).equals("§8[§9Protected§8]")) {
                                  if (!player.getName().equals(sign.getLine(1))) {
                                      event.setCancelled(true);
                                  }
                              }
                         }
                     }
                 }
            }
        }
    
    There is an error, but I don't know where it is, considering my class name isn't in it.

    Show Spoiler

    [22:13:24] [Server thread/ERROR]: Could not pass event BlockBreakEvent to Protect v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:297) ~[craftbukkit.jar:git-Bukkit-0cf233d]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-0cf233d]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-0cf233d]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-0cf233d]
    at net.minecraft.server.v1_8_R1.PlayerInteractManager.breakBlock(PlayerInteractManager.java:285) [craftbukkit.jar:git-Bukkit-0cf233d]
    at net.minecraft.server.v1_8_R1.PlayerInteractManager.a(PlayerInteractManager.java:214) [craftbukkit.jar:git-Bukkit-0cf233d]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:587) [craftbukkit.jar:git-Bukkit-0cf233d]
    at net.minecraft.server.v1_8_R1.PacketPlayInBlockDig.a(SourceFile:40) [craftbukkit.jar:git-Bukkit-0cf233d]
    at net.minecraft.server.v1_8_R1.PacketPlayInBlockDig.a(SourceFile:10) [craftbukkit.jar:git-Bukkit-0cf233d]
    at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-0cf233d]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_55]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_55]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:645) [craftbukkit.jar:git-Bukkit-0cf233d]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit.jar:git-Bukkit-0cf233d]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:598) [craftbukkit.jar:git-Bukkit-0cf233d]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:506) [craftbukkit.jar:git-Bukkit-0cf233d]
    at java.lang.Thread.run(Thread.java:745) [?:1.7.0_55]
    Caused by: java.lang.ClassCastException


    I don't know if the loop is messed up, or the block itself.
     
  2. Offline

    justin_393

    Caused by: java.lang.ClassCastException
    Error when Casting Sign sign = (Sign) player.getWorld().getBlockAt(x, y, z);
     
  3. Offline

    BrickBoy55

    @justin_393

    Thanks, but how do I make sure it's a sign though, and then read the name.
     
  4. Offline

    justin_393

    if (player.getWorld().getBlockAt(x,y,z).getType() == Material.SIGN_POST) {
    //your code here
    }
     
  5. Offline

    BrickBoy55

    @justin_393

    Sorry, I mean how do I make a variable since I already checked that its a sign post/wall sign, that I could use getLine() for.
     
  6. Offline

    justin_393

    I honestly have no clue, I've never messed with Signs :/
    Maybe @nverdier would know?
     
  7. Offline

    nverdier

    @BrickBoy55 Once you've assured it's a sign, just cast the block to a sign.
     
  8. Offline

    justin_393

    @nverdier That's what he did, but it was throwing out a java.lang.ClassCastException exception

    @BrickBoy55
    Typo? if (block.getType().equals(Material.WALL_SIGN) || block.getType().equals(Material.WALL_SIGN) )

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  9. Offline

    nverdier

    @justin_393 @BrickBoy55 Oh, you have to cast the block's state (i.e. Block#getState()) to Sign. Sorry for my forgetfulness, I suppose.
     
  10. Offline

    justin_393

    ah, that makes sense. I'll have to remember that if I ever mess with Signs.
     
  11. Offline

    BrickBoy55

    @nverdier

    Code:
    Block block = player.getWorld().getBlockAt(x, y, z);
                         
    if (block.getType().equals(Material.WALL_SIGN) || block.getType().equals(Material.WALL_SIGN) ) {
                 Sign sign = (Sign) block.getState();
    
    Would that work? I don't have anyone to test it with right now :/

    I got it working:

    Solution:
    Code:
        @EventHandler
        public void onBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            int radius = 25;
            Location loc= player.getLocation();
           
            for(int x = (int) (loc.getX() - radius); x < loc.getX() + radius; x++ ) {
                 for(int y = (int) (loc.getY() - radius); y < loc.getY() + radius; y++ ) {
                     for (int z = (int) (loc.getZ() - radius); z < loc.getZ() + radius; z++ ) {
                         
                         Block block = player.getWorld().getBlockAt(x, y, z);
                         
                         if (block.getState() instanceof Sign) {
                             Sign sign = (Sign) block.getState();
                             
                              if (sign.getLine(0).equals("§8[§9Protected§8]")) {
                                  if (!player.getName().equals(ChatColor.stripColor(sign.getLine(1)))) {
                                      event.setCancelled(true);
                                  }
                              }
                         }
                     }
                 }
            }
        }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page