Solved request system

Discussion in 'Plugin Development' started by MrHatz2, Sep 2, 2014.

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

    Skionz

    Really? I think I read somewhere that using the player instead of the players name is bad? Maybe it was for configs
     
  2. Offline

    BillyGalbreath

    You read correct. The person that said it was bad was giving misleading advice. Instead of telling the real solution for the real problem they simply took the lazy way out and just said dont do it ever.
     
  3. Offline

    xTigerRebornx

    MrHatz2 "I need an example script" just translates to "write my code". If you want people to write your code, go and make a plugin request.
     
    BillyGalbreath likes this.
  4. Offline

    Deleted user

    -
     
    Last edited by a moderator: Jan 23, 2015
  5. Offline

    MrHatz2

    xTigerRebornx
    yah but I just want to make it myself because I want to make plugins like worldedit and essentials
     
  6. Offline

    Deleted user

    -
     
    Last edited by a moderator: Jan 23, 2015
  7. Offline

    xTigerRebornx

    MrHatz2 If you want to make your own plugins, then why are you asking for us to give you code? We would be making it, not you.
     
  8. Offline

    MrHatz2

    xTigerRebornx
    im not asking for code I just want an example to see what it looks like so I can do the same.
     
  9. Offline

    dchaosknight

    MrHatz2
    Which means that you're asking for code.
     
  10. Offline

    xTigerRebornx

    MrHatz2 The Javadocs are that example, learn how to use them if you don't know.
     
  11. Offline

    Skionz

  12. Offline

    BillyGalbreath

    Copy pasting will never get you anywhere. You need to comprehend the code enough to write it yourself. Everyone has told you in extreme detail on how to accomplish what you want to do. If you don't understand plain English explanations, then you need to do more research on the programming language. And saying things like "I learn by seeing other people's code" is a b.s. If you cant be bothered to research the English explanations why should we believe you are going to research the code explanations?
     
  13. Offline

    MrHatz2

    xTigerRebornx zombiekiller753 Skionz
    k I came up with this script. Is there anything wrong?

    Code:java
    1.  
    2. Map<Player, Player> tpa = new HashMap<Player, Player>();
    3.  
    4. // request logic
    5. if(Bukkit.getPlayer(a[0]) == null){
    6. //no exist player
    7. return true;
    8. }
    9.  
    10. Player target = Bukkit.getPlayer(a[0]);
    11.  
    12. tpa.put(p, target);
    13.  
    14. return true;
    15.  
    16. //accepting request logic
    17. if(Bukkit.getPlayer(a[0]) == null){
    18. //no exist player
    19. return true;
    20. }
    21.  
    22. Player requester = Bukkit.getPlayer(a[0]);
    23.  
    24. if(tpa.get(requester) != p){
    25. //no request from him
    26. return true;
    27. }
    28.  
    29. //now I put code here
    30.  
    31. tpa.remove(requester);
    32.  
    33. return true;
     
  14. Offline

    BillyGalbreath

    Only a quick glance I see you used Bukkit.getPlayer() four times. I guess you missed my post earlier where I said this was a Bad Thing(TM).
     
  15. Offline

    MrHatz2

    BillyGalbreath
    But the requester inputs a name for a player so I want to make sure the player is online is that how you check if they are online?

    I also want player object of name so i search and it say to use that
     
  16. Offline

    BillyGalbreath

    Post all of your code. With it being out of context we can't tell what you're doing here.
     
  17. Offline

    MrHatz2

    BillyGalbreath
    k my onCommand:
    Code:java
    1. private Map<Player, Player> tpa = new HashMap<Player, Player>();
    2. public boolean onCommand(CommandSender s, Command c, String l, String[] a){
    3. if(!(s instanceof Player)){
    4. //is console
    5. return true;
    6. }
    7.  
    8. Player p = (Player) s;
    9.  
    10. if(c.getName().equalsIgnoreCase("tparequest")){
    11.  
    12. if(a.length == 0){
    13. //no arguments
    14. return true;
    15. }
    16.  
    17. if(Bukkit.getPlayer(a[0])){
    18. //no exist player
    19. return true;
    20. }
    21.  
    22. Player target = Bukkit.getPlayer(a[0]);
    23.  
    24. //Now request here right?
    25. tpa.put(p, target);
    26.  
    27. return true;
    28. }
    29.  
    30. if(c.getName().equalsIgnoreCase("tpaccept")){
    31.  
    32. if(a.length == 0){
    33. //no arguements
    34. return true;
    35. }
    36.  
    37. if(Bukkit.getPlayer(a[0]) == null){
    38. //no exist player
    39. return true;
    40. }
    41.  
    42. Player requester = Bukkit.getPlayer(a[0]);
    43.  
    44. if(tpa.get(requester) != p){
    45. //no request from him
    46. return true;
    47. }
    48.  
    49. //now rest of code here
    50.  
    51. tpa.remove(requester);
    52. return true;
    53.  
    54. }
    55.  
    56. return true;
    57.  
    58. }
     
  18. Offline

    SmooshCakez

    If you know Java, you know how to use HashMaps.
     
  19. Offline

    BillyGalbreath

    1) Why is tpaccept accepting arguments? I think its easier for the user to just /tpaccept to the last request sent instead of specifying a requester. But, its your plugin and if thats what you want, thats what you want. :p

    2) You still use Bukkit.getPlayer() too much. I see it four times, where it could easily be only two. And if you dont need arguments in tpaccept then you need only one.

    3) I suggest using more descriptive variable names. tpa, a, p, s, c, l... In this use-case its relatively simple to understand them, but practicing descriptive variables will make it easier on you in the future when you're writing bigger plugins.

    4) You are never actually teleporting the player.
     
  20. Offline

    MineStein

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Test what you have. If there is an error, come back with a pastebin of the it.
     
  21. Offline

    MrHatz2

    BillyGalbreath
    K you were very helpful and I will make the changes

    BillyGalbreath
    Wait but for tpaccept how can I accept a player who request for him because more than one people can request for him so that's why I made tpaccept have argument

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  22. Offline

    BillyGalbreath

    And thats your choice to do so, if you wish. ^_^

    Here's what I mean about having too many Bukkit.getPlayer() lines, though:

    Change this:

    Code:java
    1.  
    2. if(Bukkit.getPlayer(a[0])) {
    3. //no exist player
    4. return true;
    5. }
    6. Player target = Bukkit.getPlayer(a[0]);
    7.  


    Into this:

    Code:java
    1.  
    2. Player target = Bukkit.getPlayer(a[0]);
    3. if(target == null) {
    4. //no exist player
    5. return true;
    6. }
    7.  


    Instead of calling that heavy method twice, its now only once. ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  23. Offline

    ColonelHedgehog

    Oh.

    Kay.

    Obviously it's a bit—shall we say—early in your experiences to start worrying about fine details such as these, but don't make a habit out of storing players. Would be best to store the UUID. Memory leaks, and such sometimes happen from storing Player, isn't that right?
     
  24. Offline

    BillyGalbreath

    No.
     
  25. Offline

    ColonelHedgehog

    Wait, really? But the instance of the player is saved and prevents the system from releasing RAM, doesn't it?

    Whoa.

    EDIT: Actually did look it up, many, many people think it is in fact a bad idea to store players. I think it has to do with the fact that Java GC can't remove the player if he logs off (so I've heard... but PlayerQuitEvent, anybody?) and it's redundant to store the ENTIRE Player to a hashmap when all you can access him through UUID.

    More lightweight, I guess. Could you elaborate? I'm very interested in this topic.
     
  26. Offline

    xTigerRebornx

  27. Offline

    ColonelHedgehog


    That's what I thought. However, it seems to be a heck of a lot less painful just to use UUIDs, and as I said before, no need to save the entire instance of a Player.
     
  28. Offline

    xTigerRebornx

    ColonelHedgehog Eh, may be of use if you plan on using the "Player" that the UUID belongs to (like, if you are commonly calling Bukkit#getPlayer(UUID) it may be better to do that or use some sort of WeakRef Map/List/Set).
     
  29. Offline

    ColonelHedgehog

    I use UUIDs all the time. In fact, they're much better than player names because they're accessed quicker (I forget why, though. ;-;). Bukkit#getPlayer(UUID) is a pretty useful function. I just don't see why you'd ever want to store a player and not his UUID.
     
  30. Offline

    BillyGalbreath

    Imagine a server with 100+ people running around and you have Bukkit.getPlayer(UUID) to get the Player object in the PlayerMoveEvent, all so you can check what they are holding in their hands as they run around (so you know wht actions to take)... You just lagged out your server. But if you had a Player object already, it would not lag out. In fact, it wouldnt even drop any TPS most likely...

    Edit: This is just one of infinite number of examples.
     
Thread Status:
Not open for further replies.

Share This Page