Java (Bukkit) how to acces to the Config with out this?

1.2k views Asked by At

I have two classes "main" for commands and more and "events" for events and now I want to make that the event "PlayerdeathEvent" teleport me to a point and the cordinates from the point are saved in a config now i test this:

  1. I made it in the event class

    public  void lbbkampfrespawn()
    {
    
    FileConfiguration cfg = this.getConfig();
    
    {
            String world = cfg.getString("LBBsetkampfrespawn1.world");
            double x = cfg.getDouble("LBBsetkampfrespawn1.x");
            double y = cfg.getDouble("LBBsetkampfrespawn1.y");
            double z = cfg.getDouble("LBBsetkampfrespawn1.z");
            double yaw = cfg.getDouble("LBBsetkampfrespawn1.yaw");
            double pitch = cfg.getDouble("LBBsetkampfrespawn1.pitch");
            Location loc = new Location(Bukkit.getWorld(world), x, y, z);
            loc.setYaw((float) yaw);
            loc.setPitch((float) pitch);
            p1.teleport(loc);
    }
    {
    
            String world = cfg.getString("LBBsetkampfrespawn2.world");
            double x = cfg.getDouble("LBBsetkampfrespawn2.x");
            double y = cfg.getDouble("LBBsetkampfrespawn2.y");
            double z = cfg.getDouble("LBBsetkampfrespawn2.z");
            double yaw = cfg.getDouble("LBBsetkampfrespawn2.yaw");
            double pitch = cfg.getDouble("LBBsetkampfrespawn2.pitch");
            Location loc = new Location(Bukkit.getWorld(world), x, y, z);
            loc.setYaw((float) yaw);
            loc.setPitch((float) pitch);
            p2.teleport(loc);
    }
    
    
    
    }
    
    
    }
    

Problem : the dont know about "getConfig()" eclipse said me to crathe the method getConfig()

  1. i made it in the main class with static

    public static void lbbkampfrespawn()
     {
    
    FileConfiguration cfg = this.getConfig();
    
    {
            String world = cfg.getString("LBBsetkampfrespawn1.world");
            double x = cfg.getDouble("LBBsetkampfrespawn1.x");
            double y = cfg.getDouble("LBBsetkampfrespawn1.y");
            double z = cfg.getDouble("LBBsetkampfrespawn1.z");
            double yaw = cfg.getDouble("LBBsetkampfrespawn1.yaw");
            double pitch = cfg.getDouble("LBBsetkampfrespawn1.pitch");
            Location loc = new Location(Bukkit.getWorld(world), x, y, z);
            loc.setYaw((float) yaw);
            loc.setPitch((float) pitch);
            p1.teleport(loc);
    }
    {
    
            String world = cfg.getString("LBBsetkampfrespawn2.world");
            double x = cfg.getDouble("LBBsetkampfrespawn2.x");
            double y = cfg.getDouble("LBBsetkampfrespawn2.y");
            double z = cfg.getDouble("LBBsetkampfrespawn2.z");
            double yaw = cfg.getDouble("LBBsetkampfrespawn2.yaw");
            double pitch = cfg.getDouble("LBBsetkampfrespawn2.pitch");
            Location loc = new Location(Bukkit.getWorld(world), x, y, z);
            loc.setYaw((float) yaw);
            loc.setPitch((float) pitch);
            p2.teleport(loc);
    }
    
    
    
    }
    

Problem : I dont can uset this. in static

What I have to do thanks for your help !

When you need more from the cod please send what.

Thanks. Sry for the spelling

1

There are 1 answers

0
Adrian Sohn On BEST ANSWER

Editor's Note:
A Deutsche version of the answer was included previously, but removed because posts should be in English. If you would like to see it, use the revision history.

The getConfig() method is inherited from the JavaPlugin class which only one of your classes should extend. There are many ways you can access the config file from another class though. One could, for example, keep a reference to your "main" class or plugin (the one that extends JavaPlugin) and to then call getConfig() using that object/reference. You could also create a static getter method that returns the instance of your plugin (not sure if this is good practice). Eclipse is probably telling you to create the getConfig() method because you are calling it in a class that doesn't extend JavaPlugin.

For example, let's say your class that extends JavaPlugin is called "Main". If you for example have a listener class that wants to access your Main class, you could ask for this information in the constructor for the listener class. In your listener class you would have:

public class MainListener implements Listener {

    private Main plugin;

    public MainListener(Main plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onRespawn(PlayerRespawnEvent event) {
        FileConfiguration config = plugin.getConfig();
    }

As you can see, this enables you to use plugin.getConfig() anywhere in your MainListener class. You would instantiate the listener (for example in the onEnable() method of your Main class) with new MainListener(this);. Don't forget to register the listener of course.

I understand what you are trying to do here but I believe there is a better way to do it (teleporting the player while he is dead is glitchy or plain doesn't work). I'm not sure why you made your method static, are you calling it in the event method? P1 and p2 are not being passed as arguments in the method. This is what I would do (tried this and it worked): Instead of teleporting the player on the event of a death, change the respawn location when a player respawns like so:

@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event) {
    FileConfiguration config = getConfig(); //Or whichever method you are using
    World world = Bukkit.getWorld(config.getString("world"));
    double x = config.getDouble("x");
    double y = config.getDouble("y");
    double z = config.getDouble("z");
    float yaw = (float) config.getDouble("yaw");
    float pitch = (float) config.getDouble("pitch");
    event.setRespawnLocation(new Location(world, x, y, z, yaw, pitch));
}

Here is my test config.yml file:

world: world
x: 0
y: 80
z: 0
yaw: 0
pitch: 0

Note: I didn't check whether the world with that name really exists and whether the location is a safe one for the player to teleport to. That would be things you would need to implement.