mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-11-23 02:55:45 +01:00
Peformance improvements
This commit is contained in:
parent
c374b0cf38
commit
e15179d073
@ -44,6 +44,7 @@ public abstract class CitizensNPC extends AbstractNPC {
|
|||||||
|
|
||||||
protected CitizensNPC(int id, String name) {
|
protected CitizensNPC(int id, String name) {
|
||||||
super(id, name);
|
super(id, name);
|
||||||
|
runnables.add(navigator);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract EntityLiving createHandle(Location loc);
|
protected abstract EntityLiving createHandle(Location loc);
|
||||||
@ -101,7 +102,7 @@ public abstract class CitizensNPC extends AbstractNPC {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isSpawned() {
|
public boolean isSpawned() {
|
||||||
return getHandle() != null;
|
return mcEntity != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load(final DataKey root) {
|
public void load(final DataKey root) {
|
||||||
@ -229,10 +230,8 @@ public abstract class CitizensNPC extends AbstractNPC {
|
|||||||
public void update() {
|
public void update() {
|
||||||
try {
|
try {
|
||||||
super.update();
|
super.update();
|
||||||
if (isSpawned()) {
|
if (isSpawned())
|
||||||
NMS.trySwim(getHandle());
|
NMS.trySwim(getHandle());
|
||||||
navigator.update();
|
|
||||||
}
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
Messaging.logTr(Messages.EXCEPTION_UPDATING_NPC, getId(), ex.getMessage());
|
Messaging.logTr(Messages.EXCEPTION_UPDATING_NPC, getId(), ex.getMessage());
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
|
@ -23,7 +23,7 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.LivingEntity;
|
import org.bukkit.entity.LivingEntity;
|
||||||
|
|
||||||
public class CitizensNavigator implements Navigator {
|
public class CitizensNavigator implements Navigator, Runnable {
|
||||||
private final NavigatorParameters defaultParams = new NavigatorParameters()
|
private final NavigatorParameters defaultParams = new NavigatorParameters()
|
||||||
.baseSpeed(UNINITIALISED_SPEED).range(Setting.DEFAULT_PATHFINDING_RANGE.asFloat())
|
.baseSpeed(UNINITIALISED_SPEED).range(Setting.DEFAULT_PATHFINDING_RANGE.asFloat())
|
||||||
.stationaryTicks(Setting.DEFAULT_STATIONARY_TICKS.asInt())
|
.stationaryTicks(Setting.DEFAULT_STATIONARY_TICKS.asInt())
|
||||||
@ -184,7 +184,8 @@ public class CitizensNavigator implements Navigator {
|
|||||||
Bukkit.getPluginManager().callEvent(new NavigationBeginEvent(this));
|
Bukkit.getPluginManager().callEvent(new NavigationBeginEvent(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
@Override
|
||||||
|
public void run() {
|
||||||
if (!isNavigating())
|
if (!isNavigating())
|
||||||
return;
|
return;
|
||||||
if (!npc.isSpawned()) {
|
if (!npc.isSpawned()) {
|
||||||
|
@ -130,8 +130,8 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
|
|||||||
motX = motY = motZ = 0;
|
motX = motY = motZ = 0;
|
||||||
|
|
||||||
NMS.updateSenses(this);
|
NMS.updateSenses(this);
|
||||||
Navigation navigation = getNavigation();
|
|
||||||
if (npc.getNavigator().isNavigating()) {
|
if (npc.getNavigator().isNavigating()) {
|
||||||
|
Navigation navigation = getNavigation();
|
||||||
if (!navigation.f())
|
if (!navigation.f())
|
||||||
navigation.e();
|
navigation.e();
|
||||||
moveOnCurrentHeading();
|
moveOnCurrentHeading();
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package net.citizensnpcs.trait;
|
package net.citizensnpcs.trait;
|
||||||
|
|
||||||
import net.citizensnpcs.api.exception.NPCLoadException;
|
|
||||||
import net.citizensnpcs.api.persistence.Persist;
|
import net.citizensnpcs.api.persistence.Persist;
|
||||||
import net.citizensnpcs.api.trait.Trait;
|
import net.citizensnpcs.api.trait.Trait;
|
||||||
import net.citizensnpcs.api.util.DataKey;
|
|
||||||
import net.citizensnpcs.util.Messages;
|
import net.citizensnpcs.util.Messages;
|
||||||
import net.citizensnpcs.util.Messaging;
|
import net.citizensnpcs.util.Messaging;
|
||||||
|
|
||||||
@ -15,6 +13,7 @@ public class Age extends Trait implements Toggleable {
|
|||||||
private int age = 0;
|
private int age = 0;
|
||||||
@Persist
|
@Persist
|
||||||
private boolean locked = true;
|
private boolean locked = true;
|
||||||
|
private Ageable ageable;
|
||||||
|
|
||||||
public Age() {
|
public Age() {
|
||||||
super("age");
|
super("age");
|
||||||
@ -25,41 +24,37 @@ public class Age extends Trait implements Toggleable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isAgeable() {
|
private boolean isAgeable() {
|
||||||
return npc.getBukkitEntity() instanceof Ageable;
|
return ageable != null;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void load(DataKey key) throws NPCLoadException {
|
|
||||||
if (npc.isSpawned() && !(npc.getBukkitEntity() instanceof Ageable))
|
|
||||||
throw new NPCLoadException("NPC must be ageable");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSpawn() {
|
public void onSpawn() {
|
||||||
if (isAgeable()) {
|
if (npc.getBukkitEntity() instanceof Ageable) {
|
||||||
Ageable entity = (Ageable) npc.getBukkitEntity();
|
Ageable entity = (Ageable) npc.getBukkitEntity();
|
||||||
entity.setAge(age);
|
entity.setAge(age);
|
||||||
entity.setAgeLock(locked);
|
entity.setAgeLock(locked);
|
||||||
}
|
ageable = entity;
|
||||||
|
} else
|
||||||
|
ageable = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (!locked && isAgeable())
|
if (!locked && isAgeable())
|
||||||
age = ((Ageable) npc.getBukkitEntity()).getAge();
|
age = ageable.getAge();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAge(int age) {
|
public void setAge(int age) {
|
||||||
this.age = age;
|
this.age = age;
|
||||||
if (isAgeable())
|
if (isAgeable())
|
||||||
((Ageable) npc.getBukkitEntity()).setAge(age);
|
ageable.setAge(age);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean toggle() {
|
public boolean toggle() {
|
||||||
locked = !locked;
|
locked = !locked;
|
||||||
if (isAgeable())
|
if (isAgeable())
|
||||||
((Ageable) npc.getBukkitEntity()).setAgeLock(locked);
|
ageable.setAgeLock(locked);
|
||||||
return locked;
|
return locked;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ package net.citizensnpcs.trait;
|
|||||||
import net.citizensnpcs.api.persistence.Persist;
|
import net.citizensnpcs.api.persistence.Persist;
|
||||||
import net.citizensnpcs.api.trait.Trait;
|
import net.citizensnpcs.api.trait.Trait;
|
||||||
|
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.craftbukkit.entity.CraftEntity;
|
||||||
|
|
||||||
public class Gravity extends Trait implements Toggleable {
|
public class Gravity extends Trait implements Toggleable {
|
||||||
@Persist
|
@Persist
|
||||||
@ -21,9 +21,8 @@ public class Gravity extends Trait implements Toggleable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
if (!npc.isSpawned() || !enabled)
|
if (!npc.isSpawned() || !enabled)
|
||||||
return;
|
return;
|
||||||
Vector velocity = npc.getBukkitEntity().getVelocity();
|
net.minecraft.server.Entity entity = ((CraftEntity) npc.getBukkitEntity()).getHandle();
|
||||||
velocity.setY(Math.max(velocity.getY(), 0));
|
entity.motY = Math.max(0, entity.motY);
|
||||||
npc.getBukkitEntity().setVelocity(velocity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -6,7 +6,7 @@ import net.citizensnpcs.api.trait.Trait;
|
|||||||
import org.bukkit.entity.Skeleton;
|
import org.bukkit.entity.Skeleton;
|
||||||
|
|
||||||
public class NPCSkeletonType extends Trait {
|
public class NPCSkeletonType extends Trait {
|
||||||
private boolean skeleton;
|
private Skeleton skeleton;
|
||||||
@Persist
|
@Persist
|
||||||
private org.bukkit.entity.Skeleton.SkeletonType type = org.bukkit.entity.Skeleton.SkeletonType.NORMAL;
|
private org.bukkit.entity.Skeleton.SkeletonType type = org.bukkit.entity.Skeleton.SkeletonType.NORMAL;
|
||||||
|
|
||||||
@ -16,13 +16,13 @@ public class NPCSkeletonType extends Trait {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSpawn() {
|
public void onSpawn() {
|
||||||
skeleton = npc.getBukkitEntity() instanceof Skeleton;
|
skeleton = npc.getBukkitEntity() instanceof Skeleton ? (Skeleton) npc.getBukkitEntity() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (skeleton)
|
if (skeleton != null)
|
||||||
((Skeleton) npc.getBukkitEntity()).setSkeletonType(type);
|
skeleton.setSkeletonType(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setType(org.bukkit.entity.Skeleton.SkeletonType type) {
|
public void setType(org.bukkit.entity.Skeleton.SkeletonType type) {
|
||||||
|
@ -31,6 +31,7 @@ import org.bukkit.entity.Player;
|
|||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
import org.uncommons.maths.random.XORShiftRNG;
|
||||||
|
|
||||||
public class Text extends Trait implements Runnable, Toggleable, Listener, ConversationAbandonedListener {
|
public class Text extends Trait implements Runnable, Toggleable, Listener, ConversationAbandonedListener {
|
||||||
private final Map<String, Date> cooldowns = new HashMap<String, Date>();
|
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
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (!npc.isSpawned() || !talkClose)
|
if (!talkClose || !npc.isSpawned())
|
||||||
return;
|
return;
|
||||||
List<Entity> nearby = npc.getBukkitEntity().getNearbyEntities(range, range, range);
|
List<Entity> nearby = npc.getBukkitEntity().getNearbyEntities(range, range, range);
|
||||||
for (Entity search : nearby) {
|
for (Entity search : nearby) {
|
||||||
@ -140,7 +141,7 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
|
|||||||
return;
|
return;
|
||||||
// Add a cooldown if the text was successfully sent
|
// Add a cooldown if the text was successfully sent
|
||||||
Date wait = new Date();
|
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();
|
+ Setting.TALK_CLOSE_MINIMUM_COOLDOWN.asInt();
|
||||||
if (secondsDelta <= 0)
|
if (secondsDelta <= 0)
|
||||||
return;
|
return;
|
||||||
@ -150,6 +151,8 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Random RANDOM = new XORShiftRNG();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(DataKey key) {
|
public void save(DataKey key) {
|
||||||
key.setBoolean("talk-close", talkClose);
|
key.setBoolean("talk-close", talkClose);
|
||||||
|
@ -4,6 +4,7 @@ import java.lang.reflect.Constructor;
|
|||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
@ -34,6 +35,7 @@ import org.bukkit.entity.Player;
|
|||||||
import org.bukkit.material.Stairs;
|
import org.bukkit.material.Stairs;
|
||||||
import org.bukkit.material.Step;
|
import org.bukkit.material.Step;
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
|
import org.uncommons.maths.random.XORShiftRNG;
|
||||||
|
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
@ -259,11 +261,13 @@ public class NMS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void trySwim(EntityLiving handle, float power) {
|
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;
|
handle.motY += power;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Random RANDOM = new XORShiftRNG();
|
||||||
|
|
||||||
public static void updateAI(EntityLiving entity) {
|
public static void updateAI(EntityLiving entity) {
|
||||||
updateSenses(entity);
|
updateSenses(entity);
|
||||||
entity.getNavigation().e();
|
entity.getNavigation().e();
|
||||||
|
Loading…
Reference in New Issue
Block a user