From 3bf886ba7292fe6ad85eaca125a0e66b5db7e7fc Mon Sep 17 00:00:00 2001 From: Trever Fischer Date: Wed, 25 Apr 2012 16:22:26 -0400 Subject: [PATCH] Refactor the RegionEvent into two subclasses for future operations --- .../us/camin/regions/RegionCreateEvent.java | 38 ++++++++++++++++++ .../java/us/camin/regions/RegionEvent.java | 11 +----- .../us/camin/regions/RegionEventHandler.java | 19 ++++----- .../java/us/camin/regions/RegionManager.java | 4 +- .../us/camin/regions/RegionRemoveEvent.java | 39 +++++++++++++++++++ 5 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 src/main/java/us/camin/regions/RegionCreateEvent.java create mode 100644 src/main/java/us/camin/regions/RegionRemoveEvent.java diff --git a/src/main/java/us/camin/regions/RegionCreateEvent.java b/src/main/java/us/camin/regions/RegionCreateEvent.java new file mode 100644 index 0000000..838e1ce --- /dev/null +++ b/src/main/java/us/camin/regions/RegionCreateEvent.java @@ -0,0 +1,38 @@ +package us.camin.regions; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class RegionCreateEvent extends RegionEvent { + public static final HandlerList s_handlers = new HandlerList(); + + public RegionCreateEvent(Region region) { + super(region); + } + + public HandlerList getHandlers() { + return s_handlers; + } + + public static HandlerList getHandlerList() { + return s_handlers; + } +} diff --git a/src/main/java/us/camin/regions/RegionEvent.java b/src/main/java/us/camin/regions/RegionEvent.java index 943eeaf..2f94309 100644 --- a/src/main/java/us/camin/regions/RegionEvent.java +++ b/src/main/java/us/camin/regions/RegionEvent.java @@ -21,19 +21,12 @@ package us.camin.regions; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -public class RegionEvent extends Event { +public abstract class RegionEvent extends Event { private static final HandlerList s_handlers = new HandlerList(); - public final EventType type; public final Region region; - public enum EventType { - Added, - Removed - } - - public RegionEvent(Region region, EventType type) { + public RegionEvent(Region region) { this.region = region; - this.type = type; } @Override diff --git a/src/main/java/us/camin/regions/RegionEventHandler.java b/src/main/java/us/camin/regions/RegionEventHandler.java index 821a571..37f120b 100644 --- a/src/main/java/us/camin/regions/RegionEventHandler.java +++ b/src/main/java/us/camin/regions/RegionEventHandler.java @@ -36,14 +36,15 @@ public class RegionEventHandler implements Listener { } @EventHandler - public void onRegionEvent(RegionEvent event) { - if (event.type == RegionEvent.EventType.Added) { - Location loc = event.region.location(); - MarkerIcon icon = m_api.getMarkerIcon("default"); - m_set.createMarker(null, event.region.name(), loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ(), icon, false); - } else if (event.type == RegionEvent.EventType.Removed) { - Marker marker = m_set.findMarkerByLabel(event.region.name()); - marker.deleteMarker(); - } + public void onRegionEvent(RegionRemoveEvent event) { + Marker marker = m_set.findMarkerByLabel(event.region.name()); + marker.deleteMarker(); + } + + @EventHandler + public void onRegionEvent(RegionCreateEvent event) { + Location loc = event.region.location(); + MarkerIcon icon = m_api.getMarkerIcon("default"); + m_set.createMarker(null, event.region.name(), loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ(), icon, false); } } diff --git a/src/main/java/us/camin/regions/RegionManager.java b/src/main/java/us/camin/regions/RegionManager.java index 3ee3ed2..c3dc87e 100644 --- a/src/main/java/us/camin/regions/RegionManager.java +++ b/src/main/java/us/camin/regions/RegionManager.java @@ -73,7 +73,7 @@ public class RegionManager { if (!m_regions.containsKey(worldName)) m_regions.put(worldName, new ArrayList()); if (m_regions.get(worldName).add(r)) { - m_pm.callEvent(new RegionEvent(r, RegionEvent.EventType.Added)); + m_pm.callEvent(new RegionCreateEvent(r)); } return false; } @@ -83,7 +83,7 @@ public class RegionManager { log.fine("Removing region "+r.name()+" from "+r.location()); if (m_regions.containsKey(worldName)) { if (m_regions.get(worldName).remove(r)) { - m_pm.callEvent(new RegionEvent(r, RegionEvent.EventType.Removed)); + m_pm.callEvent(new RegionRemoveEvent(r)); } return true; } diff --git a/src/main/java/us/camin/regions/RegionRemoveEvent.java b/src/main/java/us/camin/regions/RegionRemoveEvent.java new file mode 100644 index 0000000..8584c08 --- /dev/null +++ b/src/main/java/us/camin/regions/RegionRemoveEvent.java @@ -0,0 +1,39 @@ +package us.camin.regions; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class RegionRemoveEvent extends RegionEvent { + public static final HandlerList s_handlers = new HandlerList(); + + public RegionRemoveEvent(Region region) { + super(region); + } + + public HandlerList getHandlers() { + return s_handlers; + } + + public static HandlerList getHandlerList() { + return s_handlers; + } +} +