Better threading - stop memory leak

This commit is contained in:
fullwall 2012-07-22 14:18:24 +08:00
parent 94e366ff31
commit b73afa92d6
5 changed files with 37 additions and 27 deletions

View File

@ -159,6 +159,8 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
@Override
public void onDisable() {
Bukkit.getPluginManager().callEvent(new CitizensDisableEvent());
Editor.leaveAll();
CitizensAPI.shutdown();
tearDownScripting();
// Don't bother with this part if MC versions are not compatible
@ -177,8 +179,8 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
String mcVersion = ((CraftServer) getServer()).getServer().getVersion();
compatible = mcVersion.startsWith(COMPATIBLE_MC_VERSION);
if (!compatible) {
Messaging.severeF("v%s is not compatible with Minecraft v%s. Disabling.", getDescription().getVersion(),
mcVersion);
Messaging.severeF("v%s is not compatible with Minecraft v%s. Disabling.", getDescription()
.getVersion(), mcVersion);
getServer().getPluginManager().disablePlugin(this);
return;
}
@ -270,7 +272,8 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
try {
type = EntityType.valueOf(unparsedEntityType);
} catch (IllegalArgumentException ex) {
Messaging.logF("NPC type '%s' was not recognized. Did you spell it correctly?", unparsedEntityType);
Messaging.logF("NPC type '%s' was not recognized. Did you spell it correctly?",
unparsedEntityType);
continue;
}
}
@ -298,8 +301,9 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
String type = Setting.STORAGE_TYPE.asString();
if (type.equalsIgnoreCase("db") || type.equalsIgnoreCase("database")) {
try {
saves = new DatabaseStorage(Setting.DATABASE_DRIVER.asString(), Setting.DATABASE_URL.asString(),
Setting.DATABASE_USERNAME.asString(), Setting.DATABASE_PASSWORD.asString());
saves = new DatabaseStorage(Setting.DATABASE_DRIVER.asString(),
Setting.DATABASE_URL.asString(), Setting.DATABASE_USERNAME.asString(),
Setting.DATABASE_PASSWORD.asString());
} catch (SQLException e) {
e.printStackTrace();
Messaging.log("Unable to connect to database, falling back to YAML");
@ -359,7 +363,8 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
}
private void tearDownScripting() {
Thread.currentThread().setContextClassLoader(contextClassLoader);
if (Thread.currentThread().getContextClassLoader() == getClassLoader())
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
private static final String COMPATIBLE_MC_VERSION = "1.2.5";

View File

@ -132,6 +132,11 @@ public class EventListen implements Listener {
// undesirable as player NPCs are not real players and confuse plugins.
}
@EventHandler(ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event) {
Editor.leave(event.getPlayer());
}
@EventHandler(ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (!npcRegistry.isNPC(event.getRightClicked()))
@ -142,11 +147,6 @@ public class EventListen implements Listener {
new EntityTargetEvent(event.getRightClicked(), event.getPlayer(), TargetReason.CUSTOM));
}
@EventHandler(ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event) {
Editor.leave(event.getPlayer());
}
/*
* World events
*/
@ -186,19 +186,13 @@ public class EventListen implements Listener {
private final int x;
private final int z;
private ChunkCoord(int x, int z) {
this.x = x;
this.z = z;
}
private ChunkCoord(Chunk chunk) {
this(chunk.getX(), chunk.getZ());
}
@Override
public int hashCode() {
final int prime = 31;
return prime * (prime + x) + z;
private ChunkCoord(int x, int z) {
this.x = x;
this.z = z;
}
@Override
@ -212,5 +206,11 @@ public class EventListen implements Listener {
ChunkCoord other = (ChunkCoord) obj;
return x == other.x && z == other.z;
}
@Override
public int hashCode() {
final int prime = 31;
return prime * (prime + x) + z;
}
}
}

View File

@ -2,6 +2,7 @@ package net.citizensnpcs.editor;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import net.citizensnpcs.util.Messaging;
@ -46,6 +47,10 @@ public abstract class Editor implements Listener {
}
public static void leaveAll() {
for (Entry<String, Editor> entry : editing.entrySet()) {
entry.getValue().end();
HandlerList.unregisterAll(entry.getValue());
}
editing.clear();
}
}

View File

@ -59,8 +59,6 @@ public class CitizensTraitFactory implements TraitFactory {
INTERNAL_TRAITS.add(trait);
}
private static final Set<String> INTERNAL_TRAITS = Sets.newHashSet();
public void addPlotters(Graph graph) {
for (Map.Entry<String, Class<? extends Trait>> entry : registered.entrySet()) {
if (INTERNAL_TRAITS.contains(entry.getKey()))
@ -110,4 +108,6 @@ public class CitizensTraitFactory implements TraitFactory {
Preconditions.checkNotNull(info, "info cannot be null");
registered.put(info.getTraitName(), info.getTraitClass());
}
private static final Set<String> INTERNAL_TRAITS = Sets.newHashSet();
}

View File

@ -80,6 +80,11 @@ public class CitizensNavigator implements Navigator {
return executing != null;
}
public void onSpawn() {
if (speed == -1)
this.speed = getSpeedFor(npc.getHandle());
}
@Override
public void setSpeed(float speed) {
this.speed = speed;
@ -108,11 +113,6 @@ public class CitizensNavigator implements Navigator {
Bukkit.getPluginManager().callEvent(new NavigationBeginEvent(this));
}
public void onSpawn() {
if (speed == -1)
this.speed = getSpeedFor(npc.getHandle());
}
public void update() {
if (executing == null)
return;