Solved Multiply a block drop/Make a chance system

Discussion in 'Spigot Plugin Development' started by Stellrow, Aug 10, 2018 at 1:26 PM.

  1. Som im trying to give a chance to multiply a block but i cant figure out how
    Im trying
    Code (Text):
    if(plugin.getLVL(uuid) >= 1) {
                ItemStack item = new ItemStack(block.getType());
                block.getDrops().add(item);
    Wich it seems dumb but... any ideeas how would i multiply the drop(maybe by removing the drop and add 2 instead) and also how to make a chance system? Like 10% chance to do something(scaling/one for each level)
    Thanks
     
  2. For (pseudo) random numbers, use java.util.Random. I find the system's nano time to be a decent random seed as it's unpredictable.
    Code (Text):
    Random random = new Random(System.nanoTime());
    ItemStack item = new ItemStack(block.getType(), 1 + random.nextInt(10));
    Random#nextInt(10) will yield a random number of at least 0 and at most 9. So a 10% chance would look like this:
    Code (Text):
    if (random.nextInt(10) == 0) { ... }
     
  3. Code (Java):


    int chance = ThreadLocalRandom.current().nextInt(100) + 1; // random int from 1 to 100

    if (chance <= 10) { // 10% chance

    int randomInt = ThreadLocalRandom.current().nextInt(5) + 1; // random int from 1 to 5
    ItemStack item = new ItemStack(block.getType(), randomInt);

    }



     
     
    • Informative Informative x 1
  4. Hmm i see but what about getting the broken block? Like i will use the randomint,check if its true but how would i double the broken block?
     
  5. Assuming you're doing this in a BlockBreakEvent, just tell it not to drop anything, then drop your own items.
     
    • Agree Agree x 1
  6. so i tried and got something working but is it efficient? can you tell me if there is something better?
    Code (Text):
    if(plugin.getLVL(uuid) >= 1) {
                block.getDrops().clear();
                Location loc = block.getLocation();
                ItemStack drop = new ItemStack(block.getType(), 2);
                World world = block.getWorld();
                e.setDropItems(false);
                world.dropItem(loc, drop);
             
            }
     
  7. You needn't touch the #getDrops collection.
    #setDropItems will do the work for you.

    new ItemStack(block.getType(), 2); -> This is not random, it will always return an itemstack with an amount of 2.
     
  8. ik this is just for the doubling the block part, im doing the random chance to drop it now

    So seems i said it wrong, i want a chance system.. something like everytime i break a block a "spinning wheel rolls" if it lands on like 1 the player will get double drops,dont want to put a random block number on the itemstack....
     
  9. Which version are you on? On 1.13 might work kinda fine since every item has a unique type but on 1.12 this will not always fine since items are also based on durability other than material.
    Even more, you might even lose some drops for example leaves won't drop saplings. There is no really easy way to do this unfortunately unless you use NMS (Which is doable, tested)

    getDrops is known not to be 100% reliable.
     
    • Optimistic Optimistic x 1
  10. yeah the getdrops was from previous attempt


    So seems i said it wrong, i want a chance system.. something like everytime i break a block a "spinning wheel rolls" if it lands on like 1 the player will get double drops,dont want to put a random block number on the itemstack....

    As i will make other actions to run as well..
     
  11. how dumb can i be to not see the chance system right there. mb
     

Share This Page