diff --git a/src/main/java/org/dynmap/factions/DynmapFactionsPlugin.java b/src/main/java/org/dynmap/factions/DynmapFactionsPlugin.java index 466bc5f..49bac41 100644 --- a/src/main/java/org/dynmap/factions/DynmapFactionsPlugin.java +++ b/src/main/java/org/dynmap/factions/DynmapFactionsPlugin.java @@ -9,6 +9,10 @@ import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.dynmap.DynmapAPI; +import org.dynmap.factions.config.AreaStyle; +import org.dynmap.factions.listener.DynmapFactionsListener; +import org.dynmap.factions.runnable.FactionsUpdate; +import org.dynmap.factions.runnable.PlayerSetUpdate; import org.dynmap.markers.AreaMarker; import org.dynmap.markers.Marker; import org.dynmap.markers.MarkerAPI; @@ -27,28 +31,94 @@ import java.util.logging.Logger; public class DynmapFactionsPlugin extends JavaPlugin { private static Logger log; - Plugin dynmap; - DynmapAPI api; - MarkerAPI markerAPI; - Plugin factions; - boolean playerSets; - Map factionAreaMarkers = new HashMap<>(); - Map factionMarkers = new HashMap<>(); - AreaStyle defaultStyle; - Map customStyle; - int blockSize; - MarkerSet set; - long updatePeriod; - Set visible; - Set hidden; - boolean stop; - static DynmapFactionsPlugin instance; + private DynmapAPI api; + private MarkerAPI markerAPI; + private boolean playerSets; + + private Map factionAreaMarkers = new HashMap<>(); + + private Map factionMarkers = new HashMap<>(); + private AreaStyle defaultStyle; + private Map customStyle; + private int blockSize; + private MarkerSet set; + private long updatePeriod; + + private Set visible; + private Set hidden; + private boolean stop; + private static DynmapFactionsPlugin instance; private boolean reload = false; - @Override - public void onLoad() { - log = this.getLogger(); + public void setFactionMarkers(Map factionMarkers) { + this.factionMarkers = factionMarkers; + } + + public void setFactionAreaMarkers(Map factionAreaMarkers) { + this.factionAreaMarkers = factionAreaMarkers; + } + + public void setPending(FactionsUpdate pending) { + this.pending = pending; + } + + private FactionsUpdate pending = null; + + public MarkerAPI getMarkerAPI() { + return markerAPI; + } + + public boolean isPlayerSets() { + return playerSets; + } + + public Map getFactionAreaMarkers() { + return factionAreaMarkers; + } + + public Map getFactionMarkers() { + return factionMarkers; + } + + public AreaStyle getDefaultStyle() { + return defaultStyle; + } + + public Map getCustomStyle() { + return customStyle; + } + + public int getBlockSize() { + return blockSize; + } + + public MarkerSet getSet() { + return set; + } + + public long getUpdatePeriod() { + return updatePeriod; + } + + public Set getVisible() { + return visible; + } + + public Set getHidden() { + return hidden; + } + + public boolean isStop() { + return stop; + } + + public static DynmapFactionsPlugin getInstance() { + return instance; + } + + public FactionsUpdate getPending() { + return pending; } /** @@ -64,15 +134,13 @@ public class DynmapFactionsPlugin extends JavaPlugin { log.log(Level.SEVERE, msg); } - void requestUpdatePlayerSet(String factionId) { + public void requestUpdatePlayerSet(String factionId) { if (playerSets) { getServer().getScheduler().scheduleSyncDelayedTask(this, new PlayerSetUpdate(this, factionId)); } } - FactionsUpdate pending = null; - - void updatePlayerSet(String factionId) { + public void updatePlayerSet(String factionId) { /* If Wilderness or other unassociated factions (guid-style ID), skip */ if (factionId.indexOf('-') >= 0) { return; @@ -102,6 +170,45 @@ public class DynmapFactionsPlugin extends JavaPlugin { } } + @Override + public void onLoad() { + log = this.getLogger(); + } + + @Override + public void onEnable() { + instance = this; + info("initializing"); + PluginManager pm = getServer().getPluginManager(); + /* Get dynmap */ + Plugin dynmap = pm.getPlugin("dynmap"); + if (dynmap == null) { + severe("Cannot find dynmap!"); + return; + } + api = (DynmapAPI) dynmap; /* Get API */ + /* Get Factions */ + Plugin p = pm.getPlugin("Factions"); + if (p == null) { + severe("Cannot find Factions!"); + return; + } + + /* If both enabled, activate */ + if (dynmap.isEnabled() && p.isEnabled()) { + activate(); + } + } + + @Override + public void onDisable() { + if (set != null) { + set.deleteMarkerSet(); + set = null; + } + factionAreaMarkers.clear(); + stop = true; + } private void updatePlayerSets() { if (playerSets) { @@ -115,32 +222,7 @@ public class DynmapFactionsPlugin extends JavaPlugin { } } - public void onEnable() { - instance = this; - info("initializing"); - PluginManager pm = getServer().getPluginManager(); - /* Get dynmap */ - dynmap = pm.getPlugin("dynmap"); - if (dynmap == null) { - severe("Cannot find dynmap!"); - return; - } - api = (DynmapAPI) dynmap; /* Get API */ - /* Get Factions */ - Plugin p = pm.getPlugin("Factions"); - if (p == null) { - severe("Cannot find Factions!"); - return; - } - factions = p; - - /* If both enabled, activate */ - if (dynmap.isEnabled() && factions.isEnabled()) { - activate(); - } - } - - void activate() { + private void activate() { markerAPI = api.getMarkerAPI(); if (markerAPI == null) { severe("Error loading dynmap marker API!"); @@ -234,14 +316,4 @@ public class DynmapFactionsPlugin extends JavaPlugin { info("version " + this.getDescription().getVersion() + " is activated"); } - @Override - public void onDisable() { - if (set != null) { - set.deleteMarkerSet(); - set = null; - } - factionAreaMarkers.clear(); - stop = true; - } - } diff --git a/src/main/java/org/dynmap/factions/FactionBlocks.java b/src/main/java/org/dynmap/factions/FactionBlocks.java deleted file mode 100644 index f9bbbe3..0000000 --- a/src/main/java/org/dynmap/factions/FactionBlocks.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.dynmap.factions; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class FactionBlocks { - - final Map> blocks = new HashMap<>(); - -} diff --git a/src/main/java/org/dynmap/factions/AreaStyle.java b/src/main/java/org/dynmap/factions/config/AreaStyle.java similarity index 93% rename from src/main/java/org/dynmap/factions/AreaStyle.java rename to src/main/java/org/dynmap/factions/config/AreaStyle.java index 95fb372..4906656 100644 --- a/src/main/java/org/dynmap/factions/AreaStyle.java +++ b/src/main/java/org/dynmap/factions/config/AreaStyle.java @@ -1,6 +1,7 @@ -package org.dynmap.factions; +package org.dynmap.factions.config; import org.bukkit.configuration.file.FileConfiguration; +import org.dynmap.factions.DynmapFactionsPlugin; import org.dynmap.markers.MarkerIcon; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -71,10 +72,10 @@ public record AreaStyle(String strokeColor, double strokeOpacity, int strokeWeig @Nullable String homeMarker) { MarkerIcon homeIcon = null; if (homeMarker != null) { - homeIcon = dynmapFactionsPlugin.markerAPI.getMarkerIcon(homeMarker); + homeIcon = dynmapFactionsPlugin.getMarkerAPI().getMarkerIcon(homeMarker); if (homeIcon == null) { DynmapFactionsPlugin.severe("Invalid home icon: " + homeMarker); - homeIcon = dynmapFactionsPlugin.markerAPI.getMarkerIcon("blueicon"); + homeIcon = dynmapFactionsPlugin.getMarkerAPI().getMarkerIcon("blueicon"); } } return homeIcon; diff --git a/src/main/java/org/dynmap/factions/FactionBlock.java b/src/main/java/org/dynmap/factions/container/FactionBlock.java similarity index 54% rename from src/main/java/org/dynmap/factions/FactionBlock.java rename to src/main/java/org/dynmap/factions/container/FactionBlock.java index 0d6b765..41f0172 100644 --- a/src/main/java/org/dynmap/factions/FactionBlock.java +++ b/src/main/java/org/dynmap/factions/container/FactionBlock.java @@ -1,4 +1,4 @@ -package org.dynmap.factions; +package org.dynmap.factions.container; public record FactionBlock(int x, int z) { } diff --git a/src/main/java/org/dynmap/factions/container/FactionBlocks.java b/src/main/java/org/dynmap/factions/container/FactionBlocks.java new file mode 100644 index 0000000..865696f --- /dev/null +++ b/src/main/java/org/dynmap/factions/container/FactionBlocks.java @@ -0,0 +1,13 @@ +package org.dynmap.factions.container; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public record FactionBlocks(Map> blocks) { + + public FactionBlocks() { + this(new HashMap<>()); + } + +} diff --git a/src/main/java/org/dynmap/factions/TileFlags.java b/src/main/java/org/dynmap/factions/container/TileFlags.java similarity index 97% rename from src/main/java/org/dynmap/factions/TileFlags.java rename to src/main/java/org/dynmap/factions/container/TileFlags.java index 7e39b4c..b72ca55 100644 --- a/src/main/java/org/dynmap/factions/TileFlags.java +++ b/src/main/java/org/dynmap/factions/container/TileFlags.java @@ -1,4 +1,4 @@ -package org.dynmap.factions; +package org.dynmap.factions.container; import java.util.HashMap; import java.util.Map; @@ -16,7 +16,7 @@ public class TileFlags { public TileFlags() { } - + public boolean getFlag(int x, int y) { long k = (((long) (x >> 6)) << 32) | (0xFFFFFFFFL & (long) (y >> 6)); long[] row; diff --git a/src/main/java/org/dynmap/factions/DynmapFactionsListener.java b/src/main/java/org/dynmap/factions/listener/DynmapFactionsListener.java similarity index 76% rename from src/main/java/org/dynmap/factions/DynmapFactionsListener.java rename to src/main/java/org/dynmap/factions/listener/DynmapFactionsListener.java index 02beaf3..8044f4b 100644 --- a/src/main/java/org/dynmap/factions/DynmapFactionsListener.java +++ b/src/main/java/org/dynmap/factions/listener/DynmapFactionsListener.java @@ -1,4 +1,4 @@ -package org.dynmap.factions; +package org.dynmap.factions.listener; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.event.EventFactionsChunksChange; @@ -12,8 +12,8 @@ import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.server.PluginEnableEvent; -import org.bukkit.plugin.Plugin; +import org.dynmap.factions.DynmapFactionsPlugin; +import org.dynmap.factions.runnable.FactionsUpdate; import org.jetbrains.annotations.NotNull; public class DynmapFactionsListener implements Listener { @@ -24,23 +24,12 @@ public class DynmapFactionsListener implements Listener { this.dynmapFactionsPlugin = dynmapFactionsPlugin; } - @EventHandler - public void onPluginEnable(@NotNull PluginEnableEvent event) { - Plugin p = event.getPlugin(); - String name = p.getDescription().getName(); - if (name.equals("dynmap") || name.equals("Factions")) { - if (dynmapFactionsPlugin.dynmap.isEnabled() && dynmapFactionsPlugin.factions.isEnabled()) { - dynmapFactionsPlugin.activate(); - } - } - } - @EventHandler(priority = EventPriority.MONITOR) public void onFPlayerJoin(@NotNull EventFactionsMembershipChange event) { if (event.isCancelled()) { return; } - if (dynmapFactionsPlugin.playerSets) { + if (dynmapFactionsPlugin.isPlayerSets()) { Faction f = event.getNewFaction(); dynmapFactionsPlugin.requestUpdatePlayerSet(f.getId()); } @@ -51,7 +40,7 @@ public class DynmapFactionsListener implements Listener { if (event.isCancelled()) { return; } - if (dynmapFactionsPlugin.playerSets) { + if (dynmapFactionsPlugin.isPlayerSets()) { dynmapFactionsPlugin.requestUpdatePlayerSet(event.getFactionId()); } requestUpdateFactions(); @@ -62,7 +51,7 @@ public class DynmapFactionsListener implements Listener { if (event.isCancelled()) { return; } - if (dynmapFactionsPlugin.playerSets) { + if (dynmapFactionsPlugin.isPlayerSets()) { Faction f = event.getFaction(); dynmapFactionsPlugin.requestUpdatePlayerSet(f.getId()); } @@ -102,12 +91,12 @@ public class DynmapFactionsListener implements Listener { } private void requestUpdateFactions() { - if (DynmapFactionsPlugin.instance.pending == null) { - FactionsUpdate factionsUpdate = new FactionsUpdate(DynmapFactionsPlugin.instance, - DynmapFactionsPlugin.instance.getConfig()); + if (DynmapFactionsPlugin.getInstance().getPending() == null) { + FactionsUpdate factionsUpdate = new FactionsUpdate(DynmapFactionsPlugin.getInstance(), + DynmapFactionsPlugin.getInstance().getConfig()); factionsUpdate.runonce = true; - DynmapFactionsPlugin.instance.pending = factionsUpdate; - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(DynmapFactionsPlugin.instance, factionsUpdate, 20); + DynmapFactionsPlugin.getInstance().setPending(factionsUpdate); + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(DynmapFactionsPlugin.getInstance(), factionsUpdate, 20); } } diff --git a/src/main/java/org/dynmap/factions/Direction.java b/src/main/java/org/dynmap/factions/property/Direction.java similarity index 90% rename from src/main/java/org/dynmap/factions/Direction.java rename to src/main/java/org/dynmap/factions/property/Direction.java index fa4aaa2..4f0ff21 100644 --- a/src/main/java/org/dynmap/factions/Direction.java +++ b/src/main/java/org/dynmap/factions/property/Direction.java @@ -1,4 +1,4 @@ -package org.dynmap.factions; +package org.dynmap.factions.property; /** * A representation of the directions that make sense in a Dynmap context diff --git a/src/main/java/org/dynmap/factions/FactionsUpdate.java b/src/main/java/org/dynmap/factions/runnable/FactionsUpdate.java similarity index 87% rename from src/main/java/org/dynmap/factions/FactionsUpdate.java rename to src/main/java/org/dynmap/factions/runnable/FactionsUpdate.java index 3d96fc5..f783bb7 100644 --- a/src/main/java/org/dynmap/factions/FactionsUpdate.java +++ b/src/main/java/org/dynmap/factions/runnable/FactionsUpdate.java @@ -1,4 +1,4 @@ -package org.dynmap.factions; +package org.dynmap.factions.runnable; import com.massivecraft.factions.entity.BoardColl; import com.massivecraft.factions.entity.Faction; @@ -11,6 +11,12 @@ import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.store.EntityInternalMap; import org.bukkit.ChatColor; import org.bukkit.configuration.file.FileConfiguration; +import org.dynmap.factions.DynmapFactionsPlugin; +import org.dynmap.factions.config.AreaStyle; +import org.dynmap.factions.container.FactionBlock; +import org.dynmap.factions.container.FactionBlocks; +import org.dynmap.factions.container.TileFlags; +import org.dynmap.factions.property.Direction; import org.dynmap.markers.AreaMarker; import org.dynmap.markers.Marker; import org.dynmap.markers.MarkerIcon; @@ -41,16 +47,16 @@ public class FactionsUpdate implements Runnable { @Override public void run() { - if (dynmapFactionsPlugin.stop) { + if (dynmapFactionsPlugin.isStop()) { return; } updateFactions(); if (!runonce) { dynmapFactionsPlugin.getServer().getScheduler().scheduleSyncDelayedTask(dynmapFactionsPlugin, - this, dynmapFactionsPlugin.updatePeriod); - } else if (dynmapFactionsPlugin.pending == this) { - dynmapFactionsPlugin.pending = null; + this, dynmapFactionsPlugin.getUpdatePeriod()); + } else if (dynmapFactionsPlugin.getPending() == this) { + dynmapFactionsPlugin.setPending(null); } } @@ -78,7 +84,7 @@ public class FactionsUpdate implements Runnable { String world = cc.getWorld(); /* Get block set for given world */ - List blocks = factionBlocks.blocks.computeIfAbsent(world, k -> new LinkedList<>()); + List blocks = factionBlocks.blocks().computeIfAbsent(world, k -> new LinkedList<>()); FactionBlock fb = new FactionBlock(cc.getChunkX(), cc.getChunkZ()); blocks.add(fb); /* Add to list */ } @@ -93,10 +99,10 @@ public class FactionsUpdate implements Runnable { } /* Loop through each world that faction has blocks on */ - for (Map.Entry> worldBlocks : factionBLocks.blocks.entrySet()) { + for (Map.Entry> worldBlocks : factionBLocks.blocks().entrySet()) { handleFactionOnWorld(factionName, faction, worldBlocks.getKey(), worldBlocks.getValue(), newMap); } - factionBLocks.blocks.clear(); + factionBLocks.blocks().clear(); /* Now, add marker for home location */ EntityInternalMap warps = faction.getWarps(); @@ -114,10 +120,10 @@ public class FactionsUpdate implements Runnable { String markId = fc.getUniverse() + "_" + factionName + "__home"; MarkerIcon ico = getMarkerIcon(factionName); if (ico != null) { - Marker homeMarker = dynmapFactionsPlugin.factionMarkers.remove(markId); + Marker homeMarker = dynmapFactionsPlugin.getFactionMarkers().remove(markId); String lbl = factionName + " [home]"; if (homeMarker == null) { - homeMarker = dynmapFactionsPlugin.set.createMarker(markId, lbl, homeLocation.getWorld(), + homeMarker = dynmapFactionsPlugin.getSet().createMarker(markId, lbl, homeLocation.getWorld(), homeLocation.getLocationX(), homeLocation.getLocationY(), homeLocation.getLocationZ(), ico, false); } else { homeMarker.setLocation(homeLocation.getWorld(), homeLocation.getLocationX(), homeLocation.getLocationY(), homeLocation.getLocationZ()); @@ -134,15 +140,15 @@ public class FactionsUpdate implements Runnable { blocksByFaction.clear(); /* Now, review old map - anything left is gone */ - for (AreaMarker oldMarker : dynmapFactionsPlugin.factionAreaMarkers.values()) { + for (AreaMarker oldMarker : dynmapFactionsPlugin.getFactionAreaMarkers().values()) { oldMarker.deleteMarker(); } - for (Marker oldMarker : dynmapFactionsPlugin.factionMarkers.values()) { + for (Marker oldMarker : dynmapFactionsPlugin.getFactionMarkers().values()) { oldMarker.deleteMarker(); } /* And replace with new map */ - dynmapFactionsPlugin.factionAreaMarkers = newMap; - dynmapFactionsPlugin.factionMarkers = newMarkers; + dynmapFactionsPlugin.setFactionAreaMarkers(newMap); + dynmapFactionsPlugin.setFactionMarkers(newMarkers); } @@ -274,13 +280,13 @@ public class FactionsUpdate implements Runnable { z = new double[sz]; for (int i = 0; i < sz; i++) { int[] line = lineList.get(i); - x[i] = (double) line[0] * (double) dynmapFactionsPlugin.blockSize; - z[i] = (double) line[1] * (double) dynmapFactionsPlugin.blockSize; + x[i] = (double) line[0] * (double) dynmapFactionsPlugin.getBlockSize(); + z[i] = (double) line[1] * (double) dynmapFactionsPlugin.getBlockSize(); } /* Find existing one */ - AreaMarker m = dynmapFactionsPlugin.factionAreaMarkers.remove(polyId); /* Existing area? */ + AreaMarker m = dynmapFactionsPlugin.getFactionAreaMarkers().remove(polyId); /* Existing area? */ if (m == null) { - m = dynmapFactionsPlugin.set.createAreaMarker(polyId, factionName, false, world, x, z, false); + m = dynmapFactionsPlugin.getSet().createAreaMarker(polyId, factionName, false, world, x, z, false); if (m == null) { DynmapFactionsPlugin.info("error adding area marker " + polyId); return; @@ -332,9 +338,9 @@ public class FactionsUpdate implements Runnable { } private void addStyle(String factionId, AreaMarker areaMarker) { - AreaStyle as = dynmapFactionsPlugin.customStyle.get(factionId); + AreaStyle as = dynmapFactionsPlugin.getCustomStyle().get(factionId); if (as == null) { - as = dynmapFactionsPlugin.defaultStyle; + as = dynmapFactionsPlugin.getDefaultStyle(); } int sc = 0xFF0000; int fc = 0xFF0000; @@ -349,21 +355,21 @@ public class FactionsUpdate implements Runnable { } private MarkerIcon getMarkerIcon(String factionName) { - AreaStyle as = dynmapFactionsPlugin.customStyle.get(factionName); + AreaStyle as = dynmapFactionsPlugin.getCustomStyle().get(factionName); if (as == null) { - as = dynmapFactionsPlugin.defaultStyle; + as = dynmapFactionsPlugin.getDefaultStyle(); } return as.homeIcon(); } private boolean isVisible(String id, String worldName) { - if ((dynmapFactionsPlugin.visible != null) && (!dynmapFactionsPlugin.visible.isEmpty())) { - if ((!dynmapFactionsPlugin.visible.contains(id)) && (!dynmapFactionsPlugin.visible.contains("world:" + worldName))) { + if ((dynmapFactionsPlugin.getVisible() != null) && (!dynmapFactionsPlugin.getVisible().isEmpty())) { + if ((!dynmapFactionsPlugin.getVisible().contains(id)) && (!dynmapFactionsPlugin.getVisible().contains("world:" + worldName))) { return false; } } - if ((dynmapFactionsPlugin.hidden != null) && (!dynmapFactionsPlugin.hidden.isEmpty())) { - return !dynmapFactionsPlugin.hidden.contains(id) && !dynmapFactionsPlugin.hidden.contains("world:" + worldName); + if ((dynmapFactionsPlugin.getHidden() != null) && (!dynmapFactionsPlugin.getHidden().isEmpty())) { + return !dynmapFactionsPlugin.getHidden().contains(id) && !dynmapFactionsPlugin.getHidden().contains("world:" + worldName); } return true; } diff --git a/src/main/java/org/dynmap/factions/PlayerSetUpdate.java b/src/main/java/org/dynmap/factions/runnable/PlayerSetUpdate.java similarity index 60% rename from src/main/java/org/dynmap/factions/PlayerSetUpdate.java rename to src/main/java/org/dynmap/factions/runnable/PlayerSetUpdate.java index 574115e..bbab988 100644 --- a/src/main/java/org/dynmap/factions/PlayerSetUpdate.java +++ b/src/main/java/org/dynmap/factions/runnable/PlayerSetUpdate.java @@ -1,9 +1,11 @@ -package org.dynmap.factions; +package org.dynmap.factions.runnable; -class PlayerSetUpdate implements Runnable { +import org.dynmap.factions.DynmapFactionsPlugin; + +public class PlayerSetUpdate implements Runnable { private final DynmapFactionsPlugin dynmapFactionsPlugin; - public final String faction; + private final String faction; public PlayerSetUpdate(DynmapFactionsPlugin dynmapFactionsPlugin, String fid) { this.dynmapFactionsPlugin = dynmapFactionsPlugin; @@ -11,9 +13,9 @@ class PlayerSetUpdate implements Runnable { } public void run() { - if (!dynmapFactionsPlugin.stop) { + if (!dynmapFactionsPlugin.isStop()) { dynmapFactionsPlugin.updatePlayerSet(faction); } } - + }