Solved Something is wrong in my code i need help!

Discussion in 'Plugin Development' started by CahyaAziz, Jan 15, 2021.

  1. Offline

    CahyaAziz

    Code:
    package me.cahya.randomplugin.Listeners;
    
    import me.cahya.randomplugin.RandomPlugin;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    
    import java.util.Random;
    
    public class Listeners implements Listener {
    
        private RandomPlugin plugin;
    
        public EntityType[] mobs;
    
        public Listeners(RandomPlugin plugin) {
            this.plugin = plugin;
    
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
    
       
    
        @EventHandler
        public void onBreak(BlockBreakEvent e) {
    
            Player p = e.getPlayer();
            Random rnd = new Random();
            Block blk = e.getBlock();
            Location bloc = blk.getLocation();
            EntityType rmobs = mobs[rnd.nextInt(mobs.length)];
            World w = p.getWorld();
    
            w.spawnEntity(bloc, rmobs);
    
        }
    }
    
    I want to know what i did wrong here.

    I want to spawn an entity every time a block is broken but i can't manage it to work, If anyone can help me it would really be appreciated.
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    CahyaAziz

  4. Offline

    timtower Administrator Administrator Moderator

    @CahyaAziz Kill the server, start it, stop it again.
    Then post that log.
     
  5. Offline

    CahyaAziz

  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    CahyaAziz

    SO i'm trying is to spawn random mobs every time a block is broken

    (for now i'm just gonna try to test spawning any entities and not only monsters)

    So in the code does:
    public EntityType[] mobs;

    works to get the list of all entities and then put it in a randomizer in
    EntityType rmobs = mobs[rnd.nextInt(mobs.length)];
    ?
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    CahyaAziz

    Can you give me an example of the solutions? Sorry i am new to this and still learning
     
  10. Offline

    timtower Administrator Administrator Moderator

    What do you want in that array?
     
  11. Offline

    CahyaAziz

    List of every mobs to then be randomized in my random rnd
     
  12. Offline

    timtower Administrator Administrator Moderator

    Then you need to initialize the list with all mobs.
     
  13. Offline

    CahyaAziz

    So manually list the mobs something like EntityType[] mobs = {EntityType.COW, EntityType.CREEPER, EntityType.SHEEP, etc...}
    ?
     
  14. Offline

    timtower Administrator Administrator Moderator

  15. Offline

    CahyaAziz

    Tried it and it works thanks

    Thanks

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 15, 2021

Share This Page