• Es freut uns dass du in unser Minecraft Forum gefunden hast. Hier kannst du mit über 130.000 Minecraft Fans über Minecraft diskutieren, Fragen stellen und anderen helfen. In diesem Minecraft Forum kannst du auch nach Teammitgliedern, Administratoren, Moderatoren , Supporter oder Sponsoren suchen. Gerne kannst du im Offtopic Bereich unseres Minecraft Forums auch über nicht Minecraft spezifische Themen reden. Wir hoffen dir gefällt es in unserem Minecraft Forum!

String wird als null bezeichnet bei Config-Ausgabe

KaeseKuchen_DEV

Redstoneengineer
Registriert
11 Mai 2016
Beiträge
47
Diamanten
300
Minecraft
KaeseKuchenDEV
Schönen guten Tag

Mein heutiges Problem ist das ein String bei einer Config ausgabe als Null bezecihnet wird obwohl er in der config existiert

Code für die methode die die config schreibt:

Code:
@Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(sender instanceof Player){
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("setKnockSpawn")){
                if(p.hasPermission("kbffa.setSpawn")){
                    configHandler = Main.configHandler;
                    if(args.length == 1){
                        int anz = configHandler.getInt("Maps.anz");
                        String path = "Maps." + anz + 1;
                        String displayName = args[0];
                       
                        configHandler.set(path + ".SLocation.X", p.getLocation().getX());
                        configHandler.set(path + ".SLocation.Y", p.getLocation().getY());
                        configHandler.set(path + ".SLocation.Z", p.getLocation().getZ());
                        configHandler.set(path + ".SLocation.YAW", (int)p.getLocation().getYaw());
                        configHandler.set(path + ".SLocation.PITCH", (int) p.getLocation().getPitch());
                        configHandler.set(path + ".SLocation.WORLD",  p.getLocation().getWorld().getName());
                        configHandler.set(path + ".SLocation.DISPLAY", displayName);
                       
                       
                       
                       
                        configHandler.set("Maps.anz", anz + 1);
                        Main.getInstance().saveConfig();
                    }else{
                        p.sendMessage(Main.getPrefix() + "Bitte benutze /setKnockSpawn [Name]");
                    }
                }else{
                    p.sendMessage(Main.getPrefix() + "Dazu hast du keine Berechtigung!");
                }
            }
        }else{
            sender.sendMessage("[KnockBackFFA] Du musst ein Spieler sein um diesen Befehl auszuführen!");
        }
       
        return true;
    }

Code zum auslesen der Config:

Code:
public void loadMaps(){
        int anz = configHandler.getInt("Maps.anz");
        for(int i = 1; i <= anz; i++){
            String path = "Maps." + i;
           
            Double x = configHandler.getDouble(path + "SLocation.X");
            Double y = configHandler.getDouble(path + "SLocation.Y");
            Double z = configHandler.getDouble(path + "SLocation.Z");
            Integer yaw = configHandler.getInt(path + "SLocation.YAW");
            Integer pitch = configHandler.getInt(path + "SLocation.PITCH");
           
            String displayName = configHandler.getString(path + "SLocation.DISPLAY");
           
            Location loc = new Location(Bukkit.getWorld(path + "SLocation.WORLD"), x, y, z, yaw, pitch);
            worlds world = new worlds(path + "SLocation.WORLD", loc, displayName);
           
            this.mapmanager.addWorld(world);
           
            System.out.println("[KnockBackFFA] Die Map" + displayName + "wurde geladen!");
           
        }
    }

Config:
Code:
KnockBackFFA:
  Prefix: '&7[&6KnockBackFFA&7] &6'
Maps:
  anz: 1
  '01':
    SLocation:
      X: 206.24793193541078
      Y: 69.0
      Z: 310.3395077010187
      YAW: 273
      PITCH: 5
      WORLD: world
      DISPLAY: test

Vielen Dank im vorraus für die Hilfe

MfG KaeseKuchenDEV
 

JOO200

Braumeister
Registriert
18 Dezember 2016
Beiträge
442
Diamanten
228
Vorher:
Code:
Double x = configHandler.getDouble(path + "SLocation.X");

Nachher:
Code:
Double x = configHandler.getDouble(path + ".SLocation.X");
Ich bin kein Profi, würde dir aber empfehlen, vor dem "SLocation" noch einen Punkt zu setzen. Ansonsten kannst du dir ja per System.out.println mal die Paths ausprinten lassen und das in der Konsole anschauen ;)
 

KaeseKuchen_DEV

Redstoneengineer
Registriert
11 Mai 2016
Beiträge
47
Diamanten
300
Minecraft
KaeseKuchenDEV
Vorher:
Code:
Double x = configHandler.getDouble(path + "SLocation.X");

Nachher:
Code:
Double x = configHandler.getDouble(path + ".SLocation.X");
Ich bin kein Profi, würde dir aber empfehlen, vor dem "SLocation" noch einen Punkt zu setzen. Ansonsten kannst du dir ja per System.out.println mal die Paths ausprinten lassen und das in der Konsole anschauen ;)

