From 77365f388c174460095f409f5a1baeb5b699122d Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 3 Jun 2016 17:58:53 +0200 Subject: [PATCH] Changed method of adding and removing a shop Well, I had to change quite a bit and hopefully this fixes the issue, that shop items can be collected and that shops aren't saved or reloaded correctly. --- src/de/epiceric/shopchest/Commands.java | 4 +- src/de/epiceric/shopchest/ShopChest.java | 23 ++--- .../shopchest/event/InteractShop.java | 20 +---- .../shopchest/event/NotifyUpdate.java | 2 +- .../shopchest/event/ProtectChest.java | 14 +-- .../event/RegenerateShopItemAfterRemove.java | 3 +- .../shopchest/interfaces/Hologram.java | 4 + .../interfaces/hologram/Hologram_1_8_R1.java | 24 +++-- .../interfaces/hologram/Hologram_1_8_R2.java | 24 +++-- .../interfaces/hologram/Hologram_1_8_R3.java | 24 +++-- .../interfaces/hologram/Hologram_1_9_R1.java | 24 +++-- .../interfaces/hologram/Hologram_1_9_R2.java | 24 +++-- .../interfaces/utils/Utils_1_8_R1.java | 85 ------------------ .../interfaces/utils/Utils_1_8_R2.java | 87 ------------------- .../interfaces/utils/Utils_1_8_R3.java | 86 ------------------ .../interfaces/utils/Utils_1_9_R1.java | 86 ------------------ .../interfaces/utils/Utils_1_9_R2.java | 86 ------------------ src/de/epiceric/shopchest/shop/Shop.java | 38 +++++--- src/de/epiceric/shopchest/sql/Database.java | 40 ++------- .../epiceric/shopchest/utils/ShopUtils.java | 69 ++++++++++----- .../{interfaces => utils}/Utils.java | 8 +- 21 files changed, 180 insertions(+), 595 deletions(-) delete mode 100644 src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R1.java delete mode 100644 src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R2.java delete mode 100644 src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R3.java delete mode 100644 src/de/epiceric/shopchest/interfaces/utils/Utils_1_9_R1.java delete mode 100644 src/de/epiceric/shopchest/interfaces/utils/Utils_1_9_R2.java rename src/de/epiceric/shopchest/{interfaces => utils}/Utils.java (94%) diff --git a/src/de/epiceric/shopchest/Commands.java b/src/de/epiceric/shopchest/Commands.java index 91c276a..e9ad7da 100644 --- a/src/de/epiceric/shopchest/Commands.java +++ b/src/de/epiceric/shopchest/Commands.java @@ -2,7 +2,7 @@ package de.epiceric.shopchest; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.interfaces.JsonBuilder; -import de.epiceric.shopchest.interfaces.Utils; +import de.epiceric.shopchest.utils.Utils; import de.epiceric.shopchest.interfaces.jsonbuilder.*; import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.utils.ClickType; @@ -182,7 +182,7 @@ public class Commands extends BukkitCommand { } private void reload(Player player) { - ShopChest.utils.reload(player); + ShopUtils.reloadShops(player); } private void create(String[] args, ShopType shopType, Player p) { diff --git a/src/de/epiceric/shopchest/ShopChest.java b/src/de/epiceric/shopchest/ShopChest.java index fedd3da..c7f5bff 100644 --- a/src/de/epiceric/shopchest/ShopChest.java +++ b/src/de/epiceric/shopchest/ShopChest.java @@ -3,9 +3,8 @@ package de.epiceric.shopchest; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.event.*; import de.epiceric.shopchest.interfaces.JsonBuilder; -import de.epiceric.shopchest.interfaces.Utils; +import de.epiceric.shopchest.utils.Utils; import de.epiceric.shopchest.interfaces.jsonbuilder.*; -import de.epiceric.shopchest.interfaces.utils.*; import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.sql.Database; @@ -41,7 +40,6 @@ public class ShopChest extends JavaPlugin { public static String latestVersion = ""; public static String downloadLink = ""; public static String[] broadcast = null; - public static Utils utils; private static ShopChest instance; public static ShopChest getInstance() { @@ -158,19 +156,10 @@ public class ShopChest extends JavaPlugin { switch (Utils.getVersion(getServer())) { case "v1_8_R1": - utils = new Utils_1_8_R1(); - break; case "v1_8_R2": - utils = new Utils_1_8_R2(); - break; case "v1_8_R3": - utils = new Utils_1_8_R3(); - break; case "v1_9_R1": - utils = new Utils_1_9_R1(); - break; case "v1_9_R2": - utils = new Utils_1_9_R2(); break; default: logger.severe("Incompatible Server Version: " + Utils.getVersion(getServer()) + "!"); @@ -273,25 +262,23 @@ public class ShopChest extends JavaPlugin { @Override public void onDisable() { - utils.removeShops(); + for (Shop shop : ShopUtils.getShops()) { + ShopUtils.removeShop(shop, false); + } } private void initializeShops() { int count = 0; for (int id = 1; id < database.getHighestID() + 1; id++) { - try { Shop shop = (Shop) database.get(id, Database.ShopInfo.SHOP); - shop.createHologram(); - shop.createItem(); - ShopUtils.addShop(shop); + ShopUtils.addShop(shop, false); } catch (NullPointerException e) { continue; } count++; - } logger.info("Initialized " + String.valueOf(count) + " Shops"); diff --git a/src/de/epiceric/shopchest/event/InteractShop.java b/src/de/epiceric/shopchest/event/InteractShop.java index a774602..0d92848 100644 --- a/src/de/epiceric/shopchest/event/InteractShop.java +++ b/src/de/epiceric/shopchest/event/InteractShop.java @@ -4,7 +4,7 @@ import com.griefcraft.lwc.LWC; import com.griefcraft.model.Protection; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.config.Config; -import de.epiceric.shopchest.interfaces.Utils; +import de.epiceric.shopchest.utils.Utils; import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.sql.Database; @@ -221,14 +221,9 @@ public class InteractShop implements Listener { } private void create(Player executor, Location location, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) { + Shop shop = new Shop(database.getNextFreeID(), plugin, executor, product, location, buyPrice, sellPrice, shopType); - Shop shop = new Shop(plugin, executor, product, location, buyPrice, sellPrice, shopType); - shop.createHologram(); - shop.createItem(); - - database.addShop(shop); - - ShopUtils.addShop(shop); + ShopUtils.addShop(shop, true); executor.sendMessage(Config.shop_created()); for (Player p : Bukkit.getOnlinePlayers()) { @@ -238,15 +233,8 @@ public class InteractShop implements Listener { } private void remove(Player executor, Shop shop) { - - ShopUtils.removeShop(shop); - - database.removeShop(shop); - - shop.removeHologram(); - + ShopUtils.removeShop(shop, true); executor.sendMessage(Config.shop_removed()); - } private void info(Player executor, Shop shop) { diff --git a/src/de/epiceric/shopchest/event/NotifyUpdate.java b/src/de/epiceric/shopchest/event/NotifyUpdate.java index 3fafa47..33eed44 100644 --- a/src/de/epiceric/shopchest/event/NotifyUpdate.java +++ b/src/de/epiceric/shopchest/event/NotifyUpdate.java @@ -3,7 +3,7 @@ package de.epiceric.shopchest.event; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.interfaces.JsonBuilder; -import de.epiceric.shopchest.interfaces.Utils; +import de.epiceric.shopchest.utils.Utils; import de.epiceric.shopchest.interfaces.jsonbuilder.*; import net.milkbowl.vault.permission.Permission; import org.bukkit.entity.Player; diff --git a/src/de/epiceric/shopchest/event/ProtectChest.java b/src/de/epiceric/shopchest/event/ProtectChest.java index c568d97..acd0e4c 100644 --- a/src/de/epiceric/shopchest/event/ProtectChest.java +++ b/src/de/epiceric/shopchest/event/ProtectChest.java @@ -76,24 +76,16 @@ public class ProtectChest implements Listener { if (b.getLocation().equals(r.getLocation())) { shop = ShopUtils.getShop(l.getLocation()); - ShopUtils.removeShop(shop); - ShopChest.database.removeShop(shop); } else if (b.getLocation().equals(l.getLocation())) { shop = ShopUtils.getShop(r.getLocation()); - ShopUtils.removeShop(shop); - ShopChest.database.removeShop(shop); } else { return; } - if (shop.hasItem()) shop.getItem().remove(); - - Shop newShop = new Shop(ShopChest.getInstance(), shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType()); - newShop.createHologram(); - newShop.createItem(); - ShopUtils.addShop(newShop); - ShopChest.database.addShop(newShop); + ShopUtils.removeShop(shop, true); + Shop newShop = new Shop(shop.getID(), ShopChest.getInstance(), shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType()); + ShopUtils.addShop(newShop, true); } } diff --git a/src/de/epiceric/shopchest/event/RegenerateShopItemAfterRemove.java b/src/de/epiceric/shopchest/event/RegenerateShopItemAfterRemove.java index 5607fb8..29367ab 100644 --- a/src/de/epiceric/shopchest/event/RegenerateShopItemAfterRemove.java +++ b/src/de/epiceric/shopchest/event/RegenerateShopItemAfterRemove.java @@ -1,6 +1,7 @@ package de.epiceric.shopchest.event; import de.epiceric.shopchest.ShopChest; +import de.epiceric.shopchest.utils.ShopUtils; import org.bukkit.entity.Entity; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -19,7 +20,7 @@ public class RegenerateShopItemAfterRemove implements Listener { } } - if (containsShopItem) ShopChest.utils.reload(null); + if (containsShopItem) ShopUtils.reloadShops(null); } diff --git a/src/de/epiceric/shopchest/interfaces/Hologram.java b/src/de/epiceric/shopchest/interfaces/Hologram.java index c8b3fe2..a15e193 100644 --- a/src/de/epiceric/shopchest/interfaces/Hologram.java +++ b/src/de/epiceric/shopchest/interfaces/Hologram.java @@ -18,4 +18,8 @@ public interface Hologram { public boolean isVisible(OfflinePlayer p); + public boolean exists(); + + public void remove(); + } diff --git a/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R1.java b/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R1.java index 049d489..36a2ce5 100644 --- a/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R1.java +++ b/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R1.java @@ -15,7 +15,8 @@ import java.util.List; public class Hologram_1_8_R1 implements Hologram { - int count; + private boolean exists = false; + private int count; private List entitylist = new ArrayList(); private String[] text; private Location location; @@ -28,12 +29,6 @@ public class Hologram_1_8_R1 implements Hologram { create(); } - public Hologram_1_8_R1(String text, Location location) { - this.text = new String[]{text}; - this.location = location; - create(); - } - public Location getLocation() { return location; } @@ -80,7 +75,20 @@ public class Hologram_1_8_R1 implements Hologram { for (int i = 0; i < count; i++) { this.location.add(0, this.DISTANCE, 0); } - this.count = 0; + + count = 0; + exists = true; + } + + public boolean exists() { + return exists; + } + + public void remove() { + for (EntityArmorStand e : entitylist) { + e.die(); + } + exists = false; } } diff --git a/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R2.java b/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R2.java index 7ddb252..457c9fa 100644 --- a/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R2.java +++ b/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R2.java @@ -15,7 +15,8 @@ import java.util.List; public class Hologram_1_8_R2 implements Hologram { - int count; + private boolean exists = false; + private int count; private List entitylist = new ArrayList(); private String[] text; private Location location; @@ -28,12 +29,6 @@ public class Hologram_1_8_R2 implements Hologram { create(); } - public Hologram_1_8_R2(String text, Location location) { - this.text = new String[]{text}; - this.location = location; - create(); - } - public Location getLocation() { return location; } @@ -80,7 +75,20 @@ public class Hologram_1_8_R2 implements Hologram { for (int i = 0; i < count; i++) { this.location.add(0, this.DISTANCE, 0); } - this.count = 0; + + count = 0; + exists = true; + } + + public boolean exists() { + return exists; + } + + public void remove() { + for (EntityArmorStand e : entitylist) { + e.die(); + } + exists = false; } } diff --git a/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R3.java b/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R3.java index bc03741..45c9e54 100644 --- a/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R3.java +++ b/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_8_R3.java @@ -15,7 +15,8 @@ import java.util.List; public class Hologram_1_8_R3 implements Hologram { - int count; + private boolean exists = false; + private int count; private List entitylist = new ArrayList(); private String[] text; private Location location; @@ -28,12 +29,6 @@ public class Hologram_1_8_R3 implements Hologram { create(); } - public Hologram_1_8_R3(String text, Location location) { - this.text = new String[]{text}; - this.location = location; - create(); - } - public Location getLocation() { return location; } @@ -80,7 +75,20 @@ public class Hologram_1_8_R3 implements Hologram { for (int i = 0; i < count; i++) { this.location.add(0, this.DISTANCE, 0); } - this.count = 0; + + count = 0; + exists = true; + } + + public boolean exists() { + return exists; + } + + public void remove() { + for (EntityArmorStand e : entitylist) { + e.die(); + } + exists = false; } } diff --git a/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_9_R1.java b/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_9_R1.java index 6ddd5ac..d6b39a3 100644 --- a/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_9_R1.java +++ b/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_9_R1.java @@ -15,7 +15,8 @@ import java.util.List; public class Hologram_1_9_R1 implements Hologram { - int count; + private boolean exists = false; + private int count; private List entitylist = new ArrayList(); private String[] text; private Location location; @@ -28,12 +29,6 @@ public class Hologram_1_9_R1 implements Hologram { create(); } - public Hologram_1_9_R1(String text, Location location) { - this.text = new String[]{text}; - this.location = location; - create(); - } - public Location getLocation() { return location; } @@ -80,7 +75,20 @@ public class Hologram_1_9_R1 implements Hologram { for (int i = 0; i < count; i++) { this.location.add(0, this.DISTANCE, 0); } - this.count = 0; + + count = 0; + exists = true; + } + + public boolean exists() { + return exists; + } + + public void remove() { + for (EntityArmorStand e : entitylist) { + e.die(); + } + exists = false; } } diff --git a/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_9_R2.java b/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_9_R2.java index e4ce3f3..aa2c8d3 100644 --- a/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_9_R2.java +++ b/src/de/epiceric/shopchest/interfaces/hologram/Hologram_1_9_R2.java @@ -15,7 +15,8 @@ import java.util.List; public class Hologram_1_9_R2 implements Hologram { - int count; + private boolean exists = false; + private int count; private List entitylist = new ArrayList(); private String[] text; private Location location; @@ -28,12 +29,6 @@ public class Hologram_1_9_R2 implements Hologram { create(); } - public Hologram_1_9_R2(String text, Location location) { - this.text = new String[]{text}; - this.location = location; - create(); - } - public Location getLocation() { return location; } @@ -80,7 +75,20 @@ public class Hologram_1_9_R2 implements Hologram { for (int i = 0; i < count; i++) { this.location.add(0, this.DISTANCE, 0); } - this.count = 0; + + count = 0; + exists = true; + } + + public boolean exists() { + return exists; + } + + public void remove() { + for (EntityArmorStand e : entitylist) { + e.die(); + } + exists = false; } } diff --git a/src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R1.java b/src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R1.java deleted file mode 100644 index e97ef23..0000000 --- a/src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R1.java +++ /dev/null @@ -1,85 +0,0 @@ -package de.epiceric.shopchest.interfaces.utils; - -import de.epiceric.shopchest.ShopChest; -import de.epiceric.shopchest.config.Config; -import de.epiceric.shopchest.interfaces.Hologram; -import de.epiceric.shopchest.interfaces.Utils; -import de.epiceric.shopchest.shop.Shop; -import de.epiceric.shopchest.sql.Database; -import de.epiceric.shopchest.utils.ShopUtils; -import net.minecraft.server.v1_8_R1.EntityArmorStand; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerMoveEvent; - -public class Utils_1_8_R1 extends Utils { - - @Override - public void reload(Player player) { - - for (Shop shop : ShopUtils.getShops()) { - Hologram hologram = shop.getHologram(); - - if (shop.hasItem()) shop.getItem().remove(); - ShopUtils.removeShop(shop); - - for (Player p : ShopChest.getInstance().getServer().getOnlinePlayers()) { - hologram.hidePlayer(p); - } - - for (Object o : hologram.getEntities()) { - EntityArmorStand e = (EntityArmorStand) o; - e.getWorld().removeEntity(e); - } - - - } - - int count = 0; - - for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) { - - try { - Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP); - shop.createHologram(); - shop.createItem(); - ShopUtils.addShop(shop); - } catch (NullPointerException e) { - continue; - } - - count++; - - } - - if (player != null) player.sendMessage(Config.reloaded_shops(count)); - - for (Player p : Bukkit.getOnlinePlayers()) { - Bukkit.getPluginManager().callEvent(new PlayerMoveEvent(p, p.getLocation(), p.getLocation())); - } - - - } - - @Override - public void removeShops() { - for (Shop shop : ShopUtils.getShops()) { - Hologram hologram = shop.getHologram(); - - for (Player p : Bukkit.getOnlinePlayers()) { - hologram.hidePlayer(p); - } - - for (Object o : hologram.getEntities()) { - EntityArmorStand e = (EntityArmorStand) o; - e.getWorld().removeEntity(e); - } - - - if (shop.hasItem()) shop.getItem().remove(); - - } - } - - -} diff --git a/src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R2.java b/src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R2.java deleted file mode 100644 index 35f4796..0000000 --- a/src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R2.java +++ /dev/null @@ -1,87 +0,0 @@ -package de.epiceric.shopchest.interfaces.utils; - -import de.epiceric.shopchest.ShopChest; -import de.epiceric.shopchest.config.Config; -import de.epiceric.shopchest.interfaces.Hologram; -import de.epiceric.shopchest.interfaces.Utils; -import de.epiceric.shopchest.shop.Shop; -import de.epiceric.shopchest.sql.Database; -import de.epiceric.shopchest.utils.ShopUtils; -import net.minecraft.server.v1_8_R2.EntityArmorStand; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerMoveEvent; - -public class Utils_1_8_R2 extends Utils { - - @Override - public void reload(Player player) { - - for (Shop shop : ShopUtils.getShops()) { - Hologram hologram = shop.getHologram(); - - if (shop.hasItem()) shop.getItem().remove(); - ShopUtils.removeShop(shop); - - for (Player p : ShopChest.getInstance().getServer().getOnlinePlayers()) { - hologram.hidePlayer(p); - } - - for (Object o : hologram.getEntities()) { - EntityArmorStand e = (EntityArmorStand) o; - e.getWorld().removeEntity(e); - } - - - } - - int count = 0; - - for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) { - - try { - Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP); - shop.createHologram(); - shop.createItem(); - ShopUtils.addShop(shop); - } catch (NullPointerException e) { - continue; - } - - count++; - - } - - if (player != null) player.sendMessage(Config.reloaded_shops(count)); - - for (Player p : Bukkit.getOnlinePlayers()) { - Bukkit.getPluginManager().callEvent(new PlayerMoveEvent(p, p.getLocation(), p.getLocation())); - } - - - } - - - @Override - public void removeShops() { - - for (Shop shop : ShopUtils.getShops()) { - Hologram hologram = shop.getHologram(); - - for (Player p : Bukkit.getOnlinePlayers()) { - hologram.hidePlayer(p); - } - - for (Object o : hologram.getEntities()) { - EntityArmorStand e = (EntityArmorStand) o; - e.getWorld().removeEntity(e); - } - - - if (shop.hasItem()) shop.getItem().remove(); - - } - } - - -} diff --git a/src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R3.java b/src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R3.java deleted file mode 100644 index 128c3d4..0000000 --- a/src/de/epiceric/shopchest/interfaces/utils/Utils_1_8_R3.java +++ /dev/null @@ -1,86 +0,0 @@ -package de.epiceric.shopchest.interfaces.utils; - -import de.epiceric.shopchest.ShopChest; -import de.epiceric.shopchest.config.Config; -import de.epiceric.shopchest.interfaces.Hologram; -import de.epiceric.shopchest.interfaces.Utils; -import de.epiceric.shopchest.shop.Shop; -import de.epiceric.shopchest.sql.Database; -import de.epiceric.shopchest.utils.ShopUtils; -import net.minecraft.server.v1_8_R3.EntityArmorStand; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerMoveEvent; - -public class Utils_1_8_R3 extends Utils { - - @Override - public void reload(Player player) { - - for (Shop shop : ShopUtils.getShops()) { - Hologram hologram = shop.getHologram(); - - if (shop.hasItem()) shop.getItem().remove(); - ShopUtils.removeShop(shop); - - for (Player p : ShopChest.getInstance().getServer().getOnlinePlayers()) { - hologram.hidePlayer(p); - } - - for (Object o : hologram.getEntities()) { - EntityArmorStand e = (EntityArmorStand) o; - e.getWorld().removeEntity(e); - } - - - } - - int count = 0; - - for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) { - - try { - Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP); - shop.createHologram(); - shop.createItem(); - ShopUtils.addShop(shop); - } catch (NullPointerException e) { - continue; - } - - count++; - - } - - if (player != null) player.sendMessage(Config.reloaded_shops(count)); - - for (Player p : Bukkit.getOnlinePlayers()) { - Bukkit.getPluginManager().callEvent(new PlayerMoveEvent(p, p.getLocation(), p.getLocation())); - } - - - } - - - @Override - public void removeShops() { - for (Shop shop : ShopUtils.getShops()) { - Hologram hologram = shop.getHologram(); - - for (Player p : Bukkit.getOnlinePlayers()) { - hologram.hidePlayer(p); - } - - for (Object o : hologram.getEntities()) { - EntityArmorStand e = (EntityArmorStand) o; - e.getWorld().removeEntity(e); - } - - - if (shop.hasItem()) shop.getItem().remove(); - - } - } - - -} diff --git a/src/de/epiceric/shopchest/interfaces/utils/Utils_1_9_R1.java b/src/de/epiceric/shopchest/interfaces/utils/Utils_1_9_R1.java deleted file mode 100644 index 0267397..0000000 --- a/src/de/epiceric/shopchest/interfaces/utils/Utils_1_9_R1.java +++ /dev/null @@ -1,86 +0,0 @@ -package de.epiceric.shopchest.interfaces.utils; - -import de.epiceric.shopchest.ShopChest; -import de.epiceric.shopchest.config.Config; -import de.epiceric.shopchest.interfaces.Hologram; -import de.epiceric.shopchest.interfaces.Utils; -import de.epiceric.shopchest.shop.Shop; -import de.epiceric.shopchest.sql.Database; -import de.epiceric.shopchest.utils.ShopUtils; -import net.minecraft.server.v1_9_R1.EntityArmorStand; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerMoveEvent; - -public class Utils_1_9_R1 extends Utils { - - @Override - public void reload(Player player) { - - for (Shop shop : ShopUtils.getShops()) { - Hologram hologram = shop.getHologram(); - - if (shop.hasItem()) shop.getItem().remove(); - ShopUtils.removeShop(shop); - - for (Player p : ShopChest.getInstance().getServer().getOnlinePlayers()) { - hologram.hidePlayer(p); - } - - for (Object o : hologram.getEntities()) { - EntityArmorStand e = (EntityArmorStand) o; - e.getWorld().removeEntity(e); - } - - - } - - int count = 0; - - for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) { - - try { - Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP); - shop.createHologram(); - shop.createItem(); - ShopUtils.addShop(shop); - } catch (NullPointerException e) { - continue; - } - - count++; - - } - - if (player != null) player.sendMessage(Config.reloaded_shops(count)); - - for (Player p : Bukkit.getOnlinePlayers()) { - Bukkit.getPluginManager().callEvent(new PlayerMoveEvent(p, p.getLocation(), p.getLocation())); - } - - - } - - - @Override - public void removeShops() { - for (Shop shop : ShopUtils.getShops()) { - Hologram hologram = shop.getHologram(); - - for (Player p : Bukkit.getOnlinePlayers()) { - hologram.hidePlayer(p); - } - - for (Object o : hologram.getEntities()) { - EntityArmorStand e = (EntityArmorStand) o; - e.getWorld().removeEntity(e); - } - - - if (shop.hasItem()) shop.getItem().remove(); - - } - } - - -} diff --git a/src/de/epiceric/shopchest/interfaces/utils/Utils_1_9_R2.java b/src/de/epiceric/shopchest/interfaces/utils/Utils_1_9_R2.java deleted file mode 100644 index c66670c..0000000 --- a/src/de/epiceric/shopchest/interfaces/utils/Utils_1_9_R2.java +++ /dev/null @@ -1,86 +0,0 @@ -package de.epiceric.shopchest.interfaces.utils; - -import de.epiceric.shopchest.ShopChest; -import de.epiceric.shopchest.config.Config; -import de.epiceric.shopchest.interfaces.Hologram; -import de.epiceric.shopchest.interfaces.Utils; -import de.epiceric.shopchest.shop.Shop; -import de.epiceric.shopchest.sql.Database; -import de.epiceric.shopchest.utils.ShopUtils; -import net.minecraft.server.v1_9_R2.EntityArmorStand; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerMoveEvent; - -public class Utils_1_9_R2 extends Utils { - - @Override - public void reload(Player player) { - - for (Shop shop : ShopUtils.getShops()) { - Hologram hologram = shop.getHologram(); - - if (shop.hasItem()) shop.getItem().remove(); - ShopUtils.removeShop(shop); - - for (Player p : ShopChest.getInstance().getServer().getOnlinePlayers()) { - hologram.hidePlayer(p); - } - - for (Object o : hologram.getEntities()) { - EntityArmorStand e = (EntityArmorStand) o; - e.getWorld().removeEntity(e); - } - - - } - - int count = 0; - - for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) { - - try { - Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP); - shop.createHologram(); - shop.createItem(); - ShopUtils.addShop(shop); - } catch (NullPointerException e) { - continue; - } - - count++; - - } - - if (player != null) player.sendMessage(Config.reloaded_shops(count)); - - for (Player p : Bukkit.getOnlinePlayers()) { - Bukkit.getPluginManager().callEvent(new PlayerMoveEvent(p, p.getLocation(), p.getLocation())); - } - - - } - - - @Override - public void removeShops() { - for (Shop shop : ShopUtils.getShops()) { - Hologram hologram = shop.getHologram(); - - for (Player p : Bukkit.getOnlinePlayers()) { - hologram.hidePlayer(p); - } - - for (Object o : hologram.getEntities()) { - EntityArmorStand e = (EntityArmorStand) o; - e.getWorld().removeEntity(e); - } - - - if (shop.hasItem()) shop.getItem().remove(); - - } - } - - -} diff --git a/src/de/epiceric/shopchest/shop/Shop.java b/src/de/epiceric/shopchest/shop/Shop.java index c7072d9..baa2f6c 100644 --- a/src/de/epiceric/shopchest/shop/Shop.java +++ b/src/de/epiceric/shopchest/shop/Shop.java @@ -3,9 +3,10 @@ package de.epiceric.shopchest.shop; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.interfaces.Hologram; -import de.epiceric.shopchest.interfaces.Utils; +import de.epiceric.shopchest.utils.Utils; import de.epiceric.shopchest.interfaces.hologram.*; import de.epiceric.shopchest.utils.ItemNames; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; @@ -25,6 +26,7 @@ import java.util.UUID; public class Shop { + private int id; private ShopChest plugin; private OfflinePlayer vendor; private ItemStack product; @@ -35,7 +37,8 @@ public class Shop { private double sellPrice; private ShopType shopType; - public Shop(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.plugin = plugin; this.vendor = vendor; this.product = product; @@ -43,16 +46,27 @@ public class Shop { this.buyPrice = buyPrice; this.sellPrice = sellPrice; this.shopType = shopType; + + if (hologram == null || !hologram.exists()) createHologram(); + if (item == null || item.isDead()) createItem(); } public void removeHologram() { - for (Player player : plugin.getServer().getOnlinePlayers()) { - getHologram().hidePlayer(player); + if (hologram != null && hologram.exists()) { + for (Player p : Bukkit.getOnlinePlayers()) { + hologram.hidePlayer(p); + } + + hologram.remove(); } } - public void createItem() { + public void removeItem() { + if (item != null && !item.isDead()) + item.remove(); + } + private void createItem() { Item item; Location itemLocation; ItemStack itemStack; @@ -75,7 +89,7 @@ public class Shop { this.item = item; } - public void createHologram() { + private void createHologram() { boolean doubleChest; @@ -170,6 +184,10 @@ public class Shop { } + public int getID() { + return id; + } + public OfflinePlayer getVendor() { return vendor; } @@ -198,14 +216,6 @@ public class Shop { return hologram; } - public Item getItem() { - return item; - } - - public boolean hasItem() { - return item != null; - } - public enum ShopType { NORMAL, ADMIN; diff --git a/src/de/epiceric/shopchest/sql/Database.java b/src/de/epiceric/shopchest/sql/Database.java index 0f2d11b..969ec14 100644 --- a/src/de/epiceric/shopchest/sql/Database.java +++ b/src/de/epiceric/shopchest/sql/Database.java @@ -1,7 +1,7 @@ package de.epiceric.shopchest.sql; import de.epiceric.shopchest.ShopChest; -import de.epiceric.shopchest.interfaces.Utils; +import de.epiceric.shopchest.utils.Utils; import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.utils.ShopUtils; @@ -11,8 +11,6 @@ import org.bukkit.OfflinePlayer; import org.bukkit.World; import org.bukkit.inventory.ItemStack; -import java.io.File; -import java.io.IOException; import java.sql.*; import java.util.UUID; @@ -100,29 +98,11 @@ public abstract class Database { return 0; } - public int getShopID(Shop shop) { - for (int i = 1; i < getHighestID() + 1; i++) { - try { - Shop s = (Shop) get(i, null); - if (s.getLocation().equals(shop.getLocation())) { - return i; - } - } catch (NullPointerException ex) { /* Empty catch block... */ } - } - - return 0; - } - public void removeShop(Shop shop) { - int id = getShopID(shop); - if (id == 0) return; - - if (shop.hasItem()) shop.getItem().remove(); - PreparedStatement ps = null; try { - ps = connection.prepareStatement("DELETE FROM shop_list WHERE id = " + id + ";"); + ps = connection.prepareStatement("DELETE FROM shop_list WHERE id = " + shop.getID() + ";"); ps.executeUpdate(); } catch (SQLException ex) { ex.printStackTrace(); @@ -149,7 +129,7 @@ public abstract class Database { if (shop != null) return shop; else { - return new Shop(plugin, + return new Shop(id, plugin, (OfflinePlayer) get(id, ShopInfo.VENDOR), (ItemStack) get(id, ShopInfo.PRODUCT), (Location) get(id, ShopInfo.LOCATION), @@ -180,7 +160,7 @@ public abstract class Database { if (shoptype.equals("INFINITE")) { - Shop newShop = new Shop(plugin, + Shop newShop = new Shop(id, plugin, (OfflinePlayer) get(id, ShopInfo.VENDOR), (ItemStack) get(id, ShopInfo.PRODUCT), (Location) get(id, ShopInfo.LOCATION), @@ -188,7 +168,8 @@ public abstract class Database { (double) get(id, ShopInfo.SELLPRICE), ShopType.ADMIN); - setShop(id, newShop); + addShop(newShop); + return ShopType.ADMIN; } return ShopType.valueOf(shoptype); @@ -205,13 +186,13 @@ public abstract class Database { return null; } - public void setShop(int id, Shop shop) { + public void addShop(Shop shop) { PreparedStatement ps = null; try { ps = connection.prepareStatement("REPLACE INTO shop_list (id,vendor,product,world,x,y,z,buyprice,sellprice,shoptype) VALUES(?,?,?,?,?,?,?,?,?,?)"); - ps.setInt(1, id); + ps.setInt(1, shop.getID()); ps.setString(2, shop.getVendor().getUniqueId().toString()); ps.setString(3, Utils.encode(shop.getProduct())); ps.setString(4, shop.getLocation().getWorld().getName()); @@ -230,11 +211,6 @@ public abstract class Database { } } - public void addShop(Shop shop) { - int id = getNextFreeID(); - setShop(id, shop); - } - private void close(PreparedStatement ps, ResultSet rs) { try { if (ps != null) diff --git a/src/de/epiceric/shopchest/utils/ShopUtils.java b/src/de/epiceric/shopchest/utils/ShopUtils.java index 5857210..faec20f 100644 --- a/src/de/epiceric/shopchest/utils/ShopUtils.java +++ b/src/de/epiceric/shopchest/utils/ShopUtils.java @@ -2,8 +2,9 @@ package de.epiceric.shopchest.utils; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.config.Config; -import de.epiceric.shopchest.interfaces.Utils; import de.epiceric.shopchest.shop.Shop; +import de.epiceric.shopchest.sql.Database; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; @@ -11,6 +12,7 @@ import org.bukkit.block.Block; import org.bukkit.block.Chest; import org.bukkit.block.DoubleChest; import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.inventory.InventoryHolder; import java.util.ArrayList; @@ -23,7 +25,6 @@ public class ShopUtils { private static HashMap shopLocation = new HashMap<>(); public static Shop getShop(Location location) { - Location newLocation = new Location(location.getWorld(), location.getX(), location.getY(), location.getZ()); if (shopLocation.containsKey(newLocation)) { @@ -31,19 +32,14 @@ public class ShopUtils { } else { return null; } - } public static boolean isShop(Location location) { - Location newLocation = new Location(location.getWorld(), location.getX(), location.getY(), location.getZ()); - return shopLocation.containsKey(newLocation); - } public static Shop[] getShops() { - ArrayList shops = new ArrayList<>(); for (Shop shop : shopLocation.values()) { @@ -51,16 +47,16 @@ public class ShopUtils { } return shops.toArray(new Shop[shops.size()]); - } - public static void addShop(Shop shop) { - + public static void addShop(Shop shop, boolean addToDatabase) { Location loc = shop.getLocation(); Block b = loc.getBlock(); + if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) { Chest c = (Chest) b.getState(); InventoryHolder ih = c.getInventory().getHolder(); + if (ih instanceof DoubleChest) { DoubleChest dc = (DoubleChest) ih; Chest r = (Chest) dc.getRightSide(); @@ -69,37 +65,40 @@ public class ShopUtils { shopLocation.put(r.getLocation(), shop); shopLocation.put(l.getLocation(), shop); return; - + } else { + shopLocation.put(shop.getLocation(), shop); } + + if (addToDatabase) + ShopChest.database.addShop(shop); } - - shopLocation.put(shop.getLocation(), shop); - } - public static void removeShop(Shop shop) { - + public static void removeShop(Shop shop, boolean removeFromDatabase) { Location loc = shop.getLocation(); Block b = loc.getBlock(); + if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) { Chest c = (Chest) b.getState(); InventoryHolder ih = c.getInventory().getHolder(); + if (ih instanceof DoubleChest) { DoubleChest dc = (DoubleChest) ih; Chest r = (Chest) dc.getRightSide(); Chest l = (Chest) dc.getLeftSide(); - if (shop.hasItem()) shop.getItem().remove(); - shop.removeHologram(); shopLocation.remove(r.getLocation()); shopLocation.remove(l.getLocation()); - return; - + } else { + shopLocation.remove(shop.getLocation()); } + + shop.removeItem(); + shop.removeHologram(); + + if (removeFromDatabase) + ShopChest.database.removeShop(shop); } - - shopLocation.remove(shop.getLocation()); - } public static int getShopLimit(Player p) { @@ -137,7 +136,6 @@ public class ShopUtils { limit = highestLimit; } - } for (String key : Config.shopLimits_player()) { @@ -166,5 +164,28 @@ public class ShopUtils { return shopCount; } + public static void reloadShops(Player player) { + for (Shop shop : ShopUtils.getShops()) { + ShopUtils.removeShop(shop, false); + } + int count = 0; + for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) { + + try { + Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP); + ShopUtils.addShop(shop, false); + } catch (NullPointerException e) { + continue; + } + + count++; + } + + if (player != null) player.sendMessage(Config.reloaded_shops(count)); + + for (Player p : Bukkit.getOnlinePlayers()) { + Bukkit.getPluginManager().callEvent(new PlayerMoveEvent(p, p.getLocation(), p.getLocation())); + } + } } diff --git a/src/de/epiceric/shopchest/interfaces/Utils.java b/src/de/epiceric/shopchest/utils/Utils.java similarity index 94% rename from src/de/epiceric/shopchest/interfaces/Utils.java rename to src/de/epiceric/shopchest/utils/Utils.java index f178ebf..0b7e3e4 100644 --- a/src/de/epiceric/shopchest/interfaces/Utils.java +++ b/src/de/epiceric/shopchest/utils/Utils.java @@ -1,4 +1,4 @@ -package de.epiceric.shopchest.interfaces; +package de.epiceric.shopchest.utils; import org.apache.commons.codec.binary.Base64; import org.bukkit.Bukkit; @@ -14,7 +14,7 @@ import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; import java.util.HashMap; -public abstract class Utils { +public class Utils { public static int getAmount(Inventory inventory, ItemStack itemStack) { int amount = 0; @@ -80,9 +80,5 @@ public abstract class Utils { return config.getItemStack("i", null); } - public abstract void reload(Player p); - - public abstract void removeShops(); - }