This is the code I have so far. It doesn't seem to have any effect. I want the items that it gives to be unbreakable without enchantments. Spoiler: Code Code (Text): ItemStack Sword = new ItemStack(Material.IRON_SWORD); ItemMeta im = Sword.getItemMeta(); Enchantment unbreakingEnch = new EnchantmentWrapper(34); im.setDisplayName("§a§lSpider §e- §a§lRight-Click Hold"); Sword.setItemMeta(im); p.getInventory().addItem(Sword); Sword.setDurability((short) 10); Spoiler: Extra Code Code (Text): if (args[0].equalsIgnoreCase("spider")) { spider.addPlayer(p); spiderClass(p); spider.setPrefix("§e[Spider] "); return true; } // ^^ Command Origin // Below is the method @SuppressWarnings("deprecation") private void spiderClass(Player p) { p.performCommand("d spider"); p.getInventory().clear(); p.sendMessage("§aSSM> You have chosen the §e§lSPIDER§a."); p.setHealth(20); p.setFoodLevel(20); p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 2); p.playEffect(p.getLocation(), Effect.CLOUD, 500); ItemStack Sword = new ItemStack(Material.IRON_SWORD); ItemMeta im = Sword.getItemMeta(); Enchantment unbreakingEnch = new EnchantmentWrapper(34); im.setDisplayName("§a§lSpider §e- §a§lRight-Click Hold"); Sword.setItemMeta(im); p.getInventory().addItem(Sword); Sword.setDurability((short) 10); ItemStack Axe = new ItemStack(Material.IRON_AXE); ItemMeta im1 = Axe.getItemMeta(); im1.setDisplayName("§a§lSpider §e- §a§lRight Click"); Axe.setItemMeta(im1); Axe.addEnchantment(unbreakingEnch, 2); p.getInventory().addItem(Axe); }
You can use ItemMeta.Spigot#setUnbreakable(boolean). https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/ItemMeta.Spigot.html Something like the following: Code (Java): ItemStack is = ...; ItemMeta meta = is.getItemMeta(); meta.spigot().setUnbreakable(true); is.setItemMeta(meta); Should work. There's also no need to bump after only 34 minutes.
I got an error in the console: Code (Text): [14:03:26 WARN]: Unbreakable unable to find enchantment class for Bukkit API ver sion: v1_8_R1 [14:03:27 ERROR]: Could not load 'plugins\SSM.jar' in folder 'plugins' org.bukkit.plugin.InvalidPluginException: java.lang.ExceptionInInitializerError at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j ava:135) ~[spigot.jar:git-Spigot-c3c767f-33d5de3] at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager. java:329) ~[spigot.jar:git-Spigot-c3c767f-33d5de3] at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager .java:251) [spigot.jar:git-Spigot-c3c767f-33d5de3] at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugins(CraftServer.ja va:291) [spigot.jar:git-Spigot-c3c767f-33d5de3] at net.minecraft.server.v1_8_R1.DedicatedServer.init(DedicatedServer.jav a:152) [spigot.jar:git-Spigot-c3c767f-33d5de3] at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java :505) [spigot.jar:git-Spigot-c3c767f-33d5de3] at java.lang.Thread.run(Unknown Source) [?:1.8.0_45] Caused by: java.lang.ExceptionInInitializerError at java.lang.Class.forName0(Native Method) ~[?:1.8.0_45] at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_45] at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav a:64) ~[spigot.jar:git-Spigot-c3c767f-33d5de3] at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j ava:131) ~[spigot.jar:git-Spigot-c3c767f-33d5de3] ... 6 more Caused by: java.lang.NullPointerException at ultrapvp.ssm.Main.<clinit>(Main.java:47) ~[?:?] at java.lang.Class.forName0(Native Method) ~[?:1.8.0_45] at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_45] at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav a:64) ~[spigot.jar:git-Spigot-c3c767f-33d5de3] at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j ava:131) ~[spigot.jar:git-Spigot-c3c767f-33d5de3] ... 6 more Then here is my code: Spoiler: Code Code (Text): ItemStack Sword = new ItemStack(Material.IRON_SWORD); ItemMeta im = Sword.getItemMeta(); Enchantment unbreakingEnch = new EnchantmentWrapper(34); im.setDisplayName("§a§lSpider §e- §a§lRight-Click Hold"); Sword.setItemMeta(im); p.getInventory().addItem(Sword); // Here is the line of code. im.spigot().setUnbreakable(true);
Setting durability to 0 once isn't going to do anything, though. You'd have to constantly re-set it everytime the player hits something. The unbreakable tag really is the way to go for this, it works wonderfully. It may be worth figuring out why you're getting that NPE- I don't think it's possible for spigot() to return null, though I could be wrong there. And note that, NPE aside, you would need to call setItemMeta *after* calling setUnbreakable for that to work. EDIT: And you're sure that the line of code you highlighted is the one from the stack trace? You're really creating and giving a player a sword in the constructor of your Main plugin class? That seems kind of.. unlikely
Unbreakable Armor Code (Text): @EventHandler public void onDamage(EntityDamageEvent event) { if ((event.getEntity() instanceof Player)) { Player player = (Player) event.getEntity(); player.getInventory().getHelmet().setDurability((short) 0); player.getInventory().getChestplate().setDurability((short) 0); player.getInventory().getLeggings().setDurability((short) 0); player.getInventory().getBoots().setDurability((short) 0); } } Unbreakable Items - Tools Code (Text): And for tools you will need to ItemMeta.spigot().setUnbreakable(true);