leider wird weiterhin ausgegeben, dass die welt nicht null sein darf
Code:
public void loadMaps(){
        int anz = configHandler.getInt("Maps.anz");
        for(int i = 1; i <= anz; i++){
            String path = "Maps." + i;
           
            Double x = configHandler.getDouble(path + ".SLocation.X");
            Double y = configHandler.getDouble(path + ".SLocation.Y");
            Double z = configHandler.getDouble(path + ".SLocation.Z");
            Integer yaw = configHandler.getInt(path + ".SLocation.YAW");
            Integer pitch = configHandler.getInt(path + ".SLocation.PITCH");
           
            String displayName = configHandler.getString(path + ".SLocation.DISPLAY");
            String worldname = configHandler.getString(path + ".SLocation.WORLD");
           
            Location loc = new Location(Bukkit.getWorld(worldname), x, y, z, yaw, pitch);
            worlds world = new worlds(worldname, loc, displayName);
           
            this.mapmanager.addWorld(world);
           
            System.out.println("[KnockBackFFA] Die Map " + displayName + " wurde geladen!");
           
        }
    }
 

JOO200

Braumeister
Registriert
18 Dezember 2016
Beiträge
442
Diamanten
228
Du machst dir das auch etwas kompliziert. Es ist nicht unbedingt sinnvoll, x, y, z und die Welt einzeln ab zu speichern, wenn du auch einfach eine Location speichern kannst. Also:
Code:
configHandler.set(path+".SLocation", p.getLocation();
Beim Getten musst du das entsprechend casten:
Code:
Location loc = (Location)configHandler.get(path+".SLocation");

Unabhängig davon: Wenn deine World nicht null sein darf, dann geb dir die Werte dazwischen mal mit System.out.println("Text"); aus. Wenn du vor dem Abspeichern null bekommst, dann dürfte klar sein, dass schon beim Abspeichern deine Werte quatsch beinhalten.

Wenn du dein configHandler nicht einmalig (richtig) initialisiert wurde und du 2 unterschiedliche Instanzen hast, dann ist klar, dass es nicht funktktioniert.

Wenn du vergessen hast, diese FileConfiguration zu speichern (.save() - Methode), dann bleiben deine ConfigSections im Ram und werden nicht in die Yml-Datei auf der Platte geschrieben. Und bei einem Serverneustart wird der Ram neu vergeben -> alle Objekte des Plugins sind weg.
 

xXSchrandXx

Schafhirte
Registriert
16 September 2012
Beiträge
126
Diamanten
302
Minecraft
xXSchrandXx
Ich weiß nicht mehr woher ich diese API habe, aber die kann dir helfen

Code:
package centroserver.bukkit.api;

import java.io.File;
import java.io.IOException;

import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

public class Config {
   
    JavaPlugin plugin;
   
    private String name = null;
   
    private FileConfiguration cfg = null;
    private File cfgFile = null;
   
    public Config(JavaPlugin plugin, String name) {
        this.plugin = plugin;
        this.name = name;
    }
   
    public void reload() {
        if (cfgFile == null) {
            cfgFile = new File(this.plugin.getDataFolder(), this.name);
        }
        cfg = YamlConfiguration.loadConfiguration(cfgFile);
        YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(cfgFile);
        cfg.setDefaults(defConfig);
    }
   
    public FileConfiguration get() {
        if (cfg == null) {
            reload();
        }
        return cfg;
    }
   
    public void save() {
        if (cfg == null || cfgFile == null) {
        return;
        }
        try {
            cfg.save(cfgFile);
        } catch (IOException ex) { }
    }
   
    public String getLanguage(String path, Object... args) {
        String raw = cfg.getString(path);
        if(raw == null) return null;
        for(int index = 0; index < args.length; index++) {
            raw = raw.replace("%" + index, args[index].toString());
        }
        return ChatColor.translateAlternateColorCodes('&', raw);
    }
}
 

KaeseKuchen_DEV

Redstoneengineer
Registriert
11 Mai 2016
Beiträge
47
Diamanten
300
Minecraft
KaeseKuchenDEV
Ich weiß nicht mehr woher ich diese API habe, aber die kann dir helfen

Code:
package centroserver.bukkit.api;

import java.io.File;
import java.io.IOException;

import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

public class Config {
  
    JavaPlugin plugin;
  
    private String name = null;
  
    private FileConfiguration cfg = null;
    private File cfgFile = null;
  
    public Config(JavaPlugin plugin, String name) {
        this.plugin = plugin;
        this.name = name;
    }
  
    public void reload() {
        if (cfgFile == null) {
            cfgFile = new File(this.plugin.getDataFolder(), this.name);
        }
        cfg = YamlConfiguration.loadConfiguration(cfgFile);
        YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(cfgFile);
        cfg.setDefaults(defConfig);
    }
  
    public FileConfiguration get() {
        if (cfg == null) {
            reload();
        }
        return cfg;
    }
  
    public void save() {
        if (cfg == null || cfgFile == null) {
        return;
        }
        try {
            cfg.save(cfgFile);
        } catch (IOException ex) { }
    }
  
    public String getLanguage(String path, Object... args) {
        String raw = cfg.getString(path);
        if(raw == null) return null;
        for(int index = 0; index < args.length; index++) {
            raw = raw.replace("%" + index, args[index].toString());
        }
        return ChatColor.translateAlternateColorCodes('&', raw);
    }
}
Vielen dank sowas hab ich gesucht
 
Oben