Solved How to set Random Block Drops?

Discussion in 'Plugin Development' started by VNGC, Feb 7, 2020.

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

    VNGC

    Hello,
    i am a bit new to Java coding, but already coded my first Listeners.
    Now i want to make one , that randomizes the Block drops, but makes it so that the drops are "saved", so for ex. every grassblock drops the same, every oak log, and so on. but i dont know, how to actually set the drop to "random".

    Heres my actual Listener:

    Code:
    package com.vngc.Listener;
    
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class RandomBlockDropListener implements Listener{
     
            @EventHandler
            public void randomblockdrops(BlockBreakEvent e){ //Should be the best way right?
            Block block = e.getBlock();
            e.setCancelled(true);
            block.getWorld().dropItemNaturally(block.getLocation(), new ItemStack(Material.//RANDOM));
             
        }
    }
    

    if i am completely wrong with it, please dont judge me, i know im not even good.


    Thanks in advance

    ps. sorry for my bad english, i am german :c - and it should be for 1.14.4, if that's important
     
  2. Offline

    timtower Administrator Administrator Moderator

    @VNGC Get the possible values for Material, then select a random index.
     
  3. Offline

    VNGC


    well... I don't really know what that means. as i said i don't have that much idea about plugins, would be nice if you could maybe give me a little example.

    thank you for the fast reply
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    VNGC

  6. Offline

    timtower Administrator Administrator Moderator

    I won't spoonfeed.
     
  7. Offline

    VNGC

    then idk how to make it. and if nobody explains it to me, i won't ever get it. i saw much tutorials, but they didnt really helped me
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    VNGC

  10. Offline

    timtower Administrator Administrator Moderator

    What things did you make in java then? Random numbers are one of the things that starters check out
    Index is found in any loop.
     
  11. Offline

    VNGC

    I have a challenge plugin, so i can kill the dragon with different challenges, Like:
    Code:
    package com.vngc.Listener;
    
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;
    
    import com.vngc.Commands.Command_NoCrafting;
    
    
    public class NoCraftingListener implements Listener {
       
        @EventHandler
        public void onCraft(CraftItemEvent ce) {
            if(Command_NoCrafting.nocrafting){
                    HumanEntity p = ce.getWhoClicked();
                    p.damage(100);
                    p.sendMessage("§1[§c§lChallenge§1] §4" + p.getName() + " §ehat §5ein Item gecraftet! §eDadurch ist die Challenge vorbei!");
            }
        }   
    }
    but now i want to play it with random drops. so that ex. every grassblock drops a enderpearl or so. but i dont even know a bit how to do that, thats the problem. i actually know how to make an block drop another item, but dont know how to make the item random.
     
  12. Offline

    caderapee

    @VNGC Material are enum, so if you follow the last link, that should be ok even for a beginner.
     
  13. Offline

    bowlerguy66

    @VNGC To get a random material, you're going to need a Random object from java, you can make one like this:
    Code:
    Random rand = new Random();
    With this object, you can use rand.nextInt(bound) to get a number under the specified bound. It is important to know that this method returns numbers from 0 to (bound-1), almost like the bound is a ceiling that can't be passed.

    With Enums (like the Material object from Bukkit) there is a method that returns all the values in an array from the enum. You can get the length of the array like this:
    Code:
    int materialsArrayLength = Material.values().length;
    Using both of these together will allow you to get a random drop, using the array length as a bound for our Random object.
    Code:
    Material randomMaterial = Material.values()[rand.nextInt(materialsArrayLength)];
    As for making each drop permanent, you'll have to either store it in a HashMap (which will reset every time you restart/reload the server) or a config file. Good luck!

    (is it spoonfeeding if you explain it like I did?)
     
    Last edited: Feb 8, 2020
    Sw_aG likes this.
  14. Offline

    Sw_aG

  15. Offline

    VNGC


    Now it works, thanks for your great explanation!

    Thread can get closed
     
Thread Status:
Not open for further replies.

Share This Page