-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MapTowny integration! #556
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
219 changes: 219 additions & 0 deletions
219
src/main/java/com/gmail/goosius/siegewar/integration/maptowny/MapTownyIntegration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
package com.gmail.goosius.siegewar.integration.maptowny; | ||
|
||
import com.gmail.goosius.siegewar.SiegeController; | ||
import com.gmail.goosius.siegewar.SiegeWar; | ||
import com.gmail.goosius.siegewar.enums.SiegeSide; | ||
import com.gmail.goosius.siegewar.objects.BattleSession; | ||
import com.gmail.goosius.siegewar.objects.Siege; | ||
import com.gmail.goosius.siegewar.settings.Settings; | ||
import com.palmergames.bukkit.towny.TownyEconomyHandler; | ||
import com.palmergames.bukkit.towny.TownyUniverse; | ||
import com.palmergames.bukkit.towny.object.TownyWorld; | ||
import com.palmergames.bukkit.towny.object.Translation; | ||
import com.palmergames.util.StringMgmt; | ||
import me.silverwolfg11.maptowny.MapTownyPlugin; | ||
import me.silverwolfg11.maptowny.objects.LayerOptions; | ||
import me.silverwolfg11.maptowny.objects.MarkerOptions; | ||
import me.silverwolfg11.maptowny.objects.Point2D; | ||
import me.silverwolfg11.maptowny.platform.MapLayer; | ||
import me.silverwolfg11.maptowny.platform.MapPlatform; | ||
import me.silverwolfg11.maptowny.platform.MapWorld; | ||
import org.apache.commons.lang.WordUtils; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Location; | ||
import org.bukkit.World; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.logging.Level; | ||
|
||
public class MapTownyIntegration { | ||
|
||
private final String SIEGE_MARKER_PREFIX = "siegewar_siege_"; | ||
private final String SIEGE_LAYER_PREFIX = "siegewar_layer_"; | ||
private final String PEACEFUL_BANNER_ICON_KEY = "siegewar_peaceful"; | ||
private final String BATTLE_BANNER_ICON_KEY = "siegewar_battle"; | ||
|
||
private final MapTownyPlugin mapTowny; | ||
private final Map<String, MapLayer> worldLayers = new HashMap<>(); | ||
private boolean siegesRendered = false; | ||
|
||
public MapTownyIntegration(SiegeWar plugin) { | ||
this.mapTowny = (MapTownyPlugin) Bukkit.getPluginManager().getPlugin("MapTowny"); | ||
|
||
// Check if map towny found a valid web-map platform | ||
if (mapTowny.getPlatform() == null) { | ||
return; | ||
} | ||
|
||
// Register replacement handler | ||
Bukkit.getPluginManager().registerEvents(new MapTownyReplacementsHandler(mapTowny), plugin); | ||
|
||
// Re-render the sieges every time a new short time event triggers. | ||
Bukkit.getPluginManager().registerEvents(new MapTownyShortTimeListener(this::displaySieges), plugin); | ||
|
||
final String pluginName = plugin.getName(); | ||
// Run marker and layer setup on platform initialize | ||
this.mapTowny.getPlatform().onInitialize(() -> { | ||
registerIcons(plugin); | ||
registerMarkerLayers(pluginName); | ||
}); | ||
} | ||
|
||
private BufferedImage readImage(final InputStream is, final String fileName) { | ||
try { | ||
return ImageIO.read(is); | ||
} catch (IOException ex) { | ||
String errorMsg = String.format("Error reading image from file '%s'...", fileName); | ||
SiegeWar.getSiegeWar().getLogger().log(Level.SEVERE, errorMsg, ex); | ||
return null; | ||
} | ||
} | ||
|
||
private void registerIcons(final SiegeWar plugin) { | ||
final MapPlatform platform = this.mapTowny.getPlatform(); | ||
|
||
if (!platform.hasIcon(PEACEFUL_BANNER_ICON_KEY)) { | ||
String fileName = Settings.PEACEFUL_BANNER_FILE_NAME; | ||
InputStream is = plugin.getResource(fileName); | ||
|
||
BufferedImage img = readImage(is, fileName); | ||
if (img != null) { | ||
platform.registerIcon(PEACEFUL_BANNER_ICON_KEY, img, 16, 16); | ||
} | ||
} | ||
|
||
if (!platform.hasIcon(BATTLE_BANNER_ICON_KEY)) { | ||
String fileName = Settings.BATTLE_BANNER_FILE_NAME; | ||
InputStream is = plugin.getResource(fileName); | ||
|
||
BufferedImage img = readImage(is, fileName); | ||
if (img != null) { | ||
platform.registerIcon(BATTLE_BANNER_ICON_KEY, img, 16, 16); | ||
} | ||
} | ||
} | ||
|
||
private void registerMarkerLayers(String pluginName) { | ||
MapPlatform platform = this.mapTowny.getPlatform(); | ||
LayerOptions layerOptions = new LayerOptions(pluginName, true, false, 10, 10); | ||
|
||
// Register SiegeWar marker layer in all available towny worlds | ||
for (TownyWorld townyWorld : TownyUniverse.getInstance().getTownyWorlds()) { | ||
if(!townyWorld.isUsingTowny()) | ||
continue; | ||
|
||
String worldName = townyWorld.getName(); | ||
World bukkitWorld = Bukkit.getWorld(worldName); | ||
|
||
MapWorld mapWorld = bukkitWorld != null ? platform.getWorld(bukkitWorld) : null; | ||
|
||
if (mapWorld == null) | ||
continue; | ||
|
||
MapLayer mapLayer = mapWorld.registerLayer(SIEGE_LAYER_PREFIX + worldName, layerOptions); | ||
worldLayers.put(worldName, mapLayer); | ||
} | ||
} | ||
|
||
private String getSiegeMarkerKey(UUID townUUID) { | ||
return "siegewar_siege_" + townUUID.toString(); | ||
} | ||
|
||
void displaySieges() { | ||
// Avoid attempting to remove markers if no sieges were rendered | ||
if (siegesRendered) { | ||
// Remove all siege markers before re-rendering all of them | ||
for (Map.Entry<String, MapLayer> worldLayerEntry : worldLayers.entrySet()) { | ||
final MapLayer mapLayer = worldLayerEntry.getValue(); | ||
mapLayer.removeMarkers(s -> s.startsWith(SIEGE_MARKER_PREFIX)); | ||
} | ||
|
||
siegesRendered = false; | ||
} | ||
|
||
// Add all active siege markers | ||
for (Siege siege : SiegeController.getSieges()) { | ||
|
||
String name = Translation.of("dynmap_siege_title", siege.getAttackerNameForDisplay(), siege.getDefenderNameForDisplay()); | ||
try { | ||
if (siege.getStatus().isActive()) { | ||
Location siegeLoc = siege.getFlagLocation(); | ||
final String siegeWorldName = siegeLoc.getWorld().getName(); | ||
|
||
MapLayer mapLayer = worldLayers.get(siegeWorldName); | ||
|
||
if (mapLayer == null) | ||
continue; | ||
|
||
//If siege is dormant, show the fire icon, otherwise show the crossed swords icon. | ||
String iconKey; | ||
if (isSiegeDormant(siege)) { | ||
iconKey = PEACEFUL_BANNER_ICON_KEY; | ||
} else { | ||
iconKey = BATTLE_BANNER_ICON_KEY; | ||
} | ||
|
||
List<String> lines = new ArrayList<>(); | ||
lines.add(Translation.of("dynmap_siege_town", siege.getTown().getName())); | ||
lines.add(Translation.of("dynmap_siege_type", siege.getSiegeType().getName())); | ||
lines.add(Translation.of("dynmap_siege_balance", siege.getSiegeBalance())); | ||
lines.add(Translation.of("dynmap_siege_time_left", siege.getTimeRemaining())); | ||
if(TownyEconomyHandler.isActive()) { | ||
lines.add(Translation.of("dynmap_siege_war_chest", TownyEconomyHandler.getFormattedBalance(siege.getWarChestAmount()))); | ||
} | ||
lines.add(Translation.of("dynmap_siege_banner_control", | ||
WordUtils.capitalizeFully(siege.getBannerControllingSide().name()) | ||
+ (siege.getBannerControllingSide() == SiegeSide.NOBODY ? "" : " (" + siege.getBannerControllingResidents().size() + ")"))); | ||
lines.add(Translation.of("dynmap_siege_battle_points", siege.getFormattedAttackerBattlePoints(), siege.getFormattedDefenderBattlePoints())); | ||
lines.add(Translation.of("dynmap_siege_battle_time_left", siege.getFormattedBattleTimeRemaining())); | ||
|
||
String desc = "<b>" + name + "</b><hr>" + StringMgmt.join(lines, "<br>"); | ||
|
||
double siegeX = siegeLoc.getX(); | ||
double siegeZ = siegeLoc.getZ(); | ||
final UUID townUUID = siege.getTown().getUUID(); | ||
String siegeMarkerId = getSiegeMarkerKey(townUUID); | ||
|
||
MarkerOptions markerOptions = MarkerOptions.builder() | ||
.name(name) | ||
.clickTooltip(desc) | ||
.hoverTooltip(desc) | ||
.build(); | ||
|
||
Point2D iconLoc = Point2D.of(siegeX, siegeZ); | ||
mapLayer.addIconMarker(siegeMarkerId, iconKey, iconLoc, 16, 16, markerOptions); | ||
siegesRendered = true; | ||
} | ||
} catch (Exception ex) { | ||
SiegeWar.severe("Problem adding siege marker for siege: " + name); | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
* A siege is dormant if there is no significant activity there (e.g. kills, banner control). | ||
* | ||
* This state is represented by a fire icon on the map. | ||
* If the battle becomes active, the icon changes to crossed-swords. | ||
* | ||
* @return true if siege is dormant | ||
*/ | ||
private boolean isSiegeDormant(Siege siege) { | ||
return !BattleSession.getBattleSession().isActive() | ||
|| (siege.getAttackerBattlePoints() == 0 | ||
&& siege.getDefenderBattlePoints() == 0 | ||
&& siege.getBannerControllingSide() == SiegeSide.NOBODY | ||
&& siege.getBannerControlSessions().size() == 0); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...ain/java/com/gmail/goosius/siegewar/integration/maptowny/MapTownyReplacementsHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.gmail.goosius.siegewar.integration.maptowny; | ||
|
||
import com.gmail.goosius.siegewar.TownOccupationController; | ||
import me.silverwolfg11.maptowny.MapTownyPlugin; | ||
import me.silverwolfg11.maptowny.events.MapReloadEvent; | ||
import me.silverwolfg11.maptowny.managers.LayerManager; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
|
||
public class MapTownyReplacementsHandler implements Listener { | ||
|
||
private final MapTownyPlugin mapTowny; | ||
|
||
public MapTownyReplacementsHandler(MapTownyPlugin mapTowny) { | ||
this.mapTowny = mapTowny; | ||
registerReplacements(); | ||
} | ||
|
||
private void registerReplacements() { | ||
LayerManager layerManager = mapTowny.getLayerManager(); | ||
|
||
if (layerManager == null) | ||
return; | ||
|
||
layerManager.registerReplacement("%occupier%", town -> | ||
TownOccupationController.isTownOccupied(town) ? | ||
TownOccupationController.getTownOccupier(town).getName() : "" | ||
); | ||
} | ||
|
||
// Re-register replacements when maptowny reloads | ||
@EventHandler | ||
void onMapTownyReload(MapReloadEvent event) { | ||
registerReplacements(); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/gmail/goosius/siegewar/integration/maptowny/MapTownyShortTimeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.gmail.goosius.siegewar.integration.maptowny; | ||
|
||
import com.palmergames.bukkit.towny.event.time.NewShortTimeEvent; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
|
||
public class MapTownyShortTimeListener implements Listener { | ||
|
||
private final Runnable renderSieges; | ||
|
||
public MapTownyShortTimeListener(Runnable renderSieges) { | ||
this.renderSieges = renderSieges; | ||
} | ||
|
||
@EventHandler | ||
public void onNewShortTime(NewShortTimeEvent event) { | ||
this.renderSieges.run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚧