[UNSOLVED] Unbreakable items

Discussion in 'Spigot Plugin Development' started by fujiboy4, May 21, 2015.

  1. 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.

    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);
         
           




    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);
        }
     
    • Agree Agree x 2
    • Useful Useful x 1
  2. I'll try it in an hour or two.
     
  3. Max durability is (short) 0.
     
  4. Do I really need that, or just keep the meta.spigot().setUnbreakable(true);?
     
  5. 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:
    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);
           
           
     
  6. Learning from that error, I would say it's advisable to stick with the (short) 0
     
  7. Ok thanks! :)
     
  8. NathanWolf

    Supporter

    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 :D
     
  9. 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);
     
  10. NathanWolf

    Supporter

    The Unbreakable tag works on armor, too.
     
  11. I give two ways :)
     
  12. Helpful! Thanks.
     
    • Useful Useful x 1
  13. Define the item
    Material (name) = Material.(name)
     
  14. can i use i t in 1.7 ??