Fix NPEs caused by non-existent Shop Items

This commit is contained in:
Eric 2016-11-12 15:17:13 +01:00
parent 3f3ddfe66f
commit 8ecea0d938
3 changed files with 33 additions and 9 deletions

View File

@ -73,7 +73,10 @@ public class ChestProtectListener implements Listener {
} }
} }
if (shop.getItem() != null) {
shop.getItem().resetForPlayer(p); shop.getItem().resetForPlayer(p);
}
e.setCancelled(true); e.setCancelled(true);
e.getPlayer().sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CANNOT_BREAK_SHOP)); e.getPlayer().sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CANNOT_BREAK_SHOP));
} }

View File

@ -297,12 +297,16 @@ public class ShopInteractListener implements Listener {
for (Player p : location.getWorld().getPlayers()) { for (Player p : location.getWorld().getPlayers()) {
if (p.getLocation().distanceSquared(location) <= config.maximal_distance) { if (p.getLocation().distanceSquared(location) <= config.maximal_distance) {
if (shop.getHologram() != null) {
shop.getHologram().showPlayer(p); shop.getHologram().showPlayer(p);
} }
}
if (p.getLocation().distanceSquared(location) <= config.maximal_item_distance) { if (p.getLocation().distanceSquared(location) <= config.maximal_item_distance) {
if (shop.getItem() != null) {
shop.getItem().setVisible(p, true); shop.getItem().setVisible(p, true);
} }
} }
}
} }

View File

@ -68,9 +68,11 @@ public class ShopItemListener implements Listener {
@EventHandler @EventHandler
public void onPlayerLeave(PlayerQuitEvent e) { public void onPlayerLeave(PlayerQuitEvent e) {
for (Shop shop : plugin.getShopUtils().getShops()) { for (Shop shop : plugin.getShopUtils().getShops()) {
if (shop.getItem() != null) {
shop.getItem().setVisible(e.getPlayer(), false); shop.getItem().setVisible(e.getPlayer(), false);
} }
} }
}
private void updateShopItemVisibility(Player p, boolean hideIfAway, boolean reset) { private void updateShopItemVisibility(Player p, boolean hideIfAway, boolean reset) {
updateShopItemVisibility(p, hideIfAway, reset, p.getLocation()); updateShopItemVisibility(p, hideIfAway, reset, p.getLocation());
@ -100,7 +102,10 @@ public class ShopItemListener implements Listener {
Block below = b.getRelative(BlockFace.DOWN); Block below = b.getRelative(BlockFace.DOWN);
if (shopUtils.isShop(below.getLocation())) { if (shopUtils.isShop(below.getLocation())) {
shopUtils.getShop(below.getLocation()).getItem().resetForPlayer(e.getPlayer()); Shop shop = shopUtils.getShop(below.getLocation());
if (shop.getItem() != null) {
shop.getItem().resetForPlayer(e.getPlayer());
}
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -111,7 +116,10 @@ public class ShopItemListener implements Listener {
Block below = blockState.getBlock().getRelative(BlockFace.DOWN); Block below = blockState.getBlock().getRelative(BlockFace.DOWN);
if (shopUtils.isShop(below.getLocation())) { if (shopUtils.isShop(below.getLocation())) {
shopUtils.getShop(below.getLocation()).getItem().resetForPlayer(e.getPlayer()); Shop shop = shopUtils.getShop(below.getLocation());
if (shop.getItem() != null) {
shop.getItem().resetForPlayer(e.getPlayer());
}
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -142,7 +150,10 @@ public class ShopItemListener implements Listener {
if (shopUtils.isShop(belowNewBlock.getLocation())) { if (shopUtils.isShop(belowNewBlock.getLocation())) {
e.setCancelled(true); e.setCancelled(true);
for (Player p : Bukkit.getOnlinePlayers()) { for (Player p : Bukkit.getOnlinePlayers()) {
shopUtils.getShop(belowNewBlock.getLocation()).getItem().resetForPlayer(p); Shop shop = shopUtils.getShop(belowNewBlock.getLocation());
if (shop.getItem() != null) {
shop.getItem().resetForPlayer(p);
}
} }
} }
} }
@ -163,11 +174,17 @@ public class ShopItemListener implements Listener {
if (shopUtils.isShop(clicked.getLocation())) { if (shopUtils.isShop(clicked.getLocation())) {
if (e.getBucket() == Material.LAVA_BUCKET) { if (e.getBucket() == Material.LAVA_BUCKET) {
shopUtils.getShop(clicked.getLocation()).getItem().resetForPlayer(e.getPlayer()); Shop shop = shopUtils.getShop(clicked.getLocation());
if (shop.getItem() != null) {
shop.getItem().resetForPlayer(e.getPlayer());
}
} }
} else if (shopUtils.isShop(underWater.getLocation())) { } else if (shopUtils.isShop(underWater.getLocation())) {
if (e.getBucket() == Material.LAVA_BUCKET) { if (e.getBucket() == Material.LAVA_BUCKET) {
shopUtils.getShop(underWater.getLocation()).getItem().resetForPlayer(e.getPlayer()); Shop shop = shopUtils.getShop(underWater.getLocation());
if (shop.getItem() != null) {
shop.getItem().resetForPlayer(e.getPlayer());
}
} }
} else { } else {
return; return;