Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 71 additions & 31 deletions src/main/java/codes/antti/bluemaptowny/BlueMapTowny.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
package codes.antti.bluemaptowny;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import codes.antti.bluemaptowny.TownyCommands.SetTownMarker;
import com.flowpowered.math.vector.Vector2d;
import com.technicjelle.BMUtils.Cheese;
import de.bluecolored.bluemap.api.markers.*;
import org.apache.commons.lang.WordUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.Configuration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import com.flowpowered.math.vector.Vector2i;
import com.gmail.goosius.siegewar.SiegeWarAPI;
import com.gmail.goosius.siegewar.enums.SiegeSide;
import com.gmail.goosius.siegewar.objects.Siege;
import com.gmail.goosius.siegewar.settings.SiegeWarSettings;
import com.palmergames.bukkit.config.ConfigNodes;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.TownyEconomyHandler;
import com.palmergames.bukkit.towny.TownyFormatter;
import com.palmergames.bukkit.towny.TownySettings;
import com.palmergames.bukkit.towny.*;
import com.palmergames.bukkit.towny.object.AddonCommand;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyObject;
import com.palmergames.bukkit.towny.object.TownyWorld;
import com.palmergames.bukkit.towny.utils.TownRuinUtil;
import com.technicjelle.BMUtils.Cheese;
import com.technicjelle.UpdateChecker;

