#so i have been making a plugin for a minecraft smp of mine but im running into some problems
1 messages · Page 1 of 1 (latest)
everytime i join the server it keeps assigning random lives
even though its only supposed to do it once and thats when joining for first time
and which function reads it from a file
package com.strix.voidsmp;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
public class PlayerDataManager {
private final JavaPlugin plugin;
private FileConfiguration playerDataConfig;
private final File playerDataFile;
public PlayerDataManager(JavaPlugin plugin) {
this.plugin = plugin;
this.playerDataFile = new File(plugin.getDataFolder(), "playerdata.yml");
if (!playerDataFile.exists()) {
plugin.saveResource("playerdata.yml", false);
}
loadPlayerData();
}
public int getPlayerLives(UUID playerId) {
String playerKey = playerId.toString();
if (playerDataConfig.contains("players." + playerKey + ".lives")) {
return playerDataConfig.getInt("players." + playerKey + ".lives");
}
return 0;
}
public void setPlayerLives(UUID playerId, int lives) {
String playerKey = playerId.toString();
playerDataConfig.set("players." + playerKey + ".lives", lives);
savePlayerData();
}
public void savePlayerData() {
try {
playerDataConfig.save(playerDataFile);
} catch (IOException e) {
plugin.getLogger().warning("Failed to save player data file!");
e.printStackTrace();
}
}
public void loadPlayerData() {
playerDataConfig = YamlConfiguration.loadConfiguration(playerDataFile);
}
}
i havent tried it
and im not that experienced
so can u tell me what i should be doing to try and resolve this issue?
should i try that?
wait i thought i was in another thread
id return an OptionalInt in functions that might not return a reasonable default int value
oh
i dont really see anything wrong with that code though
maybe a default value is returned, try printing it
yea but it doesnt seem to work
print what values get returned, and think what should be expected
printing the playerjoin event right
print the values of the lives
could u elaborate?
oh i get it
make the event print out in the console
i see
k so
after looking at console
it says it assigned me a life and then another text says that i was assigned 0 random lifes
which is the print i put in the code
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
UUID playerId = player.getUniqueId();
int lives = playerDataManager.getPlayerLives(playerId);
System.out.println("Player " + player.getName() + " has " + lives + " lives.");
if (lives == 0) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "assignlives random " + player.getName());
lives = playerDataManager.getPlayerLives(playerId);
System.out.println("Player " + player.getName() + " was assigned " + lives + " random lives.");
}
setDisplayNameColor(player, lives);
playerDataManager.setPlayerLives(playerId, lives);
}```
mm
what do you think?
after rejoining it does this
so basically it finds that i have 0 lives and then give me a random amount of lives but then that random amount of lives become 0
so when i rejoin it re assigns me lives
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "assignlives random " + player.getName()); ???
why are you using a command
since that was the command i use to assign lives
but the command is from your plugin isnt it?
yea
theeeen
use the methods that command uses
you shouldnt dispatch commands especially if they are your own
you have tons of other ways
wdym
Go to your command class
k
ill send it here
okay
my first recomendation is
make a class that has multiple methods to spilt this all up
that way you can use for example
the random method
wdym? (im still very new to this)
Okay so
its possible to have methods
for example
public void sayhi(){
System.out.println("Hi");
}
and use them anywhere else in your code
even in other classes
that allows you to not have duplicate code
oh
so if you are trying to do the same thing somewhere else again
do i just mention sayhi?
you can just call that method and let your previously made code help you
that was an example
you can make a method for anything
ye ik
well to be more case specific here
so what should i be making methods for?
you can make a method for
public void giveRandomLives(Player player){
//do what ever
}
do the methods ahve to be in main class or it can be anywhere?
oh
it sounds scarry but its really not
to make it short
every time you do new Class();
you create an so called instance of this class
using that instance you can then always call its methods
for example
MyAwsomeClass class = new MyAwsomeClass();
now i can do
class.randomMethodIHaveInThisClass();
in your case
you could create a PlayerLiveManager class or what ever
with methods like
giveLives(Player player, int lives)
giveRandomLives(Player player)
getLives(Player player)
etc etc
yes
yea
ingame commands are totally fine
you just shouldnt use them though your plugin
also
another advantage of bundling code
oh k
lets say theres a bug in your giveRandomLives
and you are giving random lives in 3 places
you have to fix it in all 3 places
but if you have a global method they all use
you only need to fix it once
this is my PlayerDataManager class
package com.strix.voidsmp;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
public class PlayerDataManager {
private final JavaPlugin plugin;
private FileConfiguration playerDataConfig;
private final File playerDataFile;
public PlayerDataManager(JavaPlugin plugin) {
this.plugin = plugin;
this.playerDataFile = new File(plugin.getDataFolder(), "playerdata.yml");
if (!playerDataFile.exists()) {
plugin.saveResource("playerdata.yml", false);
}
loadPlayerData();
}
public int getPlayerLives(UUID playerId) {
String playerKey = playerId.toString();
if (playerDataConfig.contains("players." + playerKey + ".lives")) {
return playerDataConfig.getInt("players." + playerKey + ".lives");
}
return 0;
}
public void setPlayerLives(UUID playerId, int lives) {
String playerKey = playerId.toString();
playerDataConfig.set("players." + playerKey + ".lives", lives);
savePlayerData();
}
public void savePlayerData() {
try {
playerDataConfig.save(playerDataFile);
} catch (IOException e) {
plugin.getLogger().warning("Failed to save player data file!");
e.printStackTrace();
}
}
public void loadPlayerData() {
playerDataConfig = YamlConfiguration.loadConfiguration(playerDataFile);
}
}
yea
now you can add giveRandomLives to it
its supposed to save the player data
mhm
so i can add methods in this class?
sure if you want
but when i restart server or specify the save command it doesnt save it goes empty file
then check out your method that handles saving
or
see if it even gets called at the right places
both could be an issue
how would i see if it gets called at the right places
debugging
add print statements
and check if they get called when you expect them to get called
so i should add it to the playerdata saving method right
yea
that is after i used the add lives
then it shows my lives in the playerdata.yml
but by default after joining it doesnt show my random lives
in the playerdata.yml its just 0
uhmm
...
so
it saves correctly first time
and then it gives me 0 random lives and then saves
that might be the problem
@safe axle could u gime a heads up?
k
okay can you show me what your management class for this looks like rn
and how you use it
i ended up taking ur advice and implemented a methods class
idk if its good
ill send it to u
sure
package com.strix.voidsmp;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import java.util.UUID;
public class PlayerLivesMethods {
private PlayerDataManager playerDataManager;
public PlayerLivesMethods(PlayerDataManager playerDataManager) {
this.playerDataManager = playerDataManager;
}
public void assignRandomLives(UUID playerId) {
int randomLives = generateRandomLives();
playerDataManager.setPlayerLives(playerId, randomLives);
playerDataManager.savePlayerData();
Player player = Bukkit.getPlayer(playerId);
if (player != null) {
player.sendMessage(ChatColor.GREEN + "You have been assigned " + randomLives + " lives.");
}
}
public void removeLife(UUID playerId) {
int currentLives = playerDataManager.getPlayerLives(playerId);
if (currentLives > 0) {
int newLives = currentLives - 1;
playerDataManager.setPlayerLives(playerId, newLives);
playerDataManager.savePlayerData();
}
}
public void addLife(UUID playerId) {
int currentLives = playerDataManager.getPlayerLives(playerId);
int newLives = currentLives + 1;
playerDataManager.setPlayerLives(playerId, newLives);
playerDataManager.savePlayerData();
}
private int generateRandomLives() {
return (int) (Math.random() * 15) + 1;
}
}
here
but when joining it gives me a failed to pass playerjoinevent error
show me this line at com.strix.voidsmp.VoidSMP.onPlayerJoin(VoidSMP.java:68) ~[?:?]
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
UUID playerId = player.getUniqueId();
int lives = playerDataManager.getPlayerLives(playerId);
System.out.println("Player " + player.getName() + " has " + lives + " lives.");
if (lives == 0) {
livesMethods.assignRandomLives(playerId);
lives = playerDataManager.getPlayerLives(playerId);
System.out.println("Player " + player.getName() + " was assigned " + lives + " random lives.");
playerDataManager.savePlayerData();
}
setDisplayNameColor(player, lives);
}
k
livesMethods.assignRandomLives(playerId);
onPlayerJoin
k
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
UUID playerId = player.getUniqueId();
int lives = playerDataManager.getPlayerLives(playerId);
System.out.println("Player " + player.getName() + " has " + lives + " lives.");
if (lives == 0) {
livesMethods.assignRandomLives(playerId);
lives = playerDataManager.getPlayerLives(playerId);
System.out.println("Player " + player.getName() + " was assigned " + lives + " random lives.");
playerDataManager.savePlayerData();
}
setDisplayNameColor(player, lives);
}
its not a seperate class its in the main class btw
so what do u think?
also 1 thing i forgot to tell you my /assignlives add command works perfectly on the playerdata.yml but the remove command makes everyones lives go 0
idk why
and also when someone dies
it becomes 0
@safe axle sooo
why not let assignRandomLives return the lives
wdym?
?
instead of querying it afterwards