#getConfig in other class than Main

1 messages · Page 1 of 1 (latest)

keen anchor
#

My code:https://pastebin.com/rHk8qW4c

Im trying to generate random loot on the playerJoinEvent. My issue is, that i cant use:
public void loadConfig(){
getConfig().options().copyDefaults(true);
saveConfig();
}
because im not in my Main class. How can i use getConfig and SaveConfig in another class?

Thanks in advance 🙂

bright apex
#

private static final FileConfiguration config = OwnStats.getInstance().getConfig();

#

String message = config.getString("message");

keen anchor
#

thanks, but what do you mean with OwnStats? It wont show on my IDE at all

bright apex
#

a

#

m

#

this is the main class

#

OwnStats

#

private static OwnStats instance;

#

@Override
public void onEnable() {
instance = this;

#

public static OwnStats getInstance() {
return instance;
}

#

Write again, what do you need?

keen anchor
# bright apex Write again, what do you need?

im confused: so my main should look like this: https://pastebin.com/uN8mJWbq and my random loot Class like this? https://pastebin.com/7yRUghUA

bright apex
#

one sec

#

Do you need to get a string from the config?

#

@keen anchor

keen anchor
#

yeah pretty much

bright apex
#

Okay, I'll show you how it's done with me

#

one sec

keen anchor
#

i want to add specific items to the random loot pool so i created a randomutils class where i list my items

keen anchor
bright apex
#
import org.bukkit.plugin.java.JavaPlugin;

public final class OwnStats extends JavaPlugin {

    private static OwnStats instance;

    @Override
    public void onEnable() {
        instance = this;
        Config.loadConfig();
    }

    @Override
    public void onDisable() {
            }
        }   
 }
#

Main class

#
public class Config {

    private static final FileConfiguration config = OwnStats.getInstance().getConfig();

    public static void loadConfig() {
        File config = new File(OwnStats.getInstance().getDataFolder() + File.separator + "config.yml");
        if (!config.exists()) {
            OwnStats.getInstance().getConfig().options().copyDefaults(true);
            OwnStats.getInstance().saveDefaultConfig();
        }
    }

    public static String message(Player p) {
        String name = config.getString("ownstats.time.test-message");
        return name(p, name);
    }
#
private static String name(Player p, String name) {
        name = name.replace("&", "§");
        return name;
    }
}
#

Config class

#

may List<String>

#

Write to me if something doesn't work out

#

@bright apex

keen anchor
#

Will do thanks 😄

bright apex
#

np

keen anchor
#

It only says create method in 'getConfig' in RandomLoot

#

@bright apex

bright apex
#

?

#

i'm Russian

#

bad translator)

#

Write again in more detail

keen anchor
#

My IDE marks it as red and tells me to create a method for getConfig

bright apex
#

screen?

keen anchor
bright apex
#

add to

#

Main class

#

oh

#

sorry

#
import org.bukkit.plugin.java.JavaPlugin;

public final class OwnStats extends JavaPlugin {

    private static OwnStats instance;

    @Override
    public void onEnable() {
        instance = this;
        Config.loadConfig();
    }

    @Override
    public void onDisable() {
            }
        }   
public static OwnStats getInstance() {
        return instance;
    }
 }
keen anchor
#

the instance part works now. But it still wants me to create a method for getConfig

bright apex
#

OwnStats.getInstance()

#

OwnStats.getInstance().getConfig().options().copyDefaults(true);
OwnStats.getInstance().saveDefaultConfig();

keen anchor
#

Problem solved. But if i load the plugin i get this error code: Caused by:
java.lang.NullPointerException: Cannot invoke "de.jonah.main.Main.getConfig()" because the return value of "de.jonah.main.Main.getInstance()" is null
at de.jonah.utils.Config.<clinit>(Config.java:11) ~[?:?]

#

@bright apex

bright apex
#

show me the code

keen anchor
bright apex
#

code

keen anchor
bright apex
#

show

#

me

#

config.yml

keen anchor
#

the one you created ?

bright apex
#

config.yml

#

your config

keen anchor
#

i said i want to add this its not finished

#

i just want to give the player random loot on join. but i could not use getConfig in my random class

