Possible problem with playing sounds

Discussion in 'Plugin Development' started by Ultracrepadarian, Nov 19, 2017.

Thread Status:
Not open for further replies.
  1. Hello! First, I'd like to say that going through this, I am completely open to criticism, I'm a fairly new programmer and love to learn! Now onto the problem...

    Basically, It all narrows down to the fact that I'm not exactly sure what's wrong here. I said it was a sound problem because I wasn't sure what else it could be. My goal for this plugin is to make it to where players can write "@(player name)" in the chat and have that player receive a sound alert in game.

    I'll attach my code below, and I've also added comments on any areas I think might be hard to understand.

    Code (open)

    Code:
    package com.krazytar.script;
    
    import java.util.logging.Logger;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.entity.Player;
    import org.bukkit.Location;
    import org.bukkit.*;
    
    
    
    
    
    public class LunaMcNotifications extends JavaPlugin implements Listener {
        public static final Logger log = Logger.getLogger("Minecraft");
      
        public static final String lmnGeneral = ChatColor.AQUA + "[" + ChatColor.GREEN +
                        "LunaMcNotifications" + ChatColor.AQUA + "] " + ChatColor.GOLD;
        public static final String lmnError = ChatColor.AQUA + "[" + ChatColor.GREEN +
                        "LunaMcNotifications" + ChatColor.AQUA + "] " + ChatColor.RED;
      
      
      
            @Override
            public void onEnable(){
    
            getServer().getPluginManager().registerEvents(this, this);
         
            log.info(lmnGeneral + "Enabling LunaMcNotifications");
        }
          
          
        public void notifyPlayer(Player p) {
            Location loc = p.getLocation();
          
            World world = p.getWorld();
            world.playEffect(loc,Effect.ANVIL_USE,1,0);
        }
          
          
          
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event)
         {
            if(event.getMessage().contains("@")) { //Checks if message contains '@'
                String text = event.getMessage();
              
              
                String[] splited = text.split("\\s+"); // Splits up message and puts each word into array
                  
                //get notified players name
                for(int i = 0; i > splited.length; i++) { // Loops through all words in array
                  
                    if(splited[i].contains("@")) { //finds the array entry with an '@'
                      
                        StringBuilder sb = new StringBuilder(splited[i]);
                        sb.deleteCharAt(1); // Removes the '@' to leave only the player name
                      
                        Player p = this.getServer().getPlayer(sb.toString());
                      
                        if(this.getServer().getOnlinePlayers().contains(p)) {//if the player specified is online
                            notifyPlayer(p); //Runs the funtions "notifyPlayer" (above)
                        }
                    }
                }    
            }
        }
    }


    Thank you for everything in advance!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Ultracrepadarian Don't use that logger.
    Just use getLogger
    Don't use "import org.bukkit.*" it is just ugly.
    Don't log your own plugins, Bukkit does that for you.
    getPlayer will return null if the player can't be found, no need to check if the online players contains it.

    Send messages to the console or sender to check which if statements run and which don't anymore.
     
  3. Offline

    MightyOne

    1. Always post the error you get. Makes it easier to detect whats wrong.
    2. Print out values like the player name to check yourself if something is wrong there

    Idk if \\s+ works as space but if its fine its fine. With deleteCharAt (1) you probly remove the first letter of the players name, not the @. Java starts counting at 0 and again: you have the ability to debug you code yourself. Be smart and check what every single step of it produces
     
  4. Offline

    RunsWithShovels

    So what's the problem you're having? You didn't really specify anything other than it could be a sound problem.

    Sent from my Nexus 6 using Tapatalk
     
  5. Hey Ultracrepadarian,

    You should add some debug messages to see where it goes wrong.
    Example:
    Code:
    if(event.getMessage().contains("@")) {
        if(blabla) {
    
        } else {
            Bukkit.broadcastMessage("test2");
        }
    }else {
        Bukkit.broadcastMessage("Test1");
    }
    
     
  6. @timtower Thank you! I'll edit these and see what I get.

    @MightyOne @RunsWithShovels Apologies for not posting the error message. I forgot to mention that simple nothing happens. Once the player says "@(playername)" simply nothing happens. Thank you for the advice on the deleteCharAt function! That could be the issue.


    Thank you guys for everything! I'll work on the code a bit and get back to you!

    EDIT: Also forgot to mention: @timtower, the reason for doing the org.bukkit.* import was because my imports aren't working for some reason. Whenever I do Ctrl+Shift+i on NetBeans (to fix imports), it never gives me the option to select the bukkit imports. As if it can't find them. The only way for me to do this is to manually find each class and import them. Any idea why this may be happening? Thanks!

    EDIT AFTER TRYING SUGGESTED: I did all the suggestions, and it seems the problem remains. There's absolutely nothing happening when I use @username. I turned sound all the way up so that's not the problem. Any idea what the problem might be?

    Current code:
    Code (open)

    Code:
    package com.krazytar.script;
    
    import java.util.logging.Logger;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.entity.Player;
    import org.bukkit.Location;
    import org.bukkit.*;
    
    
    
    
    
    public class LunaMcNotifications extends JavaPlugin implements Listener {
       
        public static final String lmnGeneral = ChatColor.AQUA + "[" + ChatColor.GREEN +
                        "LunaMcNotifications" + ChatColor.AQUA + "] " + ChatColor.GOLD;
        public static final String lmnError = ChatColor.AQUA + "[" + ChatColor.GREEN +
                        "LunaMcNotifications" + ChatColor.AQUA + "] " + ChatColor.RED;
       
       
       
            @Override
            public void onEnable(){
               
            getServer().getPluginManager().registerEvents(this, this);
           
        }
           
           
        public void notifyPlayer(Player p) {
            Location loc = p.getLocation();
           
            World world = p.getWorld();
            world.playEffect(loc,Effect.ANVIL_USE,1,0);
        }
           
           
           
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event)
         {
            if(event.getMessage().contains("@")) { //Checks if message contains '@'
                String text = event.getMessage();
               
               
                String[] splited = text.split("\\s+"); // Splits up message and puts each word into array
                   
                //get notified players name
                for(int i = 0; i > splited.length; i++) { // Loops through all words in array
                   
                    if(splited[i].contains("@")) { //finds the array entry with an '@'
                       
                        StringBuilder sb = new StringBuilder(splited[i]);
                        sb.deleteCharAt(0); // Removes the '@' to leave only the player name
                       
                        Player p = this.getServer().getPlayer(sb.toString());
                       
                        notifyPlayer(p); //Runs the funtions "notifyPlayer" (above)
                    }
                }     
            } 
        }
    }
    
     
    Last edited: Nov 19, 2017
  7. Any updates? Anything will help.
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. To add on to everything ^^

    Using private methods and fields is a good practice when they're only being used in one class, and never the less getters/setters can be made out of them if you need to access them.
     
  10. Offline

    Unknown123

    @Ultracrepadarian Don't use getServer(). Use Bukkit. instead. The name of static final vars schould be uppercase. You should use
    Code:
    String playerName = splitted[i].subString(1);
    instead of
    Code:
    StringBuilder sb = new StringBuilder(splited[i]);
                        sb.deleteCharAt(1); 
    If you want to use sb.deleteCharAt() you must start from 0 not from 1
    (Also don't check if online players contains the Player, check if the Player is not null.) oops, timtower already told you the last thing
     
    Last edited: Nov 21, 2017
  11. Offline

    timtower Administrator Administrator Moderator

    And why exactly?
     
  12. Offline

    Unknown123

    @timtower Because you can access it from anywhere and you don't need the plugin. It is not associated with the plugin. getServer() returns Bukkit.getServer(), Bukkit. methods call Bukkit.getServer().method() but it is still shorter and better.
     
Thread Status:
Not open for further replies.

Share This Page