Improved handling with shops that got errors when loading

An exception will now be thrown, when there's no space above the chest (only if items should be shown).
Holograms now won't be shown to the player if the shop has no chest, but the shop will get removed.
This commit is contained in:
Eric 2016-07-13 15:53:50 +02:00
parent 802e3585b8
commit 5ac136ffcc
3 changed files with 62 additions and 52 deletions

View File

@ -2,8 +2,9 @@ package de.epiceric.shopchest.listeners;
import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.ShopUtils;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -19,32 +20,30 @@ public class HologramUpdateListener implements Listener {
@EventHandler @EventHandler
public void onPlayerMove(PlayerMoveEvent e) { public void onPlayerMove(PlayerMoveEvent e) {
Player p = e.getPlayer(); Player p = e.getPlayer();
Location playerLocation = p.getLocation(); Location playerLocation = p.getLocation();
for (Shop shop : plugin.getShopUtils().getShops()) { for (Shop shop : plugin.getShopUtils().getShops()) {
Block b = shop.getLocation().getBlock();
if (shop.getHologram() != null) { if (b.getType() != Material.CHEST && b.getType() != Material.TRAPPED_CHEST) {
plugin.getShopUtils().removeShop(shop, plugin.getShopChestConfig().remove_shop_on_error);
return;
}
Location shopLocation = shop.getLocation(); if (shop.getHologram() == null) return;
if (playerLocation.getWorld().equals(shopLocation.getWorld())) { Location shopLocation = shop.getLocation();
if (playerLocation.distance(shop.getHologram().getLocation()) <= plugin.getShopChestConfig().maximal_distance) {
if (!shop.getHologram().isVisible(p)) {
shop.getHologram().showPlayer(p);
}
} else {
if (shop.getHologram().isVisible(p)) {
shop.getHologram().hidePlayer(p);
}
if (playerLocation.getWorld().equals(shopLocation.getWorld())) {
if (playerLocation.distance(shop.getHologram().getLocation()) <= plugin.getShopChestConfig().maximal_distance) {
if (!shop.getHologram().isVisible(p)) {
shop.getHologram().showPlayer(p);
}
} else {
if (shop.getHologram().isVisible(p)) {
shop.getHologram().hidePlayer(p);
} }
} }
} }

View File

@ -5,13 +5,13 @@ import de.epiceric.shopchest.config.Regex;
import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.language.LocalizedMessage; import de.epiceric.shopchest.language.LocalizedMessage;
import de.epiceric.shopchest.nms.IHologram; import de.epiceric.shopchest.nms.IHologram;
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.Utils; import de.epiceric.shopchest.utils.Utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest; import org.bukkit.block.DoubleChest;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
@ -36,7 +36,6 @@ public class Shop {
private double buyPrice; private double buyPrice;
private double sellPrice; private double sellPrice;
private ShopType shopType; private ShopType shopType;
private Chest chest;
public Shop(int id, ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) { public Shop(int id, ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) {
this.id = id; this.id = id;
@ -49,18 +48,22 @@ public class Shop {
this.shopType = shopType; this.shopType = shopType;
Block b = location.getBlock(); Block b = location.getBlock();
if (b.getType() == Material.CHEST || b.getType() == Material.TRAPPED_CHEST) { if (b.getType() != Material.CHEST && b.getType() != Material.TRAPPED_CHEST) {
this.chest = (Chest) b.getState();
} else {
try { try {
if (plugin.getShopChestConfig().remove_shop_on_error) plugin.getShopUtils().removeShop(this, plugin.getShopChestConfig().remove_shop_on_error);
plugin.getShopUtils().removeShop(this, true);
throw new Exception("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); throw new Exception("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ());
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
return; return;
} }
} else if ((b.getRelative(BlockFace.UP).getType() != Material.AIR) && plugin.getShopChestConfig().show_shop_items) {
try {
plugin.getShopUtils().removeShop(this, plugin.getShopChestConfig().remove_shop_on_error);
throw new Exception("No space above chest at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ());
} catch (Exception ex) {
ex.printStackTrace();
return;
}
} }
if (hologram == null || !hologram.exists()) createHologram(); if (hologram == null || !hologram.exists()) createHologram();
@ -133,7 +136,9 @@ public class Shop {
Chest[] chests = new Chest[2]; Chest[] chests = new Chest[2];
Block b = location.getBlock(); Block b = location.getBlock();
InventoryHolder ih = chest.getInventory().getHolder(); InventoryHolder ih = getInventoryHolder();
if (ih == null) return;
if (ih instanceof DoubleChest) { if (ih instanceof DoubleChest) {
DoubleChest dc = (DoubleChest) ih; DoubleChest dc = (DoubleChest) ih;
@ -148,7 +153,7 @@ public class Shop {
} else { } else {
doubleChest = false; doubleChest = false;
chests[0] = chest; chests[0] = (Chest) ih;
} }
Location holoLocation; Location holoLocation;
@ -272,10 +277,17 @@ public class Shop {
} }
/** /**
* @return Chest of the shop. If double chest, only one chest is returned * @return {@link InventoryHolder} of the shop or <b>null</b> if the shop has no chest.
*/ */
public Chest getChest() { public InventoryHolder getInventoryHolder() {
return chest; Block b = getLocation().getBlock();
if (b.getType() == Material.CHEST || b.getType() == Material.TRAPPED_CHEST) {
Chest chest = (Chest) b.getState();
return chest.getInventory().getHolder();
}
return null;
} }
/** /**

View File

@ -4,10 +4,7 @@ import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.sql.Database; import de.epiceric.shopchest.sql.Database;
import org.bukkit.Bukkit; import org.bukkit.*;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest; import org.bukkit.block.DoubleChest;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -71,7 +68,7 @@ public class ShopUtils {
* @param addToDatabase Whether the shop should also be added to the database * @param addToDatabase Whether the shop should also be added to the database
*/ */
public void addShop(Shop shop, boolean addToDatabase) { public void addShop(Shop shop, boolean addToDatabase) {
InventoryHolder ih = shop.getChest().getInventory().getHolder(); InventoryHolder ih = shop.getInventoryHolder();
if (ih instanceof DoubleChest) { if (ih instanceof DoubleChest) {
DoubleChest dc = (DoubleChest) ih; DoubleChest dc = (DoubleChest) ih;
@ -95,24 +92,24 @@ public class ShopUtils {
* @param removeFromDatabase Whether the shop should also be removed from the database * @param removeFromDatabase Whether the shop should also be removed from the database
*/ */
public void removeShop(Shop shop, boolean removeFromDatabase) { public void removeShop(Shop shop, boolean removeFromDatabase) {
InventoryHolder ih = shop.getChest().getInventory().getHolder(); InventoryHolder ih = shop.getInventoryHolder();
if (ih instanceof DoubleChest) { if (ih instanceof DoubleChest) {
DoubleChest dc = (DoubleChest) ih; DoubleChest dc = (DoubleChest) ih;
Chest r = (Chest) dc.getRightSide(); Chest r = (Chest) dc.getRightSide();
Chest l = (Chest) dc.getLeftSide(); Chest l = (Chest) dc.getLeftSide();
shopLocation.remove(r.getLocation()); shopLocation.remove(r.getLocation());
shopLocation.remove(l.getLocation()); shopLocation.remove(l.getLocation());
} else { } else {
shopLocation.remove(shop.getLocation()); shopLocation.remove(shop.getLocation());
} }
shop.removeItem(); shop.removeItem();
shop.removeHologram(); shop.removeHologram();
if (removeFromDatabase) if (removeFromDatabase)
plugin.getShopDatabase().removeShop(shop, plugin.getShopChestConfig().database_reconnect_attempts); plugin.getShopDatabase().removeShop(shop, plugin.getShopChestConfig().database_reconnect_attempts);
} }
/** /**
@ -186,7 +183,9 @@ public class ShopUtils {
if (shop.getShopType() != Shop.ShopType.ADMIN || !plugin.getShopChestConfig().exclude_admin_shops) { if (shop.getShopType() != Shop.ShopType.ADMIN || !plugin.getShopChestConfig().exclude_admin_shops) {
shopCount++; shopCount++;
if (shop.getChest().getInventory().getHolder() instanceof DoubleChest) InventoryHolder ih = shop.getInventoryHolder();
if (ih instanceof DoubleChest)
shopCount -= 0.5; shopCount -= 0.5;
} }
} }
@ -224,7 +223,7 @@ public class ShopUtils {
try { try {
Shop shop = (Shop) plugin.getShopDatabase().get(id, Database.ShopInfo.SHOP, plugin.getShopChestConfig().database_reconnect_attempts); Shop shop = (Shop) plugin.getShopDatabase().get(id, Database.ShopInfo.SHOP, plugin.getShopChestConfig().database_reconnect_attempts);
addShop(shop, false); addShop(shop, false);
} catch (NullPointerException e) { } catch (Exception e) {
continue; continue;
} }