bright apex
#

So you don't need config?

#

config.yml

keen anchor
#

no not yet

bright apex
#

Need it or not?)

keen anchor
#

no

bright apex
#

oh

#

sorry

keen anchor
#

i just could not use getConfig and save config at all because its not in my main calss

#

class*

bright apex
#

I don't understand you

#

do you need to get a random premet?

keen anchor
#

nevermind, it all works just fine 😄

#

thank you for your help

bright apex
#

okay

keen anchor
#

@bright apex you still there?

bright apex
#

yes

#

@keen anchor

keen anchor
#

sry for answering that late. It all works just fine. I try to create a constructor for getConfig so i can access it somewhere else

#

But i dont know how to do it properly

#

I dont want to have my actual code in the main class, so i have to use a constructor to pass getConfig

#

@bright apex

bright apex
#

Okay

#

@keen anchor

#
public class Config {

    private static final FileConfiguration config = OwnStats.getInstance().getConfig();

    public static void loadConfig() {
        File config = new File(OwnStats.getInstance().getDataFolder() + File.separator + "config.yml");
        if (!config.exists()) {
            OwnStats.getInstance().getConfig().options().copyDefaults(true);
            OwnStats.getInstance().saveDefaultConfig();
        }
    }

    public static String name(Player p) {
        String name = config.getString("ownstats.time.test-message");
        return name(p, name);
    }


    private static String name(Player p, String name) {
        name = name.replace("&", "§");
        name = name.replace("%player%", "" + p.getName());
    return name;
}
#

Config class

#
public final class OwnStats extends JavaPlugin {

    private static OwnStats instance;

    @Override
    public void onEnable() {
        instance = this;
        Config.loadConfig();
    }

    @Override
    public void onDisable() {
            }
        }
   
    public static OwnStats getInstance() {
        return instance;
       }
 }
#

Main class

keen anchor
#

ok but how do i use it in my RandomLoot class, how do i access it

#

@bright apex

bright apex
#

@keen anchor

#

if you don't make placeholders like %player%, there is no point in passing p

keen anchor
#

I think we are talking at cross purposes.

getConfig is part of JavaPlugin... I have to access it in my RandomLoot.class in order to load my config etc.

bright apex
#

MainClass.getInstance()

#
public final class OwnStats extends JavaPlugin {

    private static OwnStats instance;

    @Override
    public void onEnable() {
        instance = this;
        Config.loadConfig();
    }

    @Override
    public void onDisable() {
            }
        }
   
    public static OwnStats getInstance() {
        return instance;
       }
 }
#

getInstance()

#

Write this in the main class and you can use getInstance in other classes

#
private static OwnStats instance;
#
instance = this;
#
public static OwnStats getInstance() {
        return instance;
       }
#

Change it for yourself

keen anchor
#

thanks buddy works!

#

can you help me with another problem?

#

@bright apex

bright apex
keen anchor
#

the last one is the error code

#

each time a skeleton shoots me i get this error...

#

@bright apex

bright apex
#

yes

#

Try adding a check on the player

#

@keen anchor

keen anchor
#

a null check?

#

i already have one..

bright apex
#

one sec

keen anchor
#

First i cast the Player to shooter: Player shooter = (Player) projectile.getShooter();

And after this i check if its null...

    if (shooter == null) return;
#

If its not a player it should return, or am i wrong?

bright apex
#

@keen anchor

#
if (e.getEntityType() != EntityType.PLAYER)
            return;
#

there's

#

more

#

getHitEntity()

#

getHitEntity()
Gets the entity that was hit, if it was an entity that was hit.

#

getEntity()
Returns the Entity involved in this event

keen anchor
#

now it works when a skeleton actually hits me but i still prints the error when it shoots

#

@bright apex

bright apex
#

hmm

#

@keen anchor

#
if (!(e.getEntity().getShooter() instanceof Player))
            return;
keen anchor
#

@bright apex still get the error code 😦

bright apex
#

send me

#

error

bright apex
#

error

keen anchor
bright apex
#

one sec

#

send me code

keen anchor
bright apex
#

no

#

try this:

#

send me code

#

not error

keen anchor