Get creator of a block

Discussion in 'Plugin Development' started by Asgernohns, Jul 23, 2013.

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

    Asgernohns

    hello all Java coders :)

    Do anyone here no about how to get the creator of a block.
    I need this to make a doorbell plugin. It sends a message to
    the creator of the sign, about that there are somebody at the sign/door.

    somebody that has an example? :)
     
  2. Asgernohns
    HashMap<String, Location>
    String being the creator name, location being the sign location.

    Also you might want to look into metadata. :)
     
  3. Offline

    Asgernohns

    thanks Assist but how do i target String in a player object? :)
     
  4. Offline

    Chiller

    Asgernohns Just change String to Player in the HashMap?!
     
  5. Offline

    Asgernohns

    that isn't helping at all.
    did you understand my question Chiller?
     
  6. Offline

    soulofw0lf

    never advise someone to store a player object please!

    just store the player name which is a string. and you should honestly use the location as the key not the name, since you'll be using the location to find the player tied to it
     
  7. NEVER. Seriously.

    Asgernohns
    The player name.
     
  8. Offline

    Chiller

  9. Offline

    chasechocolate

    Memory leaks. If the player dies, logs out, etc.
     
    Assist likes this.
  10. Offline

    Chiller

  11. Offline

    Asgernohns

    okay i'm confussed.

    i try to send my code and can you guys so send me the solution. :)

    Here is the code!
    Code:java
    1. public class basic extends JavaPlugin {
    2.  
    3. public final Logger Logger = java.util.logging.Logger.getLogger("Minecraft");
    4. public static basic plugin;
    5. public final HashMap<Location, Player> signs =new HashMap<Location, Player>();
    6.  
    7. @Override
    8. public void onDisable() {
    9. PluginDescriptionFile pdfFile = this.getDescription();
    10. this.Logger.info(pdfFile.getName()
    11. + " ver. 0.01 beta has been disabled!");
    12. }
    13.  
    14. @Override
    15. public void onEnable() {
    16. PluginDescriptionFile pdfFile = this.getDescription();
    17. this.Logger.info(pdfFile.getName()
    18. + " ver. 0.01 beta has been enabled!");
    19.  
    20. }
    21.  
    22. @EventHandler
    23. public void onSignChange(SignChangeEvent e){
    24. Player p = e.getPlayer();
    25. if(e.getLine(0).equals("[DoorBell]")){
    26.  
    27. e.setLine(0, "["+ChatColor.YELLOW+"DoorBell"+ChatColor.BLACK+"]");
    28. String pn = e.getPlayer().getName();
    29. e.setLine(1, pn);
    30. p.sendMessage("Doorbell created Successfully!");
    31. }
    32. }
    33.  
    34. @EventHandler
    35. public void onBlockBreak(BlockBreakEvent e){
    36. Player p = e.getPlayer();
    37. if(p.isOp()){
    38. }else{
    39. e.setCancelled(true);
    40. }
    41. }
    42.  
    43. @EventHandler
    44. public void blockid(PlayerInteractEvent e){
    45. Player p = e.getPlayer();
    46.  
    47. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    48.  
    49. }
    50.  
    51. }
    52.  
    53.  
    54.  
    55.  
    56. }
     
  12. Offline

    chasechocolate

    Asgernohns did you read the above posts? Do not store the player instance in the HashMap unless you are willing to listen for many other events to prevent memory leaks. Store the player's name in a String.
     
  13. Offline

    Asgernohns

    ups chasechocolate put i didn't se that. now it says String again :)


    this Assist. i need help to understand what you're meaning.
    please send a code where you put the thing you meaning, into. :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  14. Ok.

    Put this
    Code:
    signs.addAll(signLocation, playerName);
    inside wherever you want to put it. So assuming that you want to add the sign creator on your SignChangeEvent, put this
    Code:
    signs.addAll(e.getLocation, pn);
    under your if(e.getLine(0) etc
    By the way, I'm not sure if SignChangeEvent has a getLocation method, if not, then fiddlepaddle with it.

    And then on your interact event, check if the clicked block is a sign, then compare the locations
    Code:
    if(e.getClickedBlock().getType() == Material.SIGN) { 
       Sign sign = (Sign) e.getClickedBlock();
       if(signs.containsKey(sign.getLocation()) {
            // send message
        }
    }
    I'm on my phone btw, so something might not be correct.
     
  15. Offline

    Asgernohns

    i got errors Assist on the two codes from the top (in your post), but the last one no errors.
    i got errors on the first one at SignLocaton and playername:
    SignLocation error is: SignLocation can not be changed to a variable
    playername error is: playername can not be changed to a variable
    And i got errors on the second one at e.getLocation() and pn:
    e.getLocation() error is: getLocation() is an undefined type in SignChangeEvent
    pn error is: pn can not be changed to a variable
     
  16. Asgernohns
    The first one was an example. Sorry, the code was a full fail, as I said, I was on my phone.

    Anyways, back to business. Put this inside your SignChangeEvent:
    Code:
    signs.put(e.getBlock().getLocation(), pn);
    Your new SignChangeEvent method should look something like this:
    Code:
        @EventHandler
        public void onSignChange(SignChangeEvent e) {
            Player p = e.getPlayer();
            if (e.getLine(0).equals("[DoorBell]")) {
                e.setLine(0, "[" + ChatColor.YELLOW + "DoorBell" + ChatColor.BLACK+ "]");
                String pn = e.getPlayer().getName();
                signs.put(e.getBlock().getLocation(), pn);
                e.setLine(1, pn);
                p.sendMessage("Doorbell created Successfully!");
            }
        }
    So now you added the location and player name to the list.

    Now put this inside your PlayerInteractEvent:
    Code:
                    if (e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.WALL_SIGN) {
                    Sign sign = (Sign) e.getClickedBlock();
                    if (signs.containsKey(sign.getLocation())) {
                        Player signCreator = Bukkit.getPlayerExact(signs.get(sign.getLocation()));
                        if (signCreator != null) {
                            signCreator.sendMessage(p.getName() + " IS RINGING DA BELL");
                        }
                    }
                }
    Your PlayerInteractEvent should look something like tis:
    Code:
        @EventHandler
        public void blockid(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.WALL_SIGN) {
                    Sign sign = (Sign) e.getClickedBlock();
                    if (signs.containsKey(sign.getLocation())) {
                        Player signCreator = Bukkit.getPlayerExact(signs.get(sign.getLocation()));
                        if (signCreator != null) {
                            signCreator.sendMessage(p.getName() + " IS RINGING DA BELL");
                        }
                    }
                }
            }
        }
    Everything is untested, but should work :p
     
Thread Status:
Not open for further replies.

Share This Page