[Help] Spawning round of zombie then spawning another after all of previous round are killed

Discussion in 'Plugin Development' started by dajako, Aug 18, 2013.

Thread Status:
Not open for further replies.
  1. So I want to spawn 5 zombies in 4 different locations. Once these 20 zombies have all been killed I want the number of zombies spawned to go up by +5 so that 10 zombies will spawn in the 4 locations and I want this process to continue on.

    I have been trying to do this for 2 days and this is what I have came up with:
    Code:
    package me.dajakos.plugin;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    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.CreatureSpawnEvent;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class plugin extends JavaPlugin implements Listener {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static plugin plugin;
        public final playereventlistener pl = new playereventlistener();
        public final blocklistener bl = new blocklistener();
     
        int zombieskilled = 0;
        int end = 0;
        int endround = 5;
        int actualendround = endround;
        int zombiesinround = endround*4;
        int actualround = 1;
     
     
     
        @Override
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been enabled!");
            getServer().getPluginManager().registerEvents(this, this);
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.pl, this);
            pm.registerEvents(this.bl, this);
            // TODO Insert logic to be performed when the plugin is enabled
        }
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been disabled!");
            // TODO Insert logic to be performed when the plugin is disabled
        }
     
      public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args){
            Player player = (Player) sender;
            World world = player.getWorld();
            Location spawn1 = new Location(world, -275, 51, -425);
            Location spawn2 = new Location(world, -330, 51, -416);
            Location spawn3 = new Location(world, -343, 51, -478);
            Location spawn4 = new Location(world, -271, 53, -484);
       
            if(lbl.equalsIgnoreCase("testdoa")){
            player.sendMessage("Welcome! You must kill all of the zombies in the area to proceed to the next round!");
            player.sendMessage("Round " + actualround);
            player.teleport(new Location(world, -308, 51, -444));
            do{
              world.spawnEntity(spawn1, EntityType.ZOMBIE);
              world.spawnEntity(spawn2, EntityType.ZOMBIE);
              world.spawnEntity(spawn3, EntityType.ZOMBIE);
              world.spawnEntity(spawn4, EntityType.ZOMBIE);
              actualendround--;
            }while(true);
            }
            return false;
            }
        @EventHandler
        public void EDE(EntityDeathEvent event){
            Player player = event.getEntity().getKiller();
            if(event.getEntity() instanceof Zombie){
            if(event.getEntity().getCustomName() == "ZOMBIE"){
                zombieskilled++;
                player.sendMessage("You have killed " + zombieskilled + " zombies this round!");
              if(zombieskilled == zombiesinround){
                end -= 5;
                actualendround = endround;
                player.sendMessage("Round " + actualround);
                do{
                    player.sendMessage("" + end);
                }while(actualendround == end);
                }
              }
              }
            }
        @EventHandler
        public void nodespawn(CreatureSpawnEvent event){
       
            double x = event.getEntity().getLocation().getX();
            double z = event.getEntity().getLocation().getZ();
            if(event.getEntity().getLocation().getWorld() == getServer().getWorld("map")){
            if(x > -349 && x < -268 && z > -488 && z < -407){
                event.getEntity().setCustomName("ZOMBIE");
                event.getEntity().setCustomNameVisible(true);
                event.getEntity().setRemoveWhenFarAway(false);
                }
            }
        }
    }
    The issues I am having are really confusing me and I have no idea about how to fix them. I will be happy to have to completely recode this but if I have to I will want some directions.

    Here are the issues that I have found:
    If the command is typed twice, the plugin crashes the server.
    Once the variable actualendround == end it won't update when it's isn't equal, how do I fix this?

    Ask any questions if you are confused!

    If you want to help in the long run then add me on skype, username dajakos (this may show as AdamHJ123).
     
  2. Offline

    Williscool98

    dajako
    Wrong section!
    Put this into the Plugin Development section! :)
     
  3. Damn, sorry lol
    I must have clicked wrongly
     
  4. Offline

    c0mp

    Moved to Plugin Development!
     
  5. Ah... I created a new thread and reported this one. Is that a problem?
     
  6. Offline

    c0mp

    Nope, took care of it!
     
  7. Offline

    JPG2000

    dajako You could assign each zombie a metadata, and use the entity deathe vent and check if the entiy has the zombies metatdata
     
  8. I have no issues with detecting the zombies just spawning them after the first round.
    The while() statement doesn't seem to update.
     
  9. Offline

    Kuuichi

    Alternatively, you could store entity ID's in an ArrayList. It's probably easier than getting/setting metadata, but it's personal preference really.
     
  10. I set all the zombies names in the area that I am using so that I can make sure that it's them that are selected.
    That isn't the issue though.
     
  11. Offline

    Saposhiente

    This right here is an infinite loop. You need to instead use while(actualendround != ...) or something of the such.
    You will encounter errors when executing the command more than once because lots of your variables (zombieskilled, actualendround, ...) are global to your plugin, therefore, when you run the command again, your second running will try to start up with all the wrong values. What you need to do is define a new class, ZombieSpawnManager, put these variables in that class, and then create a new instance of ZombieSpawnManager whenever the command is run.
     
  12. And sorry, I am sure I changed that lol. In the actual code it's actualendround != end.
     
  13. Offline

    Saposhiente

    Ah, then I know why Bukkit crashes. The second time you run the command, actualendround = 0 (because it's global to your plugin (see my note about creating a ZombieSpawnManager)). Then, in its first loop (since do while always runs at least once, unlike while), you call actualendround--;, which sets actualendround = -1. -1 != 0, so the loop keeps running until actualendround wraps around to zero, which is after looping 4294967296 times. Trying to spawn 17179869184 zombies will very definitely crash your server.
    Really I don't see why actualendround is an instance variable at all. You're using it to control the number of times you loop. A much better form would be
    Code:java
    1. for (int i = 0; i < endround; i++) {
    2. world.spawnEntity(spawn1, EntityType.ZOMBIE);
    3. world.spawnEntity(spawn2, EntityType.ZOMBIE);
    4. world.spawnEntity(spawn3, EntityType.ZOMBIE);
    5. world.spawnEntity(spawn4, EntityType.ZOMBIE);
    6. }

    Meanwhile, I don't understand the purpose of your
    Code:
                do{
                    player.sendMessage("" + end);
                }while(actualendround == end);
                }
     
  14. That 2nd do while loop was just to debug what the end variable was on to make sure that I hadn't done something stupid.
     
  15. Offline

    Saposhiente

    I still don't see why you didn't just
    if (DEBUG) {
    player.sendMessage("" + end);
    }
    instead of a strange loop that might execute once or might be an infinite loop.
    You really need to outline exactly what you need to do to accomplish your tasks; a lot of your methods make no sense.
     
  16. It was like 3am when I wrote that so yea.
     
Thread Status:
Not open for further replies.

Share This Page