Solved Broadcast message + play sound when specific player joins

Discussion in 'Plugin Development' started by 4non, Jul 3, 2013.

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

    4non

    I'm going to be running a server where one of my admins owns a black market where you can find rare items for cheap, but you can only buy from the shop when he's online. When he joins the server, I want a message to be broadcasted, and I want the wither spawning noise to play. I'm very new with coding, and I want either a plugin like this, or a way to make one. I've researched a bit about this plugin, and I've managed to come up with this code:
    Code:java
    1. package net.malicepvp.run;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Sound;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.Event;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.EventPriority;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.player.PlayerLoginEvent;
    13.  
    14. public class listeners implements Listener {
    15. @EventHandler (priority = EventPriority.HIGH)
    16. public void onPlayerLogin(PlayerLoginEvent event) {
    17. if(event.getPlayer().getName().equals("Skeleton_o_Dhuum")){
    18. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "He has arrived...");
    19. for(Player p : Bukkit.getOnlinePlayers()){
    20. p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
    21. }
    22. }
    23.  
    24. }
    25. }

    When my friend, Skeleton_o_Dhuum, who will be the owner of the market, joins the server, nothing happens. I probably committed a very obvious error, but like I said, I'm new to this stuff.
     
  2. Offline

    iFamasssxD

    You need to have your listener class registered in your main class file.
    Needs to be in onEnable()
    Code:
    getServer().getPluginManager().registerEvents(new listeners(this), this);
    and you need to register your plugin main in listener:
    Code:
    MainClassName plugin; <---Needs to be plugin
     
    public listeners(MainClassName instance){
    plugin = instance;
    }
     
  3. Offline

    LordManegane

    Well, i thinks this is gonna work.
    Code:java
    1. if(p.getName().equalsIgnoreCase("LordManegane"))
    2. Bukkit.getServer().broadcastMessage(ChatColor.AQUA+"["+ChatColor.GRAY + "???"+ ChatColor.AQUA+"]"+ChatColor.BLACK+ "He has arrived...");
    3. for(Player player : Bukkit.getOnlinePlayers()){
    4. player.playSound(player.getLocation(), Sound.WITHER_DEATH, 10, 1);
    5. }

    If works please notify me
     
  4. Offline

    4non

    "p" cannot be resolved in "if(p.getName()..."

    there's a red line under "(new listeners(this),"
    and i get a bunch of errors on this
    Code:java
    1. public listeners(RUN instance){
    2. plugin == instance()); }

    which are these
    Code:
    Multiple markers at this line
    - Syntax error on token(s), misplaced construct(s)
    - Syntax error, insert "enum Identifier" to complete
    EnumHeader
    Also, RUN (Main class) isn't recognized, even after I import it.

    I tried this, and there's no errors.. let's see what happens on the server :D
    (He's not online, I'm just going to use an alt)

    *Edit: same thing, didn't work. And yes I did change "Skeleton_o_Dhuum" to my alt's name for the test, I'm not that stupid xD

    Oh and if it helps any, this is my main class file, RUN

    Code:
    package net.malicepvp.run;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class RUN  extends JavaPlugin {
     
        @Override
        public void onEnable(){
            getLogger().info("RUN has been enabled, noob!");
        }
       
        @Override
        public void onDisable() {
            getLogger().info("RUN has been disabled, noob!");
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  5. Offline

    iFamasssxD

    4non
    Put this in your listnener exactly as you see it.
    Code:
    RUN plugin;
     
    public listeners(RUN instance){
    plugin = instance;
    }
    Then put this in your main class onEnable();
    Code:
    getServer().getPluginManager().registerEvents(new listeners(this), this);
    EDIT: I tried this code and it worked fine for me. Although I didnt see the message others did. If you want to make it so you can see it make a delayed task that runs when you login so it gives you time to connect.
     
  6. Offline

    4non

    RUN.java :
    Code:java
    1. package net.malicepvp.run;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public final class RUN extends JavaPlugin {
    6.  
    7. @Override
    8. public void onEnable(){
    9. getLogger().info("RUN has been enabled, noob!");
    10. getServer().getPluginManager().registerEvents(new listeners(this), this);
    11. }
    12.  
    13. @Override
    14. public void onDisable() {
    15. getLogger().info("RUN has been disabled, noob!");
    16. }
    17. }

    listeners.java
    Code:java
    1. package net.malicepvp.run;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Sound;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerLoginEvent;
    11.  
    12. public class listeners implements Listener {
    13. RUN plugin;
    14.  
    15. public listeners(RUN instance){
    16. plugin = instance;
    17. }
    18. @EventHandler (priority = EventPriority.HIGH)
    19. public void onPlayerLogin(PlayerLoginEvent event) {
    20. if(event.getPlayer().getName().equals("athbi2012")){
    21. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "He has arrived...");
    22. for(Player p : Bukkit.getOnlinePlayers()){
    23. p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
    24. }
    25. }
    26.  
    27. }
    28. }

    I don't hear a noise, no message displays, and the console shows nothing. What am I doing wrong?
     
  7. Offline

    iFamasssxD

    can you do equalsignorecase? instead of equals? This works just fine for me and thats the only difference.
     
  8. Offline

    4non

    Nothing is working. I'm so confused at what the problem could be... everything you're telling me makes perfect sense. Whenever my friend (Skeleton_o_Dhuum) joins the server, nothing happens. It doesn't display the join message because of another plugin I have, but that shouldn't affect this plugin... could you upload the file on mediafire or something?

    *Edit: I think it might be something with the way I'm packaging it into a .jar. Are there any settings that I need to have enabled for it to load in minecraft?

    *Edit #2: Actually, famas, I don't think I need anything else.. thanks for helping me! I'm probably just doing something really stupid and obvious that I didn't include in the post.. I'm just going to try again later. It might be a problem with 1.6...
     
  9. Offline

    MineoxFX

    4non
    did you remember to register the events in the main class?
     
  10. Offline

    4non

    My code worked for iFamasssxD so I think everything should be working.. it might be a problem with my system, or maybe I screwed up some option in the exporting to a .jar thing. It should be working, but it's not for some reason, and I don't think the problem is in the code.

    And yes, I did.
     
  11. Offline

    4non

    iFamasssxD It's working now, I just put my plugin.yml in the wrong place. Thanks for all the help :D
     
Thread Status:
Not open for further replies.

Share This Page