From 5c6cd57a4d9526e8c0c88fdac574256728c38096 Mon Sep 17 00:00:00 2001 From: riking Date: Fri, 30 Aug 2013 18:40:48 -0700 Subject: [PATCH 1/2] Use a MutableBoolean to synchronize on instead of a Boolean The code synchronizes on a boxed primitive constant, such as an Boolean. Since there normally exist only two Boolean objects, this code could be synchronizing on the same object as other, unrelated code, leading to unresponsiveness and possible deadlock. This commit uses the MutableBoolean class from commons-lang, which is included in bukkit.jar. --- src/com/massivecraft/mcore/InternalListener.java | 2 +- src/com/massivecraft/mcore/util/SmokeUtil.java | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/com/massivecraft/mcore/InternalListener.java b/src/com/massivecraft/mcore/InternalListener.java index c15a00b6..f28ac6b2 100644 --- a/src/com/massivecraft/mcore/InternalListener.java +++ b/src/com/massivecraft/mcore/InternalListener.java @@ -154,7 +154,7 @@ public class InternalListener implements Listener if (cause != DamageCause.BLOCK_EXPLOSION) return; // ... and that explosion is a fake ... - if (SmokeUtil.fakeExplosion == false) return; + if (!SmokeUtil.fakeExplosion.booleanValue()) return; // ... then cancel the event and the damage. event.setCancelled(true); diff --git a/src/com/massivecraft/mcore/util/SmokeUtil.java b/src/com/massivecraft/mcore/util/SmokeUtil.java index b8a6886e..370519c0 100644 --- a/src/com/massivecraft/mcore/util/SmokeUtil.java +++ b/src/com/massivecraft/mcore/util/SmokeUtil.java @@ -3,6 +3,7 @@ package com.massivecraft.mcore.util; import java.util.Collection; import java.util.Random; +import org.apache.commons.lang.mutable.MutableBoolean; import org.bukkit.Effect; import org.bukkit.Location; @@ -89,14 +90,14 @@ public class SmokeUtil fakeExplosion(location, 4F); } - public static Boolean fakeExplosion = false; + public static MutableBoolean fakeExplosion = new MutableBoolean(false); public static void fakeExplosion(Location location, float power) { synchronized (fakeExplosion) { - fakeExplosion = true; + fakeExplosion.setValue(true); location.getWorld().createExplosion(location.getX(), location.getY(), location.getZ(), power, false, false); - fakeExplosion = false; + fakeExplosion.setValue(false); } } From fe6ce0103794950835fc10be2ec9e1153b6860a0 Mon Sep 17 00:00:00 2001 From: riking Date: Fri, 30 Aug 2013 18:45:57 -0700 Subject: [PATCH 2/2] Define hashCode() for TimeUnit as it is being used in a HashMap TimeUnit is being used as the key to a map in TimeDiffUtil, so we must define a hashCode() consistent with its equals() method. The implementation is the same as java.lang.Long.hashCode(). --- src/com/massivecraft/mcore/util/TimeUnit.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/com/massivecraft/mcore/util/TimeUnit.java b/src/com/massivecraft/mcore/util/TimeUnit.java index b008654f..9411b9c6 100644 --- a/src/com/massivecraft/mcore/util/TimeUnit.java +++ b/src/com/massivecraft/mcore/util/TimeUnit.java @@ -148,4 +148,10 @@ public class TimeUnit implements Comparable return this.millis == ((TimeUnit)other).millis; } + @Override + public final int hashCode() + { + return (int)(this.millis^(this.millis>>>32)); + } + }