Help with code /r

Discussion in 'Spigot Plugin Development' started by Partykillen07, Jun 12, 2019.

  1. Please put your code in code tags like so
    Code (Text):

    [code=java ]
    this code here
    [/code ]
     
    without spaces before ]
     
  2. You should use the code tag to make the code readable
     
  3. So.. Remember this: When you don't know something, at least try to code it. You should start coding, and if you have an issue (like something is not working, or you don't know how to do something (small thing, e.g send message to sender)), then you should post here.
     
  4. Problems I see:

    Code (Java):
    if(cmd.getName().equalsIgnoreCase("msg")) {
    Don't need equalsIgnoreCase. This name will ALWAYS be the string defined in your plugin.yml

    Code (Java):
    Player targetPlayer = Bukkit.getPlayerExact(args[0]);
    This code should only be run if you have verified the length of args to be greater than 0.
    It will throw an exception because it's outside the if block..

    EDIT: I made these comments based on the messed up code from not putting it in code blocks. It looks different from it did then.
     
  5. I dont know how to code HashMaps
     
  6. Example: (pseudo-code)
    Code (Java):

    Map<Player, String> prefixes = new HashMap<>();

    onJoin {
       prefixes.put(event.getPlayer(), "admin");
    }

    onQuit {
       prefixes.remove(event.getPlayer());
    }

    String prefix = prefixes.get(player);
     
     
  7. So if I put if (cmd.getName().equals("msg"))
    and I execute /MsG it will still work? equals and equalsIgnoreCase are different things
     
  8. Yes. Because "msg" is what you put in your plugin.yml. You're doing command.getName().
    "MsG" would be the "label" argument in onCommand. cmd.getName() will always be exactly what you put in the plugin.yml, hence why it's a field on the command object.

    So if you do this
    Code (YAML):

    commands
    :
       msg
    :
        blabla
     
    then you can do this
    Code (Java):

    command.getName().equals("msg")
     
     
  9. It doesn’t really matter...
     
  10. It's less efficient code run everytime the player runs a command for no reason. And I only explained further because he replied to it specifically. :\
     
  11. Fair enough.

    As for the OP, look up how to store information in Maps. When a player /msg's another player, set their username with the sender as a key. (So like sender, lastSent). When a player does /r, see if they're in the map, if they do, send the message to the value in the map.
     
  12. HashMaps are just like physical memory, they store files for the time the file is active, once it resets then the whole HashMap is cleared.

    You will want to research HashMaps, I’m not going to spoon feed you because it never helps people.

    @Trophonix gave you a really great piece of pseudo code, learn what it does and take that knowledge and adapt to it.

    I also suggest learning by decompiling similar plugins and learning what they are doing, DO NOT just copy and paste because it’s illegal and considered copyright infringement, and that’s just not going to help you.

    Learn about HashMaps, and similar conditions here!
     
    • Like Like x 1

Share This Page