Hot Summer Deals are Here!
Celebrate with up to 99% off on 17,700 resources
02
Days
00
Hours
26
Mins
31
Secs

Java - How do I make a counter for each player?

Status
This thread has been locked.

EpicFooF

Digital Artist
Premium
Feedback score
13
Posts
1,438
Reactions
520
Resources
2
I am trying to create a plugin that has a "warnings" feature.
It basically allows staff members to use /warn [player], and if a player reaches a specific amount of warnings he will be banned.
So my question is, how do I make a counter for each player? I don't want that all of the players will have the same amount of warnings.

Thanks for those who will help!
 
Last edited:
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

iTool

Owner of Dusk Islands - DuskIslands.mcpro.io
Premium
Feedback score
2
Posts
183
Reactions
79
Resources
0
Purchase Litebans[DOUBLEPOST=1492255076][/DOUBLEPOST]Jokes :p I don't know.
 

omarhachach

Your Friendly Neighbourhood Snorlax
Supreme
Feedback score
14
Posts
627
Reactions
359
Resources
0
I am trying to create a plugin that has a "warnings" feature.
It basically allows staff members to use /warn [player], and if a player reaches a specific amount of warnings he will be banned.
So my question is, how do I make a counter for each player? I don't want that all of the players will have the same amount of warnings.

Thanks for those who will help!
Associate the UUID of the user with an amount of warning points. You can store it in a database, file or however you please.

A good idea is to only create a row for a user with warning points, that way you can just assume that if the UUID isn't there, it has 0 warning points.
Every time you add a warning point, check the warning points to see if it has reached a certain number. If it has, do whatever you please.
 

EpicFooF

Digital Artist
Premium
Feedback score
13
Posts
1,438
Reactions
520
Resources
2
Associate the UUID of the user with an amount of warning points. You can store it in a database, file or however you please.

A good idea is to only create a row for a user with warning points, that way you can just assume that if the UUID isn't there, it has 0 warning points.
Every time you add a warning point, check the warning points to see if it has reached a certain number. If it has, do whatever you please.
You'd need to log them in flat file (not recommended), local database or MySQL. For starters, just use a flat file, create like additional bans.yml file, and log every warning, with player's uuid in there.

edit: looks like i was a second late :/
Alright, thanks.
 

subbotted

Contact on Discord, subbotted#5560
Supreme
Feedback score
17
Posts
524
Reactions
407
Resources
0
I am trying to create a plugin that has a "warnings" feature.
It basically allows staff members to use /warn [player], and if a player reaches a specific amount of warnings he will be banned.
So my question is, how do I make a counter for each player? I don't want that all of the players will have the same amount of warnings.

Thanks for those who will help!

Well this is just something I came up with for you just now. Not tested in eclipse idk if its correct but.

Code:
    public int getWarnings(UUID uuid){
        return getConfig().getInt("warnings." + uuid.toString());
    }
   
    public void addWarning(UUID uuid){
        setWarnings(uuid, getWarnings(uuid) + 1);
    }
   
    public void setWarnings(UUID uuid, int amount){
        getConfig().set("warnings." + uuid.toString(), amount);
    }


EDIT: edited method names :p feel free to use this
 
Last edited:

EpicFooF

Digital Artist
Premium
Feedback score
13
Posts
1,438
Reactions
520
Resources
2
Well this is just something I came up with for you just now. Not tested in eclipse idk if its correct but.

Code:
    public int getWarnings(UUID uuid){
        return getConfig().getInt("warnings." + uuid.toString());
    }
  
    public void addWarning(UUID uuid){
        setWarnings(uuid, getWarnings(uuid) + 1);
    }
  
    public void setWarnings(UUID uuid, int amount){
        getConfig().set("warnings." + uuid.toString(), amount);
    }


EDIT: edited method names :p feel free to use this
Thank you :)
 

EpicFooF

Digital Artist
Premium
Feedback score
13
Posts
1,438
Reactions
520
Resources
2
Alrighty so. You've got two options here I guess.

You can create a new HashMap, storing the uuid like so:

Code:
private Map<UUID, Integer> warnCounterMap;
//Initialize it however you want

Or you could go overkill and create a "WarnManager" class like so (I have time to kill lol).

Code:
public class WarnManager {
private static List<WarnManager> allManagers = new ArrayList<>();
public static List<WarnManager> getManagers(){ return allManagers; }

private static Map<UUID, WarnManager> managerCache = new HashMap<>();
public static Map<UUID, WarnManager> getManagerCache(){return managerCache;}
  
    private int warningCount;
  
    private UUID id;
  
  
    public WarnManager(Player p)
    {
        this.id = p.getUniqueId();
        loadWarningCount();
        allManagers.add(this);
        managerCache.put(p.getUniqueId(), this);
    }
  
    private void loadWarningCount()
    {
        //Load data however you want, check if player is in the database
        //And generate stuff accordingly.
        this.warningCount = HOWEVERYOUGETCOUNT;
    }
  
  
    public int getWarnings()
    {
        return this.warningCount;
    }
  
    public void incrementWarnings()
    {
        warningCount++;
        //Check for amount of warnings etc. do whatever you want.
    }
  
  
    public void handleLeave()
    {
        allManagers.remove(this);
        managerCache.remove(id);
        //Store the data however you want.
    }
  

  
  
    public static WarnManager getWarnManager(UUID id)
    {
        return getManagerCache().get(id);
    }
  
    public static WarnManager getWarnManager(Player p)
    {
        return getManagerCache().get(p.getUniqueId());
    }
  
}

Adapt it however you want ;)

Remember to call a new WarnManager object on the player join and to handle the leave on the leave.
Thanks a lot :)
 
Status
This thread has been locked.
Top