From b29160dae38564bb94711624ca313c48ca18529f Mon Sep 17 00:00:00 2001 From: Staartvin Date: Fri, 15 May 2020 19:55:12 +0200 Subject: [PATCH 1/2] Add support for TownyAdvanced. --- .../utils/pluginlibrary/Library.java | 3 +- .../hooks/TownyAdvancedHook.java | 143 ++++++++++++++++++ 2 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 src/me/staartvin/utils/pluginlibrary/hooks/TownyAdvancedHook.java diff --git a/src/me/staartvin/utils/pluginlibrary/Library.java b/src/me/staartvin/utils/pluginlibrary/Library.java index 9374e13..b237fb0 100644 --- a/src/me/staartvin/utils/pluginlibrary/Library.java +++ b/src/me/staartvin/utils/pluginlibrary/Library.java @@ -39,7 +39,8 @@ public enum Library { NUVOTIFIER("Votifier", "NuVotifier", new NuVotifierHook(), "Ichbinjoe", "com.vexsoftware.votifier" + ".NuVotifierBukkit"), CMI("CMI", new CMIHook(), "Zrips"), - UHCSTATS("UhcStats", new UHCStatsHook(), "Mezy"); + UHCSTATS("UhcStats", new UHCStatsHook(), "Mezy"), + TOWNY_ADVANCED("Towny", new TownyAdvancedHook(), "Shade"); private final String internalPluginName; private final String authorName; diff --git a/src/me/staartvin/utils/pluginlibrary/hooks/TownyAdvancedHook.java b/src/me/staartvin/utils/pluginlibrary/hooks/TownyAdvancedHook.java new file mode 100644 index 0000000..b923610 --- /dev/null +++ b/src/me/staartvin/utils/pluginlibrary/hooks/TownyAdvancedHook.java @@ -0,0 +1,143 @@ +package me.staartvin.utils.pluginlibrary.hooks; + +import com.palmergames.bukkit.towny.TownyAPI; +import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; +import com.palmergames.bukkit.towny.object.Resident; +import me.staartvin.utils.pluginlibrary.Library; + +import java.util.Optional; + +/** + * TownyAdvanced library, + * link. + *

+ * + * @author Staartvin + */ +public class TownyAdvancedHook extends LibraryHook { + + /* + * (non-Javadoc) + * + * @see me.staartvin.plugins.pluginlibrary.hooks.LibraryHook#isAvailable() + */ + @Override + public boolean isAvailable() { + return this.getServer().getPluginManager().isPluginEnabled(Library.TOWNY_ADVANCED.getInternalPluginName()); + } + + @Override + public boolean isHooked() { + return isAvailable(); + } + + /* + * (non-Javadoc) + * + * @see me.staartvin.plugins.pluginlibrary.hooks.LibraryHook#hook() + */ + @Override + public boolean hook() { + return isAvailable(); + } + + public Optional getResident(String playerName) { + if (!this.isHooked()) return Optional.empty(); + + Resident resident = null; + + try { + resident = TownyAPI.getInstance().getDataSource().getResident(playerName); + } catch (NotRegisteredException e) { + return Optional.empty(); + } + + return Optional.ofNullable(resident); + } + + /** + * Check whether the given player is part of a town. + * + * @param playerName Name of the player + * @return True if the player is part of a town. False otherwise. + */ + public boolean hasTown(String playerName) { + Resident resident = this.getResident(playerName).orElse(null); + + if (resident == null) return false; + + return resident.hasTown(); + } + + /** + * Check whether the given player is part of a nation. + * + * @param playerName Name of the player + * @return True if the player is part of a nation. False otherwise. + */ + public boolean hasNation(String playerName) { + Resident resident = this.getResident(playerName).orElse(null); + + if (resident == null) return false; + + return resident.hasNation(); + } + + /** + * Check whether the given player is a king of a nation. + * + * @param playerName Name of the player + * @return true if the player is a king of a nation. + */ + public boolean isKing(String playerName) { + Resident resident = this.getResident(playerName).orElse(null); + + if (resident == null) return false; + + return resident.isKing(); + } + + /** + * Check whether the given player is a mayor of a town. + * + * @param playerName Name of the player + * @return true if the player is a mayor of a town. + */ + public boolean isMayor(String playerName) { + Resident resident = this.getResident(playerName).orElse(null); + + if (resident == null) return false; + + return resident.isMayor(); + } + + /** + * Check whether the given player is jailed in their town. + * + * @param playerName Name of the player + * @return true if the player is jailed, false otherwise. + */ + public boolean isJailed(String playerName) { + Resident resident = this.getResident(playerName).orElse(null); + + if (resident == null) return false; + + return resident.isJailed(); + } + + /** + * Get the number of town blocks the given player owns. + * + * @param playerName Name of the player + * @return the number of town blocks the player owns, or zero if this is not known. + */ + public int getNumberOfTownBlocks(String playerName) { + Resident resident = this.getResident(playerName).orElse(null); + + if (resident == null) return 0; + + return resident.getTownBlocks().size(); + } + + +} From dce5a645d56c45e3e76e1d4a357c06090cc0abcf Mon Sep 17 00:00:00 2001 From: Staartvin Date: Fri, 15 May 2020 20:41:09 +0200 Subject: [PATCH 2/2] Add support for McRPG. --- .../utils/pluginlibrary/Library.java | 3 +- .../utils/pluginlibrary/hooks/McRPGHook.java | 97 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/me/staartvin/utils/pluginlibrary/hooks/McRPGHook.java diff --git a/src/me/staartvin/utils/pluginlibrary/Library.java b/src/me/staartvin/utils/pluginlibrary/Library.java index b237fb0..f2cb8f5 100644 --- a/src/me/staartvin/utils/pluginlibrary/Library.java +++ b/src/me/staartvin/utils/pluginlibrary/Library.java @@ -40,7 +40,8 @@ public enum Library { ".NuVotifierBukkit"), CMI("CMI", new CMIHook(), "Zrips"), UHCSTATS("UhcStats", new UHCStatsHook(), "Mezy"), - TOWNY_ADVANCED("Towny", new TownyAdvancedHook(), "Shade"); + TOWNY_ADVANCED("Towny", new TownyAdvancedHook(), "Shade"), + MCRPG("McRPG", new McRPGHook(), "Eunoians"); private final String internalPluginName; private final String authorName; diff --git a/src/me/staartvin/utils/pluginlibrary/hooks/McRPGHook.java b/src/me/staartvin/utils/pluginlibrary/hooks/McRPGHook.java new file mode 100644 index 0000000..db73bd7 --- /dev/null +++ b/src/me/staartvin/utils/pluginlibrary/hooks/McRPGHook.java @@ -0,0 +1,97 @@ +package me.staartvin.utils.pluginlibrary.hooks; + +import me.staartvin.utils.pluginlibrary.Library; +import us.eunoians.mcrpg.api.exceptions.McRPGPlayerNotFoundException; +import us.eunoians.mcrpg.players.McRPGPlayer; +import us.eunoians.mcrpg.players.PlayerManager; +import us.eunoians.mcrpg.types.Skills; + +import java.util.Optional; +import java.util.UUID; + +/** + * McRPG library, + * link. + *

+ * + * @author Staartvin + */ +public class McRPGHook extends LibraryHook { + + /* + * (non-Javadoc) + * + * @see me.staartvin.plugins.pluginlibrary.hooks.LibraryHook#isAvailable() + */ + @Override + public boolean isAvailable() { + return this.getServer().getPluginManager().isPluginEnabled(Library.MCRPG.getInternalPluginName()); + } + + @Override + public boolean isHooked() { + return isAvailable(); + } + + /* + * (non-Javadoc) + * + * @see me.staartvin.plugins.pluginlibrary.hooks.LibraryHook#hook() + */ + @Override + public boolean hook() { + // All api calls are done static, so there is no need to get the plugin + // class. + // We only check if the plugin is available. + + return isAvailable(); + } + + /** + * Get the {@link McRPGPlayer} object matching the given uuid. + * + * @param uuid UUID of the player. + * @return Matched object or none if it doesn't exist. + */ + public Optional getPlayer(UUID uuid) { + + if (!this.isHooked()) return Optional.empty(); + + try { + return Optional.ofNullable(PlayerManager.getPlayer(uuid)); + } catch (McRPGPlayerNotFoundException e) { + return Optional.of(new McRPGPlayer(uuid)); + } + } + + /** + * Get the power level of the given player. + * + * @param uuid UUID of the player + * @return power level or zero if the player could not be found. + */ + public int getPowerLevel(UUID uuid) { + Optional player = this.getPlayer(uuid); + + return player.map(McRPGPlayer::getPowerLevel).orElse(0); + } + + /** + * Get the level of a skill for the given player + * + * @param uuid UUID of the player + * @param skillName Name of the skill (use {@link Skills} as reference) + * @return level of a skill or zero if the skill or player cannot be found. + */ + public int getSkillLevel(UUID uuid, String skillName) { + Optional player = this.getPlayer(uuid); + + if (!player.isPresent()) return 0; + + Skills matchingSkill = Skills.fromString(skillName); + + if (matchingSkill == null) return 0; + + return player.map(play -> play.getSkill(matchingSkill).getCurrentLevel()).orElse(0); + } +}