#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)

wraith terrace
#

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

jagged ridge
#

and which function reads it from a file

wraith terrace
#
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);
    }
}
jagged ridge
#

why not inline loadPlayerData

#

or is that a reloading strategy?

wraith terrace
#

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?

wraith terrace
jagged ridge
#

wait i thought i was in another thread

#

id return an OptionalInt in functions that might not return a reasonable default int value

wraith terrace
#

oh

jagged ridge
#

i dont really see anything wrong with that code though

#

maybe a default value is returned, try printing it

wraith terrace
jagged ridge
#

print what values get returned, and think what should be expected

wraith terrace
#

printing the playerjoin event right

jagged ridge
#

print the values of the lives

wraith terrace
#

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

wraith terrace
#

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

safe axle
#

Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "assignlives random " + player.getName()); ???

#

why are you using a command

wraith terrace
safe axle
#

but the command is from your plugin isnt it?

wraith terrace
#

yea

safe axle
#

theeeen

#

use the methods that command uses

#

you shouldnt dispatch commands especially if they are your own

#

you have tons of other ways

wraith terrace
#

wdym

safe axle
#

Go to your command class

wraith terrace
#

k

safe axle
#

check how exactly you are setting their lives

#

take that

wraith terrace
#

ill send it here

safe axle
#

and put it in there

#

instead of calling the command

#

sure

wraith terrace
safe axle
#

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

wraith terrace
#

wdym? (im still very new to this)

safe axle
#

on both your command

#

and your join class

safe axle
#

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

wraith terrace
#

oh

safe axle
#

so if you are trying to do the same thing somewhere else again

wraith terrace
#

do i just mention sayhi?

safe axle
#

you can just call that method and let your previously made code help you

safe axle
#

you can make a method for anything

wraith terrace
safe axle
#

well to be more case specific here

wraith terrace
#

so what should i be making methods for?

safe axle
#

you can make a method for
public void giveRandomLives(Player player){
//do what ever
}

wraith terrace
#

do the methods ahve to be in main class or it can be anywhere?

safe axle
#

anywhere

#

but

#

you need to understand how classes work and what statics are

wraith terrace
#

oh

safe axle
#

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

wraith terrace
#

oh

#

so instead of using the /commands i should do that

safe axle
#

yes

wraith terrace
#

but i do also need the /commands for ingame

#

so can i just keep them?

safe axle
#

yea

#

ingame commands are totally fine

#

you just shouldnt use them though your plugin

#

also

#

another advantage of bundling code

wraith terrace
#

oh k

safe axle
#

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

wraith terrace
#

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);
    }
}
safe axle
#

okay

#

this is already good

#

thats a good class

wraith terrace
#

yea

safe axle
#

now you can add giveRandomLives to it

wraith terrace
#

its supposed to save the player data

safe axle
#

mhm

wraith terrace
safe axle
#

sure if you want

wraith terrace
# safe axle mhm

but when i restart server or specify the save command it doesnt save it goes empty file

safe axle
#

then check out your method that handles saving

#

or

#

see if it even gets called at the right places

#

both could be an issue

wraith terrace
#

how would i see if it gets called at the right places

safe axle
#

debugging

#

add print statements

#

and check if they get called when you expect them to get called

wraith terrace
#

so i should add it to the playerdata saving method right

safe axle
#

yea

wraith terrace
#

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

wraith terrace
#

so

#

it saves correctly first time

#

and then it gives me 0 random lives and then saves

#

that might be the problem

wraith terrace
#

@safe axle could u gime a heads up?

safe axle
#

well

#

give me a sec

wraith terrace
#

k

safe axle
#

okay can you show me what your management class for this looks like rn

#

and how you use it

wraith terrace
#

i ended up taking ur advice and implemented a methods class

#

idk if its good

#

ill send it to u

safe axle
#

sure

wraith terrace
#
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

safe axle
#

show me the entire error

#

?paste

worldly valeBOT
wraith terrace
#

im sending u my onjoinevent

safe axle
#

show me this line at com.strix.voidsmp.VoidSMP.onPlayerJoin(VoidSMP.java:68) ~[?:?]

wraith terrace
# wraith terrace im sending u my onjoinevent
@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);
    }
wraith terrace
safe axle
#

alright show me the entire class now

#

i have a feeling

wraith terrace
#

which class

#

main?

safe axle
#

onPlayerJoin

wraith terrace
#

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

jagged ridge
#

why not let assignRandomLives return the lives

wraith terrace
#

wdym?

jagged ridge
#

instead of querying it afterwards

safe axle
#

for example

#

assignRandomLives

#

could return the lives as an int

#

so you dont need to get them again

wraith terrace
#

oh

#

so basically add asignrandomlives AS the onplayerjoin event?

#

wait

#

now im confused