I'm trying to make a minecraft plugin. I want it to create variable for every player. I'm using "for" for the loop. I'm trying to add the player's name to the variable's name. Can anyone help?
String name = "";
for (Player p : Bukkit.getOnlinePlayers()) {
name = p.getName();
int cheatdetection+name = 0;
int looking = (Math.round(p.getLocation().getPitch()) + 270) % 360;
int currentlooking = looking;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
if(looking > currentlooking+20 || looking < currentlooking-20) {
cheatdetection+name++;
}
}
if(cheatdetection >= 10) {
Bukkit.getBanList(Type.NAME).addBan(p.getName(), "You have been banned for using Baritone", todate1, "HackMiner Detection");
p.kickPlayer("HackMiner Detection");
}
}
It sounds like you want a Map with a key for every player. A Map creates an association between one variable (a key) and another (a value). Values are set using the put(key, value) method, and they retrieved using 'get(key)'
// Create an Integer counter for every player, initialized to zero.
Map<Player, Integer> playerCheatCounter = new HashMap<>();
for (Player p : Bukkit.getOnlinePlayers()) {
playerCheatCounter.put(p, 0);
}
// Now to use it:
for (Player p : Bukkit.getOnlinePlayers()) {
int looking = p.getPitch();
Thread.sleep(10)
int currentlyLooking = p.getPitch();
if (looking > currentlyLooking + 20) {
// Increment counter for the current player
playerCheatCounter.put(p, 1 + playerCheatCounter.get(p));
// Check the stored value for the current player
if (playerCheatCounter.get(p) > 10) {
// Ban player
}
}
}
Edit: Fixed some syntax errors