Make XORShiftRNG more testable, fix SlimeSize constructor

This commit is contained in:
fullwall 2012-11-29 14:53:42 +08:00
parent 9878500f18
commit 4019ee7bad
9 changed files with 72 additions and 56 deletions

View File

@ -34,19 +34,9 @@ import com.google.common.collect.Maps;
public class CommandContext {
protected String[] args;
protected final Set<Character> flags = new HashSet<Character>();
protected final Map<String, String> valueFlags = Maps.newHashMap();
private final CommandSender sender;
private Location location = null;
public Location getSenderLocation() {
if (location != null)
return location;
if (sender instanceof Player)
location = ((Player) sender).getLocation();
else if (sender instanceof BlockCommandSender)
location = ((BlockCommandSender) sender).getBlock().getLocation();
return location;
}
private final CommandSender sender;
protected final Map<String, String> valueFlags = Maps.newHashMap();
public CommandContext(CommandSender sender, String[] args) {
this.sender = sender;
@ -202,6 +192,16 @@ public class CommandContext {
return slice;
}
public Location getSenderLocation() {
if (location != null)
return location;
if (sender instanceof Player)
location = ((Player) sender).getLocation();
else if (sender instanceof BlockCommandSender)
location = ((BlockCommandSender) sender).getBlock().getLocation();
return location;
}
public String[] getSlice(int index) {
String[] slice = new String[args.length - index];
System.arraycopy(args, index, slice, 0, args.length - index);

View File

@ -22,11 +22,11 @@ import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.NPCSkeletonType;
import net.citizensnpcs.trait.Poses;
import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.trait.Saddle;
import net.citizensnpcs.trait.Sheared;
import net.citizensnpcs.trait.NPCSkeletonType;
import net.citizensnpcs.trait.SlimeSize;
import net.citizensnpcs.trait.VillagerProfession;
import net.citizensnpcs.trait.WoolColor;

View File

@ -83,11 +83,6 @@ public class NPCSelector implements Listener {
npc.data().remove("selectors");
}
private void removeMetadata(Metadatable metadatable) {
if (metadatable != null)
metadatable.removeMetadata("selected", plugin);
}
@EventHandler
public void onNPCRightClick(NPCRightClickEvent event) {
Player player = event.getClicker();
@ -105,6 +100,11 @@ public class NPCSelector implements Listener {
}
}
private void removeMetadata(Metadatable metadatable) {
if (metadatable != null)
metadatable.removeMetadata("selected", plugin);
}
public void select(CommandSender sender, NPC npc) {
// Remove existing selection if any
List<String> selectors = npc.data().get("selectors");
@ -131,15 +131,15 @@ public class NPCSelector implements Listener {
Bukkit.getPluginManager().callEvent(new NPCSelectEvent(npc, sender));
}
private String toName(Block block) {
return '@' + block.getWorld().getName() + ":" + Integer.toString(block.getX()) + ":"
+ Integer.toString(block.getY()) + ":" + Integer.toString(block.getZ());
}
private void setMetadata(NPC npc, Metadatable metadatable) {
if (metadatable.hasMetadata("selected"))
metadatable.removeMetadata("selected", plugin);
metadatable.setMetadata("selected", new FixedMetadataValue(plugin, npc.getId()));
}
private String toName(Block block) {
return '@' + block.getWorld().getName() + ":" + Integer.toString(block.getX()) + ":"
+ Integer.toString(block.getY()) + ":" + Integer.toString(block.getZ());
}
}

View File

@ -106,6 +106,31 @@ public class CitizensNavigator implements Navigator, Runnable {
}
}
@Override
public void run() {
if (!isNavigating())
return;
if (!npc.isSpawned()) {
stopNavigating(CancelReason.NPC_DESPAWNED);
return;
}
if (updateStationaryStatus())
return;
updatePathfindingRange();
boolean finished = executing.update();
if (!finished)
return;
if (executing.getCancelReason() != null) {
stopNavigating(executing.getCancelReason());
} else {
NavigationCompleteEvent event = new NavigationCompleteEvent(this);
PathStrategy old = executing;
Bukkit.getPluginManager().callEvent(event);
if (old == executing)
stopNavigating();
}
}
public void save(DataKey root) {
root.setDouble("speed", defaultParams.baseSpeed());
root.setDouble("pathfindingrange", defaultParams.range());
@ -184,31 +209,6 @@ public class CitizensNavigator implements Navigator, Runnable {
Bukkit.getPluginManager().callEvent(new NavigationBeginEvent(this));
}
@Override
public void run() {
if (!isNavigating())
return;
if (!npc.isSpawned()) {
stopNavigating(CancelReason.NPC_DESPAWNED);
return;
}
if (updateStationaryStatus())
return;
updatePathfindingRange();
boolean finished = executing.update();
if (!finished)
return;
if (executing.getCancelReason() != null) {
stopNavigating(executing.getCancelReason());
} else {
NavigationCompleteEvent event = new NavigationCompleteEvent(this);
PathStrategy old = executing;
Bukkit.getPluginManager().callEvent(event);
if (old == executing)
stopNavigating();
}
}
private void updatePathfindingRange() {
NMS.updatePathfindingRange(npc, localParams.range());
}

View File

@ -11,9 +11,9 @@ import org.bukkit.entity.Ageable;
public class Age extends Trait implements Toggleable {
@Persist
private int age = 0;
private Ageable ageable;
@Persist
private boolean locked = true;
private Ageable ageable;
public Age() {
super("age");

View File

@ -13,7 +13,7 @@ public class SlimeSize extends Trait {
private int size = 3;
private boolean slime;
protected SlimeSize(String name) {
public SlimeSize() {
super("slimesize");
}

View File

@ -31,7 +31,6 @@ 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>();
@ -151,8 +150,6 @@ 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);
@ -228,4 +225,6 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
builder.append("}");
return builder.toString();
}
private static Random RANDOM = Util.getFastRandom();
}

View File

@ -35,7 +35,6 @@ 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;
@ -56,9 +55,11 @@ public class NMS {
private static Field NAVIGATION_WORLD_FIELD;
private static Field PATHFINDING_RANGE;
private static Field PERSISTENT_FIELD;
private static final Random RANDOM = Util.getFastRandom();
private static Set<Integer> SLAB_MATERIALS = Sets.newHashSet();
private static Field SPEED_FIELD;
private static Set<Integer> STAIR_MATERIALS = Sets.newHashSet();
private static Field THREAD_STOPPER;
public static void addOrRemoveFromPlayerList(LivingEntity bukkitEntity, boolean remove) {
@ -266,8 +267,6 @@ public class NMS {
}
}
private static final Random RANDOM = new XORShiftRNG();
public static void updateAI(EntityLiving entity) {
updateSenses(entity);
entity.getNavigation().e();

View File

@ -1,5 +1,7 @@
package net.citizensnpcs.util;
import java.util.Random;
import net.citizensnpcs.api.event.NPCCollisionEvent;
import net.citizensnpcs.api.event.NPCPushEvent;
import net.citizensnpcs.api.npc.NPC;
@ -133,4 +135,20 @@ public class Util {
}
}
}
public static Random getFastRandom() {
try {
return (Random) RNG_CLASS.newInstance();
} catch (Exception e) {
return new Random();
}
}
private static Class<?> RNG_CLASS = null;
static {
try {
RNG_CLASS = Class.forName("org.uncommons.maths.random.XORShiftRNG");
} catch (ClassNotFoundException e) {
}
}
}