Skip to content

Commit

Permalink
Add web map integration to status screens (#5484)
Browse files Browse the repository at this point in the history
* Add web map integration to status screens

* Add hover message
  • Loading branch information
Warriorrrr authored Dec 2, 2021
1 parent 33e53fa commit f522ba3
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 12 deletions.
7 changes: 5 additions & 2 deletions resources/lang/en-US.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Towny
version: 0.135
version: 0.136
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down Expand Up @@ -1577,4 +1577,7 @@ tc_player_channel_sound_muted: 'The channel sound has been muted'
tc_player_channel_sound_unmuted: 'The channel sound has been unmuted'

#Added in 0.135
msg_cant_toggle_pvp_outsider_in_plot: 'There is an outsider in your plot, you can''t change your pvp status!'
msg_cant_toggle_pvp_outsider_in_plot: 'There is an outsider in your plot, you can''t change your pvp status!'

#Added in 0.136
msg_view_on_web: 'Click to view on the web.'
17 changes: 16 additions & 1 deletion src/com/palmergames/bukkit/config/ConfigNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1053,12 +1053,27 @@ public enum ConfigNodes {
"# If Reserve/Vault is not present it will attempt to find a supported economy plugin.",
"# If neither Vault/Reserve or supported economy are present it will not be possible to create towns or do any operations that require money."),

PLUGINS_LUCKPERMS_CONTEXTS(
PLUGIN_LUCKPERMS_CONTEXTS(
"plugin.interfacing.luckperms_contexts",
"false",
"",
"# If enabled, Towny contexts will be available in LuckPerms.",
"# https://luckperms.net/wiki/Context"),

PLUGIN_WEB_MAP_USING_STATUSSCREEN(
"plugin.interfacing.web_map.enabled",
"false",
"",
"# If enabled, players will be prompted to open a url when clicking on coordinates in towny status screens."
),

PLUGIN_WEB_MAP_URL(
"plugin.interfacing.web_map.url",
"https://example.com/map/?worldname={world}&mapname=flat&zoom=5&x={x}&y=64&z={z}",
"",
"# The url that players will be prompted to open when clicking on a coordinate in a status screen.",
"# Valid placeholders are {world}, {x}, and {y}, for the world name, x, and y coordinates respectively."
),

PLUGIN_DAY_HEADER("plugin.day_timer", "", ""),
PLUGIN_DAY_INTERVAL(
Expand Down
43 changes: 36 additions & 7 deletions src/com/palmergames/bukkit/towny/TownyFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.ResidentList;
import com.palmergames.bukkit.towny.object.SpawnLocation;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownBlock;
import com.palmergames.bukkit.towny.object.TownBlockType;
Expand All @@ -31,6 +32,7 @@
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
Expand Down Expand Up @@ -299,11 +301,20 @@ public static StatusScreen getStatus(Town town, Locale locale) {
screen.addComponentOf("townblocks", colourKeyValue(translator.of("status_town_size"), translator.of("status_fractions", town.getTownBlocks().size(), TownySettings.getMaxTownBlocks(town))) +
(TownySettings.isSellingBonusBlocks(town) ? colourBracketElement(translator.of("status_town_size_bought"), translator.of("status_fractions", town.getPurchasedBlocks(), TownySettings.getMaxPurchasedBlocks(town))) : "") +
(town.getBonusBlocks() > 0 ? colourBracketElement(translator.of("status_town_size_bonus"), String.valueOf(town.getBonusBlocks())) : "") +
(TownySettings.getNationBonusBlocks(town) > 0 ? colourBracketElement(translator.of("status_town_size_nationbonus"), String.valueOf(TownySettings.getNationBonusBlocks(town))) : "") +
(!town.isPublic() ? "" : (translator.of("status_home_element", (TownySettings.getTownDisplaysXYZ() ?
(town.hasSpawn() ? BukkitTools.convertCoordtoXYZ(town.getSpawnOrNull()) : translator.of("status_no_town")) :
(town.hasHomeBlock() ? town.getHomeBlockOrNull().getCoord().toString() : translator.of("status_no_town"))
)))));
(TownySettings.getNationBonusBlocks(town) > 0 ? colourBracketElement(translator.of("status_town_size_nationbonus"), String.valueOf(TownySettings.getNationBonusBlocks(town))) : ""));

if (town.isPublic()) {
Component homeComponent = Component.text(!town.isPublic() ? "" : (translator.of("status_home_element", (TownySettings.getTownDisplaysXYZ() ?
(town.hasSpawn() ? BukkitTools.convertCoordtoXYZ(town.getSpawnOrNull()) : translator.of("status_no_town")) :
(town.hasHomeBlock() ? town.getHomeBlockOrNull().getCoord().toString() : translator.of("status_no_town"))
))));

String webUrl = formatWebUrl(town);
if (!webUrl.isEmpty())
homeComponent = homeComponent.clickEvent(ClickEvent.openUrl(webUrl)).hoverEvent(HoverEvent.showText(Component.text(translator.of("msg_view_on_web"), NamedTextColor.GRAY)));

screen.addComponentOf("home", homeComponent);
}

// Outposts: 3
if (TownySettings.isAllowingOutposts()) {
Expand Down Expand Up @@ -456,8 +467,15 @@ public static StatusScreen getStatus(Nation nation, Locale locale) {
screen.addComponentOf("bankLine", bankline);
}

if (nation.isPublic())
screen.addComponentOf("home", translator.of("status_home_element", (nation.hasSpawn() ? Coord.parseCoord(nation.getSpawnOrNull()).toString() : translator.of("status_no_town"))));
if (nation.isPublic()) {
Component homeComponent = Component.text(translator.of("status_home_element", (nation.hasSpawn() ? Coord.parseCoord(nation.getSpawnOrNull()).toString() : translator.of("status_no_town"))));

String webUrl = formatWebUrl(nation);
if (!webUrl.isEmpty())
homeComponent = homeComponent.clickEvent(ClickEvent.openUrl(webUrl)).hoverEvent(HoverEvent.showText(Component.text(translator.of("msg_view_on_web"), NamedTextColor.GRAY)));

screen.addComponentOf("home", homeComponent);
}

// King: King Harlus
if (nation.getNumTowns() > 0 && nation.hasCapital() && nation.getCapital().hasMayor()) {
Expand Down Expand Up @@ -1069,6 +1087,17 @@ public static String getTime() {
return sdf.format(System.currentTimeMillis());
}

private static String formatWebUrl(SpawnLocation spawnLocation) {
String webUrl = "";
if (TownySettings.isUsingWebMapStatusScreens() && spawnLocation.hasSpawn() && !TownySettings.getWebMapUrl().isEmpty())
webUrl = TownySettings.getWebMapUrl()
.replaceAll("\\{world}", spawnLocation.getSpawnOrNull().getWorld().getName())
.replaceAll("\\{x}", "" + spawnLocation.getSpawnOrNull().getBlockX())
.replaceAll("\\{z}", "" + spawnLocation.getSpawnOrNull().getBlockZ());

return webUrl;
}

/*
* Deprecated methods.
*/
Expand Down
10 changes: 9 additions & 1 deletion src/com/palmergames/bukkit/towny/TownySettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2982,7 +2982,7 @@ public static List<String> getTouristBlockedCommands() {
}

public static boolean isContextsEnabled() {
return getBoolean(ConfigNodes.PLUGINS_LUCKPERMS_CONTEXTS);
return getBoolean(ConfigNodes.PLUGIN_LUCKPERMS_CONTEXTS);
}

public static boolean isShowingUpdateNotifications() {
Expand Down Expand Up @@ -3032,5 +3032,13 @@ public static void saveConfig() {
public static long getSpawnProtection() {
return TimeTools.getTicks(getString(ConfigNodes.GTOWN_SETTINGS_RESPAWN_PROTECTION));
}

public static boolean isUsingWebMapStatusScreens() {
return getBoolean(ConfigNodes.PLUGIN_WEB_MAP_USING_STATUSSCREEN);
}

public static String getWebMapUrl() {
return getString(ConfigNodes.PLUGIN_WEB_MAP_URL);
}
}

8 changes: 8 additions & 0 deletions src/com/palmergames/bukkit/towny/object/SpawnLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ default boolean hasSpawn(){
* @throws TownyException If the spawn could not be set.
*/
void setSpawn(Location spawn) throws TownyException;

default Location getSpawnOrNull() {
try {
return getSpawn();
} catch (TownyException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void addComponentOf(String name, String text) {
components.put(name, Component.text(text));
}

public void addComponentOf(String name, TextComponent component) {
public void addComponentOf(String name, Component component) {
components.put(name, component);
}

Expand Down

0 comments on commit f522ba3

Please sign in to comment.