Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Staartvin committed May 25, 2020
2 parents f1cb6d9 + dce5a64 commit 3441ada
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/me/staartvin/utils/pluginlibrary/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ 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"),
MCRPG("McRPG", new McRPGHook(), "Eunoians");

private final String internalPluginName;
private final String authorName;
Expand Down
97 changes: 97 additions & 0 deletions src/me/staartvin/utils/pluginlibrary/hooks/McRPGHook.java
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 src/me/staartvin/utils/pluginlibrary/hooks/TownyAdvancedHook.java
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();
}


}

0 comments on commit 3441ada

Please sign in to comment.