Tidy up + purgeWorld() -- Remove any entities in the world... this

needs improving to only remove those necessary. Quick workaround for
now.
This commit is contained in:
Simon Rigby 2011-03-04 09:06:12 +00:00
parent 4ca35d1c6a
commit 8a52e55cdf
9 changed files with 261 additions and 118 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,7 +1,17 @@
package com.onarandombox.MultiVerseCore;
import org.bukkit.event.entity.EntityDamageEvent;
import java.util.List;
import org.bukkit.World;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Fireball;
import org.bukkit.entity.Ghast;
import org.bukkit.entity.Monster;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityListener;
import org.bukkit.event.entity.ExplosionPrimedEvent;
public class MVEntityListener extends EntityListener {
@ -10,9 +20,96 @@ public class MVEntityListener extends EntityListener {
public MVEntityListener(MultiVerseCore plugin) {
this.plugin = plugin;
}
public void onEntityDamaged(EntityDamageEvent event){
}
}
// Need to find a way to stop the Ghast Fireballs damaging
// surroundings but still doing damage to players.
public void onEntityExplode(EntityExplodeEvent event){
}
public void onExplosionPrimed(ExplosionPrimedEvent event){
if(event.getEntity() instanceof Fireball){
MultiVerseCore.log.info("Fireball");
// Fireballs on Explode trigger this, sadly we can't get the blocks it would destroy... thats onEntityExplode
// However can't figure out a way to check in onEntityExplode if it was a Fireball which caused it...
}
}
/**
* Handle Animal/Monster Spawn settings, seems like a more concrete method than using CraftBukkit.
*/
public void onCreatureSpawn(CreatureSpawnEvent event){
World world = event.getEntity().getWorld();
if(event.isCancelled()) return;
if(!(plugin.worlds.containsKey(world.getName()))) return; // Check if it's a world which we are meant to be managing.
MVWorld mvworld = plugin.worlds.get(world.getName());
// TODO: Look of this and see if there's a cleaner/better method of doing so...
/**
* Animal Handling
*/
if(event.getEntity() instanceof Animals){
// If we have no exceptions for Animals then we just follow the Spawn setting.
if(mvworld.animalList.size()<=0){
if(mvworld.animals){
return;
} else {
event.setCancelled(true);
return;
}
}
// The idea of the Exceptions is they do the OPPOSITE of what the Spawn setting is...
if(mvworld.animalList.contains(event.getMobType().toString().toUpperCase())){
if(mvworld.animals){
event.setCancelled(true);
return;
} else {
return;
}
}
}
/**
* Monster Handling
*/
if(event.getEntity() instanceof Monster){
// If we have no exceptions for Monsters then we just follow the Spawn setting.
if(mvworld.monsterList.size()<=0){
if(mvworld.monsters){
return;
} else {
event.setCancelled(true);
return;
}
}
// The idea of the Exceptions is they do the OPPOSITE of what the Spawn setting is...
if(mvworld.monsterList.contains(event.getMobType().toString().toUpperCase())){
if(mvworld.monsters){
event.setCancelled(true);
return;
} else {
return;
}
}
}
/**
* Ghast Handling -- This should only be temporary, noticed a bug where Ghasts would keep spawning and flood the Nether.
* However not sure about it... not sure of the effect on performance... got a few 'server overloaded' warnings through testing but not sure of the cause.
*/
if(event.getEntity() instanceof Ghast){
List<Entity> entities = world.getEntities();
int count = 0;
for(Entity entity : entities){
if(entity instanceof Ghast){
if(count>=MultiVerseCore.configMV.getInt("ghastlimit", 50)){
event.setCancelled(true);
return;
}
count++;
}
}
}
}
}

View File

