use of com.wasteofplastic.acidisland.events.ChallengeLevelCompleteEvent in project acidisland by tastybento.
the class Challenges method onCommand.
/*
* (non-Javadoc)
* @see
* org.bukkit.command.CommandExecutor#onCommand(org.bukkit.command.CommandSender
* , org.bukkit.command.Command, java.lang.String, java.lang.String[])
*/
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] cmd) {
if (!(sender instanceof Player)) {
return false;
}
final Player player = (Player) sender;
if (player.getUniqueId() == null) {
return false;
}
// Check permissions
if (!VaultHelper.checkPerm(player, Settings.PERMPREFIX + "island.challenges")) {
Util.sendMessage(player, ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorNoPermission);
return true;
}
// Check island
if (plugin.getGrid().getIsland(player.getUniqueId()) == null) {
Util.sendMessage(player, ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorNoIsland);
return true;
}
switch(cmd.length) {
case 0:
// Display panel
player.openInventory(challengePanel(player));
return true;
case 1:
if (cmd[0].equalsIgnoreCase("help") || cmd[0].equalsIgnoreCase("complete") || cmd[0].equalsIgnoreCase("c")) {
Util.sendMessage(sender, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengeshelp1);
Util.sendMessage(sender, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengeshelp2);
} else if (isLevelAvailable(player, getChallengeConfig().getString("challenges.challengeList." + cmd[0].toLowerCase().replaceAll("\\.", "") + ".level"))) {
// Provide info on the challenge
// Challenge Name
// Description
// Type
// Items taken or not
// island or not
final String challenge = cmd[0].toLowerCase().toLowerCase().replaceAll("\\.", "");
Util.sendMessage(sender, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesname + ": " + ChatColor.WHITE + challenge);
Util.sendMessage(sender, ChatColor.WHITE + plugin.myLocale(player.getUniqueId()).challengeslevel + ": " + ChatColor.GOLD + getChallengeConfig().getString("challenges.challengeList." + challenge + ".level", ""));
String desc = ChatColor.translateAlternateColorCodes('&', getChallengeConfig().getString("challenges.challengeList." + challenge + ".description", "").replace("[label]", Settings.ISLANDCOMMAND));
List<String> result = new ArrayList<String>();
if (desc.contains("|")) {
result.addAll(Arrays.asList(desc.split("\\|")));
} else {
result.add(desc);
}
for (String line : result) {
Util.sendMessage(sender, ChatColor.GOLD + line);
}
final String type = getChallengeConfig().getString("challenges.challengeList." + challenge + ".type", "").toLowerCase();
if (type.equals("inventory")) {
if (getChallengeConfig().getBoolean("challenges.challengeList." + cmd[0].toLowerCase() + ".takeItems")) {
Util.sendMessage(sender, ChatColor.RED + plugin.myLocale(player.getUniqueId()).challengesitemTakeWarning);
}
} else if (type.equals("island")) {
Util.sendMessage(sender, ChatColor.RED + plugin.myLocale(player.getUniqueId()).challengeserrorItemsNotThere);
}
if (plugin.getPlayers().checkChallenge(player.getUniqueId(), challenge) && (!type.equals("inventory") || !getChallengeConfig().getBoolean("challenges.challengeList." + challenge + ".repeatable", false))) {
Util.sendMessage(sender, ChatColor.RED + plugin.myLocale(player.getUniqueId()).challengesnotRepeatable);
return true;
}
double moneyReward = 0;
int expReward = 0;
String rewardText = "";
if (!plugin.getPlayers().checkChallenge(player.getUniqueId(), challenge)) {
// First time
moneyReward = getChallengeConfig().getDouble("challenges.challengeList." + challenge.toLowerCase() + ".moneyReward", 0D);
rewardText = ChatColor.translateAlternateColorCodes('&', getChallengeConfig().getString("challenges.challengeList." + challenge.toLowerCase() + ".rewardText", "Goodies!"));
expReward = getChallengeConfig().getInt("challenges.challengeList." + challenge + ".expReward", 0);
Util.sendMessage(sender, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesfirstTimeRewards);
} else {
// Repeat challenge
moneyReward = getChallengeConfig().getDouble("challenges.challengeList." + challenge.toLowerCase() + ".repeatMoneyReward", 0D);
rewardText = ChatColor.translateAlternateColorCodes('&', getChallengeConfig().getString("challenges.challengeList." + challenge.toLowerCase() + ".repeatRewardText", "Goodies!"));
expReward = getChallengeConfig().getInt("challenges.challengeList." + challenge + ".repeatExpReward", 0);
Util.sendMessage(sender, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesrepeatRewards);
}
Util.sendMessage(sender, ChatColor.WHITE + rewardText);
if (expReward > 0) {
Util.sendMessage(sender, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesexpReward + ": " + ChatColor.WHITE + expReward);
}
if (Settings.useEconomy && moneyReward > 0) {
Util.sendMessage(sender, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesmoneyReward + ": " + ChatColor.WHITE + VaultHelper.econ.format(moneyReward));
}
Util.sendMessage(sender, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengestoCompleteUse + ChatColor.WHITE + " /" + label + " c " + challenge);
} else {
Util.sendMessage(sender, ChatColor.RED + plugin.myLocale(player.getUniqueId()).challengesinvalidChallengeName);
}
return true;
case 2:
if (cmd[0].equalsIgnoreCase("complete") || cmd[0].equalsIgnoreCase("c")) {
if (!player.getWorld().equals(ASkyBlock.getIslandWorld())) {
// Check if in new nether
if (!Settings.createNether || !Settings.newNether || ASkyBlock.getNetherWorld() == null || !player.getWorld().equals(ASkyBlock.getNetherWorld())) {
Util.sendMessage(player, ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorWrongWorld);
return true;
}
}
if (checkIfCanCompleteChallenge(player, cmd[1].toLowerCase().toLowerCase().replaceAll("\\.", ""))) {
int oldLevel = getLevelDone(player);
giveReward(player, cmd[1].toLowerCase().toLowerCase().replaceAll("\\.", ""));
// Save player
plugin.getPlayers().save(player.getUniqueId());
int newLevel = getLevelDone(player);
// plugin.getLogger().info("DEBUG: " + oldLevel + " " + newLevel);
if (oldLevel < newLevel) {
// Update chat
plugin.getChatListener().setPlayerChallengeLevel(player);
// Run commands and give rewards but only if they haven't done it below
// plugin.getLogger().info("DEBUG: old level = " + oldLevel + " new level = " + newLevel);
String level = Settings.challengeLevels.get(newLevel);
if (!level.isEmpty() && !plugin.getPlayers().checkChallenge(player.getUniqueId(), level)) {
// plugin.getLogger().info("DEBUG: level name = " + level);
plugin.getPlayers().completeChallenge(player.getUniqueId(), level);
String message = ChatColor.translateAlternateColorCodes('&', getChallengeConfig().getString("challenges.levelUnlock." + level + ".message", ""));
if (!message.isEmpty()) {
Util.sendMessage(player, ChatColor.GREEN + message);
}
String[] itemReward = getChallengeConfig().getString("challenges.levelUnlock." + level + ".itemReward", "").split(" ");
String rewardDesc = getChallengeConfig().getString("challenges.levelUnlock." + level + ".rewardDesc", "");
if (!rewardDesc.isEmpty()) {
Util.sendMessage(player, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesrewards + ": " + ChatColor.WHITE + rewardDesc);
}
List<ItemStack> rewardedItems = giveItems(player, itemReward);
double moneyReward = getChallengeConfig().getDouble("challenges.levelUnlock." + level + ".moneyReward", 0D);
int expReward = getChallengeConfig().getInt("challenges.levelUnlock." + level + ".expReward", 0);
if (expReward > 0) {
Util.sendMessage(player, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesexpReward + ": " + ChatColor.WHITE + expReward);
player.giveExp(expReward);
}
if (Settings.useEconomy && moneyReward > 0 && (VaultHelper.econ != null)) {
EconomyResponse e = VaultHelper.econ.depositPlayer(player, Settings.worldName, moneyReward);
if (e.transactionSuccess()) {
Util.sendMessage(player, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesmoneyReward + ": " + ChatColor.WHITE + VaultHelper.econ.format(moneyReward));
} else {
plugin.getLogger().severe("Error giving player " + player.getUniqueId() + " challenge money:" + e.errorMessage);
plugin.getLogger().severe("Reward was $" + moneyReward);
}
}
String[] permList = getChallengeConfig().getString("challenges.levelUnlock." + level + ".permissionReward", "").split(" ");
for (final String s : permList) {
if (!s.isEmpty()) {
VaultHelper.addPerm(player, s);
plugin.getLogger().info("Added permission " + s + " to " + player.getName() + "");
}
}
List<String> commands = getChallengeConfig().getStringList("challenges.levelUnlock." + level + ".commands");
runCommands(player, commands);
// Fire event
ChallengeLevelCompleteEvent event = new ChallengeLevelCompleteEvent(player, oldLevel, newLevel, rewardedItems);
plugin.getServer().getPluginManager().callEvent(event);
// Save player
plugin.getPlayers().save(player.getUniqueId());
}
}
}
return true;
}
return false;
default:
return false;
}
}
Aggregations