From 0f3d21c1ec7fe4408d1a3565e9daf949d29b4d68 Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Tue, 10 Sep 2019 15:45:26 -0500 Subject: [PATCH] sync lock health updates --- .../ultimatestacker/entity/EntityStack.java | 17 ++++++++---- .../ultimatestacker/tasks/StackingTask.java | 26 ++++++++++++------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java b/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java index 5fded6c..93b3d77 100644 --- a/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java +++ b/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java @@ -25,7 +25,8 @@ public class EntityStack { private UUID entity; private int amount; - private Deque health = new ArrayDeque<>(); + private final Deque health = new ArrayDeque<>(); + final Object healthLock = new Object(); UltimateStacker plugin = UltimateStacker.getInstance(); public EntityStack(LivingEntity entity, int amount) { @@ -192,16 +193,22 @@ public class EntityStack { public void updateHealth(LivingEntity entity) { if (entity == null) return; - entity.setHealth(Settings.STACK_ENTITY_HEALTH.getBoolean() - && !this.health.isEmpty() ? this.health.removeFirst() : entity.getMaxHealth()); + synchronized (healthLock) { + entity.setHealth(Settings.STACK_ENTITY_HEALTH.getBoolean() + && !this.health.isEmpty() ? this.health.removeFirst() : entity.getMaxHealth()); + } } public void addHealth(double health) { - this.health.addLast(health); + synchronized(healthLock) { + this.health.addLast(health); + } } public void mergeHealth(EntityStack stack) { - this.health.addAll(stack.health); + synchronized(healthLock) { + this.health.addAll(stack.health); + } } @Override diff --git a/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java b/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java index 2094794..37556af 100644 --- a/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java +++ b/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java @@ -19,16 +19,17 @@ public class StackingTask extends BukkitRunnable { private final UltimateStacker plugin; - private EntityStackManager stackManager; + private final EntityStackManager stackManager; ConfigurationSection configurationSection = UltimateStacker.getInstance().getMobFile(); - private List processed = new ArrayList<>(); + private final List processed = new ArrayList<>(); - private int maxEntityStackSize = Settings.MAX_STACK_ENTITIES.getInt(); - private int minEntityStackSize = Settings.MIN_STACK_ENTITIES.getInt(); - - private int maxPerTypeStacksPerChunk = Settings.MAX_PER_TYPE_STACKS_PER_CHUNK.getInt(); + private final HashMap entityStackSizes = new HashMap(); + private final int maxEntityStackSize = Settings.MAX_STACK_ENTITIES.getInt(); + private final int minEntityStackSize = Settings.MIN_STACK_ENTITIES.getInt(); + private final int maxPerTypeStacksPerChunk = Settings.MAX_PER_TYPE_STACKS_PER_CHUNK.getInt(); + private final List disabledWorlds = Settings.DISABLED_WORLDS.getStringList(); public StackingTask(UltimateStacker plugin) { this.plugin = plugin; @@ -76,7 +77,6 @@ public class StackingTask extends BukkitRunnable { } public boolean isWorldDisabled(World world) { - List disabledWorlds = Settings.DISABLED_WORLDS.getStringList(); return disabledWorlds.stream().anyMatch(worldStr -> world.getName().equalsIgnoreCase(worldStr)); } @@ -290,8 +290,14 @@ public class StackingTask extends BukkitRunnable { } private int getEntityStackSize(LivingEntity initialEntity) { - if (configurationSection.getInt("Mobs." + initialEntity.getType().name() + ".Max Stack Size") != -1) - maxEntityStackSize = configurationSection.getInt("Mobs." + initialEntity.getType().name() + ".Max Stack Size"); - return maxEntityStackSize; + Integer max = entityStackSizes.get(initialEntity.getType()); + if(max == null) { + max = configurationSection.getInt("Mobs." + initialEntity.getType().name() + ".Max Stack Size"); + if(max == -1) { + max = maxEntityStackSize; + } + entityStackSizes.put(initialEntity.getType(), max); + } + return max; } }