diff --git a/src/main/java/org/dynmap/factions/DynmapFactionsPlugin.java b/src/main/java/org/dynmap/factions/DynmapFactionsPlugin.java index 05446e8..ed8630f 100644 --- a/src/main/java/org/dynmap/factions/DynmapFactionsPlugin.java +++ b/src/main/java/org/dynmap/factions/DynmapFactionsPlugin.java @@ -17,6 +17,7 @@ import org.bukkit.Location; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; 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.java.JavaPlugin; @@ -28,11 +29,21 @@ import org.dynmap.markers.Marker; import org.dynmap.markers.MarkerAPI; import org.dynmap.markers.MarkerIcon; import org.dynmap.markers.MarkerSet; +import org.dynmap.markers.PlayerSet; import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Factions; import com.massivecraft.factions.Board; import com.massivecraft.factions.Faction; +import com.massivecraft.factions.event.FPlayerJoinEvent; +import com.massivecraft.factions.event.FPlayerLeaveEvent; +import com.massivecraft.factions.event.FactionCreateEvent; +import com.massivecraft.factions.event.FactionDisbandEvent; +import com.massivecraft.factions.event.FactionRenameEvent; +import com.massivecraft.factions.event.LandClaimEvent; +import com.massivecraft.factions.event.LandUnclaimAllEvent; +import com.massivecraft.factions.event.LandUnclaimEvent; +import com.massivecraft.factions.struct.FFlag; import com.massivecraft.factions.struct.TerritoryAccess; public class DynmapFactionsPlugin extends JavaPlugin { @@ -44,6 +55,7 @@ public class DynmapFactionsPlugin extends JavaPlugin { MarkerAPI markerapi; Plugin factions; Factions factapi; + boolean playersets; int blocksize; @@ -108,9 +120,62 @@ public class DynmapFactionsPlugin extends JavaPlugin { } private class FactionsUpdate implements Runnable { + public boolean runonce; + public void run() { + if(!stop) { + updateFactions(); + if(!runonce) { + getServer().getScheduler().scheduleSyncDelayedTask(DynmapFactionsPlugin.this, this, updperiod); + } + else if(pending == this) { + pending = null; + } + } + } + } + + private class PlayerSetUpdate implements Runnable { + public String faction; + public PlayerSetUpdate(String fid) { + faction = fid; + } public void run() { if(!stop) - updateFactions(); + updatePlayerSet(faction); + } + } + + private void requestUpdatePlayerSet(String factid) { + if(playersets) + getServer().getScheduler().scheduleSyncDelayedTask(this, new PlayerSetUpdate(factid)); + } + + private FactionsUpdate pending = null; + + private void requestUpdateFactions() { + if(pending == null) { + FactionsUpdate upd = new FactionsUpdate(); + upd.runonce = true; + getServer().getScheduler().scheduleSyncDelayedTask(this, upd, 20); + } + } + + private void updatePlayerSet(String factid) { + Set plids = new HashSet(); + Faction f = factapi.get(factid); /* Get faction */ + if(f != null) { + Set ps = f.getFPlayers(); + for(FPlayer fp : ps) { + plids.add(fp.getId()); + } + } + String setid = "factions." + factid; + PlayerSet set = markerapi.getPlayerSet(setid); /* See if set exists */ + if(set == null) { + set = markerapi.createPlayerSet(setid, true, plids, false); + } + else { + set.setPlayers(plids); } } @@ -120,7 +185,7 @@ public class DynmapFactionsPlugin extends JavaPlugin { private String formatInfoWindow(Faction fact) { String v = "
"+infowindow+"
"; v = v.replaceAll("%regionname%", ChatColor.stripColor(fact.getTag())); - FPlayer adm = fact.getFPlayerAdmin(); + FPlayer adm = fact.getFPlayerLeader(); v = v.replaceAll("%playerowners%", (adm!=null)?adm.getName():""); String res = ""; for(FPlayer r : fact.getFPlayers()) { @@ -132,8 +197,10 @@ public class DynmapFactionsPlugin extends JavaPlugin { v = v.replaceAll("%nation%", ChatColor.stripColor(fact.getTag())); /* Build flags */ String flgs = "open: " + fact.getOpen(); - flgs += "
peaceful: " + fact.isPeaceful(); - flgs += "
peacefulExplosionsEnabled: " + fact.getPeacefulExplosionsEnabled(); + for(FFlag ff : FFlag.values()) { + flgs += "
" + ff.getNicename() + ": " + fact.getFlag(ff); + v = v.replaceAll("%flag." + ff.name() + "%", fact.getFlag(ff)?"true":"false"); + } v = v.replaceAll("%flags%", flgs); return v; } @@ -467,13 +534,18 @@ public class DynmapFactionsPlugin extends JavaPlugin { /* And replace with new map */ resareas = newmap; resmark = newmark; - - getServer().getScheduler().scheduleSyncDelayedTask(this, new FactionsUpdate(), updperiod); - + + } + + private void updatePlayerSets() { + if(playersets) { + for(Faction f : factapi.get()) { + updatePlayerSet(f.getId()); + } + } } private class OurServerListener implements Listener { - @SuppressWarnings("unused") @EventHandler public void onPluginEnable(PluginEnableEvent event) { Plugin p = event.getPlugin(); @@ -483,6 +555,58 @@ public class DynmapFactionsPlugin extends JavaPlugin { activate(); } } + @EventHandler(priority=EventPriority.MONITOR) + public void onFPlayerJoin(FPlayerJoinEvent event) { + if(event.isCancelled()) + return; + if(playersets) + requestUpdatePlayerSet(event.getFaction().getId()); + } + @EventHandler(priority=EventPriority.MONITOR) + public void onFPlayerLeave(FPlayerLeaveEvent event) { + if(event.isCancelled()) + return; + if(playersets) + requestUpdatePlayerSet(event.getFaction().getId()); + } + @EventHandler(priority=EventPriority.MONITOR) + public void onFactionCreate(FactionCreateEvent event) { + if(event.isCancelled()) + return; + if(playersets) + requestUpdatePlayerSet(event.getFactionId()); + requestUpdateFactions(); + } + @EventHandler(priority=EventPriority.MONITOR) + public void onFactionDisband(FactionDisbandEvent event) { + if(event.isCancelled()) + return; + if(playersets) + requestUpdatePlayerSet(event.getFaction().getId()); + requestUpdateFactions(); + } + @EventHandler(priority=EventPriority.MONITOR) + public void onFactionRename(FactionRenameEvent event) { + if(event.isCancelled()) + return; + requestUpdateFactions(); + } + @EventHandler(priority=EventPriority.MONITOR) + public void onLandClaim(LandClaimEvent event) { + if(event.isCancelled()) + return; + requestUpdateFactions(); + } + @EventHandler(priority=EventPriority.MONITOR) + public void onLandUnclaim(LandUnclaimEvent event) { + if(event.isCancelled()) + return; + requestUpdateFactions(); + } + @EventHandler(priority=EventPriority.MONITOR) + public void onLandUnclaimAll(LandUnclaimAllEvent event) { + requestUpdateFactions(); + } } public void onEnable() { @@ -510,6 +634,8 @@ public class DynmapFactionsPlugin extends JavaPlugin { activate(); } + private boolean reload = false; + private void activate() { markerapi = api.getMarkerAPI(); if(markerapi == null) { @@ -522,6 +648,12 @@ public class DynmapFactionsPlugin extends JavaPlugin { blocksize = 16; /* Fixed at 16 */ /* Load configuration */ + if(reload) { + this.reloadConfig(); + } + else { + reload = true; + } FileConfiguration cfg = getConfig(); cfg.options().copyDefaults(true); /* Load defaults, if needed */ this.saveConfig(); /* Save updates, if needed */ @@ -563,6 +695,17 @@ public class DynmapFactionsPlugin extends JavaPlugin { if(hid != null) { hidden = new HashSet(hid); } + /* Chec if player sets enabled */ + playersets = cfg.getBoolean("visibility-by-faction", false); + if(playersets) { + try { + markerapi.getPlayerSets(); /* Test if API available on dynmap */ + } catch (Exception x) { + playersets = false; + log.info("Dynmap does not support function needed for 'visibilitybyfaction' - need to upgrade"); + } + } + updatePlayerSets(); /* Set up update job - based on periond */ int per = cfg.getInt("update.period", 300); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 0d4a191..9b9618f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -16,6 +16,9 @@ layer: # Format for popup - substitute values for macros infowindow: '
%regionname%
Flags
%flags%
' +# Allow players in faction to see one another on dynmap (only relevant if dynmap has 'player-info-protected' enabled) +visibility-by-faction: true + regionstyle: strokeColor: "#FF0000" strokeOpacity: 0.8