Skip to content

Commit

Permalink
Initial release 1.0.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
LlmDl committed May 3, 2024
1 parent be11f18 commit 20868fe
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 95 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.townyadvanced</groupId>
<artifactId>SimplePlugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>simpleplugin</name> <!-- Leave lower-cased -->
<artifactId>TownySpawnPointLimits9000</artifactId>
<version>1.0.0</version>
<name>townyspawnointlimits9000</name> <!-- Leave lower-cased -->

<properties>
<java.version>17</java.version>
Expand Down Expand Up @@ -41,8 +41,8 @@
</dependency>
<dependency>
<groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>Towny</artifactId>
<version>0.99.5.0</version>
<artifactId>towny</artifactId>
<version>0.100.2.7</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.github.townyadvanced.townyspawnointlimits9000;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;

import com.palmergames.bukkit.towny.event.CancellableTownyEvent;
import com.palmergames.bukkit.towny.event.TranslationLoadEvent;
import com.palmergames.bukkit.towny.event.nation.NationSetSpawnEvent;
import com.palmergames.bukkit.towny.event.town.TownSetOutpostSpawnEvent;
import com.palmergames.bukkit.towny.event.town.TownSetSpawnEvent;
import com.palmergames.bukkit.towny.object.Translatable;
import com.palmergames.bukkit.towny.object.TranslationLoader;

import io.github.townyadvanced.townyspawnointlimits9000.settings.Settings;

