Event issue

Discussion in 'Plugin Development' started by Charliebiff05, Mar 19, 2019.

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

    Charliebiff05

    Code:
    package me.charlie.Custommobs;
    
    import java.util.concurrent.ThreadLocalRandom;
    
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.metadata.FixedMetadataValue;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;
    
    public class C01 implements Listener {
        private Main plugin;
        public C01(Main main){
            this.plugin = main;
        }
        @EventHandler
        public void onBreakBlock(BlockBreakEvent e){
            if(e.getBlock().getType()== Material.ENCHANTMENT_TABLE){
                e.setCancelled(true);
                e.getBlock().setType(Material.AIR);
                Zombie zombie = e.getBlock().getWorld().spawn(e.getBlock().getLocation().add(0.5,0,0.5),Zombie.class);
                zombie.setCustomName("Summoned Dread +2");
                zombie.setCustomNameVisible(true);
            
                zombie.getEquipment().setItemInMainHand(new ItemStack(Material.STONE_SWORD));
            
                zombie.getEquipment().setHelmet(new ItemStack(Material.LEATHER_HELMET));        
                zombie.getEquipment().setLeggings(new ItemStack(Material.CHAINMAIL_LEGGINGS));
                zombie.getEquipment().setBoots(new ItemStack(Material.CHAINMAIL_BOOTS));
                zombie.setMetadata("dread",new FixedMetadataValue(plugin,"dread"));
            }
        }
        @EventHandler
        public void onDamage(EntityDamageByEntityEvent e){
            if(e.getEntity() instanceof Zombie && e.getDamager() instanceof Player){
                if(e.getEntity().hasMetadata("dread")){
                    int random = ThreadLocalRandom.current().nextInt(10);
                    if(random<5){
                        e.setCancelled(true);
                        Player player = (Player)e.getDamager();
                        player.playSound(player.getLocation(), Sound.valueOf("ANVIL_LAND"),10,10);
                        player.sendMessage("- Block");
    //This event here do not work
                    }
                    return;
                }
            }
    
                if(e.getDamager().hasMetadata("dread")){
                    int random = ThreadLocalRandom.current().nextInt(10);
                    if(random<5){
                        e.setCancelled(true);
                    
                        Player player = (Player)e.getEntity();
                        player.setVelocity(new Vector(0,1,0));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS,60,2));
                        player.sendMessage("- Critical");
                    }
                }
            }
        }
    I have tried Looking around and solving this issue but, the second event works just fine then the first does not??? I can't work it out.

    Cheers thanks
     
    Last edited by a moderator: Mar 19, 2019
  2. Online

    timtower Administrator Administrator Moderator

    @Charliebiff05 Does it not work or does it never get past the random?
     
  3. Offline

    Charliebiff05

    @timtower Yeah it gets past the first event but then it gets the the second and the message is not seen, I think the actual block still works...
     
  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    Charliebiff05

    out of ten
     
  6. Online

    timtower Administrator Administrator Moderator

    You have an if statement there that checks the value of random, what is the value of random? Print it to the console so you can check it.
     
    Chr0mosom3 and KarimAKL like this.
Thread Status:
Not open for further replies.

Share This Page