From db7f3c770d387bf26ebfb04a0b209f26dcd452c0 Mon Sep 17 00:00:00 2001 From: md_5 Date: Tue, 4 Mar 2014 20:49:53 +1100 Subject: [PATCH] Fix EventBus when used with Byte.MAX_PRIORITY - closes #910. Also includes additional unit test cases to cover any future regressions. --- .../java/net/md_5/bungee/event/EventBus.java | 8 ++++++-- .../net/md_5/bungee/event/EventPriorityTest.java | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/event/src/main/java/net/md_5/bungee/event/EventBus.java b/event/src/main/java/net/md_5/bungee/event/EventBus.java index 2afbbbd7..9e05c7a8 100644 --- a/event/src/main/java/net/md_5/bungee/event/EventBus.java +++ b/event/src/main/java/net/md_5/bungee/event/EventBus.java @@ -178,7 +178,11 @@ public class EventBus if ( handlersByPriority != null ) { List handlersList = new ArrayList<>( handlersByPriority.size() * 2 ); - for ( byte value = Byte.MIN_VALUE; value < Byte.MAX_VALUE; value++ ) + + // Either I'm really tired, or the only way we can iterate between Byte.MIN_VALUE and Byte.MAX_VALUE inclusively, + // with only a byte on the stack is by using a do {} while() format loop. + byte value = Byte.MIN_VALUE; + do { Map handlersByListener = handlersByPriority.get( value ); if ( handlersByListener != null ) @@ -192,7 +196,7 @@ public class EventBus } } } - } + } while ( value++ < Byte.MAX_VALUE ); byEventBaked.put( eventClass, handlersList.toArray( new EventHandlerMethod[ handlersList.size() ] ) ); } else { diff --git a/event/src/test/java/net/md_5/bungee/event/EventPriorityTest.java b/event/src/test/java/net/md_5/bungee/event/EventPriorityTest.java index 62356886..e5338ecb 100644 --- a/event/src/test/java/net/md_5/bungee/event/EventPriorityTest.java +++ b/event/src/test/java/net/md_5/bungee/event/EventPriorityTest.java @@ -19,10 +19,17 @@ public class EventPriorityTest Assert.assertEquals( 0, latch.getCount() ); } + @EventHandler(priority = Byte.MIN_VALUE) + public void onMinPriority(PriorityTestEvent event) + { + Assert.assertEquals( 5, latch.getCount() ); + latch.countDown(); + } + @EventHandler(priority = EventPriority.LOWEST) public void onLowestPriority(PriorityTestEvent event) { - Assert.assertEquals( 5, latch.getCount() ); + Assert.assertEquals( 4, latch.getCount() ); latch.countDown(); } @@ -35,6 +42,13 @@ public class EventPriorityTest @EventHandler(priority = EventPriority.HIGHEST) public void onHighestPriority(PriorityTestEvent event) + { + Assert.assertEquals( 2, latch.getCount() ); + latch.countDown(); + } + + @EventHandler(priority = Byte.MAX_VALUE) + public void onMaxPriority(PriorityTestEvent event) { Assert.assertEquals( 1, latch.getCount() ); latch.countDown();