Custom Config confusion

Discussion in 'Plugin Development' started by Xp10d3, Apr 6, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Xp10d3

    Hello! Man, it's been a while since I've done Bukkit development xD Anyways, I'm trying to set up a custom config and insert a player's data each time they die. Basically, a player dies, and I want to erase everything in the CUSTOM config file and insert their entire inventory. I have this much done at the moment but am confused.

    Code:
    File invFile = new File(core.getDataFolder(), player.getUniqueId() + ".yml");
            if (!invFile.exists()) {
                FileConfiguration invs = YamlConfiguration.loadConfiguration(invFile);
            }
    
    Full class:
    Code:
    package on.death.gamemode;
    
    import java.io.File;
    
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class PlayerListeners implements Listener {
        private Core core;
    
        public PlayerListeners(Core core) {
            this.core = core;
        }
    
        public void onPlayerDeath(PlayerDeathEvent event) {
            Player player = event.getEntity();
        
            File invFile = new File(core.getDataFolder(), player.getUniqueId() + ".yml");
            if (!invFile.exists()) {
                FileConfiguration invs = YamlConfiguration.loadConfiguration(invFile);
            }
        
            String gamemode = core.getConfig().getString("gamemode");
            String enable = core.getConfig().getString("enabled");
            int wait = core.getConfig().getInt("wait");
            String deathGamemode = core.getConfig().getString("ondeathgamemode");
            String excess = core.getConfig().getString("excess");
            if (excess == "true") {
                if (deathGamemode == "adventure") {
                    player.setGameMode(GameMode.ADVENTURE);
                } else if (deathGamemode == "survival") {
                    player.setGameMode(GameMode.SURVIVAL);
                } else if (gamemode == "spectator") {
                    player.setGameMode(GameMode.SPECTATOR);
                } else if (gamemode == "creative") {
                    player.setGameMode(GameMode.CREATIVE);
                } else {
                    player.sendMessage(ChatColor.RED + "Could not recognize gamemode " + deathGamemode + " in the config");
                }
            }
            if (enable == "true") {
                if (gamemode == "adventure") {
                    if (excess == "true") {
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                player.setGameMode(GameMode.ADVENTURE);
                            }
                        }.runTaskLater(core, wait);
                    } else {
                        player.setGameMode(GameMode.ADVENTURE);
                    }
                } else if (gamemode == "survival") {
                    if (excess == "true") {
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                player.setGameMode(GameMode.SURVIVAL);
                            }
                        }.runTaskLater(core, wait);
                    } else {
                        player.setGameMode(GameMode.SURVIVAL);
                    }
                } else if (gamemode == "spectator") {
                    if (excess == "true") {
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                player.setGameMode(GameMode.SPECTATOR);
                            }
                        }.runTaskLater(core, wait);
                    } else {
                        player.setGameMode(GameMode.SPECTATOR);
                    }
                } else if (gamemode == "creative") {
                    if (excess == "true") {
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                player.setGameMode(GameMode.CREATIVE);
                            }
                        }.runTaskLater(core, wait);
                    } else {
                        player.setGameMode(GameMode.CREATIVE);
                    }
                } else {
                    player.sendMessage(ChatColor.RED + "Gamemode in config not recognized. Contact the owner of the server!");
                }
            } else if (player.hasPermission("gamemodedeath.notification")){
                player.sendMessage(ChatColor.RED + "OnDeathGamemode is not enabled. Check the config!");
            }
        }
    }
    
    I have followed this tutorial/thread (https://bukkit.org/threads/make-a-custom-config-file.61840/) but it's still quite confusing. If anyone knows how to help me, that would be much appreciated. Thanks :)
    -Xp10d3
     
  2. Offline

    bowlerguy66

    You never create the file here, you'll want to use file.createNewFile() and then load it after it is created
     
  3. Offline

    Xp10d3

    Alright. Thanks :) I just decided to remove it entirely as it was causing other issues. Aside from that, none of the code I have seems to be working. I enabled everything correctly in my config.yml when I loaded it and made the true/false options a string instead of a boolean to avoid plugin issues, but it doesn't seem to be working. When I die my gamemode doesn't change at all. Is there anything I'm doing wrong?

    PlayerListeners.java:
    Code:
    package on.death.gamemode;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    public class PlayerListeners implements Listener {
        private Core core;
       
        public PlayerListeners(Core core) {
            this.core = core;
        }
       
        public void onPlayerDeath(PlayerDeathEvent event) {
            Player player = event.getEntity();
           
            String gamemode = core.getConfig().getString("gamemode");
            String enable = core.getConfig().getString("enabled");
            int wait = core.getConfig().getInt("wait");
            String deathGamemode = core.getConfig().getString("ondeathgamemode");
            String excess = core.getConfig().getString("excess");
            String spawn = core.getConfig().getString("spawn");
            if (excess == "true") {
                if (deathGamemode == "adventure") {
                    player.setGameMode(GameMode.ADVENTURE);
                } else if (deathGamemode == "survival") {
                    player.setGameMode(GameMode.SURVIVAL);
                } else if (gamemode == "spectator") {
                    player.setGameMode(GameMode.SPECTATOR);
                } else if (gamemode == "creative") {
                    player.setGameMode(GameMode.CREATIVE);
                } else {
                    player.sendMessage(ChatColor.RED + "Could not recognize gamemode " + deathGamemode + " in the config");
                }
            } else {
                if (enable == "true") {
                    if (gamemode == "adventure") {
                        if (excess == "true") {
                            new BukkitRunnable() {
                                @Override
                                public void run() {
                                    player.setGameMode(GameMode.ADVENTURE);
                                    if (spawn == "true") {
                                        player.teleport(player.getWorld().getSpawnLocation());
                                    }
                                }
                            }.runTaskLater(core, wait);
                        } else {
                            player.setGameMode(GameMode.ADVENTURE);
                        }
                    } else if (gamemode == "survival") {
                        if (excess == "true") {
                            new BukkitRunnable() {
                                @Override
                                public void run() {
                                    player.setGameMode(GameMode.SURVIVAL);
                                    if (spawn == "true") {
                                        player.teleport(player.getWorld().getSpawnLocation());
                                    }
                                }
                            }.runTaskLater(core, wait);
                        } else {
                            player.setGameMode(GameMode.SURVIVAL);
                        }
                    } else if (gamemode == "spectator") {
                        if (excess == "true") {
                            new BukkitRunnable() {
                                @Override
                                public void run() {
                                    player.setGameMode(GameMode.SPECTATOR);
                                    if (spawn == "true") {
                                        player.teleport(player.getWorld().getSpawnLocation());
                                    }
                                }
                            }.runTaskLater(core, wait);
                        } else {
                            player.setGameMode(GameMode.SPECTATOR);
                        }
                    } else if (gamemode == "creative") {
                        if (excess == "true") {
                            new BukkitRunnable() {
                                @Override
                                public void run() {
                                    player.setGameMode(GameMode.CREATIVE);
                                    if (spawn == "true") {
                                        player.teleport(player.getWorld().getSpawnLocation());
                                    }
                                }
                            }.runTaskLater(core, wait);
                        } else {
                            player.setGameMode(GameMode.CREATIVE);
                        }
                    } else {
                        player.sendMessage(ChatColor.RED + "Gamemode in config not recognized. Contact the owner of the server!");
                    }
                } else if (player.hasPermission("gamemodedeath.notification")){
                    player.sendMessage(ChatColor.RED + "OnDeathGamemode is not enabled. Check the config!");
                }
            }
        }
    }
    
    
     
  4. Offline

    bowlerguy66

    @Xp10d3 There's no @EventHandler annotation
     
  5. Offline

    timtower Administrator Administrator Moderator

    @Xp10d3 Strings are compared with equals, not with ==, those always return true.
    Try using getBool as well, as you don't always need to use strings.
     
  6. Offline

    Xp10d3

    Lol I totally forgot about that xD Whoops my bad.
    Alright. I'll try that :) Yeah it's been way too long since I've done Java/Bukkit xD Thanks for the help; will try that out.

    EDIT:
    @timtower @bowlerguy66 hmm doesn't seem to work. It says it's disabled for some reason (I get the message I set at the bottom of my PlayerListeners file). Here's what it looks like now:
    Code:
    package on.death.gamemode;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    public class PlayerListeners implements Listener {
        private Core core;
       
        public PlayerListeners(Core core) {
            this.core = core;
        }
       
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            Player player = event.getEntity();
           
            String gamemode = core.getConfig().getString("gamemode");
            boolean enable = core.getConfig().getBoolean("enabled");
            int wait = core.getConfig().getInt("wait");
            String deathGamemode = core.getConfig().getString("ondeathgamemode");
            boolean excess = core.getConfig().getBoolean("excess");
            boolean spawn = core.getConfig().getBoolean("spawn");
            if (excess == true) {
                if (deathGamemode == "adventure") {
                    player.setGameMode(GameMode.ADVENTURE);
                } else if (deathGamemode.equals("survival")) {
                    player.setGameMode(GameMode.SURVIVAL);
                } else if (deathGamemode.equals("spectator")) {
                    player.setGameMode(GameMode.SPECTATOR);
                } else if (deathGamemode.equals("creative")) {
                    player.setGameMode(GameMode.CREATIVE);
                } else {
                    player.sendMessage(ChatColor.RED + "Could not recognize gamemode " + deathGamemode + " in the config");
                }
            } else {
                if (enable == true) {
                    if (gamemode.equals("adventure")) {
                        if (excess == true) {
                            new BukkitRunnable() {
                                @Override
                                public void run() {
                                    player.setGameMode(GameMode.ADVENTURE);
                                    if (spawn == true) {
                                        player.teleport(player.getWorld().getSpawnLocation());
                                    }
                                }
                            }.runTaskLater(core, wait);
                        } else {
                            player.setGameMode(GameMode.ADVENTURE);
                        }
                    } else if (gamemode.equals("survival")) {
                        if (excess == true) {
                            new BukkitRunnable() {
                                @Override
                                public void run() {
                                    player.setGameMode(GameMode.SURVIVAL);
                                    if (spawn == true) {
                                        player.teleport(player.getWorld().getSpawnLocation());
                                    }
                                }
                            }.runTaskLater(core, wait);
                        } else {
                            player.setGameMode(GameMode.SURVIVAL);
                        }
                    } else if (gamemode.equals("spectator")) {
                        if (excess == true) {
                            new BukkitRunnable() {
                                @Override
                                public void run() {
                                    player.setGameMode(GameMode.SPECTATOR);
                                    if (spawn == true) {
                                        player.teleport(player.getWorld().getSpawnLocation());
                                    }
                                }
                            }.runTaskLater(core, wait);
                        } else {
                            player.setGameMode(GameMode.SPECTATOR);
                        }
                    } else if (gamemode.equals("creative")) {
                        if (excess == true) {
                            new BukkitRunnable() {
                                @Override
                                public void run() {
                                    player.setGameMode(GameMode.CREATIVE);
                                    if (spawn == true) {
                                        player.teleport(player.getWorld().getSpawnLocation());
                                    }
                                }
                            }.runTaskLater(core, wait);
                        } else {
                            player.setGameMode(GameMode.CREATIVE);
                        }
                    } else {
                        player.sendMessage(ChatColor.RED + "Gamemode in config not recognized. Contact the owner of the server!");
                    }
                } else if (player.hasPermission("gamemodedeath.notification")){
                    player.sendMessage(ChatColor.RED + "GamemodeDeath is not enabled. Check the config!");
                }
            }
        }
    }
    
     
    Last edited: Apr 7, 2020
    bowlerguy66 and timtower like this.
  7. Offline

    Xp10d3

    Bump
     
  8. Offline

    bowlerguy66

    @Xp10d3 Can you confirm that the config exists when you get that message? Any stacktrace errors? Try printing out your gamemode string in the message to see what it says.

    Also, I'd recommend restructuring your logic so it's easier to read instead of nesting if statements. In your second else statement for each gamemode, you test if excess == true, wouldn't it always be false since it is inside the else statement that determines if excess == true?

    Edit: Use this to clean up too:
    Code:
    GameMode gm = GameMode.valueOf(<string>);
     
    Last edited: Apr 8, 2020
  9. Offline

    Xp10d3

    Sorry for late reply seems that Eclipse won't let me launch my workspace lol. Anyways yes it says that the config exists. It is able to send me to spawn and I don't get any errors. I tried printing it out in my onEnable and I got all the correct messages. And to answer your question about excess equaling true/false, what do you mean by that? I'm a bit confused on what you mean.
     
  10. Offline

    bowlerguy66

    @Xp10d3 I trimmed the code to help illustrate my point I knew it was confusing when I wrote it sorry lol:
    Code:
                boolean excess = core.getConfig().getBoolean("excess");
                boolean spawn = core.getConfig().getBoolean("spawn");
                if (excess == true) {
                    //Excess is true <-- Will be caught & executed here first, rendering the below if-statement
                    //useless because all the code below will mean that excess is false
                } else {
                    if (enable == true) {
                        if (gamemode.equals("adventure")) {
                            if (excess == true) {
                                // Excess is true (Can't be possible because it was caught above)
                            } else {
                                // Excess is false
                            }
                        }
                    } else if (player.hasPermission("gamemodedeath.notification")){
                        player.sendMessage(ChatColor.RED + "GamemodeDeath is not enabled. Check the config!");
                    }
                }
    
    For the config files, can you reiterate what's working/not working? Also, what messages are being printed?
     
    Last edited: Apr 9, 2020
  11. Offline

    Xp10d3

    Hmm I believe my code looks like that. And the code that prints the messages are here:
    Code:
        public void onEnable() {
            getLogger().info("GamemodeDeath is enabled! Loading config.yml...");
            loadConfig();
            getLogger().info(ChatColor.GREEN + "Successfully loaded config.yml!" + ChatColor.RESET + " Registering listeners...");
            PlayerListeners playerListeners = new PlayerListeners(this);
            this.getServer().getPluginManager().registerEvents(playerListeners, this);
            getLogger().info(ChatColor.GREEN + "Successfully registered listeners.");
            getLogger().info(" --------------");
            getLogger().info(ChatColor.BLUE + "[STATISTICS]");
            getLogger().info(" --------------");
            getLogger().info("Default gamemode on death: " + this.getConfig().getString("ondeathgamemode"));
            getLogger().info(this.getConfig().getInt("wait")
                    + " ticks before setting gamemode to "
                    + this.getConfig().getString("gamemode"));
        }
    
    It is getting printed correctly. I've double checked that.
     
Thread Status:
Not open for further replies.

Share This Page