#Help with making variables per player

1 messages · Page 1 of 1 (latest)

gloomy drum
#

Hi guys. can i have some help with a plugin. So I've been trying to make a shop plugin however im struggling with 1 main issue.

Running it per player?
Like if 2 people use the shop simultaneously the variables get mixed with eachother such as prices, items etc.

What process would i go to make the command run separately with each person having their own version of the variables

I tried player variables but it ended up in a hot mess that i couldn't fix and i feel theres probably a more efficient way to do it

candid jewel
#

Have you looked into a Hashmap

umbral swan
#

java is object orientated, just create a new shop instance each time

hushed charm
#

X per Y always indicates that a Map of some sort is needed. Usually a HashMap (as the others have pointed out :))

sterile badge
#

ik its considered bad pratice to key your maps with the player instance but why not WeakHashMap<Player, Shop>, automatic cleanup

gloomy drum
umbral swan
#

Player.getUniqueId() works as key

hushed charm
#
public class ShopManager {

  private final Map<UUID, Shop> openShops;
  
  public ShopManager() {
    this.openShops = new HashMap<>();
  }
  
  public void openShop(UUID playerID, Shop shop) {
    this.openShops.put(playerID, shop);
  }
  
  public void closeShop(UUID playerID) {
    this.openShops.remove(playerID);
  }
  
  public boolean hasOpenShop(UUID playerID) {
    return this.openShops.containsKey(playerID);
  }
  
  public Shop getOpenShop(UUID playerID) {
    return this.openShops.get(playerID);
  }

}

Quick example of how this could be structured

gloomy drum
#

but i dont understand it enough and i got an issue then had a null issue i couldnt figure out

sterile badge
#

ig if you needto get it by UUID its useful

sterile badge
#

of UUIDs

#

?

candid jewel
#

oop

#

nevermind

sterile badge
#

ig those will get cleaned eventually too

hushed charm
#

Sure, when the jvm stops 🙂

#

Or do you mean a WeakMap with UUID keys?
Because those get thrown out within a second or so.

candid jewel
#

Yeah

gloomy drum
#

So, how do i set a player hash map? thingy

#

its a value i have right now thats null thats causing my problems

hushed charm
#

This only makes sense if you have learned all the java basics btw

gloomy drum
#

I have a variable i just wanna be like

map playeritem = itemInSlot

#

but for hash