public class TownyListener implements Listener {

/* Handle re-adding the lang string into Towny when Towny reloads the Translation Registry. */
@EventHandler
public void onTownyLoadLang(TranslationLoadEvent event) {
Plugin plugin = TownySpawnPointLimits9000.getPlugin();
if (!TownySpawnPointLimits9000.hasLocale())
return;
Path langFolderPath = Paths.get(plugin.getDataFolder().getPath()).resolve("lang");
TranslationLoader loader = new TranslationLoader(langFolderPath, plugin, TownySpawnPointLimits9000.class);
loader.load();
Map<String, Map<String, String>> translations = loader.getTranslations();
for (String language : translations.keySet())
for (Map.Entry<String, String> map : translations.get(language).entrySet())
event.addTranslation(language, map.getKey(), map.getValue());
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onTownSetSpawn(TownSetSpawnEvent event) {
if (!Settings.isSpawnYLevelLimitingEnabled())
return;
testY(event, event.getPlayer(), event.getNewSpawn().getY());
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onTownSetOutpostSpawn(TownSetOutpostSpawnEvent event) {
if (!Settings.isSpawnYLevelLimitingEnabled())
return;
testY(event, event.getPlayer(), event.getNewSpawn().getY());
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onNationSetSpawn(NationSetSpawnEvent event) {
if (!Settings.isSpawnYLevelLimitingEnabled())
return;
testY(event, event.getPlayer(), event.getNewSpawn().getY());
}

private void testY(CancellableTownyEvent event, Player player, double y) {
if (y <= Settings.getSpawningHighestYLevelAllowed())
cancelEventTooLow(event, player);
else if (y >= Settings.getSpawningLowestYLevelAllowed())
cancelEventTooHigh(event, player);
}

private void cancelEventTooLow(CancellableTownyEvent event, Player player) {
event.setCancelMessage(Translatable.of("spawn_points9000_msg_err_you_cannot_set_this_spawn_point_below",
Settings.getSpawningLowestYLevelAllowed()).forLocale(player));
event.setCancelled(true);
}

private void cancelEventTooHigh(CancellableTownyEvent event, Player player) {
event.setCancelMessage(Translatable.of("spawn_points9000_msg_err_you_cannot_set_this_spawn_point_above",
Settings.getSpawningLowestYLevelAllowed()).forLocale(player));
event.setCancelled(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.townyadvanced.simpleplugin;
package io.github.townyadvanced.townyspawnointlimits9000;

import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -16,18 +16,19 @@
import com.palmergames.bukkit.towny.scheduling.TaskScheduler;
import com.palmergames.bukkit.towny.scheduling.impl.BukkitTaskScheduler;
import com.palmergames.bukkit.towny.scheduling.impl.FoliaTaskScheduler;
import io.github.townyadvanced.simpleplugin.settings.Settings;

public class SimplePlugin extends JavaPlugin {
import io.github.townyadvanced.townyspawnointlimits9000.settings.Settings;

private static SimplePlugin plugin;
public class TownySpawnPointLimits9000 extends JavaPlugin {

private static TownySpawnPointLimits9000 plugin;
private final Object scheduler;
private static String requiredTownyVersion = "0.99.5.0";
boolean hasConfig = false;
boolean hasLocale = false;
private static String requiredTownyVersion = "0.100.2.7";
boolean hasConfig = true;
boolean hasLocale = true;
boolean hasListeners = true;

public SimplePlugin() {
public TownySpawnPointLimits9000() {
plugin = this;
this.scheduler = townyVersionCheck() ? isFoliaClassPresent() ? new FoliaTaskScheduler(this) : new BukkitTaskScheduler(this) : null;
}
Expand Down Expand Up @@ -69,7 +70,7 @@ public static boolean loadLocalization(boolean reload) {
try {
Plugin plugin = getPlugin();
Path langFolderPath = Paths.get(plugin.getDataFolder().getPath()).resolve("lang");
TranslationLoader loader = new TranslationLoader(langFolderPath, plugin, SimplePlugin.class);
TranslationLoader loader = new TranslationLoader(langFolderPath, plugin, TownySpawnPointLimits9000.class);
loader.load();
TownyAPI.getInstance().addTranslations(plugin, loader.getTranslations());
} catch (TownyInitException e) {
Expand All @@ -91,7 +92,7 @@ public String getVersion() {
return this.getDescription().getVersion();
}

public static SimplePlugin getPlugin() {
public static TownySpawnPointLimits9000 getPlugin() {
return plugin;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.townyadvanced.simpleplugin.settings;
package io.github.townyadvanced.townyspawnointlimits9000.settings;

public enum ConfigNodes {

Expand All @@ -7,9 +7,20 @@ public enum ConfigNodes {
"version.version",
"",
"# This is the current version. Please do not edit."),
LANGUAGE("language",
"english.yml",
"# The language file you wish to use");
SPAWNING_ROOT("spawning.y_limits","",""),
SPAWNING_Y_LIMITS_ROOT("spawning.y_limits","",""),
SPAWNING_Y_LIMITS_ENABLED("spawning.y_limits.enabled",
"false",
"",
"# Should towns, nations be limited in how low they can set a spawn point, or an outpost spawn point?"),
SPAWNING_Y_LIMITS_LOWEST_Y_ALLOWED("spawning.y_limits.lowest_y_allowed",
"0",
"",
"# When limiting is configured, what is the lowest y level allowed?"),
SPAWNING_Y_LIMITS_HIGHEST_Y_ALLOWED("spawning.y_limits.highest_y_allowed",
"100",
"",
"# When limiting is configured, what is the highest y level allowed?");

private final String Root;
private final String Default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.github.townyadvanced.simpleplugin.settings;
package io.github.townyadvanced.townyspawnointlimits9000.settings;

import java.nio.file.Path;

import com.palmergames.bukkit.config.CommentedConfiguration;
import com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException;
import com.palmergames.util.FileMgmt;

import io.github.townyadvanced.simpleplugin.SimplePlugin;
import io.github.townyadvanced.townyspawnointlimits9000.TownySpawnPointLimits9000;

public class Settings {
private static CommentedConfiguration config, newConfig;
private static Path configPath = SimplePlugin.getPlugin().getDataFolder().toPath().resolve("config.yml");
private static Path configPath = TownySpawnPointLimits9000.getPlugin().getDataFolder().toPath().resolve("config.yml");

public static void loadConfig() {
if (FileMgmt.checkOrCreateFile(configPath.toString())) {
Expand All @@ -20,7 +20,7 @@ public static void loadConfig() {
if (!config.load())
throw new TownyInitException("Failed to load config.yml.", TownyInitException.TownyError.MAIN_CONFIG);

setDefaults(SimplePlugin.getPlugin().getVersion(), configPath);
setDefaults(TownySpawnPointLimits9000.getPlugin().getVersion(), configPath);
config.save();
}
}
Expand Down Expand Up @@ -77,7 +77,7 @@ public static String getString(String root, String def) {

private static void sendError(String msg) {

SimplePlugin.severe("Error could not read " + msg);
TownySpawnPointLimits9000.severe("Error could not read " + msg);
}

public static boolean getBoolean(ConfigNodes node) {
Expand Down Expand Up @@ -110,4 +110,16 @@ public static String getString(ConfigNodes node) {
return config.getString(node.getRoot().toLowerCase(), node.getDefault());
}


public static boolean isSpawnYLevelLimitingEnabled() {
return getBoolean(ConfigNodes.SPAWNING_Y_LIMITS_ENABLED);
}

public static double getSpawningLowestYLevelAllowed() {
return getDouble(ConfigNodes.SPAWNING_Y_LIMITS_LOWEST_Y_ALLOWED);
}

public static double getSpawningHighestYLevelAllowed() {
return getDouble(ConfigNodes.SPAWNING_Y_LIMITS_HIGHEST_Y_ALLOWED);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/lang/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ description: >
# DarkPurple = &9, LightGreen = &a, LightBlue = &b
# Rose = &c, LightPurple = &d, Yellow = &e, White = &f


spawn_points9000_msg_err_you_cannot_set_this_spawn_point_below: "You cannot set this spawn point below y level %s."
spawn_points9000_msg_err_you_cannot_set_this_spawn_point_above: "You cannot set this spawn point above y level %s."

0 comments on commit 20868fe

Please sign in to comment.