Solved Getting ArmorStand

Discussion in 'Plugin Development' started by KarimAKL, Jun 20, 2018.

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

    KarimAKL

    I want to get the armorstand i've created using this:
    Code:Java
    1.  
    2. ArmorStand stand = p.getWorld().spawn(p.getLocation().add(0, +1, 0), ArmorStand.class);
    3.  

    how would i do this? (I've been trying for some time now and can't get any ideas of how to do this)
    I've tried looking this up but results were to save it in a List or HashMap but how would i get this aswell?
    Code:Java
    1.  
    2. Item barrier = (Item) p.getWorld().dropItem(p.getLocation().add(0, +2, 0), new ItemStack(Material.BARRIER));
    3.  

    I want to get both. :7
    Here is the current code i use for creating it:
    Code:Java
    1.  
    2. ArmorStand stand = op.getWorld().spawn(op.getLocation().add(0, +1, 0), ArmorStand.class);
    3. stand.setGravity(false);
    4. stand.setVisible(false);
    5. Item barrier = (Item) op.getWorld().dropItem(op.getLocation().add(0, +2, 0), new ItemStack(Material.BARRIER));
    6. barrier.setPickupDelay(Integer.MAX_VALUE);
    7. stand.setPassenger(barrier);
    8.  

    My end goal with this is to create what i already have here but only 1 per player(right now i have it in a loop so i want to be able to check if it is already there(the armor stand and barrier item)) and then remove it using a PlayerMoveEvent or something like that instead of what i currently have which is:
    Code:Java
    1.  
    2. ArmorStand stand = op.getWorld().spawn(op.getLocation().add(0, +1, 0), ArmorStand.class);
    3. stand.setGravity(false);
    4. stand.setVisible(false);
    5. Item barrier = (Item) op.getWorld().dropItem(op.getLocation().add(0, +2, 0), new ItemStack(Material.BARRIER));
    6. barrier.setPickupDelay(Integer.MAX_VALUE);
    7. stand.setPassenger(barrier);
    8. new BukkitRunnable() {
    9. @Override
    10. public void run() {
    11. if (plugin.getAfk(op.getUniqueId()) < 60*5) {
    12. stand.remove();
    13. barrier.remove();
    14. cancel();
    15. }
    16. }
    17. }.runTaskTimer(plugin, 0, 20);
    18.  

    I hope i explained myself well enough.
     
  2. Offline

    CommonSenze

    @KarimAKL
    I see you chose the armor stand route.

    You want to get both from a HashMap/List?

    A better way would be to do this:
    1. Create a AFK class per player and save it in a HashMap (Unless you do step 5 inside your class).
    2. In the class create 3 variables: the stand, the afk boolean to tell if the player is afk, and barrier
    3. Create a method that removes the stand and barrer when player is unafk for the move event to call
    4. Also in the class have the exact same thing as you have above with all the checks and loops to see if the player is afk or not.
    5. On PlayerMoveEvent, get the class check if user is afk with the boolean getter method and activate the method that removes the barrier and stand.

    Hope that made sense.
     
  3. Offline

    KarimAKL

    @CommonSenze Yes, i tried the armor stand route and it was actually exactly what i wanted. :p Anyway, about this:
    No, i meant that i wanted to get both the armor stand and the barrier. :p Anyway what do you mean by "Create a AFK class per player"? This might be easier for you to explain if i gave you some more classes to look at. :p
    Here you go:
    Main class:
    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. private HashMap<UUID, Integer> isAfk = new HashMap<UUID, Integer>();
    5.  
    6. public void setAfk(UUID uuid, Integer integer) {
    7. this.isAfk.put(uuid, integer);
    8. }
    9.  
    10. public Integer getAfk(UUID uuid) {
    11. return this.isAfk.get(uuid);
    12. }
    13.  
    14. public boolean existAfk(UUID uuid) {
    15. return this.isAfk.containsKey(uuid);
    16. }
    17.  
    18. public void onEnable() {
    19. new CommandAfk(this);
    20. new CommandGetAfk(this);
    21. new AsyncPlayerChat(this);
    22. new CommandPreprocess(this);
    23. new PlayerInteract(this);
    24. new PlayerMove(this);
    25. new PlayerQuit(this);
    26. new RepeatRun(this);
    27. }
    28. }
    29.  

    RepeatRun class(You can see what it does):
    Code:Java
    1.  
    2. public class RepeatRun {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public RepeatRun(Main plugin) {
    8. this.plugin = plugin;
    9. new BukkitRunnable() {
    10. @Override
    11. public void run() {
    12. if (!plugin.getServer().getOnlinePlayers().isEmpty()) {
    13. for (Player op : Bukkit.getOnlinePlayers()) {
    14. if (op.isOnline()) {
    15. if (plugin.existAfk(op.getUniqueId())) {
    16. if (plugin.getAfk(op.getUniqueId()) >= 60*5) {
    17. ArmorStand stand = op.getWorld().spawn(op.getLocation().add(0, +1, 0), ArmorStand.class);
    18. stand.setGravity(false);
    19. stand.setVisible(false);
    20. Item barrier = (Item) op.getWorld().dropItem(op.getLocation().add(0, +2, 0), new ItemStack(Material.BARRIER));
    21. barrier.setPickupDelay(Integer.MAX_VALUE);
    22. stand.setPassenger(barrier);
    23. new BukkitRunnable() {
    24. @Override
    25. public void run() {
    26. if (plugin.getAfk(op.getUniqueId()) < 60*5) {
    27. stand.remove();
    28. barrier.remove();
    29. cancel();
    30. }
    31. }
    32. }.runTaskTimer(plugin, 0, 20);
    33. } else {
    34. plugin.setAfk(op.getUniqueId(), plugin.getAfk(op.getUniqueId())+1);
    35. }
    36. } else {
    37. plugin.setAfk(op.getUniqueId(), 1);
    38. }
    39. }
    40. }
    41. }
    42. }
    43. }.runTaskTimer(plugin, 0, 20);
    44. }
    45. }
    46.  

    PlayerMove class:
    Code:Java
    1.  
    2. public class PlayerMove implements Listener {
    3.  
    4. private Main plugin;
    5.  
    6. public PlayerMove(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getPluginManager().registerEvents(this, plugin);
    9. }
    10.  
    11. @EventHandler
    12. public void onMove(PlayerMoveEvent e) {
    13. Player p = e.getPlayer();
    14. if (plugin.getAfk(p.getUniqueId()) >= 60*5) {
    15. p.sendMessage("You are no longer afk.");
    16. }
    17. plugin.setAfk(p.getUniqueId(), 0);
    18. }
    19. }
    20.  

    Hope this helps you explain it to me better because i didn't fully understand what you meant when you explained those 5 steps.
    EDIT: (I'm using "if (plugin.getAfk(p.getUniqueId()) >= 60*5) {" to check if the player is afk, should i instead put them in a HashSet when they reach the 60*5?)
     
  4. Offline

    CommonSenze

    @KarimAKL
    Well what I mean by create an AFK class per player is like this.

    Create a class called AFK or something.
    In another class called AFKManager or something, make a hashmap variable that stores UUIDs as keys and the AFK class as Values.
    Create a addAFK methods that takes in a player and put the players UUID with a new AFK class with the contructor taking in the player.
    Also create a removeAFK that takes in a player and removes the players uuid from the hashmap when they leave to conserve space
     
  5. Offline

    KarimAKL

    @CommonSenze So what you want is for me to create something like this?:
    Code:Java
    1.  
    2. public class AFK {
    3.  
    4. }
    5.  

    And then this?:
    Code:Java
    1.  
    2. public class AFKManager {
    3.  
    4. private HashMap<UUID, Class> whatever = new HashMap<UUID, Class>();
    5.  
    6. public void addAFK(Player p) {
    7. this.whatever.put(p.getUniqueId(), new AFK().getClass());
    8. }
    9.  
    10. public void removeAFK(Player p) {
    11. this.whatever.remove(p.getUniqueId());
    12. }
    13. }
    14.  

    Or did i misunderstand that completely? (I feel like i misunderstood :7 If not then what do you want me to do with the HashMap?)
     
  6. Offline

    CommonSenze

    @KarimAKL
    Instead of make a HashMap saving a UUID and Class save the UUID and AFK class like this:
    Code:java
    1. private HashMap<UUID, AFK> whatever = new HashMap<>();

    Other than that, you have the basic concept. I do the same for a lot of my code. It allows me to have a lot of code in bite sized pieces and allows me to reuse code for many players. I do the same thing for my Users class that gives me more methods for the Player interface.
     
  7. Offline

    KarimAKL

    @CommonSenze Okay, but then what should i do with it? (I still don't get why i should make this HashMap)
     
  8. Offline

    CommonSenze

    @KarimAKL
    In this class you will store all the information for being AFK (ie. The Stand, the barrier, the time of how long is needed for them to stand still to be considered afk, the methods to activate a player being afk and placing the stand and barrier down, etc) So that way you are able to have specific barriers and stands per player.
     
  9. Offline

    KarimAKL

    @CommonSenze I have this now:
    Code:Java
    1.  
    2. public class AFKManager {
    3.  
    4. private HashMap<UUID, AFK> whatever = new HashMap<UUID, AFK>();
    5. public void addAFK(Player p) {
    6. this.whatever.put(p.getUniqueId(), new AFK());
    7. }
    8. public void removeAFK(Player p) {
    9. this.whatever.remove(p.getUniqueId());
    10. }
    11.  
    12. public boolean isAFK(Player p) {
    13. return this.whatever.containsKey(p.getUniqueId());
    14. }
    15. }
    16.  

    And:
    Code:Java
    1.  
    2. public class AFK {
    3.  
    4. public void stand(Player p) {
    5. ArmorStand stand = p.getWorld().spawn(p.getLocation().add(0, +1, 0), ArmorStand.class);
    6. stand.setGravity(false);
    7. stand.setVisible(false);
    8. Item barrier = (Item) p.getWorld().dropItem(p.getLocation().add(0, +2, 0), new ItemStack(Material.BARRIER));
    9. barrier.setPickupDelay(Integer.MAX_VALUE);
    10. stand.setPassenger(barrier);
    11. }
    12. }
    13.  

    I'm lost on what to do next. :7
     
  10. Offline

    CommonSenze

    @KarimAKL
    I do understand I am being confusing on my explanation and do sincerely apologize. I am use to talking out my explanation rather than typing it.

    I'll try to explain step by step what to do inside the AFK class:
    1. Create these instance variables:
    Code:java
    1. private ArmorStand stand;
    2. private Item barrier;
    3. private boolean afk = false;

    2. Create a method that makes 2 local variables: the stand and barrier; with those two variables, set the boolean afk that we made to true, spawn them in the world, and make the instance variables you made in step one equal them like so:
    Code:java
    1. this.stand = stand;
    2. this.barrier = barrier

    3. Create a remove method that uses the 2 instance variables: stand and barrier; to remove those two items in the world when the player is not afk, also set afk to false
    4. Make a getter for the boolean afk in the AFK class
    Now that is all you need for the AFK class. Now in the PlayerMoveEvent do this
    Code:java
    1. if (plugin.getAFKManager().getAFK(e.getPlayer()).isAFK()){
    2. plugin.getAFKManager().getAFK(e.getPlayer()).stopAFK();
    3. }

    Now the isAFK method and stopAFK method are what I described in the steps, I just game them names on what I would name them.

    As I saw in your code, you have a way to check if the player is afk or not (The plugin.getAFK(p.getUniqueID()) thing and so one), keep all that code except in the part where you create the armor stand and all of that do this
    Code:java
    1. if (plugin.getAfk(op.getUniqueId()) >= 60*5) {
    2. AFK afk = plugin.getAFKManager().getAFK(op).startAFK();
    3. } else {
    4. plugin.setAfk(op.getUniqueId(), plugin.getAfk(op.getUniqueId())+1);
    5. }


    Now to explain the getAFKManager(): You will have a variable in your Main class that will have the AFKManager variable like this:
    Code:java
    1. private AFKManager afkManager;

    And onEnable() would look like this:
    Code:java
    1. this.afkManager = new AFKManager();

    And finally the getter method
    Code:java
    1. public AFKManager getAFKManager() {
    2. return afkManager;
    3. }


    I hope I explained well enough about how I would go about that problem. I am a little rusty at explaining on forums. Like I said, I'm no forums guy, I'm use to talking out my explanations not typing them out.
     
  11. Offline

    KarimAKL

    @CommonSenze Wow, thanks for taking the time to write and explain that. :D I'll try now, thanks again. :) (I just hope i understood all of it correctly :p)
    EDIT: I think i did something wrong. :/ (I've tried finding other ways but couldn't) Do you think you could take a look at the current code and point me in the right direction? :7
    Current AFK class:
    Code:Java
    1.  
    2. public class AFK {
    3.  
    4. private ArmorStand stand;
    5. private Item barrier;
    6. private boolean afk = false;
    7.  
    8. public void createStand(Player p) {
    9. this.afk = true;
    10. ArmorStand stand = p.getWorld().spawn(p.getLocation().add(0, +1, 0), ArmorStand.class);
    11. stand.setGravity(false);
    12. stand.setVisible(false);
    13. Item barrier = (Item) p.getWorld().dropItem(p.getLocation().add(0, +2, 0), new ItemStack(Material.BARRIER));
    14. barrier.setPickupDelay(Integer.MAX_VALUE);
    15. stand.setPassenger(barrier);
    16. this.stand = stand;
    17. this.barrier = barrier;
    18. }
    19.  
    20. public void removeStand() {
    21. this.afk = false;
    22. this.stand.remove();
    23. this.barrier.remove();
    24. }
    25.  
    26. public boolean getAFK(Player p) {
    27. return this.afk;
    28. }
    29. }
    30.  

    Current AFKManager:
    Code:Java
    1.  
    2. public class AFKManager {
    3.  
    4. private HashMap<UUID, AFK> whatever = new HashMap<UUID, AFK>();
    5. public void addAFK(Player p) {
    6. this.whatever.put(p.getUniqueId(), new AFK());
    7. }
    8. public void removeAFK(Player p) {
    9. this.whatever.remove(p.getUniqueId());
    10. }
    11.  
    12. public boolean isAFK(Player p) {
    13. return this.whatever.containsKey(p.getUniqueId());
    14. }
    15. }
    16.  

    Current Main:
    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. private HashMap<UUID, Integer> isAfk = new HashMap<UUID, Integer>();
    5.  
    6. public void setAfk(UUID uuid, Integer integer) {
    7. this.isAfk.put(uuid, integer);
    8. }
    9.  
    10. public Integer getAfk(UUID uuid) {
    11. return this.isAfk.get(uuid);
    12. }
    13.  
    14. public boolean existAfk(UUID uuid) {
    15. return this.isAfk.containsKey(uuid);
    16. }
    17.  
    18. private AFKManager afkManager;
    19.  
    20. public AFKManager getAFKManager() {
    21. return afkManager;
    22. }
    23.  
    24. public void onEnable() {
    25. this.afkManager = new AFKManager();
    26. new CommandAfk(this);
    27. new CommandGetAfk(this);
    28. new AsyncPlayerChat(this);
    29. new CommandPreprocess(this);
    30. new PlayerInteract(this);
    31. new PlayerMove(this);
    32. new PlayerQuit(this);
    33. new RepeatRun(this);
    34. }
    35. }
    36.  
     
    Last edited by a moderator: Jun 20, 2018
  12. Offline

    CommonSenze

    @KarimAKL
    You seem to have some of what I said mixed up.

    You did start in the right direction for most but here I'll explain everything you got wrong and why.

    First,
    Code:java
    1. public boolean getAFK(Player p) {
    2. return this.afk;
    3. }

    Is wrong in 3 places:
    1. The getAFK method is suppose to be in the AFKManager class because the AFKManager is the Manager for the AFK class. With the AFKManager being the Manager, it should "get the AFK Class" which is why getAFK is suppose to be in that class.
    2. That is a boolean "True/False" so you have to word it like a yes or no question thats why in my previous response I said I made a method called "isAFK()". That was the method that needed to be the getter for the boolean.
    3. The Player p variable is useless since you never needed it in the first place. Getters are a type of method, all they do is give the variable it is for. In this case, its the boolean named AFK so you won't need any arguments.

    Second,
    Code:java
    1. private HashMap<UUID, Integer> isAfk = new HashMap<UUID, Integer>();
    2.  
    3. public void setAfk(UUID uuid, Integer integer) {
    4. this.isAfk.put(uuid, integer);
    5. }
    6.  
    7. public Integer getAfk(UUID uuid) {
    8. return this.isAfk.get(uuid);
    9. }
    10.  
    11. public boolean existAfk(UUID uuid) {
    12. return this.isAfk.containsKey(uuid);
    13. }

    This is not needed at all. You misread me when I was talking about the "isAFK()". That's going to be the method name of the Getter of the boolean afk in the AFK class.

    Other than that. I think you are good. Test it out and see if it works. Come back and send me the errors if any and the class of where it is.
     
  13. Offline

    KarimAKL

    @CommonSenze The code you wrote there, is that what i'm supposed to do in the AFKManager class? If so then what about the HashMap<UUID, AFK>? Anyway, i think that cleared up everything you said, after you answer the question in this post i'll try again. :)
     
  14. Offline

    CommonSenze

    @KarimAKL
    Change getAFK in AFK class to isAFK()
    In afkmanager you wont need the isAFK(Player p) method. You can remove that.
    Also on Player Join Event make sure to use the addAFK and on Quit removeAFK methods in the AFK Manager class to add the afk class for that player
     
  15. Offline

    KarimAKL

    @CommonSenze I've done that now, here is the current code:
    Main class:
    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. private HashMap<UUID, Integer> isAfk = new HashMap<UUID, Integer>();
    5.  
    6. public void setAfk(UUID uuid, Integer integer) {
    7. this.isAfk.put(uuid, integer);
    8. }
    9.  
    10. public Integer getAfk(UUID uuid) {
    11. return this.isAfk.get(uuid);
    12. }
    13.  
    14. public boolean existAfk(UUID uuid) {
    15. return this.isAfk.containsKey(uuid);
    16. }
    17.  
    18. private AFKManager afkManager;
    19.  
    20. public AFKManager getAFKManager() {
    21. return afkManager;
    22. }
    23.  
    24. public void onEnable() {
    25. this.afkManager = new AFKManager();
    26. new CommandAfk(this);
    27. new CommandGetAfk(this);
    28. new AsyncPlayerChat(this);
    29. new CommandPreprocess(this);
    30. new PlayerInteract(this);
    31. new PlayerMove(this);
    32. new PlayerQuit(this);
    33. new RepeatRun(this);
    34. }
    35. }
    36.  

    AFK class:
    Code:Java
    1.  
    2. public class AFK {
    3.  
    4. private ArmorStand stand;
    5. private Item barrier;
    6. private boolean afk = false;
    7.  
    8. public void createStand(Player p) {
    9. this.afk = true;
    10. ArmorStand stand = p.getWorld().spawn(p.getLocation().add(0, +1, 0), ArmorStand.class);
    11. stand.setGravity(false);
    12. stand.setVisible(false);
    13. Item barrier = (Item) p.getWorld().dropItem(p.getLocation().add(0, +2, 0), new ItemStack(Material.BARRIER));
    14. barrier.setPickupDelay(Integer.MAX_VALUE);
    15. stand.setPassenger(barrier);
    16. this.stand = stand;
    17. this.barrier = barrier;
    18. }
    19.  
    20. public void removeStand() {
    21. this.afk = false;
    22. this.stand.remove();
    23. this.barrier.remove();
    24. }
    25.  
    26. public boolean isAFK() {
    27. return this.afk;
    28. }
    29. }
    30.  

    AFKManager class:
    Code:Java
    1.  
    2. public class AFKManager {
    3.  
    4. private HashMap<UUID, AFK> whatever = new HashMap<UUID, AFK>();
    5. public void addAFK(Player p) {
    6. this.whatever.put(p.getUniqueId(), new AFK());
    7. }
    8. public void removeAFK(Player p) {
    9. this.whatever.remove(p.getUniqueId());
    10. }
    11. }
    12.  

    About the Main class, the code in there is used for this:
    Code:Java
    1.  
    2. if (plugin.existAfk(op.getUniqueId())) {
    3. if (plugin.getAfk(op.getUniqueId()) >= 60*5) {
    4. plugin.setAfk(op.getUniqueId(), 1);
    5.  

    I guess i should do it another way, huh? Anyway, about the isAFK(), it should be in the AFKManager class, right? But how do i gt the afk boolean from there? It's private isn't it? I'm a little confused about that right now. :7
     
  16. Offline

    CommonSenze

    @KarimAKL
    The instance variable might be private but the getter method isn't. That's what getter methods are for. So that isAFK() method in your AFK Class is the getter method for your afk boolean.

    So you can use that method in the AFKManager class by doing the following
    Code:java
    1.  
    2. public boolean isAfk(Player p) {
    3. return this.whatever.get(p.getUniqueId()).isAFK();
    4. }
    5.  

    That method needs to be in the AFK Manager. What it's doing is getting the AFK class for that player and checking what the boolean afk is; true or false.

    And its that method that you're going to use in the PlayerMoveEvent when a player moves you will call the method like so:
    Code:java
    1.  
    2. public class PlayerMove implements Listener {
    3.  
    4. private Main plugin;
    5.  
    6. public PlayerMove(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getPluginManager().registerEvents(this, plugin);
    9. }
    10.  
    11. @EventHandler
    12. public void onMove(PlayerMoveEvent e) {
    13. Player p = e.getPlayer();
    14. if (plugin.getAfk(p.getUniqueId()) >= 60*5&&plugin.getAFKManager().isAFK(p)) {
    15. p.sendMessage("You are no longer afk.");
    16. plugin.getAFKManager().getAFK(p).removeStand();
    17. }
    18. plugin.setAfk(p.getUniqueId(), 0);
    19. }
    20. }
    21.  
     
  17. Offline

    KarimAKL

    @CommonSenze Quick question. What does the getAFK(p) do? I don't have that in my AFKManager class so what should i make it do?
     
  18. Offline

    CommonSenze

    @KarimAKL
    I realized 2 things were missing in your AFKManager and AFK class.
    1. AFKManager needs the getAFK(Player p) method that returns the AFK class for that player. This is saved in the whatever variable HashMap. You have the AFK class as the value so all you do is get the UUID and it will return the value of the AFK class for that specific player

    2. The AFK Class should have the constuctor that takes in the Player like so:
    Code:java
    1.  
    2. private Player p;
    3. public AFK(Player p) {
    4. this.p = p;
    5. }

    Now, I SINCERELY APOLOGIZE, but I forgot to tell you to make that constuctor and also make another instance variable containing the player like I did above.

    This way you can remove the Player p argument in the createStand methods so it won't need to take in a player. All it will do is use the instance variable of the player.

    Also, you will need to make sure that in your AFKManager in your addAFK method that the:
    Code:java
    1. new AFK()

    is turned into:
    Code:java
    1. new AFK(p)

    Because the AFK class will need the player to save them in the class as the instance variable.

    I sincerely apologize for not explaining this as well as I should. I hope I'm getting my message across correctly.
     
  19. Offline

    KarimAKL

    @CommonSenze Okay, thanks for that. :p It doesn't come with any errors so i tried it but it doesn't spawn the stand and barrier and it also comes with a null pointer exception every second if i'm afk. I also tested and it seems the time doesn't go down to 0 when i move. :7
    Here is the full error:
    Error (open)
    Code:
    [20:53:49 WARN]: [AFK] Task #2 for AFK v0.1 generated an exception
    java.lang.NullPointerException
            at me.karim.afk.listeners.RepeatRun$1.run(RepeatRun.java:24) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    
    As you can see it has something to do with line 24 in my RepeatRun class:
    Code:Java
    1.  
    2. plugin.getAFKManager().getAFK(op).createStand();
    3.  

    Here is the full class just in case you need it:
    Code:Java
    1.  
    2. public class RepeatRun {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public RepeatRun(Main plugin) {
    8. this.plugin = plugin;
    9. new BukkitRunnable() {
    10. @Override
    11. public void run() {
    12. if (!plugin.getServer().getOnlinePlayers().isEmpty()) {
    13. for (Player op : Bukkit.getOnlinePlayers()) {
    14. if (op.isOnline()) {
    15. if (plugin.existAfk(op.getUniqueId())) {
    16. if (plugin.getAfk(op.getUniqueId()) >= 60*5) {
    17. plugin.getAFKManager().getAFK(op).createStand();
    18. } else {
    19. plugin.setAfk(op.getUniqueId(), plugin.getAfk(op.getUniqueId())+1);
    20. }
    21. } else {
    22. plugin.setAfk(op.getUniqueId(), 1);
    23. }
    24. }
    25. }
    26. }
    27. }
    28. }.runTaskTimer(plugin, 0, 20);
    29. }
    30. }
    31.  

    And here is the AFK class:
    Code:Java
    1.  
    2. public class AFK {
    3.  
    4. private ArmorStand stand;
    5. private Item barrier;
    6. private boolean afk = false;
    7. private Player p;
    8.  
    9. public AFK(Player p) {
    10. this.p = p;
    11. }
    12.  
    13. public void createStand() {
    14. this.afk = true;
    15. ArmorStand stand = p.getWorld().spawn(p.getLocation().add(0, +1, 0), ArmorStand.class);
    16. stand.setGravity(false);
    17. stand.setVisible(false);
    18. Item barrier = (Item) p.getWorld().dropItem(p.getLocation().add(0, +2, 0), new ItemStack(Material.BARRIER));
    19. barrier.setPickupDelay(Integer.MAX_VALUE);
    20. stand.setPassenger(barrier);
    21. this.stand = stand;
    22. this.barrier = barrier;
    23. }
    24.  
    25. public void removeStand() {
    26. this.afk = false;
    27. this.stand.remove();
    28. this.barrier.remove();
    29. }
    30.  
    31. public boolean isAFK() {
    32. return this.afk;
    33. }
    34. }
    35.  

    And the AFKManager class here:
    Code:Java
    1.  
    2. public class AFKManager {
    3.  
    4. private HashMap<UUID, AFK> whatever = new HashMap<UUID, AFK>();
    5. public void addAFK(Player p) {
    6. this.whatever.put(p.getUniqueId(), new AFK(p));
    7. }
    8. public void removeAFK(Player p) {
    9. this.whatever.remove(p.getUniqueId());
    10. }
    11.  
    12. public boolean isAFK(Player p) {
    13. return this.whatever.get(p.getUniqueId()).isAFK();
    14. }
    15.  
    16. public AFK getAFK(Player p) {
    17. return this.whatever.get(p.getUniqueId());
    18. }
    19. }
    20.  

    Do you see what is returning null and if i did it correctly? Thanks again. :7
     
  20. Offline

    CommonSenze

    @KarimAKL
    So NullPointerExceptions are when something is null. Now what it's saying is you're trying to access something when it is null. And I assume it is null because you never added the player to the afk Manager when they join.

    You need to make 2 events: a Join Event and a QuitEvent that adds and removes the player from the whatever hashmap.

    If you have added and removed the players to and from the hashmap in the events then the problem could lie in the getAFKManager returning a null variable because you didn't set it to anything yet.

    Those are the only 2 ways that would give a NPE. The reason it didnt spawn the items is because the NPE happened before it can spawn the items itself.
     
  21. Offline

    KarimAKL

    @CommonSenze I believe i'm adding them on join and removing them on quit, just to be sure i'll show you the classes.
    Join class:
    Code:Java
    1.  
    2. public class PlayerJoin implements Listener {
    3.  
    4. private Main plugin;
    5.  
    6. public PlayerJoin(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getPluginManager().registerEvents(this, plugin);
    9. }
    10.  
    11. @EventHandler
    12. public void onJoin(PlayerJoinEvent e) {
    13. plugin.getAFKManager().addAFK(e.getPlayer());
    14. }
    15. }
    16.  

    Quit class:
    Code:Java
    1.  
    2. public class PlayerQuit implements Listener {
    3.  
    4. private Main plugin;
    5.  
    6. public PlayerQuit(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getPluginManager().registerEvents(this, plugin);
    9. }
    10.  
    11. @EventHandler
    12. public void onQuit(PlayerQuitEvent e) {
    13. Player p = e.getPlayer();
    14. plugin.setAfk(p.getUniqueId(), 0);
    15. plugin.getAFKManager().removeAFK(p);
    16. }
    17. }
    18.  

    You can see what the methods do in the AFKManager class above.
    So if it isn't that then do you have any idea where in the AFKManager class i'm trying to get something that's null? You can see the classes above. :/ I can't seem to find it.
     
  22. Offline

    CommonSenze

    @KarimAKL
    Did you register the constructors in the onEnable method in the Main class? If you didn't they wouldn't be registered.
     
  23. Offline

    KarimAKL

    @CommonSenze Should i do what i did with the AFKManager class with the AFK class aswell?
    Like this?(Main class):
    Code:Java
    1.  
    2. private AFK afk;
    3. public AFK getAFK() {
    4. return afk;
    5. }
    6. public void onEnable() {
    7. this.afk = new AFK();
    8. }
    9.  

    If not then what should i do?
    EDIT: Nvm that didn't work. :/
    Reason why it didn't work:
    Code:
    The constructor AFK() is undefined
    
     
  24. Offline

    CommonSenze

    @KarimAKL
    Code:java
    1.  
    2. private AFKManager afkManager;
    3.  
    4. public void onEnable() {
    5. this.afkManager = new AFKManager();
    6. }
    7.  
    8. public AFKManager getAFKManager() {
    9. return afkManager;
    10. }
    11.  


    That should be in your main
     
  25. Offline

    KarimAKL

    @CommonSenze It is.
    Here is the whole Main class just to be sure:
    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. private HashMap<UUID, Integer> isAfk = new HashMap<UUID, Integer>();
    5.  
    6. public void setAfk(UUID uuid, Integer integer) {
    7. this.isAfk.put(uuid, integer);
    8. }
    9.  
    10. public Integer getAfk(UUID uuid) {
    11. return this.isAfk.get(uuid);
    12. }
    13.  
    14. public boolean existAfk(UUID uuid) {
    15. return this.isAfk.containsKey(uuid);
    16. }
    17.  
    18. private AFKManager afkManager;
    19.  
    20. public AFKManager getAFKManager() {
    21. return afkManager;
    22. }
    23.  
    24. public void onEnable() {
    25. this.afkManager = new AFKManager();
    26. new CommandAfk(this);
    27. new CommandGetAfk(this);
    28. new AsyncPlayerChat(this);
    29. new CommandPreprocess(this);
    30. new PlayerInteract(this);
    31. new PlayerMove(this);
    32. new PlayerQuit(this);
    33. new RepeatRun(this);
    34. }
    35. }
    36.  

    What else could it be?
    EDIT: This line in the PlayerMove class also comes with a NullPointerException if it helps at all:
    Code:Java
    1.  
    2. if (plugin.getAfk(p.getUniqueId()) >= 60*5 && plugin.getAFKManager().isAFK(p)) {
    3.  
     
    Last edited by a moderator: Jun 21, 2018
  26. Offline

    CommonSenze

    @KarimAKL
    You forgot to register your PlayerJoin class
     
  27. Offline

    KarimAKL

    @CommonSenze Omg, i feel like an idiot right now.. Thanks for pointing it out, sorry for the trouble. :7 I'll register it and try again.
    EDIT: It shows up now without any errors, just 2 things.
    1. It keeps making new ones every second.
    2. When i move they don't get removed. :/
    What should i do to fix these things? I thought this:
    Code:Java
    1.  
    2. public void createStand() {
    3. this.afk = true;
    4. ArmorStand stand = p.getWorld().spawn(p.getLocation().add(0, +1, 0), ArmorStand.class);
    5. stand.setGravity(false);
    6. stand.setVisible(false);
    7. Item barrier = (Item) p.getWorld().dropItem(p.getLocation().add(0, +2, 0), new ItemStack(Material.BARRIER));
    8. barrier.setPickupDelay(Integer.MAX_VALUE);
    9. stand.setPassenger(barrier);
    10. this.stand = stand;
    11. this.barrier = barrier;
    12. }
    13.  

    Would do the trick because of the "this.afk = true;". Also a few posts back you had some .startAFK and .stopAFK methods, what did they do?
    Here is my RepeatRun class:
    Code:Java
    1.  
    2. public class RepeatRun {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public RepeatRun(Main plugin) {
    8. this.plugin = plugin;
    9. new BukkitRunnable() {
    10. @Override
    11. public void run() {
    12. if (!plugin.getServer().getOnlinePlayers().isEmpty()) {
    13. for (Player op : Bukkit.getOnlinePlayers()) {
    14. if (op.isOnline()) {
    15. if (plugin.existAfk(op.getUniqueId())) {
    16. if (plugin.getAfk(op.getUniqueId()) >= 60*5) {
    17. plugin.getAFKManager().getAFK(op).createStand();
    18. } else {
    19. plugin.setAfk(op.getUniqueId(), plugin.getAfk(op.getUniqueId())+1);
    20. }
    21. } else {
    22. plugin.setAfk(op.getUniqueId(), 1);
    23. }
    24. }
    25. }
    26. }
    27. }
    28. }.runTaskTimer(plugin, 0, 20);
    29. }
    30. }
    31.  
     
    Last edited by a moderator: Jun 21, 2018
  28. Offline

    CommonSenze

    @KarimAKL
    No it's alright. I make silly mistakes like that on my server plugin core. But then again, I do have like 70 classes in my plugin so feel like it's more common with me.
     
  29. Offline

    KarimAKL

    @CommonSenze Thanks for being patient with me. :p And wow, that's alot of classes. :eek:
     
  30. Offline

    CommonSenze

    @KarimAKL
    Yeah it is.

    Well tell me if the code works and send me the errors, if any.
     
Thread Status:
Not open for further replies.

Share This Page