Peformance improvements

This commit is contained in:
fullwall 2012-11-28 14:23:32 +08:00
parent d0e59def6b
commit 05ea37f2e9
8 changed files with 33 additions and 32 deletions

View File

@ -44,6 +44,7 @@ public abstract class CitizensNPC extends AbstractNPC {
protected CitizensNPC(int id, String name) {
super(id, name);
runnables.add(navigator);
}
protected abstract EntityLiving createHandle(Location loc);
@ -101,7 +102,7 @@ public abstract class CitizensNPC extends AbstractNPC {
@Override
public boolean isSpawned() {
return getHandle() != null;
return mcEntity != null;
}
public void load(final DataKey root) {
@ -229,10 +230,8 @@ public abstract class CitizensNPC extends AbstractNPC {
public void update() {
try {
super.update();
if (isSpawned()) {
if (isSpawned())
NMS.trySwim(getHandle());
navigator.update();
}
} catch (Exception ex) {
Messaging.logTr(Messages.EXCEPTION_UPDATING_NPC, getId(), ex.getMessage());
ex.printStackTrace();

View File

@ -23,7 +23,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
public class CitizensNavigator implements Navigator {
public class CitizensNavigator implements Navigator, Runnable {
private final NavigatorParameters defaultParams = new NavigatorParameters()
.baseSpeed(UNINITIALISED_SPEED).range(Setting.DEFAULT_PATHFINDING_RANGE.asFloat())
.stationaryTicks(Setting.DEFAULT_STATIONARY_TICKS.asInt())
@ -184,7 +184,8 @@ public class CitizensNavigator implements Navigator {
Bukkit.getPluginManager().callEvent(new NavigationBeginEvent(this));
}
public void update() {
@Override
public void run() {
if (!isNavigating())
return;
if (!npc.isSpawned()) {

View File

@ -130,8 +130,8 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
motX = motY = motZ = 0;
NMS.updateSenses(this);
Navigation navigation = getNavigation();
if (npc.getNavigator().isNavigating()) {
Navigation navigation = getNavigation();
if (!navigation.f())
navigation.e();
moveOnCurrentHeading();

View File

@ -1,9 +1,7 @@
package net.citizensnpcs.trait;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
@ -15,6 +13,7 @@ public class Age extends Trait implements Toggleable {
private int age = 0;
@Persist
private boolean locked = true;
private Ageable ageable;
public Age() {
super("age");
@ -25,41 +24,37 @@ public class Age extends Trait implements Toggleable {
}
private boolean isAgeable() {
return npc.getBukkitEntity() instanceof Ageable;
}
@Override
public void load(DataKey key) throws NPCLoadException {
if (npc.isSpawned() && !(npc.getBukkitEntity() instanceof Ageable))
throw new NPCLoadException("NPC must be ageable");
return ageable != null;
}
@Override
public void onSpawn() {
if (isAgeable()) {
if (npc.getBukkitEntity() instanceof Ageable) {
Ageable entity = (Ageable) npc.getBukkitEntity();
entity.setAge(age);
entity.setAgeLock(locked);
}
ageable = entity;
} else
ageable = null;
}
@Override
public void run() {
if (!locked && isAgeable())
age = ((Ageable) npc.getBukkitEntity()).getAge();
age = ageable.getAge();
}
public void setAge(int age) {
this.age = age;
if (isAgeable())
((Ageable) npc.getBukkitEntity()).setAge(age);
ageable.setAge(age);
}
@Override
public boolean toggle() {
locked = !locked;
if (isAgeable())
((Ageable) npc.getBukkitEntity()).setAgeLock(locked);
ageable.setAgeLock(locked);
return locked;
}

View File

@ -3,7 +3,7 @@ package net.citizensnpcs.trait;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import org.bukkit.util.Vector;
import org.bukkit.craftbukkit.entity.CraftEntity;
public class Gravity extends Trait implements Toggleable {
@Persist
@ -21,9 +21,8 @@ public class Gravity extends Trait implements Toggleable {
public void run() {
if (!npc.isSpawned() || !enabled)
return;
Vector velocity = npc.getBukkitEntity().getVelocity();
velocity.setY(Math.max(velocity.getY(), 0));
npc.getBukkitEntity().setVelocity(velocity);
net.minecraft.server.Entity entity = ((CraftEntity) npc.getBukkitEntity()).getHandle();
entity.motY = Math.max(0, entity.motY);
}
@Override

View File

@ -6,7 +6,7 @@ import net.citizensnpcs.api.trait.Trait;
import org.bukkit.entity.Skeleton;
public class NPCSkeletonType extends Trait {
private boolean skeleton;
private Skeleton skeleton;
@Persist
private org.bukkit.entity.Skeleton.SkeletonType type = org.bukkit.entity.Skeleton.SkeletonType.NORMAL;
@ -16,13 +16,13 @@ public class NPCSkeletonType extends Trait {
@Override
public void onSpawn() {
skeleton = npc.getBukkitEntity() instanceof Skeleton;
skeleton = npc.getBukkitEntity() instanceof Skeleton ? (Skeleton) npc.getBukkitEntity() : null;
}
@Override
public void run() {
if (skeleton)
((Skeleton) npc.getBukkitEntity()).setSkeletonType(type);
if (skeleton != null)
skeleton.setSkeletonType(type);
}
public void setType(org.bukkit.entity.Skeleton.SkeletonType type) {

View File

@ -31,6 +31,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.uncommons.maths.random.XORShiftRNG;
public class Text extends Trait implements Runnable, Toggleable, Listener, ConversationAbandonedListener {
private final Map<String, Date> cooldowns = new HashMap<String, Date>();
@ -122,7 +123,7 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
@Override
public void run() {
if (!npc.isSpawned() || !talkClose)
if (!talkClose || !npc.isSpawned())
return;
List<Entity> nearby = npc.getBukkitEntity().getNearbyEntities(range, range, range);
for (Entity search : nearby) {
@ -140,7 +141,7 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
return;
// Add a cooldown if the text was successfully sent
Date wait = new Date();
int secondsDelta = new Random().nextInt(Setting.TALK_CLOSE_MAXIMUM_COOLDOWN.asInt())
int secondsDelta = RANDOM.nextInt(Setting.TALK_CLOSE_MAXIMUM_COOLDOWN.asInt())
+ Setting.TALK_CLOSE_MINIMUM_COOLDOWN.asInt();
if (secondsDelta <= 0)
return;
@ -150,6 +151,8 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
}
}
private static final Random RANDOM = new XORShiftRNG();
@Override
public void save(DataKey key) {
key.setBoolean("talk-close", talkClose);

View File

@ -4,6 +4,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.WeakHashMap;
@ -34,6 +35,7 @@ import org.bukkit.entity.Player;
import org.bukkit.material.Stairs;
import org.bukkit.material.Step;
import org.bukkit.util.Vector;
import org.uncommons.maths.random.XORShiftRNG;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
@ -259,11 +261,13 @@ public class NMS {
}
public static void trySwim(EntityLiving handle, float power) {
if (inWater(handle) && Math.random() < 0.8F) {
if (RANDOM.nextFloat() < 0.8F && inWater(handle)) {
handle.motY += power;
}
}
private static final Random RANDOM = new XORShiftRNG();
public static void updateAI(EntityLiving entity) {
updateSenses(entity);
entity.getNavigation().e();