Giving Player to many items

Discussion in 'Spigot Plugin Development' started by Nutowen, May 15, 2017.

  1. Code (Text):

     public void ItemR1(Player p,int slot){
            Random ran = new Random();
            float percentChance = ran.nextInt(100);
                int i = Main.inst().getConfig().getInt("Common.Items." + slot + ".percentage");
                if(InvCLickAdmin.getInstance().invC.getItem(slot) == null){
                    return;
                }
                System.out.println(percentChance);
                if (percentChance <= i){
                    p.getInventory().addItem(InvCLickAdmin.getInstance().invC.getItem(slot));
                }
               
                }
       
     
    This the method I use to get an item and give it to the player if it meets the percentage. In this case 50%. If it is under 50 the item is given. However when I have multiple methods for each slot in the inventory it gives me multiple items. How do I only give it one. I want all the percentages to only add up to 100
     
  2. Where do you use this method? Post your full Code.
    Also Consider to use ThreadLocalRandom instead of loading a new random class each time you execute the method.
     
  3. How do I use ThreadLocalRandom and https://hastebin.com/semozodire.swift
     
  4. Int chance = ThreadLocalRandom.current().nextInt(1,100);
    \\random number between 1 and 100


    Sent from my iPhone using Tapatalk
     
  5. Example on how to use ThreadLocalRandom:

    (This will give you a random percentage between 0.0 and 100.0 in double.class )
    Code (Text):

    double percentage = ThreadLocalRandom.current().nextDouble(0.0,100.0);
     
    So back to your main issue, all i see is you apply the method for a player that interacts with a right click in air. Do you mean by receiving multiple items, receiving 4 items instead of 2 ( because you run "ItemR(player,0)" & "ItemR(player,1)" )
    Or do you mean that you only want to apply the method once with a random slot id so you only have to use once the method.
     
  6. I only want 1 item to be given from the player.
     
  7. So basically your runnable produces the "ItemR1" methods twice

    Code (Text):

    ItemR1(p, 0);
    ItemR1(p, 1);
     
    So what do you need ?

    The method "public void ItemR1(Player p,int slot){"

    would only give you the item from "InvCLickAdmin.getInstance().invC.getItem(slot)"

    So do you mean you only want 1 item with amount = 1
    or do you want 1 item from a random slot number?
     
  8. I want the player to be given 1 item for a slot in the inventory based on a percentage which is stored in the config
     
  9. i think we are speaking on two different channels xd
    So to make this clear, you want 1 player to have 1 item in his inventory nothing more right?
     
  10. It can be one item or if the slot has a item that is stackable all those stacked items are given too
     
  11. What? So you want 1 item for 1 slot like ( 9 slots in the hotbar ) should be filled up then?
    Or how do you declare "slots"?

    Example:
    You wanna add 9 items to your hotbar
    Everytime you use the method, there's a percentage ( example we get 3 times a positive result and will receive an item )
    Every 3 times you receive a stone block with different amount values.
    The first slot has an amount of 5 stone blocks, the second slot has an amount of 25 blocks and the third slot has an amount of 1 block.

    So my question is, what do you mean with receiving multiple items ?

    You execute the method twice ( for slot 0 and 1 )

    So the percentage is unknown at the time.

    Would be good to like output the percentage so you'll see if your system has the access to the items because the percentage is lower than 50 or the percentage is higher than 50 ( player won't receive any item) > Post the Values that you receive and your hotbar as example in here.

    So i can see what you mean with multiple items. By now you should only receive a max. item size of 2 ( because you run your methods only twice. )

    Note: Post some Images if possible to let us know what you are doing.
     
  12. You dont understand what I want. It is a crate system when your right click the item you are given a item based on percentages. Each crate has 9 items
     
  13. It seems as if your over complicating it.

    int count = 0;
    int crateSlot = 0;

    while(count == 0){
    check slot here if they get it set count to 1 and the loop stops, if they dont get it increment crateSlot by 1 unless its 8 then raise an error cause something messed up
    }