Skip to content

Commit

Permalink
Cleanup and performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Apr 2, 2013
1 parent c345b68 commit d603aca
Show file tree
Hide file tree
Showing 32 changed files with 174 additions and 380 deletions.
Expand Up @@ -140,7 +140,7 @@ public boolean contains(String stringValue) {
if (val.equalsIgnoreCase(stringValue)) return true;
try {
if (Double.valueOf(val).equals(Double.valueOf(stringValue))) return true;
} catch (Exception e) { /* Not a valid number, continue. */ }
} catch (NumberFormatException e) { /* Not a valid number, continue. */ }
}

return false;
Expand Down
Expand Up @@ -24,8 +24,8 @@

public class ListenerRegistry implements DenizenRegistry, Listener {

private Map<String, Map<String, AbstractListener>> listeners = new ConcurrentHashMap<String, Map<String, AbstractListener>>();
private Map<String, AbstractListenerType> types = new ConcurrentHashMap<String, AbstractListenerType>();
private Map<String, Map<String, AbstractListener>> listeners = new ConcurrentHashMap<String, Map<String, AbstractListener>>(8, 0.9f, 1);
private Map<String, AbstractListenerType> types = new ConcurrentHashMap<String, AbstractListenerType>(8, 0.9f, 1);

private Denizen denizen;

Expand Down
Expand Up @@ -100,22 +100,22 @@ public void listenItem(InventoryClickEvent event)
if (!WorldGuardUtilities.checkPlayerWGRegion(player, region)) return;

// Get the item in the result slot as an ItemStack
ItemStack item = new ItemStack(event.getCurrentItem());
final ItemStack item = new ItemStack(event.getCurrentItem());

if (event.isShiftClick())
{
// Save the quantity of items of this type that the player had
// before the event took place
int initialQty = Utilities.countItems(item, player.getInventory());
final int initialQty = Utilities.countItems(item, player.getInventory());

// Run a task 1 tick later, after the event has occurred, and
// see how many items of this type the player has then in the
// inventory
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(),
new Runnable2<ItemStack, Integer>(item, initialQty)
new Runnable()
{
@Override
public void run(ItemStack item, Integer initialQty)
public void run()
{
int newQty = Utilities.countItems(item, player.getInventory());
int difference = newQty - initialQty;
Expand Down
Expand Up @@ -48,12 +48,13 @@ public boolean addGoal(dNPC dNPC, String[] arguments, int priority) {
return false;
}

List<TaskGoal> taskGoals = new ArrayList<TaskGoal>();
if (taskMap.containsKey(dNPC))
taskGoals = taskMap.get(dNPC);
List<TaskGoal> taskGoals = taskMap.get(dNPC);
if (taskGoals == null) {
taskGoals = new ArrayList<TaskGoal>();
taskMap.put(dNPC, taskGoals);
}
taskGoals.add(0, new TaskGoal(dNPC, delay, duration, script, repeats, this));
taskMap.put(dNPC, taskGoals);
dNPC.getCitizen().getDefaultGoalController().addGoal(taskMap.get(dNPC).get(0), priority);
dNPC.getCitizen().getDefaultGoalController().addGoal(taskGoals.get(0), priority);
return true;
}

Expand Down
Expand Up @@ -81,9 +81,9 @@ else if (aH.matchesLocation(arg)) {
}

public boolean removeGoal(dNPC npc, boolean verbose) {
if (wanderMap.containsKey(npc.getCitizen())) {
npc.getCitizen().getDefaultGoalController().removeGoal(wanderMap.get(npc.getCitizen()));
wanderMap.remove(npc.getCitizen());
WanderGoal goal = wanderMap.remove(npc.getCitizen());
if (goal != null) {
npc.getCitizen().getDefaultGoalController().removeGoal(goal);
if (verbose) dB.echoDebug("Removed Wander Activities from NPC.");
return true;
}
Expand Down
Expand Up @@ -66,17 +66,14 @@ public void run(GoalSelector goalSelecter) {
// If not already navigating.. let's find a new block to navigate to.
dNPC.getNavigator().getDefaultParameters().speedModifier(speed);
wanderLocation = wA.getNewLocation(X, Y, Z, world, radius, depth);

Location checkLocation = new Location(wanderLocation.getWorld(), wanderLocation.getX(), wanderLocation.getY() + 2, wanderLocation.getZ());

if (checkLocation.getBlock().getType() != Material.AIR) {
if (wanderLocation.getWorld().getBlockAt(wanderLocation.getBlockX(), wanderLocation.getBlockY() + 2, wanderLocation.getBlockZ()).getType() != Material.AIR) {
wanderLocation = wA.getNewLocation(X, Y, Z, world, radius, depth);
goalSelecter.finish();
return;
}

if (!materials.isEmpty()) {
Boolean move = false;
boolean move = false;
for (Material acceptableMaterial : materials)
if (wanderLocation.getBlock().getType() == acceptableMaterial) move = true;
if (materialIds.contains(Integer.valueOf(wanderLocation.getBlock().getTypeId()))) move = true;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/aufdemrand/denizen/npc/dNPC.java
Expand Up @@ -25,6 +25,7 @@
public class dNPC {

private int npcid;
private final org.bukkit.Location locationCache = new org.bukkit.Location(null, 0, 0, 0);

public dNPC(NPC citizensNPC) {
this.npcid = citizensNPC.getId();
Expand Down Expand Up @@ -80,7 +81,7 @@ public InteractScriptContainer getInteractScriptQuietly(Player player, Class<? e

public Location getLocation() {
if (isSpawned()) return
new Location(getCitizen().getBukkitEntity().getLocation());
new Location(getCitizen().getBukkitEntity().getLocation(locationCache));
else return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/npc/dNPCRegistry.java
Expand Up @@ -27,7 +27,7 @@
*/
public class dNPCRegistry implements Listener {

private static Map<Integer, dNPC> denizenNPCs = new ConcurrentHashMap<Integer, dNPC>();
private static Map<Integer, dNPC> denizenNPCs = new ConcurrentHashMap<Integer, dNPC>(8, 0.9f, 1);

public static dNPCRegistry getCurrentInstance() {
return DenizenAPI.getCurrentInstance().getNPCRegistry();
Expand Down
Expand Up @@ -42,7 +42,7 @@ public class NameplateTrait extends Trait implements Listener {
private String text = null;

@Persist(value="colors", collectionType=ConcurrentHashMap.class)
private Map<String, ChatColor> colors = new ConcurrentHashMap<String, ChatColor>();
private Map<String, ChatColor> colors = new ConcurrentHashMap<String, ChatColor>(8, 0.9f, 1);

public NameplateTrait() {
super("nameplate");
Expand Down
Expand Up @@ -21,13 +21,13 @@
public class TriggerTrait extends Trait implements Listener {

@Persist(value="enabled", collectionType=ConcurrentHashMap.class)
private Map<String, Boolean> enabled = new ConcurrentHashMap<String, Boolean>();
private Map<String, Boolean> enabled = new ConcurrentHashMap<String, Boolean>(8, 0.9f, 1);
@Persist(value="duration", collectionType=ConcurrentHashMap.class)
private Map<String, Double> duration = new ConcurrentHashMap<String, Double>();
private Map<String, Double> duration = new ConcurrentHashMap<String, Double>(8, 0.9f, 1);
@Persist(value="cooldowntype", collectionType=ConcurrentHashMap.class)
private Map<String, CooldownType> type = new ConcurrentHashMap<String, CooldownType>();
private Map<String, CooldownType> type = new ConcurrentHashMap<String, CooldownType>(8, 0.9f, 1);
@Persist(value="radius", collectionType=ConcurrentHashMap.class)
private Map<String, Integer> radius = new ConcurrentHashMap<String, Integer>();
private Map<String, Integer> radius = new ConcurrentHashMap<String, Integer>(8, 0.9f, 1);

public void report() {
dB.echoDebug("enabled: " + enabled.entrySet().toString());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/aufdemrand/denizen/scripts/ScriptQueue.java
Expand Up @@ -40,7 +40,7 @@ public static String _getNextId() {
}

// Contains all currently active queues, keyed by a String id.
public static Map<String, ScriptQueue> _queues = new ConcurrentHashMap<String, ScriptQueue>();
public static Map<String, ScriptQueue> _queues = new ConcurrentHashMap<String, ScriptQueue>(8, 0.9f, 1);


/**
Expand Down Expand Up @@ -123,7 +123,7 @@ public static boolean _queueExists(String id) {

// ScriptQueues can have a bit of context, keyed by a String Id. All that can be accessed by either getContext()
// or a dScript replaceable tag <context.id>
protected Map<String, String> context = new ConcurrentHashMap<String, String>();
protected Map<String, String> context = new ConcurrentHashMap<String, String>(8, 0.9f, 1);

protected ScriptEntry lastEntryExecuted = null;

Expand Down
Expand Up @@ -16,7 +16,7 @@
*/
public class DetermineCommand extends AbstractCommand {

private static Map<Long, String> outcomes = new ConcurrentHashMap<Long, String>();
private static Map<Long, String> outcomes = new ConcurrentHashMap<Long, String>(8, 0.9f, 1);

public static long uniqueId = 0;

Expand Down
Expand Up @@ -31,9 +31,9 @@ public void onEnable() {
denizen.getServer().getPluginManager().registerEvents(this, denizen);
}

private Map<String, Item> itemStacks = new ConcurrentHashMap<String, Item>();
private Map<String, LivingEntity> entities = new ConcurrentHashMap<String, LivingEntity>();
private Map<String, Integer> npcs = new ConcurrentHashMap<String, Integer>();
private Map<String, Item> itemStacks = new ConcurrentHashMap<String, Item>(8, 0.9f, 1);
private Map<String, LivingEntity> entities = new ConcurrentHashMap<String, LivingEntity>(8, 0.9f, 1);
private Map<String, Integer> npcs = new ConcurrentHashMap<String, Integer>(8, 0.9f, 1);

/**
* Gets a currently saved ItemStack, created with Denizen's NEW command, given a
Expand Down
Expand Up @@ -30,7 +30,7 @@ public class PauseCommand extends AbstractCommand {
*
*/

private Map<String, Integer> durations = new ConcurrentHashMap<String, Integer>();
private Map<String, Integer> durations = new ConcurrentHashMap<String, Integer>(8, 0.9f, 1);
enum PauseType { ACTIVITY, WAYPOINTS, NAVIGATION }

int duration;
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.bukkit.entity.Firework;
import org.bukkit.entity.Projectile;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;

/**
Expand Down Expand Up @@ -98,27 +99,26 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
scriptEntry.addObject("fireworks", fireworks);
}

@SuppressWarnings("rawtypes")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects

Location location = (Location) scriptEntry.getObject("location");
final Location location = scriptEntry.hasObject("location") ? (Location) scriptEntry.getObject("location") : (Location) scriptEntry.getNPC().getEyeLocation().getDirection().
multiply(4).toLocation(scriptEntry.getNPC().getWorld());
EntityType entityType = (EntityType) scriptEntry.getObject("entityType");
Boolean ride = (Boolean) scriptEntry.getObject("ride");
Boolean burn = (Boolean) scriptEntry.getObject("burn");

if (location == null)
{
location = (Location) scriptEntry.getNPC().getEyeLocation().getDirection().
multiply(4).toLocation(scriptEntry.getNPC().getWorld());

}
else
{
Utilities.faceLocation(scriptEntry.getNPC().getCitizen().getBukkitEntity(), location);
}

Entity entity = scriptEntry.getNPC().getWorld().spawnEntity(
final Entity entity = scriptEntry.getNPC().getWorld().spawnEntity(
scriptEntry.getNPC().getEyeLocation().add(
scriptEntry.getNPC().getEyeLocation().getDirection())
.subtract(0, 0.4, 0),
Expand All @@ -141,13 +141,13 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
((Projectile) entity).setShooter(scriptEntry.getNPC().getCitizen().getBukkitEntity());
}

Runnable3 task = new Runnable3<ScriptEntry, Entity, Location>
(scriptEntry, entity, location)
BukkitRunnable task = new BukkitRunnable()
{
int runs = 0;
@Override
public void run(ScriptEntry scriptEntry, Entity entity, Location location) {
public void run() {

if (getRuns() < 40 && entity.isValid())
if (runs < 40 && entity.isValid())
{
//dB.echoDebug(entity.getType().name() + " flying time " + getRuns() + " in task " + getId());

Expand All @@ -156,18 +156,18 @@ public void run(ScriptEntry scriptEntry, Entity entity, Location location) {
Vector v3 = v2.clone().subtract(v1).normalize().multiply(1.5);

entity.setVelocity(v3);
addRuns();
runs++;

if (Math.abs(v2.getX() - v1.getX()) < 2 && Math.abs(v2.getY() - v1.getY()) < 2
&& Math.abs(v2.getZ() - v1.getZ()) < 2)
{
setRuns(40);
runs = 40;
}
}
else
{
this.cancel();
clearRuns();
runs = 0;

if (scriptEntry.getObject("script") != null)
{
Expand All @@ -194,7 +194,7 @@ public void run(ScriptEntry scriptEntry, Entity entity, Location location) {
}
};

task.setId(Bukkit.getScheduler().scheduleSyncRepeatingTask(denizen, task, 0, 2));
task.runTaskTimer(denizen, 0, 2);
}

}
Expand Up @@ -47,7 +47,7 @@ public void onEnable() {

private enum SwitchState { ON, OFF, TOGGLE }

private Map<Location, Integer> taskMap = new ConcurrentHashMap<Location, Integer>();
private Map<Location, Integer> taskMap = new ConcurrentHashMap<Location, Integer>(8, 0.9f, 1);

SwitchState switchState;
Location interactLocation;
Expand Down
Expand Up @@ -80,7 +80,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
}

//"PlayerName,ScriptName", TaskID
private static Map<String, Integer> durations = new ConcurrentHashMap<String, Integer>();
private static Map<String, Integer> durations = new ConcurrentHashMap<String, Integer>(8, 0.9f, 1);

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Expand Down
Expand Up @@ -35,7 +35,7 @@

public class WorldScriptHelper implements Listener {

public static Map<String, WorldScriptContainer> world_scripts = new ConcurrentHashMap<String, WorldScriptContainer>();
public static Map<String, WorldScriptContainer> world_scripts = new ConcurrentHashMap<String, WorldScriptContainer>(8, 0.9f, 1);

public WorldScriptHelper() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
Expand Down
Expand Up @@ -28,9 +28,9 @@ public enum CooldownType { NPC, PLAYER }
private Map<String, AbstractTrigger> instances = new HashMap<String, AbstractTrigger>();
private Map<Class<? extends AbstractTrigger>, String> classes = new HashMap<Class<? extends AbstractTrigger>, String>();

Map<Integer, Map<String, Long>> npcCooldown = new ConcurrentHashMap<Integer, Map<String,Long>>();
Map<Integer, Map<String, Long>> npcCooldown = new ConcurrentHashMap<Integer, Map<String,Long>>(8, 0.9f, 1);

Map<String, Map<String, Long>> playerCooldown = new ConcurrentHashMap<String, Map<String,Long>>();
Map<String, Map<String, Long>> playerCooldown = new ConcurrentHashMap<String, Map<String,Long>>(8, 0.9f, 1);

public TriggerRegistry(Denizen denizen) {
this.denizen = denizen;
Expand Down
Expand Up @@ -41,10 +41,9 @@ public boolean matches(Location location) {
}

public int getDistance(Player player) {
// TODO: get distance
return 0;
Location loc = player.getLocation();
return (int) Math.sqrt(Math.pow(loc.getX() - x, 2) + Math.pow(loc.getY() - y, 2) + Math.pow(loc.getZ() - z, 2));
}

}


Expand Down Expand Up @@ -102,7 +101,7 @@ public InteractContext(NPC npc, String scriptName, String stepName) {
//
// Stores all the Location Trigger points with context
//
private Map<Trigger, LocationContext> locationTriggers = new ConcurrentHashMap<Trigger, LocationContext>();
private Map<Trigger, LocationContext> locationTriggers = new ConcurrentHashMap<Trigger, LocationContext>(8, 0.9f, 1);


//
Expand Down

0 comments on commit d603aca

Please sign in to comment.