Merge pull request #288 from elBukkit/master

Better compass behavior, safety protection when loading bad data
This commit is contained in:
FlyingPikachu 2016-03-21 13:36:34 -04:00
commit 7d8e198b9c
4 changed files with 75 additions and 27 deletions

View File

@ -7,6 +7,7 @@ import me.blackvein.quests.util.ItemUtil;
import me.blackvein.quests.util.Lang; import me.blackvein.quests.util.Lang;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
@ -35,6 +36,7 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerFishEvent; import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.event.player.PlayerFishEvent.State; import org.bukkit.event.player.PlayerFishEvent.State;
@ -43,6 +45,7 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerShearEntityEvent; import org.bukkit.event.player.PlayerShearEntityEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.projectiles.ProjectileSource; import org.bukkit.projectiles.ProjectileSource;
@ -860,6 +863,29 @@ public class PlayerListener implements Listener, ColorUtil {
} }
@EventHandler
public void onPlayerChangeWorld(PlayerChangedWorldEvent event) {
Player player = event.getPlayer();
if (plugin.checkQuester(player.getUniqueId()) == false) {
Quester quester = plugin.getQuester(player.getUniqueId());
quester.findCompassTarget();
}
}
@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event) {
Player player = event.getPlayer();
if (plugin.checkQuester(player.getUniqueId()) == false) {
final Quester quester = plugin.getQuester(player.getUniqueId());
Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
@Override
public void run() {
quester.findCompassTarget();
}
}, 10);
}
}
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent evt) { public void onPlayerJoin(PlayerJoinEvent evt) {
@ -875,6 +901,10 @@ public class PlayerListener implements Listener, ColorUtil {
plugin.questers.put(evt.getPlayer().getUniqueId(), quester); plugin.questers.put(evt.getPlayer().getUniqueId(), quester);
if (Quests.getInstance().useCompass) {
quester.resetCompass();
}
for (String s : quester.completedQuests) { for (String s : quester.completedQuests) {
Quest q = plugin.getQuest(s); Quest q = plugin.getQuest(s);

View File

@ -89,7 +89,10 @@ public class Quest {
if (stageCompleteMessage != null) { if (stageCompleteMessage != null) {
q.getPlayer().sendMessage(Quests.parseString(stageCompleteMessage, this)); q.getPlayer().sendMessage(Quests.parseString(stageCompleteMessage, this));
} }
resetCompass(q); if (Quests.getInstance().useCompass) {
q.resetCompass();
q.findCompassTarget();
}
if (q.getCurrentStage(this).delay < 0) { if (q.getCurrentStage(this).delay < 0) {
@ -171,14 +174,13 @@ public class Quest {
} }
public void updateCompass(Quester quester, Stage nextStage) public boolean updateCompass(Quester quester, Stage nextStage)
{ {
if (!Quests.getInstance().useCompass) return; if (!Quests.getInstance().useCompass) return false;
Location targetLocation = null; Location targetLocation = null;
if (nextStage == null) { if (nextStage == null) {
resetCompass(quester); return false;
return;
} }
if (nextStage.citizensToInteract != null && nextStage.citizensToInteract.size() > 0) if (nextStage.citizensToInteract != null && nextStage.citizensToInteract.size() > 0)
{ {
@ -192,23 +194,12 @@ public class Quest {
{ {
targetLocation = nextStage.locationsToReach.getFirst(); targetLocation = nextStage.locationsToReach.getFirst();
} }
if (targetLocation != null) { if (targetLocation != null && targetLocation.getWorld().equals(quester.getPlayer().getWorld())) {
// plugin.getLogger().info("Setting compass target for " + quester.getPlayer().getName() + " to " + targetLocation);
quester.getPlayer().setCompassTarget(targetLocation); quester.getPlayer().setCompassTarget(targetLocation);
} else {
resetCompass(quester);
} }
}
protected void resetCompass(Quester q) { return targetLocation != null;
if (!Quests.getInstance().useCompass) return;
Player player = q.getPlayer();
if (player == null) return;
Location defaultLocation = player.getBedSpawnLocation();
if (defaultLocation == null) {
defaultLocation = player.getWorld().getSpawnLocation();
}
player.setCompassTarget(defaultLocation);
} }
public String getName() { public String getName() {
@ -574,7 +565,7 @@ public class Quest {
q.saveData(); q.saveData();
player.updateInventory(); player.updateInventory();
q.updateJournal(); q.updateJournal();
q.findCompassTarget();
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")

View File

@ -2590,7 +2590,7 @@ public class Quester {
ConfigurationSection dataSec = data.getConfigurationSection("questData"); ConfigurationSection dataSec = data.getConfigurationSection("questData");
if (dataSec.getKeys(false).isEmpty()) { if (dataSec == null || dataSec.getKeys(false).isEmpty()) {
return false; return false;
} }
@ -2610,11 +2610,13 @@ public class Quester {
if (stage == null) { if (stage == null) {
quest.completeQuest(this); quest.completeQuest(this);
plugin.getLogger().log(Level.SEVERE, "[Quests] Invalid stage number for player: \"" + id + "\" on Quest \"" + quest.name + "\". Quest ended."); plugin.getLogger().log(Level.SEVERE, "[Quests] Invalid stage number for player: \"" + id + "\" on Quest \"" + quest.name + "\". Quest ended.");
return true; continue;
} }
addEmpties(quest); addEmpties(quest);
if (questSec == null) continue;
if (questSec.contains("blocks-damaged-names")) { if (questSec.contains("blocks-damaged-names")) {
List<String> names = questSec.getStringList("blocks-damaged-names"); List<String> names = questSec.getStringList("blocks-damaged-names");
@ -3465,4 +3467,26 @@ if (quest != null) {
} }
public void resetCompass() {
if (!Quests.getInstance().useCompass) return;
Player player = getPlayer();
if (player == null) return;
Location defaultLocation = player.getBedSpawnLocation();
if (defaultLocation == null) {
defaultLocation = player.getWorld().getSpawnLocation();
}
player.setCompassTarget(defaultLocation);
}
public void findCompassTarget() {
if (!Quests.getInstance().useCompass) return;
Player player = getPlayer();
if (player == null) return;
for (Quest quest : currentQuests.keySet()) {
Stage stage = getCurrentStage(quest);
if (stage != null && quest.updateCompass(this, stage)) break;
}
}
} }

View File

@ -2495,12 +2495,13 @@ try{
private void showObjectives(final Player player) { private void showObjectives(final Player player) {
if (getQuester(player.getUniqueId()).currentQuests.isEmpty() == false) { Quester quester = getQuester(player.getUniqueId());
if (quester.currentQuests.isEmpty() == false) {
for (Quest q : getQuester(player.getUniqueId()).currentQuests.keySet()) { for (Quest q : quester.currentQuests.keySet()) {
Stage stage = quester.getCurrentStage(q);
q.updateCompass(quester, stage);
try { try {
if (getQuester(player.getUniqueId()).getQuestData(q).delayStartTime == 0) { if (getQuester(player.getUniqueId()).getQuestData(q).delayStartTime == 0) {
String msg = Lang.get("questObjectivesTitle"); String msg = Lang.get("questObjectivesTitle");
@ -2747,6 +2748,8 @@ try{
quester.saveData(); quester.saveData();
} }
qs.put(p.getUniqueId(), quester); qs.put(p.getUniqueId(), quester);
// Kind of hacky to put this here, works around issues with the compass on fast join
quester.findCompassTarget();
} }