Solved How do I make a sword with a name I chose, cannot be broken?

Discussion in 'Plugin Development' started by Secriz, Aug 17, 2013.

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

    Secriz

    I want to make a sword with name I chose, cannot be broken. How do I do it?
    I'll be happy if even give sample code.

    Thanks for who try to help!
     
  2. Offline

    chris4315

    Feel free to use this code:

    Code:java
    1.  
    2. // Place this snippet in whichever method or event you use to give the player a sword. Eg. onCommand()
    3. Player player = (Player) sender; // or if you're using an event, use Player player = event.getPlayer();
    4. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
    5. ItemMeta swordMeta = sword.getItemMeta();
    6. swordMeta.setDisplayName(ChatColor.AQUA + "My Sword's Name");
    7. swordMeta.addEnchant(Enchantment.DURABILITY, 10, true);
    8. sword.setItemMeta(swordMeta);
    9. player.getInventory().addItem(sword);
    10.  
     
  3. Offline

    rsod

    chris4315 still will be breaking.
    You need to catch when player using this sword and reset durability back to zero.
     
  4. Offline

    chris4315

  5. Offline

    GaaTavares

    You can use the chris4315 resource for changing names, but if you want the sword NEVER BREAKS, listen to itembreakevent, check if the item has metadata, and cancel!
    Not tested.
     
  6. Offline

    Secriz

  7. Offline

    dunbaratu

  8. Offline

    Secriz

    Now when I checked, it can break.
    I want it to not break at all.

    dunbaratu It helps, but it's not with the name specified and color.
     
  9. Offline

    chris4315

    At least the item naming work'd :D.
     
  10. Offline

    dunbaratu

    Well the thing is, like most programming problems, try to break the problem into two halves. That thread solves the "how do you stop damaging items" problem. That only leaves "how to make an if condition that triggers only on the items I want it to" which can be solved separately.

    Firstly, you'll notice that there are multiple events that have to be trapped and checked, so it's going to be a good idea to make your own method that returns boolean for the item check and re-use it all over the place.

    Something like:
    Code:java
    1.  
    2. protected boolean isDurable( Item itm ) {
    3. /* put your check for the right metadata magic keyword here */
    4. }
    5.  
     
  11. Offline

    chris4315

    Where he means put your check for the right metadata magic keyword here is to check if the item you are wanting to make infinitely durable is actually the sword you named, and not some player's regular sword.

    Use:
    Code:java
    1.  
    2. if (itm.getItemMeta().getDisplayName() == ChatColor.AQUA + "My Sword Name") {
    3. return true;
    4. }
    5. return false;
    6.  
     
Thread Status:
Not open for further replies.

Share This Page