-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
3 changed files
with
243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
* <a href="https://www.spigotmc.org/resources/mcrpg.63020/">link</a>. | ||
* <p> | ||
* | ||
* @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<McRPGPlayer> 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<McRPGPlayer> 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<McRPGPlayer> 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); | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
src/me/staartvin/utils/pluginlibrary/hooks/TownyAdvancedHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
* <a href="https://www.spigotmc.org/resources/towny-advanced.72694/">link</a>. | ||
* <p> | ||
* | ||
* @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<Resident> 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(); | ||
} | ||
|
||
|
||
} |