Strings not being added to config lists

Discussion in 'Plugin Development' started by boss86741, Jun 12, 2013.

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

    boss86741

    I am making a custom class plugin. In game, when I do /class join mage (or any class), it doesn't add my username to the "mage" list in the configuration file. Sorry, I am new to bukkit plugin building, so if you see any other problems, please feel free to let me know!

    Main Class:
    Code:
    package com.boss86741.plugins.godlyclasses;
     
    import java.io.File;
     
    import net.minecraft.server.v1_5_R3.EntityFireball;
    import net.minecraft.server.v1_5_R3.EntityLargeFireball;
    import net.minecraft.server.v1_5_R3.EntityLiving;
     
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_5_R3.CraftWorld;
    import org.bukkit.craftbukkit.v1_5_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;
     
    import com.boss86741.plugins.godlyclasses.util.ListStore;
     
    public class GodlyClasses extends JavaPlugin {
     
        public ListStore kitPlayers;
     
        @Override
        public void onEnable() {
            getLogger().info("Say hi to the class plugin :)");
            GodlyCommands gc = new GodlyCommands(this);
            getCommand("class").setExecutor(gc);
            // event register
            getServer().getPluginManager().registerEvents(new GodlyEvents(this), this);
       
            // kits
            String pluginFolder = this.getDataFolder().getAbsolutePath();
       
            (new File(pluginFolder)).mkdirs();
       
            this.kitPlayers = new ListStore(new File(pluginFolder + File.separator + "PlayersUsingKits.txt"));
       
            this.kitPlayers.load();
        }
     
        @Override
        public void onDisable() {
            getLogger().info("Bye, bye!");
            this.saveConfig();
       
            this.kitPlayers.save();
        }
     
        // Shoot fireballs (mages only).
     
        public void mageFireball(Player player) {
            String p = player.getName();
            CraftPlayer craftPlayer = (CraftPlayer) player;
            EntityLiving playerEntity = craftPlayer.getHandle();
     
            Vector lookat = player.getLocation().getDirection().multiply(8);
     
            Location loc = player.getLocation();
     
            EntityFireball fball = new EntityLargeFireball(((CraftWorld) player.getWorld()).getHandle(), playerEntity, lookat.getX(), lookat.getY(), lookat.getZ());
     
            fball.locX = loc.getX() + (lookat.getX()/5.0) + 0.25;
            fball.locY = loc.getY() + (player.getEyeHeight()/2.0) + 0.5;
            fball.locZ = loc.getZ() + (lookat.getZ()/5.0);
     
            if (getConfig().getStringList("mage").contains(p)) {
                ((CraftWorld) player.getWorld()).getHandle().addEntity(fball);
            }
       
        // Warriors: Better fighting. Located in event class.
       
       
        // Archers: Better shooting. Located in event class.
        }
    }
    
    GodlyCommands:
    Code:
    package com.boss86741.plugins.godlyclasses;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;
     
    public class GodlyCommands implements CommandExecutor {
     
        // implement the plugin method.
        private GodlyClasses plugin;
     
        public GodlyCommands(GodlyClasses plugin) {
            this.plugin = plugin;
        }
     
        String plName;
     
        GodlyKits gk;
     
        boolean playerAlreadyChose() {
            if (plugin.getConfig().getStringList("mage").contains(plName)) {
                return true;
            } else {
                if (plugin.getConfig().getStringList("archer").contains(plName)) {
                    return true;
               
                } else {
                    if (plugin.getConfig().getStringList("warrior").contains(plName)) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        }
     
        public void chooseClass(CommandSender sender, String[] args) {
            if (playerAlreadyChose() == true) {
                sender.sendMessage(ChatColor.DARK_RED + "You already chose a class!");
            } else {
                // choose the archer class
                if (args[1].equalsIgnoreCase("archer")) {
                    sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class archer!");
                    plugin.getConfig().getStringList("archer").add(plName);
                    sender.sendMessage(ChatColor.GREEN + "Use /class kit to get your kit.");
                } else {
                    // choose the warrior class
                    if (args[1].equalsIgnoreCase("warrior")) {
                        sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class warrior!");
                        String plName = sender.getName();
                        plugin.getConfig().getStringList("warrior").add(plName);
                        sender.sendMessage(ChatColor.GREEN + "Use /class kit to get your kit.");
                    } else {
                        // choose the Mage class
                        if (args[1].equalsIgnoreCase("mage")) {
                            sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class mage!");
                            String plName = sender.getName();
                            plugin.getConfig().getStringList("mage").add(plName);
                            sender.sendMessage(ChatColor.GREEN + "Use /class kit to get your kit.");
                        } else {
                            sender.sendMessage(ChatColor.DARK_RED + "Use /class list to see the list of classes.");
                        }
                    }
                }
            }
        }
        // command code
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) {
            if (cmd.getName().equalsIgnoreCase("class")) {
                plName = sender.getName();
                Player p = (Player) sender;
                if (args.length == 2) {
                    playerAlreadyChose();
                    if (args[0].equalsIgnoreCase("choose")) {
                        chooseClass(sender, args);
                    } else {
                        sender.sendMessage(ChatColor.RED + "Use /class help to see a list of commands");
                    }
                } else {
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("list")) {
                            String messageHeader = "&b-&a=&b-&a=&b-&a=&eGodlySurvival Classes&a=&b-&a=&b-&a=&b-";
                            p.sendMessage(messageHeader.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                            p.sendMessage(ChatColor.DARK_RED + "Archer - Gives You Good Skill in Archery");
                            p.sendMessage(ChatColor.RED + "Warrior - The Fearless Swordsman in Battle");
                            p.sendMessage(ChatColor.LIGHT_PURPLE + "Mage - A Person Always With a Trick Up Their Sleeve");
                        }
                        if (args[0] == "help") {
                            String message1 = "&b-&a=&b-&a=&b-&a=&eGodlySurvival Classes&a=&b-&a=&b-&a=&b-";
                            String message2 = "&2/class choose <classname> &f- &dChoose your class";
                            String message3 = "&3/class list &f- &6List all the classes";
                            String message4 = "&a/class kit &f- &cGive's you the kit for your class";
                            String message5 = "&4/class help &f- &eShows you this help menu";
                            sender.sendMessage(message1.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                            sender.sendMessage(message2.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                            sender.sendMessage(message3.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                            sender.sendMessage(message4.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                            sender.sendMessage(message5.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                        }
                        if (args[0] == "kit") {
                            if (plugin.kitPlayers.contains(plName)) {
                                p.sendMessage(ChatColor.GOLD + "You already got your kit within the past 2 days.");
                            } else {
                                gk.givePlayerKit(p);
                                plugin.kitPlayers.add(plName);
                                // Scheduler
                                new BukkitRunnable() {
                                    @Override
                                    public void run() {
                                        plugin.kitPlayers.remove(plName);
                                    }
                                }.runTaskLater(plugin, 3456000L); //20 ticks * 6 seconds = 120 ticks
                            }
                        }
                    }
                }
                if (args.length == 0 || args.length > 2) {
                    sender.sendMessage(ChatColor.RED + "Do /class help for the help menu.");
                }
            return true;
            }
        return false;
        }
    }
    
    GodlyEvents:
    Code:
    package com.boss86741.plugins.godlyclasses;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockState;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
     
    public class GodlyEvents implements Listener {
     
        private GodlyClasses plugin;
     
        public GodlyEvents(GodlyClasses plugin) {
            this.plugin = plugin;
        }
     
        @EventHandler
        public void onRightClick(PlayerInteractEvent event) {
            final Action action = event.getAction();
            Player player = event.getPlayer();
            if (action == Action.RIGHT_CLICK_AIR) {
                if (event.getItem().getType() == Material.FIREBALL) {
                    plugin.mageFireball(player);
                }
            }
        }
     
        public void playerChoseArcher(String plName, Player sender) {
            sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class archer!");
            plugin.getConfig().getStringList("archer").add(plName);
            sender.sendMessage(ChatColor.GREEN + "Use /class kit to get your kit.");
        }
     
        public void playerChoseWarrior(String plName, Player sender) {
            sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class warrior!");
            plugin.getConfig().getStringList("archer").add(plName);
            sender.sendMessage(ChatColor.GREEN + "Use /class kit to get your kit.");
        }
     
        public void playerChoseMage(String plName, Player sender) {
            sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class mage!");
            plugin.getConfig().getStringList("archer").add(plName);
            sender.sendMessage(ChatColor.GREEN + "Use /class kit to get your kit.");
        }
     
        @EventHandler
        public void onSignClick(PlayerInteractEvent event) {
            Action action  = event.getAction();
            Player p = event.getPlayer();
            String pl = p.getName();
            Block blockClicked;
            if (action == Action.RIGHT_CLICK_BLOCK) {
                blockClicked = event.getClickedBlock();
                Material sign = blockClicked.getType();
                BlockState state = event.getClickedBlock().getState();
                if (sign == Material.SIGN || sign == Material.WALL_SIGN || sign == Material.SIGN_POST) {
                    final Sign s = (Sign)state;
               
                    if ("[Class]".equals(s.getLine(1))) {
                        if ("Archer".equalsIgnoreCase(s.getLine(2))) { // Insert code to make sure player didn't choose
                            if (plugin.getConfig().contains(pl)) {
                                p.sendMessage(ChatColor.RED + "You already chose your class!");
                            } else {
                                playerChoseArcher(pl, p);
                            }
                        }
                        if ("Warrior".equalsIgnoreCase(s.getLine(2))) {
                            if (plugin.getConfig().contains(pl)) {
                                p.sendMessage(ChatColor.RED + "You already chose your class!");
                            } else {
                                playerChoseWarrior(pl, p);
                            }
                        }
                        if ("Mage".equalsIgnoreCase(s.getLine(2))) {
                            if (plugin.getConfig().contains(pl)) {
                                p.sendMessage(ChatColor.RED + "You already chose your class!");
                            } else {
                                playerChoseMage(pl, p);
                            }
                        }
                    }
                }
            }
        }
     
        public void playerAttack(EntityDamageByEntityEvent event) {
            Player p;
            p = (Player)event.getDamager();
            String pl = p.getName();
            if (p.getItemInHand().equals(Material.DIAMOND_SWORD) || p.getInventory().contains(Material.GOLD_SWORD) || p.getInventory().contains(Material.IRON_SWORD) || p.getInventory().contains(Material.STONE_SWORD) || p.getInventory().contains(Material.WOOD_SWORD)) {
                if (plugin.getConfig().getStringList("warrior").contains(pl)) {
                    event.setDamage(event.getDamage() + 2);
                }
            }
            if (p.getItemInHand().equals(Material.BOW)) {
                if (plugin.getConfig().getStringList("archer").contains(pl)) {
                    event.setDamage(event.getDamage() + 2);
                }
            }
        }
    }
    
    GodlyKits:
    Code:
    package com.boss86741.plugins.godlyclasses;
     
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
     
    public class GodlyKits {
     
        private GodlyClasses plugin;
     
        public GodlyKits(GodlyClasses plugin) {
            this.plugin = plugin;
        }
     
        // The actual kits.
        public void kitArcher(Player player) {
            ItemStack[] items = { new ItemStack(Material.BOW), new ItemStack(Material.BOW) };
            PlayerInventory inv = player.getInventory();
            for (ItemStack i : items) {
                inv.setItem(inv.firstEmpty(), i);
            }
            player.getInventory().addItem(new ItemStack(Material.ARROW, 128));
        }
     
        public void kitWarrior(Player player) {
            ItemStack[] items = {new ItemStack(Material.IRON_SWORD),/* Now for the chain armor.*/ new ItemStack(Material.CHAINMAIL_BOOTS), new ItemStack(Material.CHAINMAIL_LEGGINGS), new ItemStack(Material.CHAINMAIL_CHESTPLATE), new ItemStack(Material.CHAINMAIL_HELMET)};
            PlayerInventory inv = player.getInventory();
            for (ItemStack i : items) {
                inv.setItem(inv.firstEmpty(), i);
            }
            player.getInventory().addItem(items);
        }
     
        public void kitMage(Player player) {
            ItemStack[] items = {new ItemStack(Material.FIREBALL), new ItemStack(Material.WATER)};
            PlayerInventory inv = player.getInventory();
            for (ItemStack i : items) {
                inv.setItem(inv.firstEmpty(), i);
            }
        }
     
        // The action that happens in order for the player to receive their kit.
        public void givePlayerKit(Player p) {
            String pl = p.getName();
            if (plugin.getConfig().getStringList("archer").contains(pl)) {
                kitArcher(p);
            }
            if (plugin.getConfig().getStringList("warrior").contains(pl)) {
                kitWarrior(p);
            }
            if (plugin.getConfig().getStringList("mage").contains(pl)) {
                kitMage(p);
            }
        }
    }
    
    plugin.yml:
    Code:
    main: com.boss86741.plugins.godlyclasses.GodlyClasses
    name: GodlyClasses
    version: 0.1
    author: boss86741
    description: Class plugin.
    prefix: GodlySurvival
    website: http://www.boss86741.com/
    commands:
      class:
          description: This command is the main command of the class plugin.
          permission: classes.*
    permissions:
      classes.choose:
          description: Allows you to choose your class.
          default: true
      classes.*:
          description: Allows use of all class commands.
          default: op
    default config.yml:
    Code:
    # Please do not change anything in here. Players are added to these
    # lists through commands in-game. If you want to add another player, type
    # their name in a list with the YAML format shown below.
     
    # -=-=-= YAML EXAMPLE =-=-=- #
    # list:
    #  - list item
    # option: false
    # string: "hello"
    # comments after #
    archer:
      - huseyindavutoglu
    mage:
      - Mustache_Boss
    warrior:
      - snorlax6503
     
  2. Offline

    AmShaegar

    Wow, this is just too much code to ask a question :D If you ask for something specific you should do the work of extracting relevant parts once instead of forcing every potential helper to do it on his own.

    However, the first thing I've seen are those scary imports: org.bukkit.craftbukkit.v1_5_R3
    Do not use them unless absolutely necessary. Otherwise your plugins will break with every new update.

    Something else:
    Code:
    if(...) {
      ...
    } else {
      if(...) {
        ...
      }
    }
    is the same as
    Code:
    if(...) {
      ...
    } else if(...) {
      ...
    }
    which is way shorter and clearer.
    ----------------------------------------------------------------------
    Code:
    if (playerAlreadyChose() == true) {
    is the same as
    Code:
    if (playerAlreadyChose()) {
    ----------------------------------------------------------------------
    playerAlreadyChose(); does nothing in this piece of code, because you do not use the return value:
    Code:
    if (args.length == 2) {
      playerAlreadyChose();
    ----------------------------------------------------------------------
    After
    Code:
    plugin.getConfig().getStringList("mage").add(plName);
    you need to do
    Code:
    plugin.getConig().saveConfig();
    to make the user appear in the yml file.

    That's it for now. Have fun. :)
     
  3. Offline

    boss86741

    AmShaegar
    I did everything but it still doesn't update the config which makes it so that the /class kit and /class choose <class> command won't work properly and the player won't get their "power".

    I tried plugin.saveConfig() and I tried plugin.getConfig().save("config.yml").


    Everything else worked though!
     
  4. Offline

    AmShaegar

    Both methods are wrong. You need to do
    Code:
    plugin.getConig().saveConfig();
    If that does not work, please post the full GodlyCommands class, how it looks like now.
     
  5. Offline

    boss86741

    AmShaegar
    The method plugin.getConfig().saveConfig() doesn't work.

    GodlyCommands:
    Code:
    package com.boss86741.plugins.godlyclasses;
     
    import java.io.IOException;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;
     
    public class GodlyCommands implements CommandExecutor {
     
        // implement the plugin method.
        private GodlyClasses plugin;
       
        public GodlyCommands(GodlyClasses plugin) {
            this.plugin = plugin;
        }
       
        String plName;
       
        GodlyKits gk;
       
        boolean playerAlreadyChose() {
            if (plugin.getConfig().getStringList("mage").contains(plName)) {
                return true;
            } else {
                if (plugin.getConfig().getStringList("archer").contains(plName)) {
                    return true;
                } else {
                    if (plugin.getConfig().getStringList("warrior").contains(plName)) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        }
       
        // command code
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) {
            if (cmd.getName().equalsIgnoreCase("class")) {
                plName = sender.getName();
                Player p = (Player) sender;
                if (args.length == 2) {
                    if (args[0].equalsIgnoreCase("choose")) {
                        playerAlreadyChose();
                        if (playerAlreadyChose()) {
                            sender.sendMessage(ChatColor.DARK_RED + "You already chose a class!");
                        } else if (args[1].equalsIgnoreCase("archer")) {            // choose the archer class
                            sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class archer!");
                            plugin.getConfig().getStringList("archer").add(plName);
                            plugin.getConfig().saveConfig();
                            sender.sendMessage(ChatColor.GREEN + "Use /class kit to get your kit.");
                        } else if (args[1].equalsIgnoreCase("warrior")) { // choose the warrior class
                            sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class warrior!");
                            String plName = sender.getName();
                            plugin.getConfig().getStringList("warrior").add(plName);
                            plugin.getConfig().saveConfig();
                            sender.sendMessage(ChatColor.GREEN + "Use /class kit to get your kit.");
                        } else if (args[1].equalsIgnoreCase("mage")) { // choose the Mage class
                            sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class mage!");
                            String plName = sender.getName();
                            plugin.getConfig().getStringList("mage").add(plName);
                            plugin.getConfig().saveConfig();
                            sender.sendMessage(ChatColor.GREEN + "Use /class kit to get your kit.");
                        } else {
                            sender.sendMessage(ChatColor.DARK_RED + "Use /class list to see the list of classes.");
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + "Use /class help to see a list of commands");
                    }
                } else if (args.length == 1) {
                    if (args[0].equalsIgnoreCase("list")) {
                        String messageHeader = "&b-&a=&b-&a=&b-&a=&eGodlySurvival Classes&a=&b-&a=&b-&a=&b-";
                        p.sendMessage(messageHeader.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                        p.sendMessage(ChatColor.DARK_RED + "Archer - Gives You Good Skill in Archery");
                        p.sendMessage(ChatColor.RED + "Warrior - The Fearless Swordsman in Battle");
                        p.sendMessage(ChatColor.LIGHT_PURPLE + "Mage - A Person Always With a Trick Up Their Sleeve");
                    } else if (args[0].equalsIgnoreCase("help")) {
                        String message1 = "&b-&a=&b-&a=&b-&a=&eGodlySurvival Classes&a=&b-&a=&b-&a=&b-";
                        String message2 = "&2/class choose <classname> &f- &dChoose your class";
                        String message3 = "&3/class list &f- &6List all the classes";
                        String message4 = "&a/class kit &f- &cGive's you the kit for your class";
                        String message5 = "&4/class help &f- &eShows you this help menu";
                        sender.sendMessage(message1.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                        sender.sendMessage(message2.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                        sender.sendMessage(message3.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                        sender.sendMessage(message4.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                        sender.sendMessage(message5.replaceAll("(&([a-f0-9]))", "\u00A7$2"));
                    } else if (args[0].equalsIgnoreCase("kit")) {
                        if (plugin.kitPlayers.contains(plName)) {
                            p.sendMessage(ChatColor.GOLD + "You already got your kit within the past 2 days.");
                        } else {
                            plugin.givePlayerKit(p);
                            plugin.kitPlayers.add(plName);
                            // Scheduler
                            new BukkitRunnable() {
                                @Override
                                public void run() {
                                    plugin.kitPlayers.remove(plName);
                                }
                            }.runTaskLater(plugin, 3456000L); //20 ticks * 6 seconds = 120 ticks
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + "Do /class help for the help menu.");
                    }
                } else {
                    sender.sendMessage(ChatColor.RED + "Do /class help for the help menu.");
                }
            return true;
            }
        return false;
        }
    }
    
     
  6. Offline

    AmShaegar

    Ah, sorry. It's plugin.saveConfig(). If that does not work, maybe it is necessary to do it like this:

    Code:
    List<String> list = plugin.getConfig().getStringList("mage");
    list.add(plName);
    plugin.getConfig().set("mage", list);
    plugin.saveConfig();
     
Thread Status:
Not open for further replies.

Share This Page