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.shop.Shop;
import de.epiceric.shopchest.utils.ShopUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -19,32 +20,30 @@ public class HologramUpdateListener implements Listener {
@EventHandler
public void onPlayerMove(PlayerMoveEvent e) {
Player p = e.getPlayer();
Location playerLocation = p.getLocation();
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())) {
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);
}
Location shopLocation = shop.getLocation();
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.LocalizedMessage;
import de.epiceric.shopchest.nms.IHologram;
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.Utils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest;
import org.bukkit.entity.Item;
@ -36,7 +36,6 @@ public class Shop {
private double buyPrice;
private double sellPrice;
private ShopType shopType;
private Chest chest;
public Shop(int id, ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) {
this.id = id;
@ -49,18 +48,22 @@ public class Shop {
this.shopType = shopType;
Block b = location.getBlock();
if (b.getType() == Material.CHEST || b.getType() == Material.TRAPPED_CHEST) {
this.chest = (Chest) b.getState();
} else {
if (b.getType() != Material.CHEST && b.getType() != Material.TRAPPED_CHEST) {
try {
if (plugin.getShopChestConfig().remove_shop_on_error)
plugin.getShopUtils().removeShop(this, true);
plugin.getShopUtils().removeShop(this, plugin.getShopChestConfig().remove_shop_on_error);
throw new Exception("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ());
} catch (Exception ex) {
ex.printStackTrace();
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();
@ -133,7 +136,9 @@ public class Shop {
Chest[] chests = new Chest[2];
Block b = location.getBlock();
InventoryHolder ih = chest.getInventory().getHolder();
InventoryHolder ih = getInventoryHolder();
if (ih == null) return;
if (ih instanceof DoubleChest) {
DoubleChest dc = (DoubleChest) ih;
@ -148,7 +153,7 @@ public class Shop {
} else {
doubleChest = false;
chests[0] = chest;
chests[0] = (Chest) ih;
}
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() {
return chest;
public InventoryHolder getInventoryHolder() {
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.shop.Shop;
import de.epiceric.shopchest.sql.Database;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest;
import org.bukkit.entity.Entity;
@ -71,7 +68,7 @@ public class ShopUtils {
* @param addToDatabase Whether the shop should also be added to the database
*/
public void addShop(Shop shop, boolean addToDatabase) {
InventoryHolder ih = shop.getChest().getInventory().getHolder();
InventoryHolder ih = shop.getInventoryHolder();
if (ih instanceof DoubleChest) {
DoubleChest dc = (DoubleChest) ih;
@ -95,24 +92,24 @@ public class ShopUtils {
* @param removeFromDatabase Whether the shop should also be removed from the database
*/
public void removeShop(Shop shop, boolean removeFromDatabase) {
InventoryHolder ih = shop.getChest().getInventory().getHolder();
InventoryHolder ih = shop.getInventoryHolder();
if (ih instanceof DoubleChest) {
DoubleChest dc = (DoubleChest) ih;
Chest r = (Chest) dc.getRightSide();
Chest l = (Chest) dc.getLeftSide();
if (ih instanceof DoubleChest) {
DoubleChest dc = (DoubleChest) ih;
Chest r = (Chest) dc.getRightSide();
Chest l = (Chest) dc.getLeftSide();
shopLocation.remove(r.getLocation());
shopLocation.remove(l.getLocation());
} else {
shopLocation.remove(shop.getLocation());
}
shopLocation.remove(r.getLocation());
shopLocation.remove(l.getLocation());
} else {
shopLocation.remove(shop.getLocation());
}
shop.removeItem();
shop.removeHologram();
shop.removeItem();
shop.removeHologram();
if (removeFromDatabase)
plugin.getShopDatabase().removeShop(shop, plugin.getShopChestConfig().database_reconnect_attempts);
if (removeFromDatabase)
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) {
shopCount++;
if (shop.getChest().getInventory().getHolder() instanceof DoubleChest)
InventoryHolder ih = shop.getInventoryHolder();
if (ih instanceof DoubleChest)
shopCount -= 0.5;
}
}
@ -224,7 +223,7 @@ public class ShopUtils {
try {
Shop shop = (Shop) plugin.getShopDatabase().get(id, Database.ShopInfo.SHOP, plugin.getShopChestConfig().database_reconnect_attempts);
addShop(shop, false);
} catch (NullPointerException e) {
} catch (Exception e) {
continue;
}