Checkpoint on split between bukkit-specific and common code core
This commit is contained in:
parent
9c49054c89
commit
b64094795e
50 changed files with 2333 additions and 1477 deletions
43
src/main/java/org/dynmap/bukkit/Armor.java
Normal file
43
src/main/java/org/dynmap/bukkit/Armor.java
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package org.dynmap.bukkit;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
|
||||
public class Armor {
|
||||
/**
|
||||
* http://www.minecraftwiki.net/wiki/Item_Durability#Armor_durability
|
||||
* We rely on getArmorContents() to return 4 armor pieces in the order
|
||||
* of: boots, pants, chest, helmet
|
||||
*/
|
||||
private static final int armorPoints[] = {3, 6, 8, 3};
|
||||
|
||||
public static final int getArmorPoints(Player player) {
|
||||
int currentDurability = 0;
|
||||
int baseDurability = 0;
|
||||
int baseArmorPoints = 0;
|
||||
ItemStack[] itm = new ItemStack[4];
|
||||
PlayerInventory inv = player.getInventory();
|
||||
itm[0] = inv.getBoots();
|
||||
itm[1]= inv.getLeggings();
|
||||
itm[2] = inv.getChestplate();
|
||||
itm[3] = inv.getHelmet();
|
||||
for(int i = 0; i < 4; i++) {
|
||||
if(itm[i] == null) continue;
|
||||
int dur = itm[i].getDurability();
|
||||
int max = itm[i].getType().getMaxDurability();
|
||||
if(max <= 0) continue;
|
||||
if(i == 2)
|
||||
max = max + 1; /* Always 1 too low for chestplate */
|
||||
else
|
||||
max = max - 3; /* Always 3 too high, versus how client calculates it */
|
||||
baseDurability += max;
|
||||
currentDurability += max - dur;
|
||||
baseArmorPoints += armorPoints[i];
|
||||
}
|
||||
int ap = 0;
|
||||
if(baseDurability > 0)
|
||||
ap = ((baseArmorPoints - 1) * currentDurability) / baseDurability + 1;
|
||||
return ap;
|
||||
}
|
||||
}
|
||||
416
src/main/java/org/dynmap/bukkit/BukkitEventProcessor.java
Normal file
416
src/main/java/org/dynmap/bukkit/BukkitEventProcessor.java
Normal file
|
|
@ -0,0 +1,416 @@
|
|||
package org.dynmap.bukkit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.event.CustomEventListener;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockBurnEvent;
|
||||
import org.bukkit.event.block.BlockFadeEvent;
|
||||
import org.bukkit.event.block.BlockFormEvent;
|
||||
import org.bukkit.event.block.BlockFromToEvent;
|
||||
import org.bukkit.event.block.BlockListener;
|
||||
import org.bukkit.event.block.BlockPhysicsEvent;
|
||||
import org.bukkit.event.block.BlockPistonExtendEvent;
|
||||
import org.bukkit.event.block.BlockPistonRetractEvent;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.block.BlockSpreadEvent;
|
||||
import org.bukkit.event.block.LeavesDecayEvent;
|
||||
import org.bukkit.event.block.SignChangeEvent;
|
||||
import org.bukkit.event.entity.EntityExplodeEvent;
|
||||
import org.bukkit.event.entity.EntityListener;
|
||||
import org.bukkit.event.player.PlayerBedLeaveEvent;
|
||||
import org.bukkit.event.player.PlayerChatEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerListener;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.event.world.ChunkLoadEvent;
|
||||
import org.bukkit.event.world.ChunkPopulateEvent;
|
||||
import org.bukkit.event.world.SpawnChangeEvent;
|
||||
import org.bukkit.event.world.WorldListener;
|
||||
import org.bukkit.event.world.WorldLoadEvent;
|
||||
import org.bukkit.event.world.WorldUnloadEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.dynmap.Log;
|
||||
|
||||
public class BukkitEventProcessor {
|
||||
private Plugin plugin;
|
||||
private PluginManager pm;
|
||||
|
||||
private HashMap<Event.Type, List<Listener>> event_handlers = new HashMap<Event.Type, List<Listener>>();
|
||||
|
||||
public BukkitEventProcessor(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
this.pm = plugin.getServer().getPluginManager();
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
/* Clean up all registered handlers */
|
||||
for(Event.Type t : event_handlers.keySet()) {
|
||||
List<Listener> ll = event_handlers.get(t);
|
||||
ll.clear(); /* Empty list - we use presence of list to remember that we've registered with Bukkit */
|
||||
}
|
||||
pm = null;
|
||||
plugin = null;
|
||||
}
|
||||
|
||||
private BlockListener ourBlockEventHandler = new BlockListener() {
|
||||
|
||||
@Override
|
||||
public void onBlockPlace(BlockPlaceEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onBlockPlace(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockBreak(BlockBreakEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onBlockBreak(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLeavesDecay(LeavesDecayEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onLeavesDecay(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockBurn(BlockBurnEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onBlockBurn(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockForm(BlockFormEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onBlockForm(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onBlockFade(BlockFadeEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onBlockFade(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onBlockSpread(BlockSpreadEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onBlockSpread(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onBlockFromTo(BlockFromToEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onBlockFromTo(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onBlockPhysics(BlockPhysicsEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onBlockPhysics(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onBlockPistonRetract(BlockPistonRetractEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onBlockPistonRetract(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onBlockPistonExtend(BlockPistonExtendEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onBlockPistonExtend(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onSignChange(SignChangeEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((BlockListener)l).onSignChange(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
private PlayerListener ourPlayerEventHandler = new PlayerListener() {
|
||||
@Override
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((PlayerListener)l).onPlayerJoin(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onPlayerLogin(PlayerLoginEvent event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((PlayerListener)l).onPlayerLogin(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerMove(PlayerMoveEvent event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((PlayerListener)l).onPlayerMove(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((PlayerListener)l).onPlayerQuit(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerBedLeave(PlayerBedLeaveEvent event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((PlayerListener)l).onPlayerBedLeave(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerChat(PlayerChatEvent event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((PlayerListener)l).onPlayerChat(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private WorldListener ourWorldEventHandler = new WorldListener() {
|
||||
@Override
|
||||
public void onWorldLoad(WorldLoadEvent event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((WorldListener)l).onWorldLoad(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onWorldUnload(WorldUnloadEvent event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((WorldListener)l).onWorldUnload(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onChunkLoad(ChunkLoadEvent event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((WorldListener)l).onChunkLoad(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onChunkPopulate(ChunkPopulateEvent event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((WorldListener)l).onChunkPopulate(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onSpawnChange(SpawnChangeEvent event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((WorldListener)l).onSpawnChange(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private CustomEventListener ourCustomEventHandler = new CustomEventListener() {
|
||||
@Override
|
||||
public void onCustomEvent(Event event) {
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((CustomEventListener)l).onCustomEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private EntityListener ourEntityEventHandler = new EntityListener() {
|
||||
@Override
|
||||
public void onEntityExplode(EntityExplodeEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
/* Call listeners */
|
||||
List<Listener> ll = event_handlers.get(event.getType());
|
||||
if(ll != null) {
|
||||
for(Listener l : ll) {
|
||||
((EntityListener)l).onEntityExplode(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Register event listener - this will be cleaned up properly on a /dynmap reload, unlike
|
||||
* registering with Bukkit directly
|
||||
*/
|
||||
public void registerEvent(Event.Type type, Listener listener) {
|
||||
List<Listener> ll = event_handlers.get(type);
|
||||
if(ll == null) {
|
||||
switch(type) { /* See if it is a type we're brokering */
|
||||
case PLAYER_LOGIN:
|
||||
case PLAYER_CHAT:
|
||||
case PLAYER_JOIN:
|
||||
case PLAYER_QUIT:
|
||||
case PLAYER_MOVE:
|
||||
case PLAYER_BED_LEAVE:
|
||||
pm.registerEvent(type, ourPlayerEventHandler, Event.Priority.Monitor, plugin);
|
||||
break;
|
||||
case BLOCK_PLACE:
|
||||
case BLOCK_BREAK:
|
||||
case LEAVES_DECAY:
|
||||
case BLOCK_BURN:
|
||||
case BLOCK_FORM:
|
||||
case BLOCK_FADE:
|
||||
case BLOCK_SPREAD:
|
||||
case BLOCK_FROMTO:
|
||||
case BLOCK_PHYSICS:
|
||||
case BLOCK_PISTON_EXTEND:
|
||||
case BLOCK_PISTON_RETRACT:
|
||||
pm.registerEvent(type, ourBlockEventHandler, Event.Priority.Monitor, plugin);
|
||||
break;
|
||||
case SIGN_CHANGE:
|
||||
pm.registerEvent(type, ourBlockEventHandler, Event.Priority.Low, plugin);
|
||||
break;
|
||||
case WORLD_LOAD:
|
||||
case WORLD_UNLOAD:
|
||||
case CHUNK_LOAD:
|
||||
case CHUNK_POPULATED:
|
||||
case SPAWN_CHANGE:
|
||||
pm.registerEvent(type, ourWorldEventHandler, Event.Priority.Monitor, plugin);
|
||||
break;
|
||||
case CUSTOM_EVENT:
|
||||
pm.registerEvent(type, ourCustomEventHandler, Event.Priority.Monitor, plugin);
|
||||
break;
|
||||
case ENTITY_EXPLODE:
|
||||
pm.registerEvent(type, ourEntityEventHandler, Event.Priority.Monitor, plugin);
|
||||
break;
|
||||
default:
|
||||
Log.severe("registerEvent() in DynmapPlugin does not handle " + type);
|
||||
return;
|
||||
}
|
||||
ll = new ArrayList<Listener>();
|
||||
event_handlers.put(type, ll); /* Add list for this event */
|
||||
}
|
||||
ll.add(listener);
|
||||
}
|
||||
}
|
||||
94
src/main/java/org/dynmap/bukkit/BukkitWorld.java
Normal file
94
src/main/java/org/dynmap/bukkit/BukkitWorld.java
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
package org.dynmap.bukkit;
|
||||
/**
|
||||
* Bukkit specific implementation of DynmapWorld
|
||||
*/
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.dynmap.DynmapChunk;
|
||||
import org.dynmap.DynmapLocation;
|
||||
import org.dynmap.DynmapWorld;
|
||||
import org.dynmap.utils.BlockLightLevel;
|
||||
import org.dynmap.utils.MapChunkCache;
|
||||
|
||||
public class BukkitWorld extends DynmapWorld {
|
||||
private World world;
|
||||
private static BlockLightLevel bll = new BlockLightLevel();
|
||||
|
||||
public BukkitWorld(World w) {
|
||||
super(w.getName());
|
||||
|
||||
world = w;
|
||||
}
|
||||
/* Test if world is nether */
|
||||
@Override
|
||||
public boolean isNether() {
|
||||
return world.getEnvironment() == World.Environment.NETHER;
|
||||
}
|
||||
/* Get world spawn location */
|
||||
@Override
|
||||
public DynmapLocation getSpawnLocation() {
|
||||
DynmapLocation dloc = new DynmapLocation();
|
||||
Location sloc = world.getSpawnLocation();
|
||||
dloc.x = sloc.getBlockX(); dloc.y = sloc.getBlockY();
|
||||
dloc.z = sloc.getBlockZ(); dloc.world = sloc.getWorld().getName();
|
||||
return dloc;
|
||||
}
|
||||
/* Get world time */
|
||||
@Override
|
||||
public long getTime() {
|
||||
return world.getTime();
|
||||
}
|
||||
/* World is storming */
|
||||
@Override
|
||||
public boolean hasStorm() {
|
||||
return world.hasStorm();
|
||||
}
|
||||
/* World is thundering */
|
||||
@Override
|
||||
public boolean isThundering() {
|
||||
return world.isThundering();
|
||||
}
|
||||
/* World is loaded */
|
||||
@Override
|
||||
public boolean isLoaded() {
|
||||
return (world != null);
|
||||
}
|
||||
/* Get light level of block */
|
||||
@Override
|
||||
public int getLightLevel(int x, int y, int z) {
|
||||
return world.getBlockAt(x, y, z).getLightLevel();
|
||||
}
|
||||
/* Get highest Y coord of given location */
|
||||
@Override
|
||||
public int getHighestBlockYAt(int x, int z) {
|
||||
return world.getHighestBlockYAt(x, z);
|
||||
}
|
||||
/* Test if sky light level is requestable */
|
||||
@Override
|
||||
public boolean canGetSkyLightLevel() {
|
||||
return bll.isReady();
|
||||
}
|
||||
/* Return sky light level */
|
||||
@Override
|
||||
public int getSkyLightLevel(int x, int y, int z) {
|
||||
return bll.getSkyLightLevel(world.getBlockAt(x, y, z));
|
||||
}
|
||||
/**
|
||||
* Get world environment ID (lower case - normal, the_end, nether)
|
||||
*/
|
||||
@Override
|
||||
public String getEnvironment() {
|
||||
return world.getEnvironment().name().toLowerCase();
|
||||
}
|
||||
/**
|
||||
* Get map chunk cache for world
|
||||
*/
|
||||
@Override
|
||||
public MapChunkCache getChunkCache(List<DynmapChunk> chunks) {
|
||||
MapChunkCache c = new NewMapChunkCache();
|
||||
c.setChunks(world, chunks);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
846
src/main/java/org/dynmap/bukkit/DynmapPlugin.java
Normal file
846
src/main/java/org/dynmap/bukkit/DynmapPlugin.java
Normal file
|
|
@ -0,0 +1,846 @@
|
|||
package org.dynmap.bukkit;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.Event.Type;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockBurnEvent;
|
||||
import org.bukkit.event.block.BlockFadeEvent;
|
||||
import org.bukkit.event.block.BlockFormEvent;
|
||||
import org.bukkit.event.block.BlockFromToEvent;
|
||||
import org.bukkit.event.block.BlockListener;
|
||||
import org.bukkit.event.block.BlockPhysicsEvent;
|
||||
import org.bukkit.event.block.BlockPistonExtendEvent;
|
||||
import org.bukkit.event.block.BlockPistonRetractEvent;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.block.BlockSpreadEvent;
|
||||
import org.bukkit.event.block.LeavesDecayEvent;
|
||||
import org.bukkit.event.block.SignChangeEvent;
|
||||
import org.bukkit.event.entity.EntityExplodeEvent;
|
||||
import org.bukkit.event.entity.EntityListener;
|
||||
import org.bukkit.event.player.PlayerBedLeaveEvent;
|
||||
import org.bukkit.event.player.PlayerChatEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerListener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.event.world.ChunkLoadEvent;
|
||||
import org.bukkit.event.world.ChunkPopulateEvent;
|
||||
import org.bukkit.event.world.SpawnChangeEvent;
|
||||
import org.bukkit.event.world.WorldListener;
|
||||
import org.bukkit.event.world.WorldLoadEvent;
|
||||
import org.bukkit.event.world.WorldUnloadEvent;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.dynmap.DynmapAPI;
|
||||
import org.dynmap.DynmapCore;
|
||||
import org.dynmap.DynmapLocation;
|
||||
import org.dynmap.DynmapWebChatEvent;
|
||||
import org.dynmap.DynmapWorld;
|
||||
import org.dynmap.Log;
|
||||
import org.dynmap.MapManager;
|
||||
import org.dynmap.common.DynmapCommandSender;
|
||||
import org.dynmap.common.DynmapPlayer;
|
||||
import org.dynmap.common.DynmapServerInterface;
|
||||
import org.dynmap.common.DynmapListenerManager.EventType;
|
||||
import org.dynmap.markers.MarkerAPI;
|
||||
import org.dynmap.permissions.BukkitPermissions;
|
||||
import org.dynmap.permissions.NijikokunPermissions;
|
||||
import org.dynmap.permissions.OpPermissions;
|
||||
import org.dynmap.permissions.PermissionProvider;
|
||||
|
||||
public class DynmapPlugin extends JavaPlugin implements DynmapAPI {
|
||||
private DynmapCore core;
|
||||
private PermissionProvider permissions;
|
||||
private String version;
|
||||
public BukkitEventProcessor bep;
|
||||
|
||||
private MapManager mapManager;
|
||||
public static DynmapPlugin plugin;
|
||||
|
||||
public DynmapPlugin() {
|
||||
plugin = this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Server access abstraction class
|
||||
*/
|
||||
public class BukkitServer implements DynmapServerInterface {
|
||||
@Override
|
||||
public void scheduleServerTask(Runnable run, long delay) {
|
||||
getServer().getScheduler().scheduleSyncDelayedTask(DynmapPlugin.this, run, delay);
|
||||
}
|
||||
@Override
|
||||
public DynmapPlayer[] getOnlinePlayers() {
|
||||
Player[] players = getServer().getOnlinePlayers();
|
||||
DynmapPlayer[] dplay = new DynmapPlayer[players.length];
|
||||
for(int i = 0; i < players.length; i++)
|
||||
dplay[i] = new BukkitPlayer(players[i]);
|
||||
return dplay;
|
||||
}
|
||||
@Override
|
||||
public void reload() {
|
||||
PluginManager pluginManager = getServer().getPluginManager();
|
||||
pluginManager.disablePlugin(DynmapPlugin.this);
|
||||
pluginManager.enablePlugin(DynmapPlugin.this);
|
||||
}
|
||||
@Override
|
||||
public DynmapPlayer getPlayer(String name) {
|
||||
Player p = getServer().getPlayerExact(name);
|
||||
if(p != null) {
|
||||
return new BukkitPlayer(p);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Set<String> getIPBans() {
|
||||
return getServer().getIPBans();
|
||||
}
|
||||
@Override
|
||||
public <T> Future<T> callSyncMethod(Callable<T> task) {
|
||||
return getServer().getScheduler().callSyncMethod(DynmapPlugin.this, task);
|
||||
}
|
||||
@Override
|
||||
public String getServerName() {
|
||||
return getServer().getServerName();
|
||||
}
|
||||
@Override
|
||||
public boolean isPlayerBanned(String pid) {
|
||||
OfflinePlayer p = getServer().getOfflinePlayer(pid);
|
||||
if((p != null) && p.isBanned())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public String stripChatColor(String s) {
|
||||
return ChatColor.stripColor(s);
|
||||
}
|
||||
private Set<EventType> registered = new HashSet<EventType>();
|
||||
@Override
|
||||
public boolean requestEventNotification(EventType type) {
|
||||
if(registered.contains(type))
|
||||
return true;
|
||||
switch(type) {
|
||||
case WORLD_LOAD:
|
||||
case WORLD_UNLOAD:
|
||||
/* Already called for normal world activation/deactivation */
|
||||
break;
|
||||
case WORLD_SPAWN_CHANGE:
|
||||
bep.registerEvent(Type.SPAWN_CHANGE, new WorldListener() {
|
||||
public void onWorldSpawnChange(SpawnChangeEvent evt) {
|
||||
DynmapWorld w = new BukkitWorld(evt.getWorld());
|
||||
core.listenerManager.processWorldEvent(EventType.WORLD_SPAWN_CHANGE, w);
|
||||
}
|
||||
});
|
||||
break;
|
||||
case PLAYER_JOIN:
|
||||
case PLAYER_QUIT:
|
||||
/* Already handled */
|
||||
break;
|
||||
case PLAYER_BED_LEAVE:
|
||||
bep.registerEvent(Type.PLAYER_BED_LEAVE, new PlayerListener() {
|
||||
@Override
|
||||
public void onPlayerBedLeave(PlayerBedLeaveEvent evt) {
|
||||
DynmapPlayer p = new BukkitPlayer(evt.getPlayer());
|
||||
core.listenerManager.processPlayerEvent(EventType.PLAYER_BED_LEAVE, p);
|
||||
}
|
||||
});
|
||||
break;
|
||||
case PLAYER_CHAT:
|
||||
bep.registerEvent(Type.PLAYER_CHAT, new PlayerListener() {
|
||||
@Override
|
||||
public void onPlayerChat(PlayerChatEvent evt) {
|
||||
DynmapPlayer p = null;
|
||||
if(evt.getPlayer() != null)
|
||||
p = new BukkitPlayer(evt.getPlayer());
|
||||
core.listenerManager.processChatEvent(EventType.PLAYER_CHAT, p, evt.getMessage());
|
||||
}
|
||||
});
|
||||
break;
|
||||
case BLOCK_BREAK:
|
||||
bep.registerEvent(Type.BLOCK_BREAK, new BlockListener() {
|
||||
@Override
|
||||
public void onBlockBreak(BlockBreakEvent evt) {
|
||||
Block b = evt.getBlock();
|
||||
Location l = b.getLocation();
|
||||
core.listenerManager.processBlockEvent(EventType.BLOCK_BREAK, b.getType().getId(),
|
||||
l.getWorld().getName(), l.getBlockX(), l.getBlockY(), l.getBlockZ());
|
||||
}
|
||||
});
|
||||
break;
|
||||
case SIGN_CHANGE:
|
||||
bep.registerEvent(Type.SIGN_CHANGE, new BlockListener() {
|
||||
@Override
|
||||
public void onSignChange(SignChangeEvent evt) {
|
||||
Block b = evt.getBlock();
|
||||
Location l = b.getLocation();
|
||||
String[] lines = evt.getLines(); /* Note: changes to this change event - intentional */
|
||||
DynmapPlayer dp = null;
|
||||
Player p = evt.getPlayer();
|
||||
if(p != null) dp = new BukkitPlayer(p);
|
||||
core.listenerManager.processSignChangeEvent(EventType.SIGN_CHANGE, b.getType().getId(),
|
||||
l.getWorld().getName(), l.getBlockX(), l.getBlockY(), l.getBlockZ(), lines, dp);
|
||||
}
|
||||
});
|
||||
break;
|
||||
default:
|
||||
Log.severe("Unhandled event type: " + type);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean sendWebChatEvent(String source, String name, String msg) {
|
||||
DynmapWebChatEvent evt = new DynmapWebChatEvent(source, name, msg);
|
||||
getServer().getPluginManager().callEvent(evt);
|
||||
return (evt.isCancelled() == false);
|
||||
}
|
||||
@Override
|
||||
public void broadcastMessage(String msg) {
|
||||
getServer().broadcastMessage(msg);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Player access abstraction class
|
||||
*/
|
||||
public class BukkitPlayer extends BukkitCommandSender implements DynmapPlayer {
|
||||
private Player player;
|
||||
|
||||
public BukkitPlayer(Player p) {
|
||||
super(p);
|
||||
player = p;
|
||||
}
|
||||
@Override
|
||||
public boolean isConnected() {
|
||||
return player.isOnline();
|
||||
}
|
||||
@Override
|
||||
public String getName() {
|
||||
return player.getName();
|
||||
}
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return player.getDisplayName();
|
||||
}
|
||||
@Override
|
||||
public boolean isOnline() {
|
||||
return player.isOnline();
|
||||
}
|
||||
@Override
|
||||
public DynmapLocation getLocation() {
|
||||
Location loc = player.getLocation();
|
||||
return toLoc(loc);
|
||||
}
|
||||
@Override
|
||||
public String getWorld() {
|
||||
World w = player.getWorld();
|
||||
if(w != null)
|
||||
return w.getName();
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public InetSocketAddress getAddress() {
|
||||
return player.getAddress();
|
||||
}
|
||||
@Override
|
||||
public boolean isSneaking() {
|
||||
return player.isSneaking();
|
||||
}
|
||||
@Override
|
||||
public int getHealth() {
|
||||
return player.getHealth();
|
||||
}
|
||||
@Override
|
||||
public int getArmorPoints() {
|
||||
return Armor.getArmorPoints(player);
|
||||
}
|
||||
@Override
|
||||
public DynmapLocation getBedSpawnLocation() {
|
||||
Location loc = player.getBedSpawnLocation();
|
||||
if(loc != null) {
|
||||
return toLoc(loc);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/* Handler for generic console command sender */
|
||||
public class BukkitCommandSender implements DynmapCommandSender {
|
||||
private CommandSender sender;
|
||||
|
||||
public BukkitCommandSender(CommandSender send) {
|
||||
sender = send;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrivilege(String privid) {
|
||||
return permissions.has(sender, privid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String msg) {
|
||||
sender.sendMessage(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnected() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean isOp() {
|
||||
return sender.isOp();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
PluginDescriptionFile pdfFile = this.getDescription();
|
||||
version = pdfFile.getVersion();
|
||||
|
||||
/* Initialize event processor */
|
||||
if(bep == null)
|
||||
bep = new BukkitEventProcessor(this);
|
||||
|
||||
/* Set up player login/quit event handler */
|
||||
registerPlayerLoginListener();
|
||||
|
||||
permissions = NijikokunPermissions.create(getServer(), "dynmap");
|
||||
if (permissions == null)
|
||||
permissions = BukkitPermissions.create("dynmap");
|
||||
if (permissions == null)
|
||||
permissions = new OpPermissions(new String[] { "fullrender", "cancelrender", "radiusrender", "resetstats", "reload", "purgequeue", "pause", "ips-for-id", "ids-for-ip", "add-id-for-ip", "del-id-for-ip" });
|
||||
/* Get and initialize data folder */
|
||||
File dataDirectory = this.getDataFolder();
|
||||
if(dataDirectory.exists() == false)
|
||||
dataDirectory.mkdirs();
|
||||
/* Get MC version */
|
||||
String bukkitver = getServer().getVersion();
|
||||
String mcver = "1.0.0";
|
||||
int idx = bukkitver.indexOf("(MC: ");
|
||||
if(idx > 0) {
|
||||
mcver = bukkitver.substring(idx+5);
|
||||
idx = mcver.indexOf(")");
|
||||
if(idx > 0) mcver = mcver.substring(0, idx);
|
||||
}
|
||||
|
||||
/* Instantiate core */
|
||||
if(core == null)
|
||||
core = new DynmapCore();
|
||||
/* Inject dependencies */
|
||||
core.setPluginVersion(version);
|
||||
core.setMinecraftVersion(mcver);
|
||||
core.setDataFolder(dataDirectory);
|
||||
core.setServer(new BukkitServer());
|
||||
|
||||
/* Enable core */
|
||||
if(!core.enableCore()) {
|
||||
this.setEnabled(false);
|
||||
return;
|
||||
}
|
||||
/* Get map manager from core */
|
||||
mapManager = core.getMapManager();
|
||||
/* Initialized the currently loaded worlds */
|
||||
for (World world : getServer().getWorlds()) {
|
||||
BukkitWorld w = new BukkitWorld(world);
|
||||
if(core.processWorldLoad(w)) /* Have core process load first - fire event listeners if good load after */
|
||||
core.listenerManager.processWorldEvent(EventType.WORLD_LOAD, w);
|
||||
}
|
||||
|
||||
/* Register our update trigger events */
|
||||
registerEvents();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
/* Reset registered listeners */
|
||||
bep.cleanup();
|
||||
/* Disable core */
|
||||
core.disableCore();
|
||||
}
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
|
||||
DynmapCommandSender dsender;
|
||||
if(sender instanceof Player) {
|
||||
dsender = new BukkitPlayer((Player)sender);
|
||||
}
|
||||
else {
|
||||
dsender = new BukkitCommandSender(sender);
|
||||
}
|
||||
return core.processCommand(dsender, cmd.getName(), commandLabel, args);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final MarkerAPI getMarkerAPI() {
|
||||
return core.getMarkerAPI();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean markerAPIInitialized() {
|
||||
return core.markerAPIInitialized();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean sendBroadcastToWeb(String sender, String msg) {
|
||||
return core.sendBroadcastToWeb(sender, msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int triggerRenderOfVolume(String wid, int minx, int miny, int minz,
|
||||
int maxx, int maxy, int maxz) {
|
||||
return core.triggerRenderOfVolume(wid, minx, miny, minz, maxx, maxy, maxz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int triggerRenderOfBlock(String wid, int x, int y, int z) {
|
||||
return core.triggerRenderOfBlock(wid, x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setPauseFullRadiusRenders(boolean dopause) {
|
||||
core.setPauseFullRadiusRenders(dopause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean getPauseFullRadiusRenders() {
|
||||
return core.getPauseFullRadiusRenders();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setPauseUpdateRenders(boolean dopause) {
|
||||
core.setPauseUpdateRenders(dopause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean getPauseUpdateRenders() {
|
||||
return core.getPauseUpdateRenders();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setPlayerVisiblity(String player, boolean is_visible) {
|
||||
core.setPlayerVisiblity(player, is_visible);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean getPlayerVisbility(String player) {
|
||||
return core.getPlayerVisbility(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void postPlayerMessageToWeb(String playerid, String playerdisplay,
|
||||
String message) {
|
||||
core.postPlayerMessageToWeb(playerid, playerdisplay, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void postPlayerJoinQuitToWeb(String playerid, String playerdisplay,
|
||||
boolean isjoin) {
|
||||
core.postPlayerJoinQuitToWeb(playerid, playerdisplay, isjoin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getDynmapCoreVersion() {
|
||||
return core.getDynmapCoreVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int triggerRenderOfVolume(Location l0, Location l1) {
|
||||
int x0 = l0.getBlockX(), y0 = l0.getBlockY(), z0 = l0.getBlockZ();
|
||||
int x1 = l1.getBlockX(), y1 = l1.getBlockY(), z1 = l1.getBlockZ();
|
||||
|
||||
return core.triggerRenderOfVolume(l0.getWorld().getName(), Math.min(x0, x1), Math.min(y0, y1),
|
||||
Math.min(z0, z1), Math.max(x0, x1), Math.max(y0, y1), Math.max(z0, z1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setPlayerVisiblity(Player player, boolean is_visible) {
|
||||
core.setPlayerVisiblity(player.getName(), is_visible);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean getPlayerVisbility(Player player) {
|
||||
return core.getPlayerVisbility(player.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void postPlayerMessageToWeb(Player player, String message) {
|
||||
core.postPlayerMessageToWeb(player.getName(), player.getDisplayName(), message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postPlayerJoinQuitToWeb(Player player, boolean isjoin) {
|
||||
core.postPlayerJoinQuitToWeb(player.getName(), player.getDisplayName(), isjoin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDynmapVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
private static DynmapLocation toLoc(Location l) {
|
||||
return new DynmapLocation(l.getWorld().getName(), l.getBlockX(), l.getBlockY(), l.getBlockZ());
|
||||
}
|
||||
|
||||
private void registerPlayerLoginListener() {
|
||||
PlayerListener pl = new PlayerListener() {
|
||||
public void onPlayerJoin(PlayerJoinEvent evt) {
|
||||
DynmapPlayer dp = new BukkitPlayer(evt.getPlayer());
|
||||
core.listenerManager.processPlayerEvent(EventType.PLAYER_JOIN, dp);
|
||||
}
|
||||
public void onPlayerQuit(PlayerQuitEvent evt) {
|
||||
DynmapPlayer dp = new BukkitPlayer(evt.getPlayer());
|
||||
core.listenerManager.processPlayerEvent(EventType.PLAYER_QUIT, dp);
|
||||
}
|
||||
};
|
||||
bep.registerEvent(Type.PLAYER_JOIN, pl);
|
||||
bep.registerEvent(Type.PLAYER_QUIT, pl);
|
||||
}
|
||||
|
||||
private boolean onplace;
|
||||
private boolean onbreak;
|
||||
private boolean onblockform;
|
||||
private boolean onblockfade;
|
||||
private boolean onblockspread;
|
||||
private boolean onblockfromto;
|
||||
private boolean onblockphysics;
|
||||
private boolean onleaves;
|
||||
private boolean onburn;
|
||||
private boolean onpiston;
|
||||
private boolean onplayerjoin;
|
||||
private boolean onplayermove;
|
||||
private boolean ongeneratechunk;
|
||||
private boolean onloadchunk;
|
||||
private boolean onexplosion;
|
||||
|
||||
private void registerEvents() {
|
||||
BlockListener blockTrigger = new BlockListener() {
|
||||
@Override
|
||||
public void onBlockPlace(BlockPlaceEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
Location loc = event.getBlock().getLocation();
|
||||
String wn = loc.getWorld().getName();
|
||||
mapManager.sscache.invalidateSnapshot(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
|
||||
if(onplace) {
|
||||
mapManager.touch(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "blockplace");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockBreak(BlockBreakEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
Location loc = event.getBlock().getLocation();
|
||||
String wn = loc.getWorld().getName();
|
||||
mapManager.sscache.invalidateSnapshot(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
|
||||
if(onbreak) {
|
||||
mapManager.touch(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "blockbreak");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLeavesDecay(LeavesDecayEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
Location loc = event.getBlock().getLocation();
|
||||
String wn = loc.getWorld().getName();
|
||||
mapManager.sscache.invalidateSnapshot(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
|
||||
if(onleaves) {
|
||||
mapManager.touch(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "leavesdecay");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockBurn(BlockBurnEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
Location loc = event.getBlock().getLocation();
|
||||
String wn = loc.getWorld().getName();
|
||||
mapManager.sscache.invalidateSnapshot(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
|
||||
if(onburn) {
|
||||
mapManager.touch(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "blockburn");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockForm(BlockFormEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
Location loc = event.getBlock().getLocation();
|
||||
String wn = loc.getWorld().getName();
|
||||
mapManager.sscache.invalidateSnapshot(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
|
||||
if(onblockform) {
|
||||
mapManager.touch(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "blockform");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockFade(BlockFadeEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
Location loc = event.getBlock().getLocation();
|
||||
String wn = loc.getWorld().getName();
|
||||
mapManager.sscache.invalidateSnapshot(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
|
||||
if(onblockfade) {
|
||||
mapManager.touch(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "blockfade");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockSpread(BlockSpreadEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
Location loc = event.getBlock().getLocation();
|
||||
String wn = loc.getWorld().getName();
|
||||
mapManager.sscache.invalidateSnapshot(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
|
||||
if(onblockspread) {
|
||||
mapManager.touch(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "blockspread");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockFromTo(BlockFromToEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
Location loc = event.getToBlock().getLocation();
|
||||
String wn = loc.getWorld().getName();
|
||||
mapManager.sscache.invalidateSnapshot(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
|
||||
if(onblockfromto)
|
||||
mapManager.touch(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "blockfromto");
|
||||
loc = event.getBlock().getLocation();
|
||||
wn = loc.getWorld().getName();
|
||||
mapManager.sscache.invalidateSnapshot(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
|
||||
if(onblockfromto)
|
||||
mapManager.touch(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "blockfromto");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockPhysics(BlockPhysicsEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
Location loc = event.getBlock().getLocation();
|
||||
String wn = loc.getWorld().getName();
|
||||
mapManager.sscache.invalidateSnapshot(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
|
||||
if(onblockphysics) {
|
||||
mapManager.touch(wn, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "blockphysics");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockPistonRetract(BlockPistonRetractEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
Block b = event.getBlock();
|
||||
Location loc = b.getLocation();
|
||||
BlockFace dir;
|
||||
try { /* Workaround Bukkit bug = http://leaky.bukkit.org/issues/1227 */
|
||||
dir = event.getDirection();
|
||||
} catch (ClassCastException ccx) {
|
||||
dir = BlockFace.NORTH;
|
||||
}
|
||||
String wn = loc.getWorld().getName();
|
||||
int x = loc.getBlockX(), y = loc.getBlockY(), z = loc.getBlockZ();
|
||||
mapManager.sscache.invalidateSnapshot(wn, x, y, z);
|
||||
if(onpiston)
|
||||
mapManager.touch(wn, x, y, z, "pistonretract");
|
||||
for(int i = 0; i < 2; i++) {
|
||||
x += dir.getModX();
|
||||
y += dir.getModY();
|
||||
z += dir.getModZ();
|
||||
mapManager.sscache.invalidateSnapshot(wn, x, y, z);
|
||||
if(onpiston)
|
||||
mapManager.touch(wn, x, y, z, "pistonretract");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onBlockPistonExtend(BlockPistonExtendEvent event) {
|
||||
if(event.isCancelled())
|
||||
return;
|
||||
Block b = event.getBlock();
|
||||
Location loc = b.getLocation();
|
||||
BlockFace dir;
|
||||
try { /* Workaround Bukkit bug = http://leaky.bukkit.org/issues/1227 */
|
||||
dir = event.getDirection();
|
||||
} catch (ClassCastException ccx) {
|
||||
dir = BlockFace.NORTH;
|
||||
}
|
||||
String wn = loc.getWorld().getName();
|
||||
int x = loc.getBlockX(), y = loc.getBlockY(), z = loc.getBlockZ();
|
||||
mapManager.sscache.invalidateSnapshot(wn, x, y, z);
|
||||
if(onpiston)
|
||||
mapManager.touch(wn, x, y, z, "pistonretract");
|
||||
for(int i = 0; i < 1+event.getLength(); i++) {
|
||||
x += dir.getModX();
|
||||
y += dir.getModY();
|
||||
z += dir.getModZ();
|
||||
mapManager.sscache.invalidateSnapshot(wn, x, y, z);
|
||||
if(onpiston)
|
||||
mapManager.touch(wn, x, y, z, "pistonretract");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// To trigger rendering.
|
||||
onplace = core.isTrigger("blockplaced");
|
||||
bep.registerEvent(Event.Type.BLOCK_PLACE, blockTrigger);
|
||||
|
||||
onbreak = core.isTrigger("blockbreak");
|
||||
bep.registerEvent(Event.Type.BLOCK_BREAK, blockTrigger);
|
||||
|
||||
if(core.isTrigger("snowform")) Log.info("The 'snowform' trigger has been deprecated due to Bukkit changes - use 'blockformed'");
|
||||
|
||||
onleaves = core.isTrigger("leavesdecay");
|
||||
bep.registerEvent(Event.Type.LEAVES_DECAY, blockTrigger);
|
||||
|
||||
onburn = core.isTrigger("blockburn");
|
||||
bep.registerEvent(Event.Type.BLOCK_BURN, blockTrigger);
|
||||
|
||||
onblockform = core.isTrigger("blockformed");
|
||||
bep.registerEvent(Event.Type.BLOCK_FORM, blockTrigger);
|
||||
|
||||
onblockfade = core.isTrigger("blockfaded");
|
||||
bep.registerEvent(Event.Type.BLOCK_FADE, blockTrigger);
|
||||
|
||||
onblockspread = core.isTrigger("blockspread");
|
||||
bep.registerEvent(Event.Type.BLOCK_SPREAD, blockTrigger);
|
||||
|
||||
onblockfromto = core.isTrigger("blockfromto");
|
||||
bep.registerEvent(Event.Type.BLOCK_FROMTO, blockTrigger);
|
||||
|
||||
onblockphysics = core.isTrigger("blockphysics");
|
||||
bep.registerEvent(Event.Type.BLOCK_PHYSICS, blockTrigger);
|
||||
|
||||
onpiston = core.isTrigger("pistonmoved");
|
||||
bep.registerEvent(Event.Type.BLOCK_PISTON_EXTEND, blockTrigger);
|
||||
bep.registerEvent(Event.Type.BLOCK_PISTON_RETRACT, blockTrigger);
|
||||
/* Register player event trigger handlers */
|
||||
PlayerListener playerTrigger = new PlayerListener() {
|
||||
@Override
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
if(onplayerjoin) {
|
||||
Location loc = event.getPlayer().getLocation();
|
||||
mapManager.touch(loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "playerjoin");
|
||||
}
|
||||
core.listenerManager.processPlayerEvent(EventType.PLAYER_JOIN, new BukkitPlayer(event.getPlayer()));
|
||||
}
|
||||
@Override
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
core.listenerManager.processPlayerEvent(EventType.PLAYER_QUIT, new BukkitPlayer(event.getPlayer()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerMove(PlayerMoveEvent event) {
|
||||
Location loc = event.getPlayer().getLocation();
|
||||
mapManager.touch(loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), "playermove");
|
||||
}
|
||||
};
|
||||
|
||||
onplayerjoin = core.isTrigger("playerjoin");
|
||||
onplayermove = core.isTrigger("playermove");
|
||||
bep.registerEvent(Event.Type.PLAYER_JOIN, playerTrigger);
|
||||
bep.registerEvent(Event.Type.PLAYER_QUIT, playerTrigger);
|
||||
if(onplayermove)
|
||||
bep.registerEvent(Event.Type.PLAYER_MOVE, playerTrigger);
|
||||
|
||||
/* Register entity event triggers */
|
||||
EntityListener entityTrigger = new EntityListener() {
|
||||
@Override
|
||||
public void onEntityExplode(EntityExplodeEvent event) {
|
||||
Location loc = event.getLocation();
|
||||
String wname = loc.getWorld().getName();
|
||||
int minx, maxx, miny, maxy, minz, maxz;
|
||||
minx = maxx = loc.getBlockX();
|
||||
miny = maxy = loc.getBlockY();
|
||||
minz = maxz = loc.getBlockZ();
|
||||
/* Calculate volume impacted by explosion */
|
||||
List<Block> blocks = event.blockList();
|
||||
for(Block b: blocks) {
|
||||
Location l = b.getLocation();
|
||||
int x = l.getBlockX();
|
||||
if(x < minx) minx = x;
|
||||
if(x > maxx) maxx = x;
|
||||
int y = l.getBlockY();
|
||||
if(y < miny) miny = y;
|
||||
if(y > maxy) maxy = y;
|
||||
int z = l.getBlockZ();
|
||||
if(z < minz) minz = z;
|
||||
if(z > maxz) maxz = z;
|
||||
}
|
||||
mapManager.sscache.invalidateSnapshot(wname, minx, miny, minz, maxx, maxy, maxz);
|
||||
if(onexplosion) {
|
||||
mapManager.touchVolume(wname, minx, miny, minz, maxx, maxy, maxz, "entityexplode");
|
||||
}
|
||||
}
|
||||
};
|
||||
onexplosion = core.isTrigger("explosion");
|
||||
bep.registerEvent(Event.Type.ENTITY_EXPLODE, entityTrigger);
|
||||
|
||||
|
||||
/* Register world event triggers */
|
||||
WorldListener worldTrigger = new WorldListener() {
|
||||
@Override
|
||||
public void onChunkLoad(ChunkLoadEvent event) {
|
||||
if(core.ignore_chunk_loads)
|
||||
return;
|
||||
Chunk c = event.getChunk();
|
||||
/* Touch extreme corners */
|
||||
int x = c.getX() << 4;
|
||||
int z = c.getZ() << 4;
|
||||
mapManager.touchVolume(event.getWorld().getName(), x, 0, z, x+15, 128, z+16, "chunkload");
|
||||
}
|
||||
@Override
|
||||
public void onChunkPopulate(ChunkPopulateEvent event) {
|
||||
Chunk c = event.getChunk();
|
||||
/* Touch extreme corners */
|
||||
int x = c.getX() << 4;
|
||||
int z = c.getZ() << 4;
|
||||
mapManager.touchVolume(event.getWorld().getName(), x, 0, z, x+15, 128, z+16, "chunkpopulate");
|
||||
}
|
||||
@Override
|
||||
public void onWorldLoad(WorldLoadEvent event) {
|
||||
core.updateConfigHashcode();
|
||||
BukkitWorld w = new BukkitWorld(event.getWorld());
|
||||
if(core.processWorldLoad(w)) /* Have core process load first - fire event listeners if good load after */
|
||||
core.listenerManager.processWorldEvent(EventType.WORLD_LOAD, w);
|
||||
}
|
||||
@Override
|
||||
public void onWorldUnload(WorldUnloadEvent event) {
|
||||
core.updateConfigHashcode();
|
||||
DynmapWorld w = core.getWorld(event.getWorld().getName());
|
||||
if(w != null)
|
||||
core.listenerManager.processWorldEvent(EventType.WORLD_UNLOAD, w);
|
||||
}
|
||||
};
|
||||
|
||||
ongeneratechunk = core.isTrigger("chunkgenerated");
|
||||
if(ongeneratechunk) {
|
||||
bep.registerEvent(Event.Type.CHUNK_POPULATED, worldTrigger);
|
||||
}
|
||||
onloadchunk = core.isTrigger("chunkloaded");
|
||||
if(onloadchunk) {
|
||||
bep.registerEvent(Event.Type.CHUNK_LOAD, worldTrigger);
|
||||
}
|
||||
|
||||
// To link configuration to real loaded worlds.
|
||||
bep.registerEvent(Event.Type.WORLD_LOAD, worldTrigger);
|
||||
bep.registerEvent(Event.Type.WORLD_UNLOAD, worldTrigger);
|
||||
}
|
||||
|
||||
}
|
||||
770
src/main/java/org/dynmap/bukkit/NewMapChunkCache.java
Normal file
770
src/main/java/org/dynmap/bukkit/NewMapChunkCache.java
Normal file
|
|
@ -0,0 +1,770 @@
|
|||
package org.dynmap.bukkit;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.ChunkSnapshot;
|
||||
import org.dynmap.DynmapChunk;
|
||||
import org.dynmap.DynmapCore;
|
||||
import org.dynmap.DynmapWorld;
|
||||
import org.dynmap.Log;
|
||||
import org.dynmap.MapManager;
|
||||
import org.dynmap.utils.MapChunkCache;
|
||||
import org.dynmap.utils.MapIterator;
|
||||
import org.dynmap.utils.MapChunkCache.HiddenChunkStyle;
|
||||
import org.dynmap.utils.MapChunkCache.VisibilityLimit;
|
||||
import org.dynmap.utils.MapIterator.BlockStep;
|
||||
|
||||
/**
|
||||
* Container for managing chunks - dependent upon using chunk snapshots, since rendering is off server thread
|
||||
*/
|
||||
public class NewMapChunkCache implements MapChunkCache {
|
||||
private static boolean init = false;
|
||||
private static Method poppreservedchunk = null;
|
||||
private static Method gethandle = null;
|
||||
private static Method removeentities = null;
|
||||
private static Method getworldhandle = null;
|
||||
private static Field chunkbiome = null;
|
||||
private static Field ticklist = null;
|
||||
private static Method processticklist = null;
|
||||
|
||||
private static final int MAX_PROCESSTICKS = 20;
|
||||
private static final int MAX_TICKLIST = 20000;
|
||||
|
||||
private World w;
|
||||
private Object craftworld;
|
||||
private List<DynmapChunk> chunks;
|
||||
private ListIterator<DynmapChunk> iterator;
|
||||
private int x_min, x_max, z_min, z_max;
|
||||
private int x_dim;
|
||||
private boolean biome, biomeraw, highesty, blockdata;
|
||||
private HiddenChunkStyle hidestyle = HiddenChunkStyle.FILL_AIR;
|
||||
private List<VisibilityLimit> visible_limits = null;
|
||||
private List<VisibilityLimit> hidden_limits = null;
|
||||
private boolean do_generate = false;
|
||||
private boolean do_save = false;
|
||||
private boolean isempty = true;
|
||||
private ChunkSnapshot[] snaparray; /* Index = (x-x_min) + ((z-z_min)*x_dim) */
|
||||
private Biome[][] snapbiomes; /* Biome cache - getBiome() is expensive */
|
||||
private TreeSet<?> ourticklist;
|
||||
|
||||
private int chunks_read; /* Number of chunks actually loaded */
|
||||
private int chunks_attempted; /* Number of chunks attempted to load */
|
||||
private long total_loadtime; /* Total time loading chunks, in nanoseconds */
|
||||
|
||||
private long exceptions;
|
||||
|
||||
private static final BlockStep unstep[] = { BlockStep.X_MINUS, BlockStep.Y_MINUS, BlockStep.Z_MINUS,
|
||||
BlockStep.X_PLUS, BlockStep.Y_PLUS, BlockStep.Z_PLUS };
|
||||
|
||||
/**
|
||||
* Iterator for traversing map chunk cache (base is for non-snapshot)
|
||||
*/
|
||||
public class OurMapIterator implements MapIterator {
|
||||
private int x, y, z, chunkindex, bx, bz;
|
||||
private ChunkSnapshot snap;
|
||||
private BlockStep laststep;
|
||||
private int typeid = -1;
|
||||
private int blkdata = -1;
|
||||
|
||||
OurMapIterator(int x0, int y0, int z0) {
|
||||
initialize(x0, y0, z0);
|
||||
}
|
||||
public final void initialize(int x0, int y0, int z0) {
|
||||
this.x = x0;
|
||||
this.y = y0;
|
||||
this.z = z0;
|
||||
this.chunkindex = ((x >> 4) - x_min) + (((z >> 4) - z_min) * x_dim);
|
||||
this.bx = x & 0xF;
|
||||
this.bz = z & 0xF;
|
||||
try {
|
||||
snap = snaparray[chunkindex];
|
||||
} catch (ArrayIndexOutOfBoundsException aioobx) {
|
||||
snap = EMPTY;
|
||||
exceptions++;
|
||||
}
|
||||
laststep = BlockStep.Y_MINUS;
|
||||
typeid = blkdata = -1;
|
||||
}
|
||||
public final int getBlockTypeID() {
|
||||
if(typeid < 0)
|
||||
typeid = snap.getBlockTypeId(bx, y, bz);
|
||||
return typeid;
|
||||
}
|
||||
public final int getBlockData() {
|
||||
if(blkdata < 0)
|
||||
blkdata = snap.getBlockData(bx, y, bz);
|
||||
return blkdata;
|
||||
}
|
||||
public final int getHighestBlockYAt() {
|
||||
return snap.getHighestBlockYAt(bx, bz);
|
||||
}
|
||||
public int getBlockSkyLight() {
|
||||
return snap.getBlockSkyLight(bx, y, bz);
|
||||
}
|
||||
public final int getBlockEmittedLight() {
|
||||
return snap.getBlockEmittedLight(bx, y, bz);
|
||||
}
|
||||
public final Biome getBiome() {
|
||||
Biome[] b = snapbiomes[chunkindex];
|
||||
if(b == null) {
|
||||
b = snapbiomes[chunkindex] = new Biome[256];
|
||||
}
|
||||
int off = bx + (bz << 4);
|
||||
Biome bio = b[off];
|
||||
if(bio == null) {
|
||||
bio = b[off] = snap.getBiome(bx, bz);
|
||||
}
|
||||
return bio;
|
||||
}
|
||||
public double getRawBiomeTemperature() {
|
||||
return snap.getRawBiomeTemperature(bx, bz);
|
||||
}
|
||||
public double getRawBiomeRainfall() {
|
||||
return snap.getRawBiomeRainfall(bx, bz);
|
||||
}
|
||||
/**
|
||||
* Step current position in given direction
|
||||
*/
|
||||
public final void stepPosition(BlockStep step) {
|
||||
switch(step.ordinal()) {
|
||||
case 0:
|
||||
x++;
|
||||
bx++;
|
||||
if(bx == 16) { /* Next chunk? */
|
||||
try {
|
||||
bx = 0;
|
||||
chunkindex++;
|
||||
snap = snaparray[chunkindex];
|
||||
} catch (ArrayIndexOutOfBoundsException aioobx) {
|
||||
snap = EMPTY;
|
||||
exceptions++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
y++;
|
||||
break;
|
||||
case 2:
|
||||
z++;
|
||||
bz++;
|
||||
if(bz == 16) { /* Next chunk? */
|
||||
try {
|
||||
bz = 0;
|
||||
chunkindex += x_dim;
|
||||
snap = snaparray[chunkindex];
|
||||
} catch (ArrayIndexOutOfBoundsException aioobx) {
|
||||
snap = EMPTY;
|
||||
exceptions++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
x--;
|
||||
bx--;
|
||||
if(bx == -1) { /* Next chunk? */
|
||||
try {
|
||||
bx = 15;
|
||||
chunkindex--;
|
||||
snap = snaparray[chunkindex];
|
||||
} catch (ArrayIndexOutOfBoundsException aioobx) {
|
||||
snap = EMPTY;
|
||||
exceptions++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
y--;
|
||||
break;
|
||||
case 5:
|
||||
z--;
|
||||
bz--;
|
||||
if(bz == -1) { /* Next chunk? */
|
||||
try {
|
||||
bz = 15;
|
||||
chunkindex -= x_dim;
|
||||
snap = snaparray[chunkindex];
|
||||
} catch (ArrayIndexOutOfBoundsException aioobx) {
|
||||
snap = EMPTY;
|
||||
exceptions++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
laststep = step;
|
||||
typeid = -1;
|
||||
blkdata = -1;
|
||||
}
|
||||
/**
|
||||
* Unstep current position to previous position
|
||||
*/
|
||||
public BlockStep unstepPosition() {
|
||||
BlockStep ls = laststep;
|
||||
stepPosition(unstep[ls.ordinal()]);
|
||||
return ls;
|
||||
}
|
||||
/**
|
||||
* Unstep current position in oppisite director of given step
|
||||
*/
|
||||
public void unstepPosition(BlockStep s) {
|
||||
stepPosition(unstep[s.ordinal()]);
|
||||
}
|
||||
public final void setY(int y) {
|
||||
if(y > this.y)
|
||||
laststep = BlockStep.Y_PLUS;
|
||||
else
|
||||
laststep = BlockStep.Y_MINUS;
|
||||
this.y = y;
|
||||
typeid = -1;
|
||||
blkdata = -1;
|
||||
}
|
||||
public final int getX() {
|
||||
return x;
|
||||
}
|
||||
public final int getY() {
|
||||
return y;
|
||||
}
|
||||
public final int getZ() {
|
||||
return z;
|
||||
}
|
||||
public final int getBlockTypeIDAt(BlockStep s) {
|
||||
if(s == BlockStep.Y_MINUS) {
|
||||
if(y > 0)
|
||||
return snap.getBlockTypeId(bx, y-1, bz);
|
||||
}
|
||||
else if(s == BlockStep.Y_PLUS) {
|
||||
if(y < 127)
|
||||
return snap.getBlockTypeId(bx, y+1, bz);
|
||||
}
|
||||
else {
|
||||
BlockStep ls = laststep;
|
||||
stepPosition(s);
|
||||
int tid = snap.getBlockTypeId(bx, y, bz);
|
||||
unstepPosition();
|
||||
laststep = ls;
|
||||
return tid;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
public BlockStep getLastStep() {
|
||||
return laststep;
|
||||
}
|
||||
}
|
||||
|
||||
private class OurEndMapIterator extends OurMapIterator {
|
||||
|
||||
OurEndMapIterator(int x0, int y0, int z0) {
|
||||
super(x0, y0, z0);
|
||||
}
|
||||
public final int getBlockSkyLight() {
|
||||
return 15;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Chunk cache for representing unloaded chunk (or air)
|
||||
*/
|
||||
private static class EmptyChunk implements ChunkSnapshot {
|
||||
/* Need these for interface, but not used */
|
||||
public int getX() { return 0; }
|
||||
public int getZ() { return 0; }
|
||||
public String getWorldName() { return ""; }
|
||||
public long getCaptureFullTime() { return 0; }
|
||||
|
||||
public final int getBlockTypeId(int x, int y, int z) {
|
||||
return 0;
|
||||
}
|
||||
public final int getBlockData(int x, int y, int z) {
|
||||
return 0;
|
||||
}
|
||||
public final int getBlockSkyLight(int x, int y, int z) {
|
||||
return 15;
|
||||
}
|
||||
public final int getBlockEmittedLight(int x, int y, int z) {
|
||||
return 0;
|
||||
}
|
||||
public final int getHighestBlockYAt(int x, int z) {
|
||||
return 0;
|
||||
}
|
||||
public Biome getBiome(int x, int z) {
|
||||
return null;
|
||||
}
|
||||
public double getRawBiomeTemperature(int x, int z) {
|
||||
return 0.0;
|
||||
}
|
||||
public double getRawBiomeRainfall(int x, int z) {
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Chunk cache for representing generic stone chunk
|
||||
*/
|
||||
private static class PlainChunk implements ChunkSnapshot {
|
||||
private int fillid;
|
||||
PlainChunk(int fillid) { this.fillid = fillid; }
|
||||
/* Need these for interface, but not used */
|
||||
public int getX() { return 0; }
|
||||
public int getZ() { return 0; }
|
||||
public String getWorldName() { return ""; }
|
||||
public Biome getBiome(int x, int z) { return null; }
|
||||
public double getRawBiomeTemperature(int x, int z) { return 0.0; }
|
||||
public double getRawBiomeRainfall(int x, int z) { return 0.0; }
|
||||
public long getCaptureFullTime() { return 0; }
|
||||
|
||||
public final int getBlockTypeId(int x, int y, int z) {
|
||||
if(y < 64) return fillid;
|
||||
return 0;
|
||||
}
|
||||
public final int getBlockData(int x, int y, int z) {
|
||||
return 0;
|
||||
}
|
||||
public final int getBlockSkyLight(int x, int y, int z) {
|
||||
if(y < 64)
|
||||
return 0;
|
||||
return 15;
|
||||
}
|
||||
public final int getBlockEmittedLight(int x, int y, int z) {
|
||||
return 0;
|
||||
}
|
||||
public final int getHighestBlockYAt(int x, int z) {
|
||||
return 64;
|
||||
}
|
||||
}
|
||||
|
||||
private static final EmptyChunk EMPTY = new EmptyChunk();
|
||||
private static final PlainChunk STONE = new PlainChunk(1);
|
||||
private static final PlainChunk OCEAN = new PlainChunk(9);
|
||||
|
||||
/**
|
||||
* Construct empty cache
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public NewMapChunkCache() {
|
||||
if(!init) {
|
||||
/* Get CraftWorld.popPreservedChunk(x,z) - reduces memory bloat from map traversals (optional) */
|
||||
try {
|
||||
Class c = Class.forName("org.bukkit.craftbukkit.CraftWorld");
|
||||
poppreservedchunk = c.getDeclaredMethod("popPreservedChunk", new Class[] { int.class, int.class });
|
||||
/* getHandle() */
|
||||
getworldhandle = c.getDeclaredMethod("getHandle", new Class[0]);
|
||||
} catch (ClassNotFoundException cnfx) {
|
||||
} catch (NoSuchMethodException nsmx) {
|
||||
}
|
||||
/* Get CraftChunk.getChunkSnapshot(boolean,boolean,boolean) and CraftChunk.getHandle() */
|
||||
try {
|
||||
Class c = Class.forName("org.bukkit.craftbukkit.CraftChunk");
|
||||
gethandle = c.getDeclaredMethod("getHandle", new Class[0]);
|
||||
} catch (ClassNotFoundException cnfx) {
|
||||
} catch (NoSuchMethodException nsmx) {
|
||||
}
|
||||
/* Get Chunk.removeEntities() */
|
||||
try {
|
||||
Class c = Class.forName("net.minecraft.server.Chunk");
|
||||
removeentities = c.getDeclaredMethod("removeEntities", new Class[0]);
|
||||
} catch (ClassNotFoundException cnfx) {
|
||||
} catch (NoSuchMethodException nsmx) {
|
||||
}
|
||||
|
||||
/* Get CraftChunkSnapshot.biome field */
|
||||
try {
|
||||
Class c = Class.forName("org.bukkit.craftbukkit.CraftChunkSnapshot");
|
||||
chunkbiome = c.getDeclaredField("biome");
|
||||
chunkbiome.setAccessible(true);
|
||||
} catch (ClassNotFoundException cnfx) {
|
||||
} catch (NoSuchFieldException nsmx) {
|
||||
}
|
||||
/* ticklist for World */
|
||||
try {
|
||||
Class c = Class.forName("net.minecraft.server.World");
|
||||
try {
|
||||
ticklist = c.getDeclaredField("K"); /* 1.0.0 */
|
||||
} catch (NoSuchFieldException nsfx) {
|
||||
ticklist = c.getDeclaredField("N"); /* 1.8.1 */
|
||||
}
|
||||
ticklist.setAccessible(true);
|
||||
if(ticklist.getType().isAssignableFrom(TreeSet.class) == false)
|
||||
ticklist = null;
|
||||
processticklist = c.getDeclaredMethod("a", new Class[] { boolean.class } );
|
||||
} catch (ClassNotFoundException cnfx) {
|
||||
} catch (NoSuchFieldException nsmx) {
|
||||
} catch (NoSuchMethodException nsmx) {
|
||||
}
|
||||
|
||||
init = true;
|
||||
}
|
||||
}
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
public void setChunks(World w, List<DynmapChunk> chunks) {
|
||||
this.w = w;
|
||||
if((getworldhandle != null) && (craftworld == null)) {
|
||||
try {
|
||||
craftworld = getworldhandle.invoke(w); /* World.getHandle() */
|
||||
if(ticklist != null)
|
||||
ourticklist = (TreeSet)ticklist.get(craftworld);
|
||||
} catch (Exception x) {
|
||||
}
|
||||
}
|
||||
this.chunks = chunks;
|
||||
/* Compute range */
|
||||
if(chunks.size() == 0) {
|
||||
this.x_min = 0;
|
||||
this.x_max = 0;
|
||||
this.z_min = 0;
|
||||
this.z_max = 0;
|
||||
x_dim = 1;
|
||||
}
|
||||
else {
|
||||
x_min = x_max = chunks.get(0).x;
|
||||
z_min = z_max = chunks.get(0).z;
|
||||
for(DynmapChunk c : chunks) {
|
||||
if(c.x > x_max)
|
||||
x_max = c.x;
|
||||
if(c.x < x_min)
|
||||
x_min = c.x;
|
||||
if(c.z > z_max)
|
||||
z_max = c.z;
|
||||
if(c.z < z_min)
|
||||
z_min = c.z;
|
||||
}
|
||||
x_dim = x_max - x_min + 1;
|
||||
}
|
||||
|
||||
snaparray = new ChunkSnapshot[x_dim * (z_max-z_min+1)];
|
||||
snapbiomes = new Biome[x_dim * (z_max-z_min+1)][];
|
||||
}
|
||||
|
||||
public int loadChunks(int max_to_load) {
|
||||
long t0 = System.nanoTime();
|
||||
int cnt = 0;
|
||||
if(iterator == null)
|
||||
iterator = chunks.listIterator();
|
||||
|
||||
checkTickList();
|
||||
|
||||
DynmapCore.setIgnoreChunkLoads(true);
|
||||
//boolean isnormral = w.getEnvironment() == Environment.NORMAL;
|
||||
// Load the required chunks.
|
||||
while((cnt < max_to_load) && iterator.hasNext()) {
|
||||
DynmapChunk chunk = iterator.next();
|
||||
boolean vis = true;
|
||||
if(visible_limits != null) {
|
||||
vis = false;
|
||||
for(VisibilityLimit limit : visible_limits) {
|
||||
if((chunk.x >= limit.x0) && (chunk.x <= limit.x1) && (chunk.z >= limit.z0) && (chunk.z <= limit.z1)) {
|
||||
vis = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(vis && (hidden_limits != null)) {
|
||||
for(VisibilityLimit limit : hidden_limits) {
|
||||
if((chunk.x >= limit.x0) && (chunk.x <= limit.x1) && (chunk.z >= limit.z0) && (chunk.z <= limit.z1)) {
|
||||
vis = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Check if cached chunk snapshot found */
|
||||
ChunkSnapshot ss = MapManager.mapman.sscache.getSnapshot(w.getName(), chunk.x, chunk.z, blockdata, biome, biomeraw, highesty);
|
||||
if(ss != null) {
|
||||
if(!vis) {
|
||||
if(hidestyle == HiddenChunkStyle.FILL_STONE_PLAIN)
|
||||
ss = STONE;
|
||||
else if(hidestyle == HiddenChunkStyle.FILL_OCEAN)
|
||||
ss = OCEAN;
|
||||
else
|
||||
ss = EMPTY;
|
||||
}
|
||||
snaparray[(chunk.x-x_min) + (chunk.z - z_min)*x_dim] = ss;
|
||||
continue;
|
||||
}
|
||||
long tt0 = 0;
|
||||
chunks_attempted++;
|
||||
boolean wasLoaded = w.isChunkLoaded(chunk.x, chunk.z);
|
||||
boolean didload = w.loadChunk(chunk.x, chunk.z, false);
|
||||
boolean didgenerate = false;
|
||||
/* If we didn't load, and we're supposed to generate, do it */
|
||||
if((!didload) && do_generate && vis)
|
||||
didgenerate = didload = w.loadChunk(chunk.x, chunk.z, true);
|
||||
/* If it did load, make cache of it */
|
||||
if(didload) {
|
||||
if(!vis) {
|
||||
if(hidestyle == HiddenChunkStyle.FILL_STONE_PLAIN)
|
||||
ss = STONE;
|
||||
else if(hidestyle == HiddenChunkStyle.FILL_OCEAN)
|
||||
ss = OCEAN;
|
||||
else
|
||||
ss = EMPTY;
|
||||
}
|
||||
else {
|
||||
Chunk c = w.getChunkAt(chunk.x, chunk.z);
|
||||
if(blockdata || highesty)
|
||||
ss = c.getChunkSnapshot(highesty, biome, biomeraw);
|
||||
else
|
||||
ss = w.getEmptyChunkSnapshot(chunk.x, chunk.z, biome, biomeraw);
|
||||
if(ss != null) {
|
||||
MapManager.mapman.sscache.putSnapshot(w.getName(), chunk.x, chunk.z, ss, blockdata, biome, biomeraw, highesty);
|
||||
}
|
||||
}
|
||||
snaparray[(chunk.x-x_min) + (chunk.z - z_min)*x_dim] = ss;
|
||||
}
|
||||
if ((!wasLoaded) && didload) {
|
||||
chunks_read++;
|
||||
/* It looks like bukkit "leaks" entities - they don't get removed from the world-level table
|
||||
* when chunks are unloaded but not saved - removing them seems to do the trick */
|
||||
if(!(didgenerate && do_save)) {
|
||||
boolean did_remove = false;
|
||||
Chunk cc = w.getChunkAt(chunk.x, chunk.z);
|
||||
if((gethandle != null) && (removeentities != null)) {
|
||||
try {
|
||||
Object chk = gethandle.invoke(cc);
|
||||
if(chk != null) {
|
||||
removeentities.invoke(chk);
|
||||
did_remove = true;
|
||||
}
|
||||
} catch (InvocationTargetException itx) {
|
||||
} catch (IllegalArgumentException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
}
|
||||
}
|
||||
if(!did_remove) {
|
||||
if(cc != null) {
|
||||
for(Entity e: cc.getEntities())
|
||||
e.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Since we only remember ones we loaded, and we're synchronous, no player has
|
||||
* moved, so it must be safe (also prevent chunk leak, which appears to happen
|
||||
* because isChunkInUse defined "in use" as being within 256 blocks of a player,
|
||||
* while the actual in-use chunk area for a player where the chunks are managed
|
||||
* by the MC base server is 21x21 (or about a 160 block radius).
|
||||
* Also, if we did generate it, need to save it */
|
||||
w.unloadChunk(chunk.x, chunk.z, didgenerate && do_save, false);
|
||||
/* And pop preserved chunk - this is a bad leak in Bukkit for map traversals like us */
|
||||
try {
|
||||
if(poppreservedchunk != null)
|
||||
poppreservedchunk.invoke(w, chunk.x, chunk.z);
|
||||
} catch (Exception x) {
|
||||
Log.severe("Cannot pop preserved chunk - " + x.toString());
|
||||
}
|
||||
}
|
||||
cnt++;
|
||||
}
|
||||
DynmapCore.setIgnoreChunkLoads(false);
|
||||
|
||||
if(iterator.hasNext() == false) { /* If we're done */
|
||||
isempty = true;
|
||||
/* Fill missing chunks with empty dummy chunk */
|
||||
for(int i = 0; i < snaparray.length; i++) {
|
||||
if(snaparray[i] == null)
|
||||
snaparray[i] = EMPTY;
|
||||
else if(snaparray[i] != EMPTY)
|
||||
isempty = false;
|
||||
}
|
||||
}
|
||||
total_loadtime += System.nanoTime() - t0;
|
||||
|
||||
return cnt;
|
||||
}
|
||||
/**
|
||||
* Test if done loading
|
||||
*/
|
||||
public boolean isDoneLoading() {
|
||||
if(iterator != null)
|
||||
return !iterator.hasNext();
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Test if all empty blocks
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return isempty;
|
||||
}
|
||||
/**
|
||||
* Unload chunks
|
||||
*/
|
||||
public void unloadChunks() {
|
||||
if(snaparray != null) {
|
||||
for(int i = 0; i < snaparray.length; i++) {
|
||||
snaparray[i] = null;
|
||||
}
|
||||
snaparray = null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Get block ID at coordinates
|
||||
*/
|
||||
public int getBlockTypeID(int x, int y, int z) {
|
||||
ChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim];
|
||||
return ss.getBlockTypeId(x & 0xF, y, z & 0xF);
|
||||
}
|
||||
/**
|
||||
* Get block data at coordiates
|
||||
*/
|
||||
public byte getBlockData(int x, int y, int z) {
|
||||
ChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim];
|
||||
return (byte)ss.getBlockData(x & 0xF, y, z & 0xF);
|
||||
}
|
||||
/* Get highest block Y
|
||||
*
|
||||
*/
|
||||
public int getHighestBlockYAt(int x, int z) {
|
||||
ChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim];
|
||||
return ss.getHighestBlockYAt(x & 0xF, z & 0xF);
|
||||
}
|
||||
/* Get sky light level
|
||||
*/
|
||||
public int getBlockSkyLight(int x, int y, int z) {
|
||||
ChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim];
|
||||
return ss.getBlockSkyLight(x & 0xF, y, z & 0xF);
|
||||
}
|
||||
/* Get emitted light level
|
||||
*/
|
||||
public int getBlockEmittedLight(int x, int y, int z) {
|
||||
ChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim];
|
||||
return ss.getBlockEmittedLight(x & 0xF, y, z & 0xF);
|
||||
}
|
||||
public Biome getBiome(int x, int z) {
|
||||
ChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim];
|
||||
return ss.getBiome(x & 0xF, z & 0xF);
|
||||
}
|
||||
public double getRawBiomeTemperature(int x, int z) {
|
||||
ChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim];
|
||||
return ss.getRawBiomeTemperature(x & 0xF, z & 0xF);
|
||||
}
|
||||
public double getRawBiomeRainfall(int x, int z) {
|
||||
ChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim];
|
||||
return ss.getRawBiomeRainfall(x & 0xF, z & 0xF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache iterator
|
||||
*/
|
||||
public MapIterator getIterator(int x, int y, int z) {
|
||||
if(w.getEnvironment().toString().equals("THE_END"))
|
||||
return new OurEndMapIterator(x, y, z);
|
||||
return new OurMapIterator(x, y, z);
|
||||
}
|
||||
/**
|
||||
* Set hidden chunk style (default is FILL_AIR)
|
||||
*/
|
||||
public void setHiddenFillStyle(HiddenChunkStyle style) {
|
||||
this.hidestyle = style;
|
||||
}
|
||||
/**
|
||||
* Set autogenerate - must be done after at least one visible range has been set
|
||||
*/
|
||||
public void setAutoGenerateVisbileRanges(DynmapWorld.AutoGenerateOption generateopt) {
|
||||
if((generateopt != DynmapWorld.AutoGenerateOption.NONE) && ((visible_limits == null) || (visible_limits.size() == 0))) {
|
||||
Log.severe("Cannot setAutoGenerateVisibleRanges() without visible ranges defined");
|
||||
return;
|
||||
}
|
||||
this.do_generate = (generateopt != DynmapWorld.AutoGenerateOption.NONE);
|
||||
this.do_save = (generateopt == DynmapWorld.AutoGenerateOption.PERMANENT);
|
||||
}
|
||||
/**
|
||||
* Add visible area limit - can be called more than once
|
||||
* Needs to be set before chunks are loaded
|
||||
* Coordinates are block coordinates
|
||||
*/
|
||||
public void setVisibleRange(VisibilityLimit lim) {
|
||||
VisibilityLimit limit = new VisibilityLimit();
|
||||
if(lim.x0 > lim.x1) {
|
||||
limit.x0 = (lim.x1 >> 4); limit.x1 = ((lim.x0+15) >> 4);
|
||||
}
|
||||
else {
|
||||
limit.x0 = (lim.x0 >> 4); limit.x1 = ((lim.x1+15) >> 4);
|
||||
}
|
||||
if(lim.z0 > lim.z1) {
|
||||
limit.z0 = (lim.z1 >> 4); limit.z1 = ((lim.z0+15) >> 4);
|
||||
}
|
||||
else {
|
||||
limit.z0 = (lim.z0 >> 4); limit.z1 = ((lim.z1+15) >> 4);
|
||||
}
|
||||
if(visible_limits == null)
|
||||
visible_limits = new ArrayList<VisibilityLimit>();
|
||||
visible_limits.add(limit);
|
||||
}
|
||||
/**
|
||||
* Add hidden area limit - can be called more than once
|
||||
* Needs to be set before chunks are loaded
|
||||
* Coordinates are block coordinates
|
||||
*/
|
||||
public void setHiddenRange(VisibilityLimit lim) {
|
||||
VisibilityLimit limit = new VisibilityLimit();
|
||||
if(lim.x0 > lim.x1) {
|
||||
limit.x0 = (lim.x1 >> 4); limit.x1 = ((lim.x0+15) >> 4);
|
||||
}
|
||||
else {
|
||||
limit.x0 = (lim.x0 >> 4); limit.x1 = ((lim.x1+15) >> 4);
|
||||
}
|
||||
if(lim.z0 > lim.z1) {
|
||||
limit.z0 = (lim.z1 >> 4); limit.z1 = ((lim.z0+15) >> 4);
|
||||
}
|
||||
else {
|
||||
limit.z0 = (lim.z0 >> 4); limit.z1 = ((lim.z1+15) >> 4);
|
||||
}
|
||||
if(hidden_limits == null)
|
||||
hidden_limits = new ArrayList<VisibilityLimit>();
|
||||
hidden_limits.add(limit);
|
||||
}
|
||||
@Override
|
||||
public boolean setChunkDataTypes(boolean blockdata, boolean biome, boolean highestblocky, boolean rawbiome) {
|
||||
this.biome = biome;
|
||||
this.biomeraw = rawbiome;
|
||||
this.highesty = highestblocky;
|
||||
this.blockdata = blockdata;
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public World getWorld() {
|
||||
return w;
|
||||
}
|
||||
@Override
|
||||
public int getChunksLoaded() {
|
||||
return chunks_read;
|
||||
}
|
||||
@Override
|
||||
public int getChunkLoadsAttempted() {
|
||||
return chunks_attempted;
|
||||
}
|
||||
@Override
|
||||
public long getTotalRuntimeNanos() {
|
||||
return total_loadtime;
|
||||
}
|
||||
@Override
|
||||
public long getExceptionCount() {
|
||||
return exceptions;
|
||||
}
|
||||
|
||||
private boolean checkTickList() {
|
||||
boolean isok = true;
|
||||
if((ourticklist != null) && (processticklist != null)) {
|
||||
int cnt = 0;
|
||||
int ticksize = ourticklist.size();
|
||||
while((cnt < MAX_PROCESSTICKS) && (ticksize > MAX_TICKLIST) && (ourticklist.size() > MAX_TICKLIST)) {
|
||||
try {
|
||||
processticklist.invoke(craftworld, true);
|
||||
} catch (Exception x) {
|
||||
}
|
||||
ticksize -= 1000;
|
||||
cnt++;
|
||||
MapManager.mapman.incExtraTickList();
|
||||
}
|
||||
if(cnt >= MAX_PROCESSTICKS) { /* If still behind, delay processing */
|
||||
isok = false;
|
||||
}
|
||||
}
|
||||
return isok;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue