Moves classes to packages

This commit is contained in:
Kristian Knarvik 2024-05-14 14:34:02 +02:00
parent bb54a5db48
commit bc7a8bebea
10 changed files with 202 additions and 130 deletions

View File

@ -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<String, AreaMarker> factionAreaMarkers = new HashMap<>();
Map<String, Marker> factionMarkers = new HashMap<>();
AreaStyle defaultStyle;
Map<String, AreaStyle> customStyle;
int blockSize;
MarkerSet set;
long updatePeriod;
Set<String> visible;
Set<String> hidden;
boolean stop;
static DynmapFactionsPlugin instance;
private DynmapAPI api;
private MarkerAPI markerAPI;
private boolean playerSets;
private Map<String, AreaMarker> factionAreaMarkers = new HashMap<>();
private Map<String, Marker> factionMarkers = new HashMap<>();
private AreaStyle defaultStyle;
private Map<String, AreaStyle> customStyle;
private int blockSize;
private MarkerSet set;
private long updatePeriod;
private Set<String> visible;
private Set<String> hidden;
private boolean stop;
private static DynmapFactionsPlugin instance;
private boolean reload = false;
@Override
public void onLoad() {
log = this.getLogger();
public void setFactionMarkers(Map<String, Marker> factionMarkers) {
this.factionMarkers = factionMarkers;
}
public void setFactionAreaMarkers(Map<String, AreaMarker> 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<String, AreaMarker> getFactionAreaMarkers() {
return factionAreaMarkers;
}
public Map<String, Marker> getFactionMarkers() {
return factionMarkers;
}
public AreaStyle getDefaultStyle() {
return defaultStyle;
}
public Map<String, AreaStyle> getCustomStyle() {
return customStyle;
}
public int getBlockSize() {
return blockSize;
}
public MarkerSet getSet() {
return set;
}
public long getUpdatePeriod() {
return updatePeriod;
}
public Set<String> getVisible() {
return visible;
}
public Set<String> 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;
}
}

View File

@ -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<String, List<FactionBlock>> blocks = new HashMap<>();
}

View File

@ -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;

View File

@ -1,4 +1,4 @@
package org.dynmap.factions;
package org.dynmap.factions.container;
public record FactionBlock(int x, int z) {
}

View File

@ -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<String, List<FactionBlock>> blocks) {
public FactionBlocks() {
this(new HashMap<>());
}
}

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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

View File

@ -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<FactionBlock> blocks = factionBlocks.blocks.computeIfAbsent(world, k -> new LinkedList<>());
List<FactionBlock> 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<String, List<FactionBlock>> worldBlocks : factionBLocks.blocks.entrySet()) {
for (Map.Entry<String, List<FactionBlock>> 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<Warp> 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;
}

View File

@ -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);
}
}
}