import de.bluecolored.bluemap.api.BlueMapAPI;
import de.bluecolored.bluemap.api.markers.Marker;
import de.bluecolored.bluemap.api.markers.MarkerSet;
import de.bluecolored.bluemap.api.markers.POIMarker;
import de.bluecolored.bluemap.api.markers.ShapeMarker;
import de.bluecolored.bluemap.api.math.Color;
import de.bluecolored.bluemap.api.math.Shape;
import org.apache.commons.lang.WordUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.Configuration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public final class BlueMapTowny extends JavaPlugin {


private final Map<UUID, MarkerSet> townMarkerSets = new ConcurrentHashMap<>();
private Configuration config;

public static BlueMapTowny plugin;

@Override
public void onEnable() {
try {
Expand All @@ -61,7 +60,10 @@ public void onEnable() {
reloadConfig();
saveDefaultConfig();
this.config = getConfig();
this.plugin = this;
initMarkerSets();
registerCommands();
makeAssetsFolder();
if (isFolia) {
Bukkit.getServer().getAsyncScheduler().runAtFixedRate(this, task -> this.updateMarkers(), 1L, this.config.getLong("update-interval"), TimeUnit.SECONDS);
} else {
Expand Down Expand Up @@ -205,7 +207,8 @@ private String fillPlaceholders(String template, Town town) {
flags.add("Nation: " + nation);
if (TownySettings.getBoolean(ConfigNodes.TOWN_RUINING_TOWN_RUINS_ENABLED)) {
String ruinedString = "Ruined: " + town.isRuined();
if (town.isRuined()) ruinedString += " (Time left: " + (TownySettings.getTownRuinsMaxDurationHours() - TownRuinUtil.getTimeSinceRuining(town)) + " hours)";
if (town.isRuined())
ruinedString += " (Time left: " + (TownySettings.getTownRuinsMaxDurationHours() - TownRuinUtil.getTimeSinceRuining(town)) + " hours)";
flags.add(ruinedString);
}
t = t.replace("%flags%", String.join("<br />", flags));
Expand All @@ -222,7 +225,7 @@ private String fillPlaceholders(String template, Town town) {
t = t.replace("%town_resources%", "");
}

if(getServer().getPluginManager().isPluginEnabled("SiegeWar")) {
if (getServer().getPluginManager().isPluginEnabled("SiegeWar")) {
if (SiegeWarAPI.hasSiege(town)) {
Siege siege = SiegeWarAPI.getSiege(town).get();
t = t.replace("%attacker%", siege.getAttackerNameForDisplay());
Expand All @@ -240,7 +243,7 @@ private String fillPlaceholders(String template, Town town) {
}

t = t.replace("%banner_control%", WordUtils.capitalizeFully(siege.getBannerControllingSide().name())
+ (siege.getBannerControllingSide() == SiegeSide.NOBODY ? "" : " (" + siege.getBannerControllingResidents().size() + ")"));
+ (siege.getBannerControllingSide() == SiegeSide.NOBODY ? "" : " (" + siege.getBannerControllingResidents().size() + ")"));

t = t.replace("%siege_status%", siege.getStatus().getName());

Expand All @@ -258,6 +261,29 @@ private String fillPlaceholders(String template, Town town) {
return t;
}

private String fillTownyIcons(Town town) {
String icon;
if (town.hasMeta("mapMarker")){
switch (config.getInt("use-links-as-image-source")) {
case 1 -> {
icon = "assets/TownMarkers/" + town.getMetadata("mapMarker").getValue().toString();
return icon;
}
case 2 -> {
icon = town.getMetadata("mapMarker").getValue().toString();
return icon;
}
default -> {
icon = this.config.getString("style.home-icon");
return icon;
}
}
}else{
icon = this.config.getString("style.home-icon");
}
return icon;
}

private void updateMarkers() {
BlueMapAPI.getInstance().ifPresent((api) -> {
for (World world : Bukkit.getWorlds()) {
Expand Down Expand Up @@ -328,7 +354,7 @@ private void updateMarkers() {
POIMarker iconMarker = new POIMarker.Builder()
.label(townName)
.detail(townDetails)
.icon(this.config.getString("style.home-icon"), 8, 8)
.icon(fillTownyIcons(town), 8, 8)
.styleClasses("towny-icon")
.position(spawn.get().getX(), layerY, spawn.get().getZ())
.build();
Expand All @@ -351,4 +377,18 @@ private void updateMarkers() {
}
});
}

private void registerCommands(){
if(config.getInt("use-links-as-image-source") == 1 || config.getInt("use-links-as-image-source") == 2){
AddonCommand setTownMarker = new AddonCommand(TownyCommandAddonAPI.CommandType.TOWN_SET, "marker", new SetTownMarker());
TownyCommandAddonAPI.addSubCommand(setTownMarker);
}
}

private void makeAssetsFolder(){
File file = new File(BlueMapAPI.getInstance().get().getWebApp().getWebRoot().toFile(), "/assets/TownMarkers");
if(!file.exists()){
file.mkdir();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package codes.antti.bluemaptowny.TownyCommands;

import codes.antti.bluemaptowny.BlueMapTowny;
import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.metadata.StringDataField;
import de.bluecolored.bluemap.api.BlueMapAPI;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SetTownMarker implements CommandExecutor, TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player)) {
TownyMessaging.sendErrorMsg(sender, "Must be run by a Player!");
return true;
}
Resident resident = TownyUniverse.getInstance().getResident(((Player) sender).getUniqueId());

if (!resident.hasTown()) {
TownyMessaging.sendErrorMsg(sender, "You don't have a town!");
return true;
}

if (!resident.isMayor() && !((Player) sender).getPlayer().hasPermission("towny.command.town.set.townmarker")) {
TownyMessaging.sendErrorMsg(sender, "You don't have permission for this or aren't the Mayor!");
return true;
}

TownyMessaging.sendPrefixedTownMessage(resident.getTownOrNull(), "Town Marker has been set to: " + String.join(" ", args));
resident.getTownOrNull().addMetaData(new StringDataField("mapMarker", String.join(" ", args)));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command should check that the marker is actually one of the markers in the presets if the preset mode is selected. Currently one can set anything and even do path traversal (../) to set unrelated files.



return true;
}

@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
Plugin plugin = BlueMapTowny.plugin;
switch (plugin.getConfig().getInt("use-links-as-image-source")) {
case 1 -> {
File file = new File(BlueMapAPI.getInstance().get().getWebApp().getWebRoot().toFile(), "/assets/TownMarkers");
String[] listOfFiles = file.list();
if (args.length == 1) {
return Arrays.stream(listOfFiles).toList();
}
return Collections.emptyList();
}
case 2 -> {
return Collections.singletonList("<insert link here>");
}
default -> {
return Collections.emptyList();
}
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ popup-siege: '<span style="font-size: 120%"><strong>Siege: %attacker% vs %defend
# If you want claims with holes in them to show up as fully claimed if only the perimeter is claimed.
# This means that the map will LIE about the actual claim status of the area!!!
lie-about-holes: false
# If you want to have towns have the option to use their own image markers (By default it's only a locked to 1 default image for every town)
# 0 for default, 1 as customizable within a preset of images inside a folder created by the plugin, 2 as letting the town/players choose their own town marker via a link (BE WARNED THAT YOU HAVE TO TRUST THE GOODWILL OF YOUR PLAYERS FOR THIS)
use-links-as-image-source: 0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would much prefer for this option to be removed and the home-icon-enabled option to be renamed as home-icon-style with string options "disabled", "only-default", "preset" and "link"
Should also probably have a warning that changing between the preset and link types will break existing markers.
Also update the config in the README file.

style:
# Y-level to put markers at
y-level: 62
Expand Down