Permit /questadmin commands on offline players, part 1. Per #1040

This commit is contained in:
PikaMug 2020-01-21 02:29:14 -05:00
parent 3e42dcbc33
commit 83f39e487b
3 changed files with 197 additions and 168 deletions

View File

@ -20,6 +20,7 @@ import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
@ -289,7 +290,7 @@ public class Quest {
*
* Method may be called as often as needed.
*
* @param quester The quester to have their compass updated
* @param quester The online quester to have their compass updated
* @param nextStage The stage to process for targets
* @return true if successful
*/
@ -303,6 +304,9 @@ public class Quest {
if (nextStage == null) {
return false;
}
if (!quester.getOfflinePlayer().isOnline()) {
return false;
}
Location targetLocation = null;
if (nextStage.citizensToInteract != null && nextStage.citizensToInteract.size() > 0) {
targetLocation = plugin.getDependencies().getNPCLocation(nextStage.citizensToInteract.getFirst());
@ -324,53 +328,36 @@ public class Quest {
}
/**
* Check that a quester has met all Requirements to accept this quest
* Check that a quester has met all Requirements to accept this quest<p>
*
* Item, permission and custom Requirements are only checked for online players
*
* @param quester The quester to check
* @return true if all Requirements have been met
*/
public boolean testRequirements(Quester quester) {
return testRequirements(quester.getPlayer());
return testRequirements(quester.getOfflinePlayer());
}
/**
* Check that a player has met all Requirements to accept this quest
* Check that a player has met all Requirements to accept this quest<p>
*
* Item, permission and custom Requirements are only checked for online players
*
* @param player The player to check
* @return true if all Requirements have been met
*/
protected boolean testRequirements(Player player) {
protected boolean testRequirements(OfflinePlayer player) {
Quester quester = plugin.getQuester(player.getUniqueId());
if (reqs.getMoney() != 0 && plugin.getDependencies().getVaultEconomy() != null) {
if (plugin.getDependencies().getVaultEconomy().getBalance(Bukkit.getOfflinePlayer(player.getUniqueId()))
< reqs.getMoney()) {
return false;
}
}
PlayerInventory inventory = player.getInventory();
int num = 0;
for (ItemStack is : reqs.getItems()) {
for (ItemStack stack : inventory.getContents()) {
if (stack != null) {
if (ItemUtil.compareItems(is, stack, true) == 0) {
num += stack.getAmount();
}
}
}
if (num < is.getAmount()) {
return false;
}
num = 0;
}
for (String s : reqs.getPermissions()) {
if (player.hasPermission(s) == false) {
if (plugin.getDependencies().getVaultEconomy().getBalance(player) < reqs.getMoney()) {
return false;
}
}
for (String s : reqs.getMcmmoSkills()) {
final SkillType st = Quests.getMcMMOSkill(s);
final int lvl = reqs.getMcmmoAmounts().get(reqs.getMcmmoSkills().indexOf(s));
if (UserManager.getPlayer(player).getProfile().getSkillLevel(st) < lvl) {
if (UserManager.getOfflinePlayer(player).getProfile().getSkillLevel(st) < lvl) {
return false;
}
}
@ -386,23 +373,6 @@ public class Quest {
return false;
}
}
for (String s : reqs.getCustomRequirements().keySet()) {
CustomRequirement found = null;
for (CustomRequirement cr : plugin.getCustomRequirements()) {
if (cr.getName().equalsIgnoreCase(s)) {
found = cr;
break;
}
}
if (found != null) {
if (found.testRequirement(player, reqs.getCustomRequirements().get(s)) == false) {
return false;
}
} else {
plugin.getLogger().warning("Quester \"" + player.getName() + "\" attempted to take Quest \"" + name
+ "\", but the Custom Requirement \"" + s + "\" could not be found. Does it still exist?");
}
}
if (quester.questPoints < reqs.getQuestPoints()) {
return false;
}
@ -416,6 +386,46 @@ public class Quest {
return false;
}
}
if (player.isOnline()) {
Player p = (Player)player;
PlayerInventory inventory = p.getInventory();
int num = 0;
for (ItemStack is : reqs.getItems()) {
for (ItemStack stack : inventory.getContents()) {
if (stack != null) {
if (ItemUtil.compareItems(is, stack, true) == 0) {
num += stack.getAmount();
}
}
}
if (num < is.getAmount()) {
return false;
}
num = 0;
}
for (String s : reqs.getPermissions()) {
if (p.hasPermission(s) == false) {
return false;
}
}
for (String s : reqs.getCustomRequirements().keySet()) {
CustomRequirement found = null;
for (CustomRequirement cr : plugin.getCustomRequirements()) {
if (cr.getName().equalsIgnoreCase(s)) {
found = cr;
break;
}
}
if (found != null) {
if (found.testRequirement(p, reqs.getCustomRequirements().get(s)) == false) {
return false;
}
} else {
plugin.getLogger().warning("Quester \"" + p.getName() + "\" attempted to take Quest \"" + name
+ "\", but the Custom Requirement \"" + s + "\" could not be found. Does it still exist?");
}
}
}
return true;
}

View File

@ -412,7 +412,9 @@ public class Quester {
if (preEvent.isCancelled()) {
return;
}
Player player = getPlayer();
OfflinePlayer player = getOfflinePlayer();
if (player.isOnline()) {
Player p = getPlayer();
Planner pln = q.getPlanner();
long start = pln.getStartInMillis(); // Start time in milliseconds since UTC epoch
long end = pln.getEndInMillis(); // End time in milliseconds since UTC epoch
@ -424,7 +426,7 @@ public class Quester {
early = early.replace("<quest>", ChatColor.AQUA + q.getName() + ChatColor.YELLOW);
early = early.replace("<time>", ChatColor.DARK_PURPLE
+ MiscUtil.getTime(start - System.currentTimeMillis()) + ChatColor.YELLOW);
player.sendMessage(ChatColor.YELLOW + early);
p.sendMessage(ChatColor.YELLOW + early);
return;
}
}
@ -434,7 +436,7 @@ public class Quester {
late = late.replace("<quest>", ChatColor.AQUA + q.getName() + ChatColor.RED);
late = late.replace("<time>", ChatColor.DARK_PURPLE
+ MiscUtil.getTime(System.currentTimeMillis() - end) + ChatColor.RED);
player.sendMessage(ChatColor.RED + late);
p.sendMessage(ChatColor.RED + late);
return;
}
}
@ -475,11 +477,12 @@ public class Quester {
early = early.replace("<quest>", ChatColor.AQUA + q.getName() + ChatColor.YELLOW);
early = early.replace("<time>", ChatColor.DARK_PURPLE
+ MiscUtil.getTime(nextStart - System.currentTimeMillis()) + ChatColor.YELLOW);
player.sendMessage(ChatColor.YELLOW + early);
p.sendMessage(ChatColor.YELLOW + early);
return;
}
}
}
}
if (q.testRequirements(player) == true || override) {
addEmptiesFor(q, 0);
try {
@ -496,15 +499,17 @@ public class Quester {
plugin.getDependencies().getVaultEconomy().withdrawPlayer(getOfflinePlayer(), reqs.getMoney());
}
}
if (player.isOnline()) {
Player p = getPlayer();
for (ItemStack is : reqs.getItems()) {
if (reqs.getRemoveItems().get(reqs.getItems().indexOf(is)) == true) {
InventoryUtil.removeItem(player.getInventory(), is);
InventoryUtil.removeItem(p.getInventory(), is);
}
}
String accepted = Lang.get(getPlayer(), "questAccepted");
accepted = accepted.replace("<quest>", q.getName());
player.sendMessage(ChatColor.GREEN + accepted);
player.sendMessage("");
p.sendMessage(ChatColor.GREEN + accepted);
p.sendMessage("");
if (plugin.getSettings().canShowQuestTitles()) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "title "
+ player.getName() + " title " + "{\"text\":\"" + Lang.get(getPlayer(), "quest") + " "
@ -514,15 +519,19 @@ public class Quester {
+ "\",\"color\":\"yellow\"}");
}
}
String msg = Lang.get(getPlayer(), "questObjectivesTitle");
}
if (player.isOnline()) {
Player p = getPlayer();
String msg = Lang.get(p, "questObjectivesTitle");
msg = msg.replace("<quest>", q.getName());
getPlayer().sendMessage(ChatColor.GOLD + msg);
p.sendMessage(ChatColor.GOLD + msg);
plugin.showObjectives(q, this, false);
String stageStartMessage = stage.startMessage;
if (stageStartMessage != null) {
getPlayer().sendMessage(ConfigUtil
p.sendMessage(ConfigUtil
.parseStringWithPossibleLineBreaks(stageStartMessage, q, getPlayer()));
}
}
if (stage.chatActions.isEmpty() == false) {
for (String chatTrigger : stage.chatActions.keySet()) {
questData.get(q).actionFired.put(chatTrigger, false);
@ -542,7 +551,9 @@ public class Quester {
q.updateCompass(this, stage);
saveData();
} else {
player.sendMessage(q.getRequirements().getFailRequirements());
if (player.isOnline()) {
((Player)player).sendMessage(q.getRequirements().getFailRequirements());
}
}
QuesterPostStartQuestEvent postEvent = new QuesterPostStartQuestEvent(this, q);
plugin.getServer().getPluginManager().callEvent(postEvent);

View File

@ -42,6 +42,7 @@ import me.blackvein.quests.util.MiscUtil;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@ -986,10 +987,15 @@ public class CmdExecutor implements CommandExecutor {
private void adminGive(final CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.give")) {
Player target = getPlayer(args[1]);
OfflinePlayer target = getPlayer(args[1]);
if (target == null) {
try {
target = Bukkit.getOfflinePlayer(UUID.fromString(args[1]));
} catch (IllegalArgumentException e) {
cs.sendMessage(ChatColor.YELLOW + Lang.get("playerNotFound"));
} else {
return;
}
}
Quest questToGive;
String name = "";
if (args.length == 3) {
@ -1025,12 +1031,14 @@ public class CmdExecutor implements CommandExecutor {
msg1 = msg1.replace("<player>", ChatColor.GREEN + target.getName() + ChatColor.GOLD);
msg1 = msg1.replace("<quest>", ChatColor.DARK_PURPLE + questToGive.getName() + ChatColor.GOLD);
cs.sendMessage(ChatColor.GOLD + msg1);
String msg2 = Lang.get(target, "questForcedTake");
if (target.isOnline()) {
Player p = (Player)target;
String msg2 = Lang.get(p, "questForcedTake");
msg2 = msg2.replace("<player>", ChatColor.GREEN + cs.getName() + ChatColor.GOLD);
msg2 = msg2.replace("<quest>", ChatColor.DARK_PURPLE + questToGive.getName() + ChatColor.GOLD);
target.sendMessage(ChatColor.GREEN + msg2);
quester.takeQuest(questToGive, true);
p.sendMessage(ChatColor.GREEN + msg2);
}
quester.takeQuest(questToGive, true);
}
} else {
cs.sendMessage(ChatColor.RED + Lang.get("noPermission"));
@ -1474,7 +1482,7 @@ public class CmdExecutor implements CommandExecutor {
}
/**
* Get a Player by name
* Get an online Player by name
*
* @param name Name of the player
* @return Player or null if not found