Add sewer mobs

This commit is contained in:
Sam 2017-02-20 21:02:49 +00:00
parent 77dfc2a7c7
commit 4fc991dd24
2 changed files with 89 additions and 111 deletions

View File

@ -1,111 +0,0 @@
package mineplex.gemhunters.world;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilMath;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.gemhunters.spawn.SpawnModule;
public class SewerMobs implements Listener
{
private static final int MAX_MOBS = 100;
private final Set<Entity> _entities;
public SewerMobs(JavaPlugin plugin)
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
_entities = new HashSet<>();
}
@EventHandler
public void update(UpdateEvent event)
{
if (event.getType() != UpdateType.SLOWEST)
{
return;
}
World world = Bukkit.getWorlds().get(0);
Iterator<Entity> iterator= _entities.iterator();
while (iterator.hasNext())
{
Entity entity = iterator.next();
if (entity.isDead() || !entity.isValid())
{
iterator.remove();
}
}
for (int i = 0; i < MAX_MOBS / 4; i++)
{
if (_entities.size() >= MAX_MOBS)
{
break;
}
EntityType type = UtilMath.random.nextBoolean() ? EntityType.ZOMBIE : EntityType.SKELETON;
Location location = getRandomLocation(world);
if (location == null)
{
continue;
}
_entities.add(world.spawnEntity(location, type));
}
}
public Location getRandomLocation(World world)
{
int attempts = 0;
while (attempts < 40)
{
Location location = new Location(world, UtilMath.r(SpawnModule.WORLD_BORDER_RADIUS * 2) - SpawnModule.WORLD_BORDER_RADIUS, 51, UtilMath.r(SpawnModule.WORLD_BORDER_RADIUS * 2) - SpawnModule.WORLD_BORDER_RADIUS);
if (!isSuitable(location.getBlock()))
{
continue;
}
attempts++;
}
return null;
}
public boolean isSuitable(Block block)
{
Block up = block.getRelative(BlockFace.UP);
Block down = block.getRelative(BlockFace.DOWN);
if (block.getType() != Material.AIR || up.getType() != Material.AIR || down.getType() == Material.AIR || UtilBlock.liquid(down) || UtilBlock.liquid(up) || UtilBlock.liquid(block))
{
return false;
}
return true;
}
}

View File

@ -0,0 +1,89 @@
package mineplex.gemhunters.world;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Skeleton;
import org.bukkit.entity.Spider;
import org.bukkit.entity.Zombie;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.Managers;
import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilMath;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
public class UndergroundMobs implements Listener
{
private static final int MAX_MOBS = 100;
private static final String SEWER_KEY = "SEWER_MOB";
private static final String SUBWAY_KEY = "SUBWAY_MOBS";
private final WorldDataModule _worldData;
private final World _world;
private final Set<Entity> _entities;
public UndergroundMobs(JavaPlugin plugin)
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
_worldData = Managers.require(WorldDataModule.class);
_world = Bukkit.getWorlds().get(0);
_entities = new HashSet<>();
}
@EventHandler
public void update(UpdateEvent event)
{
if (event.getType() != UpdateType.SEC_20)
{
return;
}
Iterator<Entity> iterator = _entities.iterator();
while (iterator.hasNext())
{
Entity entity = iterator.next();
if (entity.isDead() || !entity.isValid())
{
entity.remove();
iterator.remove();
}
}
for (int i = 0; i < 10; i++)
{
if (_entities.size() >= MAX_MOBS)
{
break;
}
{
Location location = UtilAlg.Random(_worldData.getCustomLocation(SEWER_KEY));
Class<? extends Entity> clazz = UtilMath.random.nextBoolean() ? Zombie.class : Skeleton.class;
Entity entity = _world.spawn(location, clazz);
_entities.add(entity);
}
{
Location location = UtilAlg.Random(_worldData.getCustomLocation(SUBWAY_KEY));
Class<? extends Entity> clazz = Spider.class;
Entity entity = _world.spawn(location, clazz);
_entities.add(entity);
}
}
}
}