Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Commit

Permalink
Delete a pointless class, clean up TickTask a little... Change the tr…
Browse files Browse the repository at this point in the history
…ader busy message.
  • Loading branch information
fullwall committed May 24, 2012
1 parent b8c5873 commit ad2473e
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 162 deletions.
52 changes: 20 additions & 32 deletions src/core/net/citizensnpcs/TickTask.java
@@ -1,12 +1,8 @@
package net.citizensnpcs;

import java.util.Map;

import net.citizensnpcs.api.event.NPCCreateEvent.NPCCreateReason;
import net.citizensnpcs.misc.Actions;
import net.citizensnpcs.resources.npclib.HumanNPC;
import net.citizensnpcs.resources.npclib.NPCManager;
import net.citizensnpcs.resources.npclib.NPCSpawner;
import net.citizensnpcs.utils.LocationUtils;
import net.citizensnpcs.utils.MessageUtils;
import net.citizensnpcs.utils.Messaging;
Expand All @@ -15,32 +11,38 @@
import net.citizensnpcs.waypoints.WaypointPath;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;

import com.google.common.collect.MapMaker;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;

public class TickTask implements Runnable {
private static final Map<HumanNPC, Actions> cachedActions = new MapMaker().weakKeys().makeMap();
private static final SetMultimap<HumanNPC, String> cachedActions = HashMultimap.create();

@Override
public void run() {
Player[] online = Bukkit.getServer().getOnlinePlayers();
for (HumanNPC npc : NPCManager.getList().values()) {
updateWaypoints(npc);
npc.doTick();
NPCSpawner.removeNPCFromPlayerList(npc);
if (!npc.getNPCData().isLookClose() && !npc.getNPCData().isTalkClose())
continue;
boolean canLookClose = npc.getHandle().pathFinished() && !npc.getHandle().hasTarget()
&& npc.getNPCData().isLookClose();
if (!npc.getNPCData().isTalkClose() && !canLookClose)
continue;
Location npcLoc = npc.getLocation();
for (Player player : online) {

This comment has been minimized.

Copy link
@ammaraskar

ammaraskar May 24, 2012

Replace online with

npc.getWorld().getPlayers()

for more efficiency

This comment has been minimized.

Copy link
@fullwall

fullwall May 31, 2012

Member

npc.getWorld().getPlayers() allocates a new list of entities from the world every time it gets called. I thought it would be better just to use all the players (and iterate a few more times in the case of multiworld) rather than use that method.

if (!npc.getNPCData().isLookClose() && !npc.getNPCData().isTalkClose())
continue;
// If the player is within 'seeing' range
if (LocationUtils.withinRange(npc.getLocation(), player.getLocation(), Settings.getDouble("NPCRange"))) {
if (npc.getHandle().pathFinished() && !npc.getHandle().hasTarget()
&& npc.getNPCData().isLookClose()) {
if (LocationUtils.withinRange(npcLoc, player.getLocation(), Settings.getDouble("NPCRange"))) {
if (canLookClose) {
NPCManager.faceEntity(npc, player);
}
cacheActions(npc, player);
if (npc.getNPCData().isTalkClose())
cacheActions(npc, player);
} else {
clearActions(npc, player);
cachedActions.remove(npc, player.getName());
}
}
}
Expand Down Expand Up @@ -87,24 +89,10 @@ private void updateWaypoints(HumanNPC npc) {
}
}

private static void clearActions(HumanNPC npc, Player player) {
Actions actions = cachedActions.get(npc);
if (actions == null) {
cachedActions.put(npc, new Actions());
return;
}
actions.clear("saidText", player.getName());
}

private static void cacheActions(HumanNPC npc, Player player) {
Actions actions = cachedActions.get(npc);
if (actions == null) {
cachedActions.put(npc, new Actions());
return;
}
if (!actions.has("saidText", player.getName()) && npc.getNPCData().isTalkClose()) {
if (!cachedActions.containsEntry(npc, player.getName().toLowerCase()) && npc.getNPCData().isTalkClose()) {
MessageUtils.sendText(npc, player);
actions.set("saidText", player.getName());
cachedActions.put(npc, player.getName().toLowerCase());
}
}

Expand All @@ -122,7 +110,7 @@ public RespawnTask(HumanNPC npc) {
}

public void register(int delay) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Citizens.plugin, this, delay);
Bukkit.getScheduler().scheduleSyncDelayedTask(Citizens.plugin, this, delay);
}

@Override
Expand All @@ -134,7 +122,7 @@ public void run() {

public static void clearActions(Player player) {
for (HumanNPC npc : cachedActions.keySet()) {
clearActions(npc, player);
cachedActions.remove(npc, player.getName().toLowerCase());
}
}
}
25 changes: 0 additions & 25 deletions src/core/net/citizensnpcs/misc/Actions.java

This file was deleted.

34 changes: 24 additions & 10 deletions src/core/net/citizensnpcs/resources/npclib/NPCSpawner.java
@@ -1,5 +1,6 @@
package net.citizensnpcs.resources.npclib;

import net.citizensnpcs.Citizens;
import net.citizensnpcs.api.event.NPCRemoveEvent;
import net.citizensnpcs.api.event.NPCRemoveEvent.NPCRemoveReason;
import net.citizensnpcs.resources.npclib.creatures.CreatureNPCType;
Expand All @@ -26,28 +27,39 @@ private static MinecraftServer getMinecraftServer(Server server) {
return ((CraftServer) server).getServer();
}

public static HumanNPC spawnNPC(int UID, String name, Location loc) {
public static HumanNPC spawnNPC(int UID, String name, final Location loc) {
if (loc == null || loc.getWorld() == null) {
Messaging.log("Null location or world while spawning", name, "UID", UID
+ ". Is the location unloaded or missing?");
return null;
}
WorldServer ws = getWorldServer(loc.getWorld());
CraftNPC eh = new CraftNPC(getMinecraftServer(ws.getServer()), ws, name, new ItemInWorldManager(ws));
final CraftNPC eh = new CraftNPC(getMinecraftServer(ws.getServer()), ws, name, new ItemInWorldManager(ws));
eh.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
eh.X = loc.getYaw();
Bukkit.getScheduler().scheduleSyncDelayedTask(Citizens.plugin, new Runnable() {
@Override
public void run() {
eh.X = loc.getYaw();
}
});
ws.addEntity(eh);
ws.players.remove(eh);
return new HumanNPC(eh, UID, name);
}

public static HumanNPC spawnNPC(Location loc, CreatureNPCType type) {
public static HumanNPC spawnNPC(final Location loc, CreatureNPCType type) {
try {
String name = type.chooseRandomName();
WorldServer ws = getWorldServer(loc.getWorld());
CraftNPC eh = type.getEntityConstructor().newInstance(getMinecraftServer(ws.getServer()), ws, name,
final CraftNPC eh = type.getEntityConstructor().newInstance(getMinecraftServer(ws.getServer()), ws, name,
new ItemInWorldManager(ws));
eh.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
Bukkit.getScheduler().scheduleSyncDelayedTask(Citizens.plugin, new Runnable() {
@Override
public void run() {
eh.X = loc.getYaw();
}
});
ws.addEntity(eh);
ws.players.remove(eh);
return new HumanNPC(eh, -1 /*Fake UID*/, name);
Expand All @@ -57,10 +69,16 @@ public static HumanNPC spawnNPC(Location loc, CreatureNPCType type) {
return null;
}

public static HumanNPC spawnNPC(HumanNPC npc, Location loc) {
public static HumanNPC spawnNPC(final HumanNPC npc, final Location loc) {
WorldServer ws = getWorldServer(loc.getWorld());
npc.getHandle().setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
ws.addEntity(npc.getHandle());
Bukkit.getScheduler().scheduleSyncDelayedTask(Citizens.plugin, new Runnable() {
@Override
public void run() {
npc.getHandle().X = loc.getYaw();
}
});
ws.players.remove(npc.getHandle());
return npc;
}
Expand All @@ -77,8 +95,4 @@ public static void despawnNPC(HumanNPC npc, NPCRemoveReason reason) {
public static void despawnNPC(CraftNPC npc, NPCRemoveReason reason) {
despawnNPC(npc.npc, reason);
}

public static void removeNPCFromPlayerList(HumanNPC npc) {
getWorldServer(npc.getWorld()).players.remove(npc.getHandle());
}
}
177 changes: 87 additions & 90 deletions src/core/net/citizensnpcs/resources/npclib/creatures/CreatureTask.java
Expand Up @@ -17,94 +17,91 @@
import com.google.common.collect.MapMaker;

public class CreatureTask implements Runnable {
public final static Map<Integer, CreatureNPC> creatureNPCs = new MapMaker()
.makeMap();
private final static EnumMap<CreatureNPCType, Integer> spawned = new EnumMap<CreatureNPCType, Integer>(
CreatureNPCType.class);
private Player[] online;
private static boolean dirty = true;
private static Random random = new Random(System.currentTimeMillis());

@Override
public void run() {
if (dirty) {
online = Bukkit.getServer().getOnlinePlayers();
dirty = false;
}
if (online != null && online.length > 0) {
Player player = online[random.nextInt(online.length)];
CreatureNPCType type = CreatureNPCType.getRandomType(random);
if (type == null)
return;
// TODO: favour certain biomes perhaps?
spawnCreature(type, player.getLocation());
}
}

private void spawnCreature(CreatureNPCType type, Location location) {
if (spawned.get(type) == null) {
spawned.put(type, 0);
} else if (type.canSpawn(spawned.get(type))) {
HumanNPC npc = type.spawn(location);
if (npc != null) {
spawned.put(type, spawned.get(type) + 1);
creatureNPCs.put(npc.getPlayer().getEntityId(),
(CreatureNPC) npc.getHandle());
onSpawn(creatureNPCs.get(npc.getPlayer().getEntityId()));
}
}
}

private void onSpawn(CreatureNPC creatureNPC) {
creatureNPC.onSpawn();
}

public static void setDirty() {
dirty = true;
}

public static void despawnAll(NPCRemoveReason reason) {
for (CreatureNPC creature : creatureNPCs.values()) {
NPCSpawner.despawnNPC(creature, reason);
}
}

public static void despawn(CreatureNPC npc, NPCRemoveReason reason) {
removeFromMaps(npc);
NPCSpawner.despawnNPC(npc, reason);
}

public static void removeFromMaps(CreatureNPC npc) {
creatureNPCs.remove(npc.getBukkitEntity().getEntityId());
spawned.put(npc.getType(), spawned.get(npc.getType()) - 1);
}

public static void onDamage(Entity entity, EntityDamageEvent event) {
if (getCreature(entity) != null) {
creatureNPCs.get(entity.getEntityId()).onDamage(event);
}
}

public static void onEntityDeath(Entity entity) {
if (getCreature(entity) != null) {
CreatureNPC creatureNPC = creatureNPCs.get(entity.getEntityId());
creatureNPC.onDeath();
removeFromMaps(creatureNPC);
NPCSpawner.despawnNPC(creatureNPC, NPCRemoveReason.DEATH);
}
}

public static CreatureNPC getCreature(Entity entity) {
return creatureNPCs.get(entity.getEntityId());
}

public static class CreatureTick implements Runnable {
@Override
public void run() {
for (CreatureNPC npc : creatureNPCs.values()) {
NPCSpawner.removeNPCFromPlayerList(npc.npc);
npc.doTick();
}
}
}
public final static Map<Integer, CreatureNPC> creatureNPCs = new MapMaker().makeMap();
private final static EnumMap<CreatureNPCType, Integer> spawned = new EnumMap<CreatureNPCType, Integer>(
CreatureNPCType.class);
private Player[] online;
private static boolean dirty = true;
private static Random random = new Random(System.currentTimeMillis());

@Override
public void run() {
if (dirty) {
online = Bukkit.getServer().getOnlinePlayers();
dirty = false;
}
if (online != null && online.length > 0) {
Player player = online[random.nextInt(online.length)];
CreatureNPCType type = CreatureNPCType.getRandomType(random);
if (type == null)
return;
// TODO: favour certain biomes perhaps?
spawnCreature(type, player.getLocation());
}
}

private void spawnCreature(CreatureNPCType type, Location location) {
if (spawned.get(type) == null) {
spawned.put(type, 0);
} else if (type.canSpawn(spawned.get(type))) {
HumanNPC npc = type.spawn(location);
if (npc != null) {
spawned.put(type, spawned.get(type) + 1);
creatureNPCs.put(npc.getPlayer().getEntityId(), (CreatureNPC) npc.getHandle());
onSpawn(creatureNPCs.get(npc.getPlayer().getEntityId()));
}
}
}

private void onSpawn(CreatureNPC creatureNPC) {
creatureNPC.onSpawn();
}

public static void setDirty() {
dirty = true;
}

public static void despawnAll(NPCRemoveReason reason) {
for (CreatureNPC creature : creatureNPCs.values()) {
NPCSpawner.despawnNPC(creature, reason);
}
}

public static void despawn(CreatureNPC npc, NPCRemoveReason reason) {
removeFromMaps(npc);
NPCSpawner.despawnNPC(npc, reason);
}

public static void removeFromMaps(CreatureNPC npc) {
creatureNPCs.remove(npc.getBukkitEntity().getEntityId());
spawned.put(npc.getType(), spawned.get(npc.getType()) - 1);
}

public static void onDamage(Entity entity, EntityDamageEvent event) {
if (getCreature(entity) != null) {
creatureNPCs.get(entity.getEntityId()).onDamage(event);
}
}

public static void onEntityDeath(Entity entity) {
if (getCreature(entity) != null) {
CreatureNPC creatureNPC = creatureNPCs.get(entity.getEntityId());
creatureNPC.onDeath();
removeFromMaps(creatureNPC);
NPCSpawner.despawnNPC(creatureNPC, NPCRemoveReason.DEATH);
}
}

public static CreatureNPC getCreature(Entity entity) {
return creatureNPCs.get(entity.getEntityId());
}

public static class CreatureTick implements Runnable {
@Override
public void run() {
for (CreatureNPC npc : creatureNPCs.values()) {
npc.doTick();
}
}
}
}

0 comments on commit ad2473e

Please sign in to comment.