From db97cd7944f64e01c63fa6a1feb152c1b5904c52 Mon Sep 17 00:00:00 2001 From: Laika Date: Sun, 24 Apr 2022 00:19:28 -0700 Subject: [PATCH] Dynmap API start and some other fixes... --- pom.xml | 8 +++ src/main/java/moe/oko/opennaw/OpenNAW.java | 26 ++++++++-- src/main/java/moe/oko/opennaw/model/City.java | 23 +++++++- .../java/moe/oko/opennaw/model/Nation.java | 13 +++++ .../moe/oko/opennaw/util/DynmapHandler.java | 52 +++++++++++++++++++ src/main/resources/plugin.yml | 1 + 6 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 src/main/java/moe/oko/opennaw/util/DynmapHandler.java diff --git a/pom.xml b/pom.xml index 333639d..98ebdec 100644 --- a/pom.xml +++ b/pom.xml @@ -78,5 +78,13 @@ 5.4 provided + + webukkit + dynmap + 3.4-beta-3-spigot + ${pom.basedir}/libs/Dynmap-3.4-beta-3-spigot.jar + + system + diff --git a/src/main/java/moe/oko/opennaw/OpenNAW.java b/src/main/java/moe/oko/opennaw/OpenNAW.java index cf5a983..bc977e5 100644 --- a/src/main/java/moe/oko/opennaw/OpenNAW.java +++ b/src/main/java/moe/oko/opennaw/OpenNAW.java @@ -2,12 +2,13 @@ package moe.oko.opennaw; import moe.oko.opennaw.command.CityCommand; import moe.oko.opennaw.command.NationCommand; -import moe.oko.opennaw.util.ChatHandler; -import moe.oko.opennaw.util.CityHandler; -import moe.oko.opennaw.util.GroupHandler; -import moe.oko.opennaw.util.NationHandler; +import moe.oko.opennaw.util.*; import net.luckperms.api.LuckPerms; +import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import org.dynmap.DynmapCommonAPI; +import org.dynmap.DynmapCommonAPIListener; +import org.dynmap.bukkit.DynmapPlugin; import static moe.oko.opennaw.util.CommandHelper.info; @@ -19,6 +20,10 @@ public final class OpenNAW extends JavaPlugin { public GroupHandler groupHandler; private LuckPerms luckPerms; + private DynmapHandler dynmapHandler; + + private boolean dynmapEnabled; + public static OpenNAW getInstance() { return instance; } @@ -44,6 +49,12 @@ public final class OpenNAW extends JavaPlugin { this.getCommand("nation").setExecutor(new NationCommand()); this.getCommand("city").setExecutor(new CityCommand()); getServer().getPluginManager().registerEvents(new ChatHandler(), this); + + if(Bukkit.getPluginManager().isPluginEnabled("dynmap")) { + this.dynmapEnabled = true; + //Only initialize if dyn is loaded. + dynmapHandler = new DynmapHandler(); + } } @Override @@ -51,4 +62,11 @@ public final class OpenNAW extends JavaPlugin { info("OpenNAW is disabled!"); } + public boolean isDynmapEnabled() { + return dynmapEnabled; + } + + public DynmapHandler getDynmapHandler() { + return dynmapHandler; + } } diff --git a/src/main/java/moe/oko/opennaw/model/City.java b/src/main/java/moe/oko/opennaw/model/City.java index 0f156ce..92b95f1 100644 --- a/src/main/java/moe/oko/opennaw/model/City.java +++ b/src/main/java/moe/oko/opennaw/model/City.java @@ -1,5 +1,11 @@ package moe.oko.opennaw.model; +import moe.oko.opennaw.OpenNAW; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.Location; public class City { @@ -13,7 +19,8 @@ public class City { public City(String name, String resource, Location location) { this.name = name; this.resource = resource; - this.owner = null; + //Default is unclaimed. + this.owner = Nation.UNCLAIMED; this.state = CityState.NORMAL; this.point = location; this.health = 300; @@ -33,6 +40,7 @@ public class City { public void setOwner(Nation nation) { this.owner = nation; + capture(); } public Location getPoint() { @@ -46,4 +54,17 @@ public class City { public void damage(short damage) { this.health -= damage; } + + public void capture() { + //This is our "capture" logic. So now we do shenanigans. + if(OpenNAW.getInstance().isDynmapEnabled()) { + //Dyn is enabled, we need to do setters. + OpenNAW.getInstance().getDynmapHandler().updateCityInfoMarker(this); + } + Bukkit.broadcast(Component.text(ChatColor.AQUA + getName() + ChatColor.GOLD + " has been successfully captured by " + ChatColor.RED + this.owner.getName())); + } + + public boolean isUnclaimed() { + return owner == Nation.UNCLAIMED; + } } \ No newline at end of file diff --git a/src/main/java/moe/oko/opennaw/model/Nation.java b/src/main/java/moe/oko/opennaw/model/Nation.java index 543125c..8cbbdd3 100644 --- a/src/main/java/moe/oko/opennaw/model/Nation.java +++ b/src/main/java/moe/oko/opennaw/model/Nation.java @@ -4,14 +4,19 @@ import net.luckperms.api.model.group.Group; import org.bukkit.Location; public class Nation { + public static final Nation UNCLAIMED = new Nation("UNCLAIMED", null); private String name; private Group group; private Location spawn; + //The icon used for cities owned by this nation (default is a tower) + private String dynmapIcon; + public Nation(String name, Group group) { this.name = name; this.group = group; + this.dynmapIcon = "tower"; this.spawn = null; } @@ -21,4 +26,12 @@ public class Nation { public void setSpawn(Location l) { this.spawn = l; } + + public String getDynmapIcon() { + return dynmapIcon; + } + + public void setDynmapIcon(String dynmapIcon) { + this.dynmapIcon = dynmapIcon; + } } diff --git a/src/main/java/moe/oko/opennaw/util/DynmapHandler.java b/src/main/java/moe/oko/opennaw/util/DynmapHandler.java new file mode 100644 index 0000000..0bc06df --- /dev/null +++ b/src/main/java/moe/oko/opennaw/util/DynmapHandler.java @@ -0,0 +1,52 @@ +package moe.oko.opennaw.util; + +import moe.oko.opennaw.model.City; +import org.bukkit.Location; +import org.dynmap.bukkit.DynmapPlugin; +import org.dynmap.markers.Marker; +import org.dynmap.markers.MarkerAPI; +import org.dynmap.markers.MarkerSet; + +public class DynmapHandler { + + public static final String SET_ID = "nawCity"; + private MarkerAPI markerAPI; + private MarkerSet citySet; + + public DynmapHandler() { + markerAPI = DynmapPlugin.plugin.getMarkerAPI(); + if(markerAPI.getMarkerSet(SET_ID) != null) { + citySet = markerAPI.getMarkerSet(SET_ID); + } else { + //Creating a new Dynmap set with the ID predefined (finally) in the class, alongside the label "Cities", + //that has no limit on icon usage (any), and is persistent. (Dynmap has nearly no docs pertaining to api usage). + citySet = markerAPI.createMarkerSet(SET_ID, "Cities", markerAPI.getMarkerIcons(), true); + } + } + + public boolean initializeCityMarker(City city) { + //Generate a new marker pertaining to shenanigans + try { + Location cityLoc = city.getPoint(); + citySet.createMarker(city.getName(), city.getName() + " [**" + city.getOwner().getName() + "**]", + true, cityLoc.getWorld().getName(), cityLoc.getX(), cityLoc.getY(), cityLoc.getZ(), markerAPI.getMarkerIcon(city.getOwner().getDynmapIcon()), true); + return true; + } catch (Exception ex) { + ex.printStackTrace(); + return false; + } + } + + //Updates the name and icon of the marker. + public boolean updateCityInfoMarker(City city) { + try { + Marker marker = citySet.findMarker(city.getName()); + marker.setLabel(city.getName() + " [**" + city.getOwner().getName() + "**]"); + marker.setMarkerIcon(markerAPI.getMarkerIcon(city.getOwner().getDynmapIcon())); + return true; + } catch (Exception ex) { + ex.printStackTrace(); + return false; + } + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 03c4ad6..0950e4b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,6 +3,7 @@ version: '${project.version}' main: moe.oko.opennaw.OpenNAW api-version: 1.18 prefix: OpenNAW +softdepend: [dynmap] authors: [ oko ] description: Open source recreation of Nations at War website: https://oko.moe