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 {
} }
} }
shop.getItem().resetForPlayer(p); if (shop.getItem() != null) {
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,10 +297,14 @@ 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) {
shop.getHologram().showPlayer(p); if (shop.getHologram() != null) {
shop.getHologram().showPlayer(p);
}
} }
if (p.getLocation().distanceSquared(location) <= config.maximal_item_distance) { if (p.getLocation().distanceSquared(location) <= config.maximal_item_distance) {
shop.getItem().setVisible(p, true); if (shop.getItem() != null) {
shop.getItem().setVisible(p, true);
}
} }
} }

View File

@ -68,7 +68,9 @@ 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()) {
shop.getItem().setVisible(e.getPlayer(), false); if (shop.getItem() != null) {
shop.getItem().setVisible(e.getPlayer(), false);
}
} }
} }
@ -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;