Message to PLAYER, not BROADCAST!

Discussion in 'Plugin Development' started by Marius A. Winsjansen, Sep 23, 2011.

Thread Status:
Not open for further replies.
  1. Greetings,

    I am currently working on my first plugin, and i wonder if some1 can help me whit one code!

    The plugin is working great, i just need one more thing to happen!

    If you look at the code first i can explain:


    Code:
    public class BL extends BlockListener {
        public void onBlockPlace(BlockPlaceEvent event){
            Player p = event.getPlayer();
            String playername = p.getName();
            Server s = p.getServer();
            if(event.getBlock().getType() == Material.LAVA)
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " LAVA");
            if(event.getBlock().getType() == Material.TNT)
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " TNT!");
            if(event.getBlock().getType() == Material.FIRE)
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " FIRE!");
    Now, this plugin broadcast if a player place lava/tnt/fire, BUT i whant it to send a message to the player to, not showing up for everyone!

    I just need the code for that, hope some1 can help!
     
  2. Offline

    Darkman2412

    Change every s.broadcastMessage to p.sendMessage. Like this:
    Code:
    if(event.getBlock().getType() == Material.LAVA)
            p.sendMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " LAVA");
    if(event.getBlock().getType() == Material.TNT)
            p.sendMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " TNT!");
    if(event.getBlock().getType() == Material.FIRE)
            p.sendMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " FIRE!");
    
     
  3. Offline

    Brain

    Player.sendMessage

    @Darkman2412 was faster... this time!
     
  4. Offline

    Darkman2412

    :]
     
  5. I dont get it to work:/

    Can you make a player message to the first IF statment for me and send me it? :(

    But i wnat it to broadcast too! and ALSO send message to player!

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

    Brain

    Darkman2412 already gave you a piece of code, and it looks like it would work.
    Personal opinion: I would convert the if clauses into a switch statement.
     
  7. Offline

    Darkman2412

    You can only use switch statement with integer constants or literals.

    Edit: I think that. :)
     
  8. @Darkman2412

    @Brain

    I think i explained what i whanted a little bad!

    What i need is the plugin to broadcast a messeages, AND sending a messages to the player that placed lava!

    Here is my code so far, that dont work (it gives the player message to the player on whatever block he place, and it gives all three player messages at the same time)


    Code:
    public class BL extends BlockListener {
        public void onBlockPlace(BlockPlaceEvent event){
            Player p = event.getPlayer();
            String playername = p.getName();
            Server s = p.getServer();
            if(event.getBlock().getType() == Material.LAVA)
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " LAVA")
            p.sendMessage(ChatColor.RED + playername + ", its NOT allowed to place LAVA! You need to stop or else you will be banned from this server, OKEY?!");
            if(event.getBlock().getType() == Material.TNT)
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " TNT!")
            p.sendMessage(ChatColor.RED + playername + ", its NOT allowed to place TNT! You need to stop or else you will be banned from this server, OKEY?!");
            if(event.getBlock().getType() == Material.FIRE)
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " FIRE!")
            p.sendMessage(ChatColor.RED + playername + ", its NOT allowed to place FIRE! You need to stop or else you will be banned from this server, OKEY?!");
            }
     
  9. Offline

    DirtyStarfish

    Shouldn't you use else if?
    Code:
    public class BL extends BlockListener {
        public void onBlockPlace(BlockPlaceEvent event){
            Player p = event.getPlayer();
            String playername = p.getName();
            Server s = p.getServer();
            if(event.getBlock().getType() == Material.LAVA) {
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " LAVA")
            p.sendMessage(ChatColor.RED + playername + ", its NOT allowed to place LAVA! You need to stop or else you will be banned from this server, OKEY?!");
           } else  if (event.getBlock().getType() == Material.TNT) {
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " TNT!")
            p.sendMessage(ChatColor.RED + playername + ", its NOT allowed to place TNT! You need to stop or else you will be banned from this server, OKEY?!");
            } else if(event.getBlock().getType() == Material.FIRE) {
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " FIRE!")
            p.sendMessage(ChatColor.RED + playername + ", its NOT allowed to place FIRE! You need to stop or else you will be banned from this server, OKEY?!");
            }
     
  10. Offline

    Darkman2412

    Little fix @DirtyStarfish :
    Code:
    public class BL extends BlockListener {
        public void onBlockPlace(BlockPlaceEvent event){
            Player p = event.getPlayer();
            String playername = p.getName();
            Server s = p.getServer();
            if(event.getBlock().getType() == Material.LAVA) {
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " LAVA")
            p.sendMessage(ChatColor.RED + playername + ", its NOT allowed to place LAVA! You need to stop or else you will be banned from this server, OKEY?!");
           } else  if (event.getBlock().getType() == Material.TNT) {
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " TNT!")
            p.sendMessage(ChatColor.RED + playername + ", its NOT allowed to place TNT! You need to stop or else you will be banned from this server, OKEY?!");
            } else if(event.getBlock().getType() == Material.FIRE) {
            s.broadcastMessage(ChatColor.RED + playername + ChatColor.YELLOW + " has placed" + ChatColor.RED + " FIRE!")
            p.sendMessage(ChatColor.RED + playername + ", its NOT allowed to place FIRE! You need to stop or else you will be banned from this server, OKEY?!");
            } // <-- Close the last if
    }
     
  11. Offline

    Brain

    Material is an enum and you can use switch on enums.
     
  12. Offline

    Darkman2412

    :cool:
     
  13. @Darkman2412

    hmm it says i need to have an semicolon after the broadcast message... why?:/

    Thank you all for helping me btw, i am REALLY happy you do!
     
  14. Offline

    Brain

    Because every Java command has to be followed by a semicolon and Darkman2412 just forgot them. So after putting them in everything should be fine.
     
  15. Offline

    Darkman2412

    Actually, you forgot them ;)
    I just copied you and added the last }
     
  16. Still giving me an syntex error:/
     
  17. Offline

    Kohle

    Are you defining 'p' as player?
     
  18. Yeah, the plugin works in it self, it was just that it gave the messege to the player no mather what block he placed!

    But i fix that, it was the semicolon, but the eclipse didnt understand that i put it there after i did! Some bug!

    The plugin works great now, JUST the way i whant it to!

    My first plugin for bukkit is DONE :D

    Thanks all ! :D

    Someone have any idea of some more things i can make in this plugin (not hard stuff) but so i can learn more? :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
Thread Status:
Not open for further replies.

Share This Page