Saving updated List to config.yml

Discussion in 'Plugin Development' started by InsertNameHere, Jun 29, 2019.

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

    InsertNameHere

    For practice, I am coding a plugin that allows you to disable certain mob drops. My plan is to have a command that automatically adds an item into an ArrayList that is then saved to the config. Mobs will not be able to drop the items in the ArrayList. This is the code for the remove command.

    Code:
    if (sender instanceof Player) {
        Player player = (Player) sender;
        if (!player.hasPermission("MobLoot.rdrop")) {
            player.sendMessage(ChatColor.DARK_RED + "You do not have access to this command.");
            return true;
        }
    
        if (args.length != 0) {
            player.sendMessage(ChatColor.RED + "This command does not require any arguments.");
            return true;
        }
    
        if (!player.getInventory().getItemInMainHand().getType().equals(Material.AIR)) {
            Material heldItem = player.getInventory().getItemInMainHand().getType();
            if (censoredDrops.contains(heldItem.toString())) {
                censoredDrops.remove(heldItem.toString());player.sendMessage(ChatColor.RED + "Mobs can now drop " + ChatColor.GOLD + heldItem.toString());
                player.sendMessage(ChatColor.RED + "The list is now: " + printStringList(censoredDrops));
                p.getConfig().set("Censored drops", censoredDrops);
                p.saveConfig(); return true;
            } else {
                censoredDrops.add(heldItem.toString());player.sendMessage(ChatColor.RED + "Mobs can no longer drop " + ChatColor.GOLD + heldItem.toString());
                player.sendMessage(ChatColor.RED + "The list is now: " + printStringList(censoredDrops));
                p.getConfig().set("Censored drops", censoredDrops);
                p.saveConfig();
            }
        } else {
            player.sendMessage(ChatColor.RED + "You must be holding the item you want removed in order for the command to work.");
        }
    } else {
    Bukkit.getConsoleSender().sendMessage("This command cannot be performed over console.");
    }
    
    return true;
    
    The only issue is that whenever I run the command /rdrop in game, it doesn't update the list until I /reload the server. Is there any way around this where the config.yml automatically gets updated when a player runs a command?

    Thanks
     
    Last edited: Jun 30, 2019
Thread Status:
Not open for further replies.

Share This Page