Config Enchants

Discussion in 'Plugin Development' started by Niknea, May 15, 2014.

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

    Niknea

    Hey Guys,

    I want to create a config where the user can put the enchantment they want and the level, then it would be added to the item in game, how can I do this?

    Thanks,
    Niknea
     
  2. Offline

    DxDy

    That's quite an open-ended question. You can't expect us to do all the work for you ;)

    Basically you need to figure out the steps involved in doing this plugin, and then tackle each one individually (divide and conquer, as it's called). Also I think you need to figure out what exactly you mean by "added to the item in game".
     
  3. Offline

    RamonPeek

    I don't know if this works, but I think this should work. (The args do you have to figure out by your self).

    public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    if(alias.equalsignorecase("DiaPickaxeSilkTouch") {
    ItemStack SilkTouch = new ItemStack(Material.DIAMOND_PICKAXE);
    SilkTouch.AddEnchantments(Enchantment.SILK_TOUCH, 1);
    p.getInventory.remove(ITEM BEFORE ENCHANTED);
    p.getInventory.add(SilkTouch);
    }
    }

    Something like this should work, I am not on my 'Coding PC' So I can't test it by my self, I hope this helped you a little bit.
    (You can figure out by yourself how you put this in a config, I did this in a command because this is easier for me).

    Kindley Regards, Ramon Peek
    (Sorry for my bad English, I'm Dutch.)
     
  4. Offline

    Onlineids

    Make a string like item: 276 1 enchants:sharpness:1,unbreaking:3 name:&6DatoneItem
    then split the strings and loop through them looking for a string that contains "enchants:"
     
  5. Offline

    Niknea

    Onlineids RamonPeek DxDy
    Alright so here is what I did, yet the enchantment wont add onto the item, here is the code and config. http://pastie.org/9179729.

    Any help would be great.

    Here is my full code.
    Code:java
    1. package me.niknea.supremechests;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Block;
    7. import org.bukkit.block.Chest;
    8. import org.bukkit.enchantments.Enchantment;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.BlockBreakEvent;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15.  
    16. import java.util.ArrayList;
    17.  
    18. /**
    19. * Created by Niknea on 5/14/14.
    20. */
    21. public class SpawningChest implements Listener{
    22.  
    23. Main plugin;
    24. public SpawningChest(Main plugin){
    25. this.plugin = plugin;
    26. }
    27.  
    28. Chest chest;
    29.  
    30. @EventHandler
    31. public void onChance(BlockBreakEvent e){
    32. Player p = e.getPlayer();
    33. e.setCancelled(true);
    34. // Random random = new Random();
    35. // int dice = random.nextInt(21);
    36. // if(dice == 7){
    37. Block block = e.getBlock();
    38. Location blockloc = block.getLocation();
    39. blockloc.getBlock().setType(Material.CHEST);
    40. chest = (Chest) blockloc.getBlock().getState();
    41. getItems();
    42.  
    43.  
    44. // }
    45. }
    46.  
    47.  
    48. public void getItems(){
    49. if(plugin.getConfig().getString("Item0") != null){
    50. int amount = plugin.getConfig().getInt("Item0Amount");
    51. ItemStack Item0 = new ItemStack(Material.getMaterial(plugin.getConfig().getString("Item0")), amount);
    52. // Item0.addUnsafeEnchantment(Enchantment.getById(1), 2);
    53. ItemMeta meta = Item0.getItemMeta();
    54. if(plugin.getConfig().getInt("Item0Enchantment1") != 99 && plugin.getConfig().getInt("Item0Enchantment1Level") != 99){
    55. int enchantment1 = plugin.getConfig().getInt("Item0Enchantment1");
    56. int enchantment1Level = plugin.getConfig().getInt("Item0Enchantment1Level");
    57. //Item0.addUnsafeEnchantment(Enchantment.getById(Integer.valueOf(enchantment1)), Integer.valueOf(enchantment1Level));
    58. Item0.addUnsafeEnchantment(Enchantment.getById(1), 2);
    59. System.out.println("1");
    60. }
    61. if(plugin.getConfig().getString("Item0Name") != null){
    62. String displayName = plugin.getConfig().getString("Item0Name");
    63. meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', displayName));
    64. }
    65. if(plugin.getConfig().getString("Item0Lore") != null){
    66. ArrayList<String> Item0Lore = new ArrayList<String>();
    67. Item0Lore.add(plugin.getConfig().getString("Item0Lore"));
    68. meta.setLore(Item0Lore);
    69. Item0Lore.clear();
    70. }
    71. Item0.setItemMeta(meta);
    72. chest.getBlockInventory().setItem(0, Item0);
    73. }
    74. }
    75. }
    76.  


    The enchantment only gets added if the Item0.setItemMeta(meta); is gone, how can I fix that?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  6. Offline

    metalhedd

    Add the enchantments to the meta instead using ItemMeta.addEnchantment, it takes a boolean to ignore safety checks.
     
  7. Offline

    Niknea

    metalhedd how would I do that? Also I will be adding multiple enchantments, would it still work?
     
  8. Offline

    Onlineids

    ItemMeta im = item.getItemMeta()
    im.addEnchant()
    pseudo code
     
  9. Offline

    metalhedd


    I dont meant to be rude but I'm not going to answer either of those. far too obvious, and if they're not, you could always read the documentation, I gave you the method name.
     
  10. Offline

    RamonPeek

    Niknea I will try to figure it out for you this afternoon. :). If I figure it out, I will say it to you.
     
Thread Status:
Not open for further replies.

Share This Page