Code mistake ?

Discussion in 'Plugin Development' started by lilian58660, Jun 20, 2015.

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

    lilian58660

    Hi, i'm creating a plugin, when you die you become a zombie with your current inventory.
    Problem:
    When I die, the server create and keep creating much zombie ( like 10 000) and make my server crash.

    My Main class:
    Code:
    package minez.zombie;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class Main extends JavaPlugin implements Listener {
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
       
        List<ItemStack> items = new ArrayList<ItemStack>();
       
        protected transient Player player = null;
        protected transient Zombie zombie = null;
    
        protected transient ItemStack[] equipment = null;
        protected transient ItemStack itemInHand = null;
        protected transient ItemStack[] inventory = null;
       
       
       
       
       
       
        public void InvPlayer(Player player)
        {
          this.player = player;
          this.equipment = ((ItemStack[])player.getEquipment().getArmorContents().clone());
          this.itemInHand = player.getItemInHand();
          this.inventory = ((ItemStack[])player.getInventory().getContents().clone());
          this.player.getInventory().clear();
          this.player.getEquipment().clear();
          this.zombie = ((Zombie)player.getWorld().spawnEntity(player.getLocation(),
            EntityType.ZOMBIE));
          this.zombie.setBaby(false);
          this.zombie.getEquipment().setArmorContents(this.equipment);
          this.zombie.getEquipment().setItemInHand(this.itemInHand);
          this.zombie.getEquipment().setItemInHandDropChance(0.0F);
          this.zombie.getEquipment().setHelmetDropChance(0.0F);
          this.zombie.getEquipment().setChestplateDropChance(0.0F);
          this.zombie.getEquipment().setLeggingsDropChance(0.0F);
          this.zombie.getEquipment().setBootsDropChance(0.0F);
          this.zombie.setCanPickupItems(false);
          this.zombie.setCustomName(player.getName());
          this.zombie.setCustomNameVisible(true);
    
        }
       
       
       
    
    
        @EventHandler
        public void onDeath(PlayerDeathEvent event) {
            Player player = (Player) event.getEntity();
    
            if (player instanceof Player) {
    
                InvPlayer(player);
            }
        }
       
       
       
       
       
       
       
       
       
        @EventHandler
        public void onEntityDeathEvent(EntityDeathEvent e){
            for (ItemStack is : this.equipment)
            {
              if (is != null)
              {
                if (is.getType() != Material.AIR)
                {
                  this.zombie.getWorld().dropItemNaturally(this.zombie.getLocation(), is);
                }
              }
            }
            for (ItemStack is : this.inventory)
            {
              if (is != null)
              {
                if (is.getType() != Material.AIR)
                {
                  this.zombie.getWorld().dropItemNaturally(this.zombie.getLocation(), is);
                }
              }
            }
            this.player.setHealth(0.0D);
            this.zombie.remove();
            e.getDrops().clear();
        }
    
    
        @EventHandler
        public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
            if (!event.getEntityType().equals(EntityType.ZOMBIE)) {
                return;
            }
           
            event.getEntity()
                    .getWorld()
                    .playEffect(
                            event.getEntity().getLocation().add(0.0D, 0.6D, 0.0D),
                            Effect.STEP_SOUND, Material.ENDER_STONE);
            event.getEntity()
                    .getWorld()
                    .playSound(
                            event.getEntity().getLocation().add(0.0D, 0.6D, 0.0D),
                            Sound.HURT_FLESH, 1.0F, 1.0F);
           
        }
    
    
    
    
    
    
    
    
    
    
        public Zombie getEntity()
        {
            return this.zombie;
        }
    
        public Player getPlayer()
        {
            return this.player;
        }
    }
    
    
    
    
    
     
  2. Offline

    Ratismal

    Not sure why it's spawning 10000 zombies, but I found a flaw in your code.
    Code:
        @EventHandler
        public void onDeath(PlayerDeathEvent event) {
            Player player = (Player) event.getEntity();
    
            if (player instanceof Player) {
    
                InvPlayer(player);
            }
        }
    
    Variable 'player' is always of type Player, so checking if player is an instance of Player is redundant. I would make 'player' of type Entity instead, instead of casting it to Player.
    Another note is that PlayerDeathEvents only return Players as a victim, so you could revise the entire block to this:
    Code:
        @EventHandler
        public void onDeath(PlayerDeathEvent event) {
              InvPlayer(event.getEntity());
        }
    
    EDIT:
    EntityDeathEvent triggers on the death of ANY ENTITY, so you should make sure that it's an instance of the zombie in question.
     
  3. Offline

    lilian58660

    Okay Thank you.
    I have another problem:

    My updated codes:
    Code:
    package minez.zombie;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class Main extends JavaPlugin implements Listener {
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
       
        List<ItemStack> items = new ArrayList<ItemStack>();
       
        protected transient Player player = null;
        protected transient Zombie zombie = null;
    
        protected transient ItemStack[] equipment = null;
        protected transient ItemStack itemInHand = null;
        protected transient ItemStack[] inventory = null;
       
       
       
       
       
       
        public void InvPlayer(Player player)
        {
          this.player = player;
          this.equipment = ((ItemStack[])player.getEquipment().getArmorContents().clone());
          this.itemInHand = player.getItemInHand();
          this.inventory = ((ItemStack[])player.getInventory().getContents().clone());
          this.player.getInventory().clear();
          this.player.getEquipment().clear();
          this.zombie = ((Zombie)player.getWorld().spawnEntity(player.getLocation(),
            EntityType.ZOMBIE));
          this.zombie.setBaby(false);
          this.zombie.getEquipment().setArmorContents(this.equipment);
          this.zombie.getEquipment().setItemInHand(this.itemInHand);
          this.zombie.getEquipment().setItemInHandDropChance(0.0F);
          this.zombie.getEquipment().setHelmetDropChance(0.0F);
          this.zombie.getEquipment().setChestplateDropChance(0.0F);
          this.zombie.getEquipment().setLeggingsDropChance(0.0F);
          this.zombie.getEquipment().setBootsDropChance(0.0F);
          this.zombie.setCanPickupItems(false);
          this.zombie.setCustomName(player.getName());
          this.zombie.setCustomNameVisible(true);
         
    
        }
       
       
       
    
    
        @EventHandler
        public void onDeath(PlayerDeathEvent event) {
    
                InvPlayer(event.getEntity());
                event.getDrops().clear();
           
        }
       
       
       
       
       
       
       
       
       
        @EventHandler
        public void onEntityDeathEvent(EntityDeathEvent e){
            for (ItemStack is : this.equipment)
            {
              if (is != null)
              {
                if (is.getType() != Material.AIR)
                {
                  this.zombie.getWorld().dropItemNaturally(this.zombie.getLocation(), is);
                }
              }
            }
            for (ItemStack is : this.inventory)
            {
              if (is != null)
              {
                if (is.getType() != Material.AIR)
                {
                  this.zombie.getWorld().dropItemNaturally(this.zombie.getLocation(), is);
                }
              }
            }
        }
    
    
        @EventHandler
        public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
            if (!event.getEntityType().equals(EntityType.ZOMBIE)) {
                return;
            }
           
            event.getEntity()
                    .getWorld()
                    .playEffect(
                            event.getEntity().getLocation().add(0.0D, 0.6D, 0.0D),
                            Effect.STEP_SOUND, Material.ENDER_STONE);
            event.getEntity()
                    .getWorld()
                    .playSound(
                            event.getEntity().getLocation().add(0.0D, 0.6D, 0.0D),
                            Sound.HURT_FLESH, 1.0F, 1.0F);
           
        }
    
    
    
    
    
    
    
    
    
    
        public Zombie getEntity()
        {
            return this.zombie;
        }
    
        public Player getPlayer()
        {
            return this.player;
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    everything is working, exept when you die, the zombie spawn AND your drops are dropping in the ground

    I don't understand : Ihave

    Code:
        @EventHandler
        public void onDeath(PlayerDeathEvent event) {
    
                InvPlayer(event.getEntity());
                event.getDrops().clear();  //THIS
           
        }
    Thanks in advance :)
     
  4. Offline

    Ratismal

    Try replacing
    Code:
    event.getDrops().clear();
    with something like
    Code:
    PlayerInventory inv = event.getEntity().getInventory();
    inv.clear();
    inv.setArmorContents(null);
    
    This clears the player's inventory so they have nothing to drop. I haven't tested this so I'm not sure if it'll work.
     
  5. Offline

    lilian58660

    Not working :/
     
  6. Offline

    JBoss925

    Putting all of your code in one class like this makes debugging hard. Just a suggestion: make a "ZombiePlayer" class with attributes of inventory, player, and zombie instances that can perform methods on these attributes. Then have a manager that keeps track of all the "ZombiePlayer"s that can perform methods on the lists. Then have a listener class that uses events to effect change on the other two classes.
     
  7. Offline

    lilian58660

  8. Offline

    lilian58660

  9. Offline

    Zombie_Striker

    @lilian58660
    I had a quick look at your code. Do you even know what you're doing? If you have made any changes/ have any other classes, can you show the updated code.
     
Thread Status:
Not open for further replies.

Share This Page