@ -9,7 +9,7 @@ import org.bukkit.util.config.Configuration;
public class MVPlayerSession {
private Player player; // Player holder, may be unnecessary.
public Location loc; // Contain the Players Location so on player move we can compare this and check if they've moved a block.
public Location loc = new Location(null, 0,0,0); // Contain the Players Location so on player move we can compare this and check if they've moved a block.
public String portal = null; // Allow a player to target a portal to prevent them typing its name every command.

View File

@ -23,16 +23,23 @@ public class MVTeleport {
* @param player
* @return
*/
public Location getDestination(World world, Player player) {
public Location getDestination(World world, Player player, Location location) {
Location location = player.getLocation();
double x, y, z;
if(location==null){
location = player.getLocation();
double srcComp = plugin.worlds.get(player.getWorld().getName()).compression;
double trgComp = plugin.worlds.get(world.getName()).compression;
double x = location.getX() / (srcComp != 0 ? srcComp : 1) * trgComp + 0.5;
double y = location.getY();
double z = location.getZ() / (srcComp != 0 ? srcComp : 1) * trgComp + 0.5;
double srcComp = plugin.worlds.get(player.getWorld().getName()).compression;
double trgComp = plugin.worlds.get(world.getName()).compression;
x = location.getX() / (srcComp != 0 ? srcComp : 1) * trgComp + 0.5;
y = location.getY();
z = location.getZ() / (srcComp != 0 ? srcComp : 1) * trgComp + 0.5;
} else {
x = location.getX();
y = location.getY();
z = location.getZ();
}
if (y < 1 && world.getEnvironment() == Environment.NORMAL)
y = 1;
@ -42,14 +49,13 @@ public class MVTeleport {
}
while (this.blockIsNotSafe(world, x, y, z)) {
y++;
if (y == 110) {
if (y>=120) { // We don't want them being teleported on top of Bedrock in the Nether.
y = 1;
x = x + 1;
z = z + 1;
}
}
return new Location(world, x, y, z, location.getYaw(),
location.getPitch());
return new Location(world, x, y, z, location.getYaw(),location.getPitch());
}
/**
@ -63,13 +69,12 @@ public class MVTeleport {
* @return
*/
private boolean blockIsAboveAir(World world, double x, double y, double z) {
return (world.getBlockAt((int) Math.floor(x), (int) Math.floor(y - 1),
(int) Math.floor(z)).getType() == Material.AIR);
return (world.getBlockAt((int) Math.floor(x), (int) Math.floor(y - 1), (int) Math.floor(z)).getType() == Material.AIR);
}
/**
* This function checks whether the block at the coordinates given is safe
* or not by checking for Laval/Fire/Air etc.
* or not by checking for Lava/Fire/Air etc.
*
* @param world
* @param x
@ -78,20 +83,19 @@ public class MVTeleport {
* @return
*/
private boolean blockIsNotSafe(World world, double x, double y, double z) {
if ((world.getBlockAt((int) Math.floor(x), (int) Math.floor(y - 1),
(int) Math.floor(z)).getType() == Material.LAVA))
if ((world.getBlockAt((int) Math.floor(x), (int) Math.floor(y - 1),(int) Math.floor(z)).getType() == Material.LAVA))
return true;
if ((world.getBlockAt((int) Math.floor(x), (int) Math.floor(y - 1),
(int) Math.floor(z)).getType() == Material.STATIONARY_LAVA))
if ((world.getBlockAt((int) Math.floor(x), (int) Math.floor(y - 1),(int) Math.floor(z)).getType() == Material.STATIONARY_LAVA))
return true;
if ((world.getBlockAt((int) Math.floor(x), (int) Math.floor(y - 1),
(int) Math.floor(z)).getType() == Material.FIRE))
if ((world.getBlockAt((int) Math.floor(x), (int) Math.floor(y - 1),(int) Math.floor(z)).getType() == Material.FIRE))
return true;
if (world.getBlockAt((int) Math.floor(x), (int) Math.floor(y),
(int) Math.floor(z)).getType() != Material.AIR
|| world.getBlockAt((int) Math.floor(x),
(int) Math.floor(y + 1), (int) Math.floor(z)).getType() != Material.AIR)
if (world.getBlockAt((int) Math.floor(x), (int) Math.floor(y),(int) Math.floor(z)).getType() != Material.AIR
|| world.getBlockAt((int) Math.floor(x),(int) Math.floor(y + 1), (int) Math.floor(z)).getType() != Material.AIR)
return true;
if (blockIsAboveAir(world, x, y, z))
return true;
@ -123,11 +127,74 @@ public class MVTeleport {
return returnValue;
}
public boolean teleport(World w, Player p) {
/**
* Check if the Player has the permissions to enter this world.
*
* @param p
* @param w
* @return
*/
// TODO: To be sorted when Permissions is introduced.
/*public Boolean canEnterWorld(Player p, World w) {
List<String> whiteList = this.plugin.MVWorlds.get(w.getName()).joinWhitelist;
List<String> blackList = this.plugin.MVWorlds.get(w.getName()).joinBlacklist;
String group = MultiVerseCore.Permissions.getGroup(p.getName());
boolean returnValue = true;
if (whiteList.size() > 0)
returnValue = false;
for (int i = 0; i < whiteList.size(); i++){
if (whiteList.get(i).contains("g:") && group.equalsIgnoreCase(whiteList.get(i).split(":")[1])) {
returnValue = true;
break;
}
}
for (int i = 0; i < blackList.size(); i++){
if (blackList.get(i).contains("g:") && group.equalsIgnoreCase(blackList.get(i).split(":")[1])) {
returnValue = false;
break;
}
}
for (int i = 0; i < whiteList.size(); i++){
if (whiteList.get(i).equalsIgnoreCase(p.getName())) {
returnValue = true;
break;
}
}
for (int i = 0; i < blackList.size(); i++){
if (blackList.get(i).equalsIgnoreCase(p.getName())) {
returnValue = false;
break;
}
}
return returnValue;
}*/
/**
* This is to be used to travel to exact coordinates without Compression being taken into effect.
* EG: Portal to Portal teleportation
* EG: Portal to Specific Location
*/
public boolean teleport(World w, Player p, Location location) {
if (canTravelToWorld(w, p)) {
p.teleportTo(getDestination(w, p));
p.teleportTo(getDestination(w, p, location));
return true;
} else return false;
}
/**
* This is to be used when we wan't Compression to be used.
*/
public boolean teleport(World w, Player p) {
if (canTravelToWorld(w, p)) {
p.teleportTo(getDestination(w, p, null));
return true;
} else return false;
}
}

View File

@ -7,7 +7,6 @@ import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.util.config.Configuration;
import com.onarandombox.utils.stringLocation;
@ -25,7 +24,11 @@ public class MVWorld {
public String alias = ""; // Short Alias for the World, this will be used in Chat Prefixes.
public Boolean animals; // Does this World allow Animals to Spawn?
public List<String> animalList = new ArrayList<String>(); // Contain a list of Animals which we want to ignore the Spawn Setting.
public Boolean monsters; // Does this World allow Monsters to Spawn?
public List<String> monsterList = new ArrayList<String>(); // Contain a list of Monsters which we want to ignore the Spawn Setting.
public Boolean pvp; // Does this World allow PVP?
public List<String> blockBlacklist; // Contain a list of Blocks which we won't allow on this World.
@ -34,11 +37,9 @@ public class MVWorld {
public List<String> editWhitelist; // Contain a list of Players/Groups which can edit this World. (Place/Destroy Blocks)
public List<String> editBlacklist; // Contain a list of Players/Groups which cannot edit this World. (Place/Destroy Blocks)
public List<String> worldBlacklist; // Contain a list of Worlds which Players cannot use to Portal to this World.
public Double compression; //How stretched/compressed distances are
/**
*/
public Double compression; //How stretched/compressed distances are
public MVWorld(World world, Configuration config, MultiVerseCore instance){
this.config = config;
this.plugin = instance;
@ -47,13 +48,6 @@ public class MVWorld {
this.name = world.getName();
this.environment = world.getEnvironment();
// The following should already of been set when the World was created, so we don't wan't to overwrite these values we'll just grab them.
this.monsters = ((CraftWorld) world).getHandle().D; // TODO: Swap to Bukkit Function when implemented.
this.animals = ((CraftWorld) world).getHandle().E; // TODO: Swap to Bukkit Function when implemented.
// If MultiVerse created/loaded the World then it means we wan't to handle it as well, otherwise
// we don't touch any settings unless the user specifically asks us to.
this.alias = config.getString("worlds." + this.name + ".alias","");
this.pvp = config.getBoolean("worlds." + this.name + ".pvp", true);
@ -65,5 +59,18 @@ public class MVWorld {
this.blockBlacklist = config.getStringList("worlds." + name + ".blockBlacklist", new ArrayList<String>());
this.editWhitelist = config.getStringList("worlds." + name + ".editWhitelist", new ArrayList<String>());
this.editBlacklist = config.getStringList("worlds." + name + ".editBlacklist", new ArrayList<String>());
this.animals = config.getBoolean("worlds." + name + ".animals.spawn", true);
this.monsters = config.getBoolean("worlds." + name + ".monsters.spawn", true);
List<String> temp;
temp = config.getStringList("worlds." + name + ".animals.exceptions", new ArrayList<String>());
for(String s : temp){
this.animalList.add(s.toUpperCase());
}
temp = config.getStringList("worlds." + name + ".monsters.exceptions", new ArrayList<String>());
for(String s : temp){
this.monsterList.add(s.toUpperCase());
}
}
}

View File

@ -1,19 +0,0 @@
package com.onarandombox.MultiVerseCore;
import org.bukkit.World;
import org.bukkit.event.world.WorldEvent;
import org.bukkit.event.world.WorldListener;
public class MVWorldListener extends WorldListener {
MultiVerseCore plugin;
public MVWorldListener(MultiVerseCore plugin){
this.plugin = plugin;
}
public void onWorldLoad(WorldEvent event){
World world = event.getWorld();
System.out.print(world.getName());
}
}

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import org.bukkit.Server;
@ -11,7 +12,11 @@ import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.entity.Animals;
import org.bukkit.entity.CreatureType;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
@ -57,7 +62,6 @@ public class MultiVerseCore extends JavaPlugin {
private MVBlockListener blockListener = new MVBlockListener(this);
private MVEntityListener entityListener = new MVEntityListener(this);
private MVPluginListener pluginListener = new MVPluginListener(this);
private MVWorldListener worldListener = new MVWorldListener(this);
// HashMap to contain all the Worlds which this Plugin will manage.
public HashMap<String,MVWorld> worlds = new HashMap<String,MVWorld>();
@ -73,11 +77,10 @@ public class MultiVerseCore extends JavaPlugin {
}
/**
* What happens when the plugin gets around to be enabled...
* What happens when the plugin gets around to being enabled...
*/
public void onEnable() {
// Create the Plugin Data folder.
//this.getDataFolder().mkdir();
dataFolder.mkdir();
// Output a little snippet to show it's enabled.
@ -92,12 +95,12 @@ public class MultiVerseCore extends JavaPlugin {
}*/
// Call the defaultConfiguration class to create the config files if they don't already exist.
new defaultConfiguration(this.getDataFolder(), "config.yml");
new defaultConfiguration(this.getDataFolder(), "worlds.yml");
new defaultConfiguration(dataFolder, "config.yml");
new defaultConfiguration(dataFolder, "worlds.yml");
// Now grab the Configuration Files.
configMV = new Configuration(new File(this.getDataFolder(), "config.yml"));
configWorlds = new Configuration(new File(this.getDataFolder(), "worlds.yml"));
configMV = new Configuration(new File(dataFolder, "config.yml"));
configWorlds = new Configuration(new File(dataFolder, "worlds.yml"));
// Now attempt to Load the configurations.
try{ configMV.load(); } catch (Exception e){ log.info(MultiVerseCore.logPrefix + "- Failed to load config.yml"); }
@ -111,29 +114,50 @@ public class MultiVerseCore extends JavaPlugin {
pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener,Priority.Normal, this); // To remove Player Sessions
// These 3 events should only be required in the Portals module.
//pm.registerEvent(Event.Type.BLOCK_DAMAGED, blockListener,Priority.Normal, this); // For Set Coord 1 & Info Wand etc...
//pm.registerEvent(Event.Type.BLOCK_RIGHTCLICKED, blockListener,Priority.Normal, this); // For Set Coord 2
//pm.registerEvent(Event.Type.BLOCK_FLOW, blockListener, Priority.High,this); // To create Water/Lava Portals
pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener, Priority.Normal, this); // To prevent Blocks being destroyed.
pm.registerEvent(Event.Type.BLOCK_PLACED, blockListener, Priority.High, this); // To prevent Blocks being placed.
pm.registerEvent(Event.Type.BLOCK_PLACED, blockListener, Priority.Normal, this); // To prevent Blocks being placed.
pm.registerEvent(Event.Type.ENTITY_DAMAGED, entityListener, Priority.High, this); // To Allow/Disallow PVP.
pm.registerEvent(Event.Type.ENTITY_DAMAGED, entityListener, Priority.Normal, this); // To Allow/Disallow PVP.
pm.registerEvent(Event.Type.CREATURE_SPAWN, entityListener, Priority.Normal, this);
pm.registerEvent(Event.Type.WORLD_LOADED, worldListener, Priority.Highest, this); // Setup the Worlds config when a World is created.
pm.registerEvent(Event.Type.ENTITY_EXPLODE, entityListener, Priority.Normal, this);
pm.registerEvent(Event.Type.EXPLOSION_PRIMED, entityListener, Priority.Normal, this);
pm.registerEvent(Event.Type.PLUGIN_ENABLE, pluginListener, Priority.Normal, this); // Monitor for Permissions Plugin etc.
// Call the Function to load all the Worlds and setup the HashMap
loadWorlds();
// Purge Worlds of old Monsters/Animals which don't adhere to the setup.
purgeWorlds();
// Call the Function to assign all the Commands to their Class.
setupCommands();
}
/**
*
* Purge the Worlds of Entities that are disallowed.
*/
private void purgeWorlds() {
if(worlds.size()<=0) return;
Set<String> worldKeys = worlds.keySet();
for (String key : worldKeys){
World world = getServer().getWorld(key);
if(world==null) continue;
List<LivingEntity> entities = world.getLivingEntities();
// TODO: Refine this... need to cast to CreatureType or something, we only wan't to remove the creatures they don't want. Not all of them.
// TODO: Lack of Internet & JavaDocs ftw...
for (Entity entity: entities){
if(entity instanceof Monster || entity instanceof Animals){
entity.remove();
}
}
}
}
/**
* Setup commands to the Command Handler
*/
private void setupCommands() {
commands.put("mvcreate", new MVCreate(this));
@ -153,25 +177,7 @@ public class MultiVerseCore extends JavaPlugin {
private void loadWorlds() {
// Basic Counter to count how many Worlds we are loading.
int count = 0;
// Grab all the Worlds that already exist.
List<World> lworlds = getServer().getWorlds();
// You never know these days... bloody NPE's.
if(lworlds != null && lworlds.size()>0){
for (World world : lworlds){
log.info(logPrefix + "Loading existing World - '" + world.getName() + "' - " + world.getEnvironment().toString()); // Output to the Log that we are loading a world, specify the name and environment type.
worlds.put(world.getName(), new MVWorld(world, MultiVerseCore.configWorlds, this)); // Place the World into the HashMap.
count++; // Increment the World Count.
}
}
log.info(logPrefix + count + " - World(s) found.");
List<String> worldKeys = MultiVerseCore.configWorlds.getKeys("worlds"); // Grab all the Worlds from the Config.
count = 0;
if(worldKeys != null){
for (String worldKey : worldKeys){
// If this World already exists within the HashMap then we don't need to process it.
@ -193,19 +199,6 @@ public class MultiVerseCore extends JavaPlugin {
log.info(logPrefix + "Loading World & Settings - '" + worldKey + "' - " + wEnvironment); // Output to the Log that wea re loading a world, specify the name and environment type.
World world = getServer().createWorld(worldKey, env);
// Beta 1.3 =
// D = Monsters
// E = Animals
((CraftWorld) world).getHandle().D = monsters;
((CraftWorld) world).getHandle().E = animals;
//((CraftWorld) world).getHandle().q.a(i, j, k);
//Spawn Crap
// The following will be used once they accept the pull request.
//world.setMonsterSpawn = monsters;
//world.setAnimalSpawn = animals;
worlds.put(worldKey, new MVWorld(world, MultiVerseCore.configWorlds, this)); // Place the World into the HashMap.
@ -213,7 +206,6 @@ public class MultiVerseCore extends JavaPlugin {
}
}
log.info(logPrefix + count + " - World(s) loaded.");
}
/**
@ -265,7 +257,7 @@ public class MultiVerseCore extends JavaPlugin {
/**
* Basic Debug Output function, if we've enabled debugging we'll output more information.
*/
public void debugMsg(String msg, Player p){
public static void debugMsg(String msg, Player p){
if(debug){
log.info(msg);
if(p!=null){

View File

@ -16,8 +16,7 @@ public class defaultConfiguration {
File actual = new File(folder, name);
if (!actual.exists()) {
InputStream input = this.getClass().getResourceAsStream("/default/" + name);
InputStream input = this.getClass().getResourceAsStream("/defaults/" + name);
if (input != null) {
FileOutputStream output = null;
@ -30,7 +29,7 @@ public class defaultConfiguration {
output.write(buf, 0, length);
}
System.out.println(MultiVerseCore.logPrefix + "Default setup file written: " + name);
MultiVerseCore.log.info(MultiVerseCore.logPrefix + "Default setup file written: " + name);
} catch (Exception e) {
e.printStackTrace();
} finally {