Solved Access HashMap in Interface

Discussion in 'Spigot Plugin Development' started by Nitoblock, Jun 17, 2018.

  1. Hi! I have a big problem, I want to access my HashMap in different classes, also in an interface. The problem with interfaces is that they don't have a constructor. So I tried static, but it loses values. Please, I need it fast, thanks
     
  2. I presume you mean you need to access it in the default implementation of an interface? Making it static should work, if it's "losing values" try making it final, you're probably reassigning it in some other class. Afraid I can't do much more to help without an actual example, your explanation is somewhat vague.

    Sent from my SM-G903F using Tapatalk
     
  3. JanTuck

    Supporter

    Use an Abstract class that implements your interface for this purpose instead of trying to use the interface for it.


    Could you let me know why you need to access the map in an interface? I think this might be a http://xyproblem.info/
     
    • Agree Agree x 2
  4. How can I do that? My inferface is a custom Player interface

    Edit: I want to make a player cache
     
  5. Have you read the previous 2 threads at all? I have given an answer and also mentioned that more explanation is needed for us to properly help you, and other guy (can't see your name from TapaTalk, sorry dude) said that this is probably not what you should be doing.

    Now instead of making a meaningless post just asking for help again, elaborate and help us to help you.

    Also, I'm now even more sure you shouldn't be doing what you're doing - to get a Custom player interface you will have to be overriding CraftPlayer which generally shouldn't be done for version compatibility (unless that's what you're aiming for, although chances are you should either be doing it differently or just using Player with your implementation). Chances are, you should be using a Wrapper class for Player, not whatever you're doing.

    Sent from my SM-G903F using Tapatalk
     
  6. That is the bit of code where the HashMap is:
    Code (Java):
    public class Main extends JavaPlugin {
        public HashMap<Player, PlayerCache> cache = new HashMap<>();

        @Override
        public void onEnable() {
            /* ... */
        }
    }
    And I wanted to put the map static to use

    Code (Java):
    Main.cache.someMethod();
    Not working because cache in static does lose value in another class
     
  7. No it doesn't. You're either re-assigning it somewhere else (try making it final) or calling Map#clear or Map#remove



    Sent from my SM-G903F using Tapatalk
     
    • Agree Agree x 1
  8. There is nowhere I am re-assigning the map or clearing it. The only thing I use is remove with an arg. I tried to remove it, to add final to map, but same

    Edit: The problem was that I haven't set the value that I wanted (after 3 hours of searching), I am sooooo dumb, thanks anyways for the help :)
     
    #8 Nitoblock, Jun 17, 2018
    Last edited: Jun 18, 2018