mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-08-20 12:54:03 +02:00
Make XORShiftRNG more testable, fix SlimeSize constructor
This commit is contained in:
parent
9878500f18
commit
4019ee7bad
|
@ -34,19 +34,9 @@ import com.google.common.collect.Maps;
|
||||||
public class CommandContext {
|
public class CommandContext {
|
||||||
protected String[] args;
|
protected String[] args;
|
||||||
protected final Set<Character> flags = new HashSet<Character>();
|
protected final Set<Character> flags = new HashSet<Character>();
|
||||||
protected final Map<String, String> valueFlags = Maps.newHashMap();
|
|
||||||
private final CommandSender sender;
|
|
||||||
private Location location = null;
|
private Location location = null;
|
||||||
|
private final CommandSender sender;
|
||||||
public Location getSenderLocation() {
|
protected final Map<String, String> valueFlags = Maps.newHashMap();
|
||||||
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 CommandContext(CommandSender sender, String[] args) {
|
public CommandContext(CommandSender sender, String[] args) {
|
||||||
this.sender = sender;
|
this.sender = sender;
|
||||||
|
@ -202,6 +192,16 @@ public class CommandContext {
|
||||||
return slice;
|
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) {
|
public String[] getSlice(int index) {
|
||||||
String[] slice = new String[args.length - index];
|
String[] slice = new String[args.length - index];
|
||||||
System.arraycopy(args, index, slice, 0, args.length - index);
|
System.arraycopy(args, index, slice, 0, args.length - index);
|
||||||
|
|
|
@ -22,11 +22,11 @@ import net.citizensnpcs.trait.Controllable;
|
||||||
import net.citizensnpcs.trait.CurrentLocation;
|
import net.citizensnpcs.trait.CurrentLocation;
|
||||||
import net.citizensnpcs.trait.Gravity;
|
import net.citizensnpcs.trait.Gravity;
|
||||||
import net.citizensnpcs.trait.LookClose;
|
import net.citizensnpcs.trait.LookClose;
|
||||||
|
import net.citizensnpcs.trait.NPCSkeletonType;
|
||||||
import net.citizensnpcs.trait.Poses;
|
import net.citizensnpcs.trait.Poses;
|
||||||
import net.citizensnpcs.trait.Powered;
|
import net.citizensnpcs.trait.Powered;
|
||||||
import net.citizensnpcs.trait.Saddle;
|
import net.citizensnpcs.trait.Saddle;
|
||||||
import net.citizensnpcs.trait.Sheared;
|
import net.citizensnpcs.trait.Sheared;
|
||||||
import net.citizensnpcs.trait.NPCSkeletonType;
|
|
||||||
import net.citizensnpcs.trait.SlimeSize;
|
import net.citizensnpcs.trait.SlimeSize;
|
||||||
import net.citizensnpcs.trait.VillagerProfession;
|
import net.citizensnpcs.trait.VillagerProfession;
|
||||||
import net.citizensnpcs.trait.WoolColor;
|
import net.citizensnpcs.trait.WoolColor;
|
||||||
|
|
|
@ -83,11 +83,6 @@ public class NPCSelector implements Listener {
|
||||||
npc.data().remove("selectors");
|
npc.data().remove("selectors");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeMetadata(Metadatable metadatable) {
|
|
||||||
if (metadatable != null)
|
|
||||||
metadatable.removeMetadata("selected", plugin);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onNPCRightClick(NPCRightClickEvent event) {
|
public void onNPCRightClick(NPCRightClickEvent event) {
|
||||||
Player player = event.getClicker();
|
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) {
|
public void select(CommandSender sender, NPC npc) {
|
||||||
// Remove existing selection if any
|
// Remove existing selection if any
|
||||||
List<String> selectors = npc.data().get("selectors");
|
List<String> selectors = npc.data().get("selectors");
|
||||||
|
@ -131,15 +131,15 @@ public class NPCSelector implements Listener {
|
||||||
Bukkit.getPluginManager().callEvent(new NPCSelectEvent(npc, sender));
|
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) {
|
private void setMetadata(NPC npc, Metadatable metadatable) {
|
||||||
if (metadatable.hasMetadata("selected"))
|
if (metadatable.hasMetadata("selected"))
|
||||||
metadatable.removeMetadata("selected", plugin);
|
metadatable.removeMetadata("selected", plugin);
|
||||||
|
|
||||||
metadatable.setMetadata("selected", new FixedMetadataValue(plugin, npc.getId()));
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
public void save(DataKey root) {
|
||||||
root.setDouble("speed", defaultParams.baseSpeed());
|
root.setDouble("speed", defaultParams.baseSpeed());
|
||||||
root.setDouble("pathfindingrange", defaultParams.range());
|
root.setDouble("pathfindingrange", defaultParams.range());
|
||||||
|
@ -184,31 +209,6 @@ public class CitizensNavigator implements Navigator, Runnable {
|
||||||
Bukkit.getPluginManager().callEvent(new NavigationBeginEvent(this));
|
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() {
|
private void updatePathfindingRange() {
|
||||||
NMS.updatePathfindingRange(npc, localParams.range());
|
NMS.updatePathfindingRange(npc, localParams.range());
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,9 @@ import org.bukkit.entity.Ageable;
|
||||||
public class Age extends Trait implements Toggleable {
|
public class Age extends Trait implements Toggleable {
|
||||||
@Persist
|
@Persist
|
||||||
private int age = 0;
|
private int age = 0;
|
||||||
|
private Ageable ageable;
|
||||||
@Persist
|
@Persist
|
||||||
private boolean locked = true;
|
private boolean locked = true;
|
||||||
private Ageable ageable;
|
|
||||||
|
|
||||||
public Age() {
|
public Age() {
|
||||||
super("age");
|
super("age");
|
||||||
|
|
|
@ -13,7 +13,7 @@ public class SlimeSize extends Trait {
|
||||||
private int size = 3;
|
private int size = 3;
|
||||||
private boolean slime;
|
private boolean slime;
|
||||||
|
|
||||||
protected SlimeSize(String name) {
|
public SlimeSize() {
|
||||||
super("slimesize");
|
super("slimesize");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,6 @@ 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>();
|
||||||
|
@ -151,8 +150,6 @@ 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);
|
||||||
|
@ -228,4 +225,6 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
|
||||||
builder.append("}");
|
builder.append("}");
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Random RANDOM = Util.getFastRandom();
|
||||||
}
|
}
|
|
@ -35,7 +35,6 @@ 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;
|
||||||
|
@ -56,9 +55,11 @@ public class NMS {
|
||||||
private static Field NAVIGATION_WORLD_FIELD;
|
private static Field NAVIGATION_WORLD_FIELD;
|
||||||
private static Field PATHFINDING_RANGE;
|
private static Field PATHFINDING_RANGE;
|
||||||
private static Field PERSISTENT_FIELD;
|
private static Field PERSISTENT_FIELD;
|
||||||
|
private static final Random RANDOM = Util.getFastRandom();
|
||||||
private static Set<Integer> SLAB_MATERIALS = Sets.newHashSet();
|
private static Set<Integer> SLAB_MATERIALS = Sets.newHashSet();
|
||||||
private static Field SPEED_FIELD;
|
private static Field SPEED_FIELD;
|
||||||
private static Set<Integer> STAIR_MATERIALS = Sets.newHashSet();
|
private static Set<Integer> STAIR_MATERIALS = Sets.newHashSet();
|
||||||
|
|
||||||
private static Field THREAD_STOPPER;
|
private static Field THREAD_STOPPER;
|
||||||
|
|
||||||
public static void addOrRemoveFromPlayerList(LivingEntity bukkitEntity, boolean remove) {
|
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) {
|
public static void updateAI(EntityLiving entity) {
|
||||||
updateSenses(entity);
|
updateSenses(entity);
|
||||||
entity.getNavigation().e();
|
entity.getNavigation().e();
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package net.citizensnpcs.util;
|
package net.citizensnpcs.util;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
import net.citizensnpcs.api.event.NPCCollisionEvent;
|
import net.citizensnpcs.api.event.NPCCollisionEvent;
|
||||||
import net.citizensnpcs.api.event.NPCPushEvent;
|
import net.citizensnpcs.api.event.NPCPushEvent;
|
||||||
import net.citizensnpcs.api.npc.NPC;
|
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) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user