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
@bronzzze So you want to read all the values in from the config and invoke that constructor with arguments obtained from the config?
@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.
@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.
@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?
@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"); } } } } 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?
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
@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.
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.
@bronzzze Code: this.getConfig().getInt("Bombs." + args + ".Itemid"); thats an example to get the id of the bomb
@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
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'
@DoppelRR yes I tryed this before and didn't work.. Edit: working I forgot about hasDisplayname() Ty Doppelerr
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?
@bronzzze Where is your ItemStack created? Of course, if you only create it on command after a reload it will be away.
@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)
@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.