how to make an enchant list and player get one in random way

Discussion in 'Plugin Development' started by Lorey879, Oct 20, 2014.

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

    Lorey879

    Hi ;)
    Guys how Can i Make aan Enchantmet list and
    when a player want to use it by command he get one randomly
    (sorry For bad english)


    Thanks.
     
  2. Offline

    SleepyDog

    I suggest that you attempt this before you ask for code, if you want a plugin made for you hop over to plugin requests forum.
    If you want to make this yourself make sure you can make default plugin layout then send us you attempt.
     
    FerusGrim likes this.
  3. Offline

    Unica

    Lorey879

    • Create a list / set with enchantments you want
      Code:java
      1. HashSet<Enchantment> enchants = new HashSet<Enchantment>(); //HashSet
      2.  
      3. private Enchantment getRandomEnchantment(){ //create a method
      4. enchants.clear(); //Clear current list
      5. enchants.add(Enchantment.SOMETHING); //add enchantments
      6. enchants.add(Enchantment.OTHER);
      7. return enchants.get(new Random().nextInt(enchants.size())); //Return a random value out of the list
      8. }
    • return random value out of that list
     
  4. Offline

    fireblast709

    Unica No Sets, only Lists (since they are meant for random access). Also, no need to clear the collection. Lastly, create a Random object and reuse it.
     
  5. Offline

    Lorey879


    Thanks Dude ~<3

    no
    no I dont want the code ;)
    because iam trying to make my own plugin


    Didnt Work :(
    My Code Is ..


    Code:java
    1. ItemStack item = p.getItemInHand();
    2.  
    3.  
    4. addEnchantments(item, 2);
    5.  
    6.  
    7.  
    8.  
    9. Bukkit.getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
    10.  
    11. @Override
    12. public void run() {
    13. players.remove(p);
    14. }
    15. }, 200);
    16.  
    17. return false;
    18. }
    19. return false;
    20. }
    21.  
    22. private void addEnchantments(ItemStack item, int count) {
    23. Random rnd = new Random();
    24. int enchCount = rnd.nextInt(count) + 1;
    25.  
    26. for(Enchantment en : Enchantment.values()) {
    27.  
    28.  
    29. for (int i=0; i<enchCount; i++) {
    30.  
    31. if(en.canEnchantItem(item)) {
    32. Enchantment ench = en;
    33. item.addUnsafeEnchantment(ench, rnd.nextInt(ench.getMaxLevel()) + 8);
    34.  
    35. return;
    36. }



    This Is The Enchant Part :)

    BumP

    Any One There ?

    :/

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

    RingOfStorms

    Don;t bump within 24 hours of the last post. Be patient and wait for a reply.

    Also you didn't specify what isn't working in your code. Paste a stacktrace or tell us what part is not working.
     
  7. Offline

    Lorey879

    ok sorry xD
    my problem is i want to add more than one enchant and get it randomly /:
    iam trying to make it but it didnt work :/

    can you help me ;( ?

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

    TheCodingCat

    here is what you do Lorey879
    [1] you create an ArrayList<EnchantmentType>. in your onEnable loop through aall known enchantments and add them to the list
    [2] in whatever method you want create a random and make on thats from the size of your list to 0
    [3] once you get the random int, create an enchantment and get it using list.get(yourRandomInt)
     
  9. Offline

    Lorey879

Thread Status:
Not open for further replies.

Share This Page