Config

Discussion in 'Plugin Development' started by bronzzze, Apr 30, 2015.

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

    bronzzze

    Code:
    public BombItem(Main main, ItemStack item, String displayname, ItemMeta itemmeta, int radius, Player p){
    //I did stuff here
    }
    I have crated this bomb method. But I want to that player can add in config ther own bombs.
    For example config

    Code:
    Bombs:
    
      BombNameExample1:
        Radius: 3
        Displayname: &3Bombname1
        Itemid: 341
    
      BombNameExample2
        Radius: 4
        Displayname: &3Bombname2
        Itemid: 378
    ##and so on
    
    
    I never knew how to do this. Just give me advice or example do I help with this loop:
    Code:
    for (int z = 0; z < (idk what here); z++)
    
    }
    Sorry if this is stupid question And hope you understand it
     
    Last edited: Apr 30, 2015
  2. Offline

    mythbusterma

    @bronzzze

    So you want to read all the values in from the config and invoke that constructor with arguments obtained from the config?
     
  3. Offline

    bronzzze

    Yeah.

    I want players they can create custom bombs.
     
  4. Offline

    mythbusterma

    @bronzzze

    Then you'll probably want iterate over the return value of ConfigurationSection#getKeys(false) and then get each section it returns and look for the values of "radius," "displayname," and "itemid" within it.
     
  5. Offline

    bronzzze

    @mythbusterma
    hmm I was never doing with this. what getKeys do?
     
  6. Offline

    mythbusterma

    @bronzzze

    It returns a Set of Strings containing the children of a ConfigurationSection. Passing in false for an argument prevents it from doing so recursively.
     
  7. Offline

    bronzzze

    @mythbusterma
    Code:
    for(String key : config.getConfigurationSection(Bombs.).getKeys(false)){
    config.getString("Now I am not sure how to get path of display name, id...")
    //more values
    }
    
    So something like this?
     
  8. Offline

    mythbusterma

    @bronzzze

    The location of it would be key + ".name", key + ".id" etc.
     
    bronzzze likes this.
  9. Offline

    bronzzze

    ty dude gonna test it out.
     
  10. Offline

    bronzzze

    @mythbusterma
    So I make it like this
    I am sure this is not correct and can somone check it what is wrong and how could I fix it
    Bukkitrunnable
    Code:
    public class BombTask extends BukkitRunnable{
    
        Main plugin;
        Entity grenade;
        Player player;
        int radius;
    
    
        public BombTask(Main plugin, Entity grenade, int radius, Player player, ItemStack is) {
            this.plugin = plugin;
            this.grenade = grenade;
            this.radius = radius;
            this.player = player;
    
            grenade = player.getWorld().dropItem(player.getEyeLocation(),
                    is);
            player.getInventory().removeItem(is);
            grenade.setVelocity(player.getEyeLocation().getDirection());
    
        }
    
    
    
    
        @SuppressWarnings("deprecation")
        @Override
        public void run() {
    
             Location loc = grenade.getLocation();
    
    
                grenade.remove();
                loc.getWorld().playSound(loc, Sound.EXPLODE, 1,
                        1);
                loc.getWorld().playEffect(loc,
                        Effect.SMOKE, 4);
    
    
    
        for (Location l : plugin.explosion.CreateExplosion(player, loc,
                radius)){
            l.getBlock().setTypeId(0);
    
    
        }
    
        }
    Listener
    Code:
    public class ClickListener implements Listener {
    
       int radius;
       Item item;
       public ItemStack is;
       int delay;
    
       Main main;
    
       public ClickListener(Main i) {
         main = i;
       }
    
       @SuppressWarnings("deprecation")
       @EventHandler
       public void onThrow(final PlayerInteractEvent event) {
         final Player player = event.getPlayer();
         ItemStack hand = (ItemStack) player.getItemInHand();
         if (event.getAction() == Action.RIGHT_CLICK_AIR
             || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
           if (hand != null && hand.isSimilar(is)) {
    
             player.getWorld().playSound(player.getLocation(),
                 Sound.WITHER_SHOOT, 1, 2);
    
             for (String key : main.getConfig()
                 .getConfigurationSection("CustomBombs").getKeys(false)) {
               is = new ItemStack(Material.getMaterial(main.getConfig()
                   .getInt(key + ".id")));
               ItemMeta im = is.getItemMeta();
               im.setDisplayName(main.getConfig().getString(key + ".name"));
               List<String> converted = new ArrayList<String>();
               for (String s : main.getConfig().getStringList(
                   key + ".lore")) {
                 converted.add(ChatColor.translateAlternateColorCodes(
                     '&', s));
               }
               im.setLore(converted);
    
               radius = main.getConfig().getInt(key + ".radius");
               delay = main.getConfig().getInt(key + ".explosiondelay");
    
             }
    
             new BombTask(main, item, radius, player, is).runTaskLater(main,
                 delay);
             main.setGrenade(item, player);
             System.out.println("working");
    
           }
    
         }
       }
    }
    
    
    [​IMG]


    How could I get this item if I want to get command /bombmine give BombExample1. I dont know path of BombExample1. is it only key?
     
    Last edited: May 2, 2015
  11. Offline

    bronzzze

    Can anoyone check if this 2 classes are correct(I know they are not so how could I make it). And how could I get this item with command for example /bomb give BombExample1
     
    Last edited: May 3, 2015
  12. Offline

    CrystallFTW

    @bronzzze Thats how I would do this:
    1. I would create a new method something like giveBomb(Player p, String args)
    2. In this method I would create an Itemstack with the id,name everything else from the config but using "args" not key
    3. Then I would add
    Code:
    p.getInventory().addItem(itemstack);
    4. and then just create the command and check for args and then add the method to the sender, and instead of "args" add args[1].
    I hope that helped.
     
    bronzzze likes this.
  13. Offline

    bronzzze

    Yes but how could I get path for all of this if I would't use keys.

    Code:
    Bombs:
      BombNameExample1:
        Radius: 3
        Displayname: &3Bombname1
        Itemid: 341
      BombNameExample2
        Radius: 4
        Displayname: &3Bombname2
        Itemid: 378
    ##and so on
    Edit:
    Or wait. First I didn't know what you mean Now I know. I will test it ty.
     
  14. Offline

    CrystallFTW

    @bronzzze
    Code:
            this.getConfig().getInt("Bombs." + args + ".Itemid");
    thats an example to get the id of the bomb
     
    bronzzze likes this.
  15. Offline

    bronzzze

    @CrystallFTW
    I get NPE error at this line:
    Code:
     itemmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&',
                    "CustomBombs." + args + ".displayname"));

    whole class:
    Code:
    public class GiveBomb {
    
        Player p;
        String args;
        ItemStack is;
        Main main;
    
        @SuppressWarnings("deprecation")
        public GiveBomb(Main main, Player p, String args) {
            this.p = p;
            this.args = args;
            this.main = main;
    
            is = new ItemStack(Material.getMaterial(main.getConfig().getInt(
                    "CustomBombs." + args + ".id")));
            ItemMeta itemmeta = is.getItemMeta();
            itemmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&',
                    "CustomBombs." + args + ".displayname"));
            List<String> converted = new ArrayList<String>();
            for (String s : main.getConfig().getStringList(
                    "CustomBombs." + args + ".lore")) {
                converted.add(ChatColor.translateAlternateColorCodes('&', s));
            }
            itemmeta.setLore(converted);
            is.setItemMeta(itemmeta);
            p.getInventory().addItem(is);
    
        }
    
    }
    
    Any ideas

    Edit: If I remove display name is working. Lol what did I do wrong. If anyone know what is wrong please tell me
     
    Last edited: May 4, 2015
  16. Offline

    bronzzze

    Code:
    CustomBombs:
      TestBomb:
        id: 1
        name: '&1Displayname'
        lore:
        - '&4Lore1'
        - '&1lore2'
    So this is error:
    Code:
    [15:35:28] [Server thread/INFO]: bronzzze issued server command: /minebombs testbomb
    [15:35:28] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'minebombs' in plugin MineBombs v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1133) [spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:968) [spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_75]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_75]
        at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:710) [spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot_server.jar:git-Spigot-2f787bd-ea28011]
        at java.lang.Thread.run(Unknown Source) [?:1.7.0_75]
    Caused by: java.lang.NullPointerException
        at me.bronzzze.bombs.GiveBomb.<init>(GiveBomb.java:28) ~[?:?]
        at me.bronzzze.bombs.Commands.onCommand(Commands.java:26) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot_server.jar:git-Spigot-2f787bd-ea28011]
        ... 15 more
    
    Line 28:
    Code:
            itemmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&',"CustomBombs." + args + ".name"));
    
    Edit:
    I have feeling that itemmeta is null

    If i remove displayname itemeta.setlore get npe error

    This is config maybe is something there wrong:
    Code:
    CustomBombs:
      TestBomb:
        id: 1
        name: '&1Displayname'
        lore:
        - '&4Lore1'
        - '&1lore2'
     
    Last edited: May 5, 2015
  17. @bronzzze
    Not every item directly has an itemmeta, so first check this with is.hasItemMeta();
     
  18. Offline

    bronzzze

    @DoppelRR
    yes I tryed this before and didn't work..

    Edit:
    working I forgot about hasDisplayname() Ty Doppelerr
     
    Last edited: May 5, 2015
  19. Offline

    bronzzze

    I Just have 1 more problem.

    Everyting is working but when I reload it stop working rightclickevent when I click item when I reloaed happend this.
    Code:
    if (hand != null && hand.isSimilar(GiveBomb.is)) {
    ...
    }
    this is my itemstack
    Code:
          
            is = new ItemStack(Material.getMaterial(main.getConfig().getInt(
                    "CustomBombs." + args + ".id")));
            is.hasItemMeta();
            is.getItemMeta().hasDisplayName();
            ItemMeta itemmeta = is.getItemMeta();
            itemmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&' , main.getConfig().getString("CustomBombs." + args + ".name")));
            List<String> converted = new ArrayList<String>();
            for (String s : main.getConfig().getStringList(
                    "CustomBombs." + args + ".lore")) {
                converted.add(ChatColor.translateAlternateColorCodes('&', s));
            }
            itemmeta.setLore(converted);
            is.setItemMeta(itemmeta);
            p.getInventory().addItem(is);
    Code:
        new GiveBomb(main, p, args[0]);
    ''args'' is here args[0]
    And everytime I reload it remove this args[0] untill I dont use this command again. so how could I save it should I put something onEnable(){} maybe commandexectue or something like this?
     
    Last edited: May 6, 2015
  20. @bronzzze
    Where is your ItemStack created? Of course, if you only create it on command after a reload it will be away.
     
  21. Offline

    bronzzze

    @DoppelRR
    Its created inside of this method
    Code:
    GiveBomb(Player p, String args){
    
    ItemStack is = new It....
    }
    so is there anyway ot save this String(args)
     
    Last edited: May 7, 2015
  22. Offline

    bronzzze

    /command args[0]
    Anyone know Is there anyway to save args[0] on reload?
     
  23. @bronzzze
    Saving something which should survive a reload is only possible through configs. Just google "Bukkit custom config" or something like that there are APIs for it.
     
Thread Status:
Not open for further replies.

Share This Page