Sign Help.

Discussion in 'Plugin Development' started by -_Husky_-, Feb 23, 2012.

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

    -_Husky_-

    Code:
    package husky.code;
    import java.io.File;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    public class Listen implements Listener {
        YamlConfiguration config = YamlConfiguration.loadConfiguration(new File("plugins/ModVote/config.yml"));
        public Listen(main plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);   
        }
     
        @EventHandler
        public void click(PlayerInteractEvent ev) {
            int counter = 0;
            Player p = ev.getPlayer();
            String pname = p.getName();
            if ((ev.getClickedBlock() instanceof Sign)) {
                  Sign sign = (Sign) ev.getClickedBlock();
                  String playername = sign.getLine(2);
                  if(sign.getLine(1).equalsIgnoreCase("[ModVote]")) {
                      if(counter == 0) {
                          if(!config.contains(pname + "-Voted_for-" + playername)) {
                            sign.setLine(4, "1");
                            counter++;
                            config.set(pname, "-Voted_For-" + playername);
                          }
                            } else if(counter == 1) {
                                if(!config.contains(pname + "-Voted_for-" + playername)) {
                                sign.setLine(4, "2");
                                counter++;
                                config.set(pname, "-Voted_For-" + playername);
                                }
                            } else if(counter == 2) {
                                if(!config.contains(pname + "-Voted_for-" + playername)) {
                                sign.setLine(4, "3");
                                counter++;
                                config.set(pname, "-Voted_For-" + playername);
                                }
                            } else if(counter == 3) {
                                if(!config.contains(pname + "-Voted_for-" + playername)) {
                                sign.setLine(4, "4");
                                counter++;
                                config.set(pname, "-Voted_For-" + playername);
                                }
                            } else if(counter == 4) {
                                if(!config.contains(pname + "-Voted_for-" + playername)) {
                                sign.setLine(4, "5");
                                counter++;
                                config.set(pname, "-Voted_For-" + playername);
                                Bukkit.getServer().broadcastMessage(ChatColor.GREEN + playername + " Has Just Reached 5 ModVotes!");
                                }
                            } else if(counter == 5) {
                                if(!config.contains(pname + "-Voted_for-" + playername)) {
                                sign.setLine(4, "6");
                                counter++;
                                config.set(pname, "-Voted_For-" + playername);
                                }
                            } else if(counter == 6) {
                                if(!config.contains(pname + "-Voted_for-" + playername)) {
                                sign.setLine(4, "7");
                                counter++;
                                config.set(pname, "-Voted_For-" + playername);
                                }
                            } else if(counter == 7) {
                                if(!config.contains(pname + "-Voted_for-" + playername)) {
                                sign.setLine(4, "8");
                                counter++;
                                config.set(pname, "-Voted_For-" + playername);
                                }
                            } else if(counter == 8) {
                                if(!config.contains(pname + "-Voted_for-" + playername)) {
                                sign.setLine(4, "9");
                                counter++;
                                config.set(pname, "-Voted_For-" + playername);
                                }
                            } else if(counter == 9) {
                                if(!config.contains(pname + "-Voted_for-" + playername)) {
                                sign.setLine(4, "10");
                                config.set(pname, "-Voted_For-" + playername);
                                Bukkit.getServer().broadcastMessage(ChatColor.GREEN + playername + " Has Just Reached 10 ModVotes!");
                        }
                    }
                }
            }
        }
        @EventHandler
        public void change(SignChangeEvent e) {
            Block b = e.getBlock();
            if(b instanceof Sign) {
                Sign sign = (Sign)e.getBlock();
                Player p = e.getPlayer();
                if(sign.getLine(1).equalsIgnoreCase("[ModVote]") && sign.getLine(2).isEmpty()) {
                    p.sendMessage(ChatColor.RED + "Error Making a ModVote Sign");
                } else {
                    sign.setLine(4, "0");
                    p.sendMessage(ChatColor.GREEN + "ModVote Created");
                }
            }
           
        }
        @EventHandler
        public void Place(BlockPlaceEvent e) {
            Block b = e.getBlock();
            if(b instanceof Sign) {
                Sign sign = (Sign)e.getBlockPlaced();
                Player p = e.getPlayer();
                if(sign.getLine(1).equalsIgnoreCase("[ModVote]")) {
                    sign.setLine(4, "0");
                    p.sendMessage(ChatColor.GREEN + "ModVote Created");
                } else {
                    p.sendMessage(ChatColor.RED + "Error Making a ModVote Sign");
                }
            }
        }
    }
    that's what i've done, but, no errors. nothing happens
     
  2. Offline

    tehbeard

    you need to update the blockstate using sign.update()

    Blockstate can be thought of as a snapshot of the state of block, since you are operating on the snapshot, you must sync it back to the actual block.
     
  3. Offline

    -_Husky_-

    Ok, done, still nothing sadly.
     
  4. Offline

    Bone008

    The Sign you get of the BlockState of the block of the event (<-- sounds weird, yeah) is always empty for BlockPlaceEvent and SignChangeEvent.
    BlockPlaceEvent is called when you right-click to place down the sign, then the input dialog for the text is shown, and at the same time the event is launched - so no chance for any text to be displayed.
    SignChangeEvent is what you are looking for, however, the even is launched before the changes are applied to the sign (because part of the point of the event is to be able to change what will be written on it).
    That's why event.getBlock().getState() gives you a Sign with empty lines.
    There are methods like event.getLine(int) and event.getLines() ... for you to use the actual text being written on the sign.

    I think you'll figure out the rest ;)
     
  5. Offline

    -_Husky_-

    Ive got that, its just not displaying the info
     
  6. Offline

    Bone008

    No, you need to use the lines provided by event.getLine(...), not coming from the sign you got from the block.
    Apart from that, event.getBlock() will never be instanceof "Sign", because Sign extends BlockState. You'd need event.getBlock().getState(). In your SignChangeEvent, you don't need that at all, though, as I am trying to explain - because you don't use the Block/BlockState/Sign methods but the event methods.
     
  7. Offline

    -_Husky_-

    ahhh, thanks for that, explained a lot

    righto, got the other things working, Just got a Issue with Interact Now, I can't actually GET the Lines, Nor Set them,
    Event.getClickedBlock();
    Doesen't Support Signs. :/
    Anyone?

    bergerkiller
    I've seen you've done some things with sign's care to help out?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  8. Offline

    ShootToMaim

    What do you mean it doesn't "support" signs?
    :D
     
  9. Offline

    -_Husky_-

    it's Not, supporting the method .getLine(int)


    anyway, i passed that hill, now the next one is:
    every time i break a block, it comes up with a stack trace pointing to Line 37 being:
    Sign s = (Sign) b.getState();
    Yet that actually outputs Words onto the sign...
     
  10. Offline

    Bone008

    Okay, I didn't really look at that part of your code. The point of "the event supporting signs" is only important in SignChangeEvent - because that's where the text is not yet on the sign. When you right-click the sign, the text is there before the event, so you can use sign.getLine/-s and don't have to (or rather you can't) use event.getLine/-s.

    The thing why that isn't working was the fact that Sign doesn't extend Block, but BlockState, so your instanceof if thing fails:
     
  11. Offline

    -_Husky_-

    Ok, got it all going ;3. now, I need to register the counter PER sign, etc

    One player clicks one sign. it doesn't go for the other one
    I'm Not keen on making 1million different Ints...
     
Thread Status:
Not open for further replies.

Share This Page