Store player name and message (globally?)

Discussion in 'Plugin Development' started by Quasindro, Apr 15, 2014.

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

    Quasindro

    Hi.
    I've created a simple plugin which handles player reporting. I would like to add a feature of storing reports and players who have written them - locally, with a command access to list for people with permission. I've been trying using HashMaps and ArrayLists, but it didn't do much. What to use and how to use it? :c
     
  2. Offline

    Gater12

    Quasindro
    HashMap store the player (name or UUID) as the key and their report (message) as the value.
     
  3. Offline

    Quasindro

    Gater12
    I've tried to use it, but it gave me a null on call. I might've messed something up.
     
  4. Offline

    Gater12

    Quasindro
    Pots the code what have you tried so far.
     
  5. Offline

    Quasindro

    Gater12
    Code:java
    1.  
    2. Player player = (Player) sender;
    3. Map<String, String> reportData = new HashMap<String, String>();
    4.  
    5. StringBuilder message = new StringBuilder();
    6. for (int i = 1; i < args.length; i++) {
    7. message.append(args[i]).append(" ");
    8. }
    9.  
    10. String s = sender.getName();
    11. String m = message.toString();
    12.  
    13. if (cmd.getName().equalsIgnoreCase("report") || cmd.getName().equalsIgnoreCase("skarga")) {
    14. if (args.length > 0) {
    15. Player target = Bukkit.getPlayerExact(args[0]);
    16. if (target == null) {
    17. [...]
    18. return true;
    19. } else if (target.getName() == sender.getName()) {
    20. [...]
    21. return true;
    22. } else {
    23. reportData.put(s, m);
    24. [...]
    25. target.playSound(target.getLocation(), Sound.NOTE_PLING, 5, 0);
    26. return true;
    27. }
    28. } else {
    29. [...]
    30. return true;
    31. }
    32. }
    33.  
    34. if (cmd.getName().equalsIgnoreCase("reqs")) {
    35. if (player.hasPermission("heavenchat.report")) {
    36. player.sendMessage(reportData.get(s) + " " + reportData.get(m));
    37. return true;
    38. }[/i]
     
  6. Offline

    Garris0n

    This is Java, you can't do that. Use .equals() instead.
     
  7. Offline

    Quasindro

    Garris0n
    It worked though. But thanks for the heads up, I completely forgot about it.
     
  8. Offline

    Garris0n

    Because in that case they just happen to be the string. That's rarely the case.

    As for whatever issue you're still having, you should post the stack trace if there is one or give more details.
     
  9. Offline

    Quasindro

    Garris0n
    There is none. The plugin works correctly, except for that one detail. It just doesn't save the name of sender and message - throws both as nulls on call. And, as I previously said, I would like it to output a list of written reports.
     
  10. Offline

    Garris0n

    Well "s" is your key so you can't use data.get(m), only data.get(s).
     
  11. Offline

    Quasindro

    Garris0n
    Hm. Now it gives me a blank message.
     
  12. Offline

    Garris0n

    Then you put a blank string in the map.
     
  13. Offline

    Quasindro

    Garris0n
    So how should I do this to get the message and name of the previous sender? Because well.. It would be nice if it worked.
     
  14. Offline

    Garris0n

    Wait, are you putting one player's name in as the key and trying to get the message out with another player's name? Obviously that's not going to work.
     
  15. Offline

    Quasindro

    Garris0n
    So what's your advice? Am I even able to list strings with their "owners" through hashmap then?
     
  16. Offline

    Garris0n

    Well first of all what do you even intend the code to do, I'm honestly not sure.
     
  17. Offline

    Quasindro

    Garris0n
    Umm... I've already said that I would like to list reports written by players - an useful tool for administrators to handle reports. Are my intentions clarified now?
     
  18. Offline

    coasterman10

    First, it seems you are blindly casting the sender of a command to a player. Do not do this, ever! This sounds like something out of a BcBroz tutorial. If you ever send this command through the console, it will immediately crash the onCommand and nothing will happen.

    Secondly, if you're defining your hashmap within the command, it will not be persistent. It needs to be an instance variable of some sort.
     
    amhokies likes this.
  19. Offline

    Garris0n

    Lol

    I meant what is that "reqs" part supposed to do, for one.
     
  20. Offline

    Quasindro

    Uh-oh, sorry, I've created an independent handler for console which I happened to forgot to copy.
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd,
    2. String commandLabel, String[] args) {
    3.  
    4. if (!(sender instanceof Player)) {
    5. [...]
    6. }
    7. //and here it goes
    8. Player player = (Player) sender;
    9.  
    10. Map<String, String> reportData = new HashMap<String, String>();


    Was I doing this right?

    Garris0n
    The /reqs command is supposed to do exactly what I said in my previous post. To loop through recent reports and list them for the calling administrator.
     
  21. Offline

    coasterman10

    Yes, that works, so long as you make sure that there is a return statement within the console handler to make sure the code execution doesn't accidentally get out of that if bracket.

    The HashMap should still be defined outside of that method, though, since any variable defined within a method will not live after the end of the method, and each time the method is called, a new variable is created.
     
  22. Offline

    Quasindro

    coasterman10
    Aight, I've put it outside. But it still gives me a blank message when called. I'm pretty sure that I'm being retarded right now, but I honestly don't know what to do now. I'm completely lost.
     
  23. Offline

    Garris0n

    But it didn't loop through anything...
     
  24. Offline

    Quasindro

    Garris0n
    I'd set up the loop afterwards. Right now nothing's working, so why would I bother making the code more complicated?
     
  25. Offline

    Garris0n

    Post the current code, I suppose.
     
  26. Offline

    Quasindro

    Garris0n
    I would set up the loop afterwards, then. Sorry, I might've misled you. Nothing has changed except for putting the HashMap before the onCommand.
     
  27. Offline

    Garris0n

    Wait you had the map inside the onCommand? That would never work, it would be deleted when the command is finished.
     
  28. Offline

    Quasindro

    Garris0n
    Yup, I had. And coasterman10 pointed it out, so I corrected it in my code. I'm a noob, so I sometimes happen to be... Dense.
     
  29. Offline

    Garris0n

    So if that's fixed, what is the problem?
     
Thread Status:
Not open for further replies.

Share This Page