Solved ArrayList not containing a player when they are specifically added!

Discussion in 'Plugin Development' started by MayoDwarf, May 17, 2014.

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

    MayoDwarf

    ArrayList:
    Code:java
    1. public ArrayList<String> hasGolem = new ArrayList<String>();

    ArrayList player being added:
    Code:java
    1. public void enterGolem(Player p, Entity e) {
    2. if(e instanceof IronGolem) {
    3. IronGolem ig = (IronGolem) e;
    4. if(!hasGolem.contains(p.getDisplayName())) {
    5. if(p.getDisplayName().equals(ChatColor.stripColor(ig.getCustomName()))) {
    6. e.remove();
    7. BarAPI.setMessage(p, "Golem Health", 100F);
    8. golem.remove(p.getDisplayName());
    9. hasGolem.add(p.getDisplayName());
    10. p.getInventory().clear();
    11. MobDisguise d = new MobDisguise(DisguiseType.IRON_GOLEM);
    12. LivingWatcher w = (LivingWatcher) d.getWatcher();
    13. w.setCustomNameVisible(true);
    14. w.setCustomName(ChatColor.DARK_RED+""+p.getDisplayName());
    15. d.setWatcher(w);
    16. DisguiseAPI.disguiseEntity(p, d);
    17. //TODO REMOVE:
    18. primaryH.put(p.getDisplayName(), Primary.CHAINGUN);
    19. secondaryH.put(p.getDisplayName(), Secondary.ROCKET_SALVO);
    20. switch (primaryH.get(p.getDisplayName())) {
    21. case CHAINGUN: ItemStack chainGun = new ItemStack(Material.WOOD_HOE);
    22. ItemMeta cIM = chainGun.getItemMeta();
    23. cIM.setDisplayName(ChatColor.LIGHT_PURPLE+"XO-16 Chaingun");chainGun.setItemMeta(cIM);
    24. p.getInventory().setItem(0, chainGun);
    25. }
    26. switch (secondaryH.get(p.getDisplayName())) {
    27. case ROCKET_SALVO: ItemStack rocketSalvo = new ItemStack(Material.WOOD_AXE);
    28. ItemMeta rIM = rocketSalvo.getItemMeta();
    29. rIM.setDisplayName(ChatColor.DARK_PURPLE+"Rocket Salvo");
    30. rocketSalvo.setItemMeta(rIM);
    31. p.getInventory().setItem(1, rocketSalvo);
    32. }
    33. p.setFoodLevel(6);
    34. }
    35. }
    36. }
    37. }

    ArrayList Player being tested for if contains:
    Code:java
    1. public Boolean isGolem(Player p) {
    2. if(hasGolem.contains(p.getDisplayName())) {
    3. return true;
    4. }
    5. return false;
    6. }

    Problem is it is always coming up as false meaning it doesn't contain them however it does... Please help, thanks. - Jared
     
  2. Offline

    Insomniac10102

    My first thought is that you're not making it past that second if clause:
    Code:java
    1. if(p.getDisplayName().equals(ChatColor.stripColor(ig.getCustomName())))

    Are you positive you're making it that far?

    If that's not the issue, then I'd suggest storing users based on some other data instead, like their UID for example. Is the display name an important part of the process?
     
  3. Offline

    MayoDwarf

    Insomniac10102 Could use either or, but I prefer using Display Name as it is less typing to get the player. Also the problem is the ArrayList and yes it does make it that far...

    BUMP Anyone else?

    Anyone?

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

    ChazSchmidt

    Where is isGolem being called from? That might be issue.
     
  5. Offline

    Rocoty

    MayoDwarf I think it would help if you told us whether this code is in the main class or not, and in which context the methods are called. Maybe post the entire class and the entire main class along with it?
     
  6. Offline

    coasterman10

    Don't use their display name. Use UUIDs instead.
     
  7. Offline

    MayoDwarf

    In a separate class. Here:
    Code:java
    1. final int task1 = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable() {
    2. int count = 4;
    3. @Override
    4. public void run() {
    5. count--;
    6. if(count >= 1) {
    7. if(g.isGolem(p)) {
    8. short j = (short)(((int)i.getType().getMaxDurability())*count);
    9. p.getInventory().removeItem(i);
    10. ItemStack chainGun = new ItemStack(i.getType(), i.getAmount(), j);
    11. ItemMeta cIM = chainGun.getItemMeta();
    12. cIM.setDisplayName(ChatColor.LIGHT_PURPLE+"XO-16 Chaingun"+ChatColor.RED+" - Reloading... "+count);
    13. chainGun.setItemMeta(cIM);
    14. p.getInventory().setItem(0, chainGun);
    15. p.updateInventory();
    16. }
    17. } else {
    18. if(g.isGolem(p)) {
    19. cooldown.remove(p.getDisplayName());
    20. shots.remove(p.getDisplayName());
    21. p.getInventory().removeItem(i);
    22. ItemStack chainGun = new ItemStack(i.getType(), i.getAmount());
    23. ItemMeta cIM = chainGun.getItemMeta();
    24. cIM.setDisplayName(ChatColor.LIGHT_PURPLE+"XO-16 Chaingun");
    25. chainGun.setItemMeta(cIM);
    26. p.getInventory().setItem(0, chainGun);
    27. p.updateInventory();
    28. }
    29. }
    30. }
    31. }, 0, 20);


    coasterman10 ChazSchmidt Rocoty

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

    Rocoty

    MayoDwarf That's not the entire class. Can you post the entire enclosing top level outer class and the main class?
     
  9. Offline

    Wingzzz

    My recommendation is that you debug your control flow by logging information where you're unsure if the check was passed or not. This way you can determine where it's failing and fix it quickly.
     
  10. Offline

    MayoDwarf

    Last edited by a moderator: Jun 8, 2016
  11. Offline

    Wingzzz

    If you're making a List that is not static, in a singleton, or in a defined manager/registry that you're using- you cannot expect the results to be consistent throughout each instance of Golem. You're making a new list each time you create a "new Golem(...);", so unless you only use 1 Golem, the results will vary.

    I see numerous issues with your code. It's all over the place, messy, you're using things such as a switch statement for 1 case- you're returning a Boolean object when it always returns a non-null value (which isn't an issue if you need the ability to nullify it later down the road although seeing as it looks like you only use the method for a check there would be no need).

    So I feel like there's a few things you need to change and clean up, as well as a few things you need to learn in order to better design your classes for what you need. I'm willing to help you if you're willing to work on it.

    What is each class meant to be for?
     
  12. Offline

    MayoDwarf

    Wingzzz
    I do not need your help in this. The only help I need is to figure out how I can get Golem g as not null without initializing it every time the class is called. Thanks. - Jared PS: Found it necessary to tag the people to have been posting here, Rocoty, coasterman10, ChazSchmidt
     
  13. Offline

    Wingzzz

    MayoDwarf
    That's a shame. There are numerous areas in which you can improve your code and I'm still unsure how you use these classes and what they're ultimately for. Only thing I can think of currently is what I stated previously...
    Regardless, hope you find the solution!
     
  14. Offline

    MayoDwarf

    Anyone!?

    ?
     
  15. Offline

    MayoDwarf

  16. Offline

    xTigerRebornx

    MayoDwarf Its hard to help when you've removed the pastes to the classes, as well as when you only post snippets of each class. Mind posting all (relevant) classes?
     
  17. Offline

    MayoDwarf

    I've solved all the problems. The only question I have is how to have the Golem class as a variable in the SalvoAndChain class instead of doing Golem g = new Golem(this) as it is initialized every time the class is. xTigerRebornx
     
  18. Offline

    xTigerRebornx

    MayoDwarf Could you post the SalvoAndChain class?
     
  19. Offline

    MayoDwarf

  20. Offline

    xTigerRebornx

    MayoDwarf Is Golem a per-player class, or a manager of sorts? I'm not sure what the 'Golem' class actually does, may I see that as well?
     
  21. Offline

    MayoDwarf

    xTigerRebornx The Golem class has an ArrayList and that is what I am trying to access in the SalvoAndChain class.
     
  22. Offline

    xTigerRebornx

    MayoDwarf But, does your Golem class require a new instance to be made everytime your SalvoAndChain has a new instance made? If not, you could try implementing a Singleton-like design for your Golem class.
     
  23. Offline

    MayoDwarf

    xTigerRebornx Nope they don't every time, they only need it once.
     
  24. Offline

    xTigerRebornx

    MayoDwarf Then have some sort of way to keep a single instance? Use a singleton design, only reference one instance globally, etc.
     
  25. Offline

    MayoDwarf

  26. Offline

    xTigerRebornx

    MayoDwarf
    Code:
    public class SingletonExample{
     
     
      private SingletonExample(){} // Private constructor
      private static SingletonExample instance = new SingletonExample(); // Not Thread-safe, I believe
      // Public getter
      public static SingletonExample getInstance() { return instance; }
     
     
    }
    Some thread-safe examples on http://en.wikipedia.org/wiki/Singleton_pattern
     
  27. Offline

    MayoDwarf

    Still need help with this as xTigerRebornx 's example did not work... Please help! Thank you - Jared
     
Thread Status:
Not open for further replies.

Share This Page