From 24c32b5e644b9426630b08963344029877a8eea8 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 2 Sep 2015 16:18:32 +0200 Subject: [PATCH] Added ClearLagg Support --- src/de/epiceric/shopchest/ShopChest.java | 9 +-- .../shopchest/event/RegenerateShopItem.java | 4 +- .../event/RegenerateShopItemAfterRemove.java | 80 +++++++++++++++++++ 3 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 src/de/epiceric/shopchest/event/RegenerateShopItemAfterRemove.java diff --git a/src/de/epiceric/shopchest/ShopChest.java b/src/de/epiceric/shopchest/ShopChest.java index 4254686..ad9d359 100644 --- a/src/de/epiceric/shopchest/ShopChest.java +++ b/src/de/epiceric/shopchest/ShopChest.java @@ -24,6 +24,7 @@ import de.epiceric.shopchest.event.InteractShop; import de.epiceric.shopchest.event.NotifyUpdate; import de.epiceric.shopchest.event.ProtectChest; import de.epiceric.shopchest.event.RegenerateShopItem; +import de.epiceric.shopchest.event.RegenerateShopItemAfterRemove; import de.epiceric.shopchest.event.UpdateHolograms; import de.epiceric.shopchest.shop.Hologram; import de.epiceric.shopchest.shop.Shop; @@ -33,7 +34,6 @@ import de.epiceric.shopchest.utils.JsonBuilder.HoverAction; import de.epiceric.shopchest.utils.Metrics; import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.UpdateChecker; -import de.epiceric.shopchest.utils.Utils; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; import net.minecraft.server.v1_8_R3.EntityArmorStand; @@ -75,12 +75,6 @@ public class ShopChest extends JavaPlugin{ return perm != null; } - - @Override - public void onLoad() { - getLogger().info(Utils.getVersion()); - } - @Override public void onEnable() { @@ -162,6 +156,7 @@ public class ShopChest extends JavaPlugin{ getServer().getPluginManager().registerEvents(new InteractShop(this), this); getServer().getPluginManager().registerEvents(new NotifyUpdate(), this); getServer().getPluginManager().registerEvents(new ProtectChest(), this); + if (getServer().getPluginManager().getPlugin("ClearLag") != null) getServer().getPluginManager().registerEvents(new RegenerateShopItemAfterRemove(), this); } diff --git a/src/de/epiceric/shopchest/event/RegenerateShopItem.java b/src/de/epiceric/shopchest/event/RegenerateShopItem.java index ad4a738..a584cb0 100644 --- a/src/de/epiceric/shopchest/event/RegenerateShopItem.java +++ b/src/de/epiceric/shopchest/event/RegenerateShopItem.java @@ -8,10 +8,12 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.ItemDespawnEvent; import org.bukkit.event.player.PlayerPickupItemEvent; -public class RegenerateShopItem implements Listener{ +public class RegenerateShopItem implements Listener { public RegenerateShopItem() {} + + @EventHandler(priority = EventPriority.HIGH) public void onItemDespawn(ItemDespawnEvent e) { Item item = e.getEntity(); diff --git a/src/de/epiceric/shopchest/event/RegenerateShopItemAfterRemove.java b/src/de/epiceric/shopchest/event/RegenerateShopItemAfterRemove.java new file mode 100644 index 0000000..25bf0b1 --- /dev/null +++ b/src/de/epiceric/shopchest/event/RegenerateShopItemAfterRemove.java @@ -0,0 +1,80 @@ +package de.epiceric.shopchest.event; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.inventory.ItemStack; + +import de.epiceric.shopchest.ShopChest; +import de.epiceric.shopchest.shop.Hologram; +import de.epiceric.shopchest.shop.Shop; +import de.epiceric.shopchest.utils.ShopUtils; +import net.minecraft.server.v1_8_R3.EntityArmorStand; + +public class RegenerateShopItemAfterRemove implements Listener { + + + @EventHandler(priority = EventPriority.HIGH) + public void onEntityRemove(me.minebuilders.clearlag.events.EntityRemoveEvent e) { + boolean containsShopItem = false; + for (Entity entity : e.getEntityList()) { + if (entity.hasMetadata("shopItem")) { + containsShopItem = true; + break; + } + } + + if (containsShopItem) reload(); + } + + private void reload() { + + for (Shop shop : ShopUtils.getShops()) { + Hologram hologram = shop.getHologram(); + + 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); + } + + + } + + for (String key : ShopChest.getInstance().shopChests.getKeys(false)) { + + OfflinePlayer vendor = ShopChest.getInstance().shopChests.getOfflinePlayer(key + ".vendor"); + int locationX = ShopChest.getInstance().shopChests.getInt(key + ".location.x"); + int locationY = ShopChest.getInstance().shopChests.getInt(key + ".location.y"); + int locationZ = ShopChest.getInstance().shopChests.getInt(key + ".location.z"); + World locationWorld = ShopChest.getInstance().getServer().getWorld(ShopChest.getInstance().shopChests.getString(key + ".location.world")); + Location location = new Location(locationWorld, locationX, locationY, locationZ); + ItemStack product = ShopChest.getInstance().shopChests.getItemStack(key + ".product"); + double buyPrice = ShopChest.getInstance().shopChests.getDouble(key + ".price.buy"); + double sellPrice = ShopChest.getInstance().shopChests.getDouble(key + ".price.sell"); + boolean infinite = ShopChest.getInstance().shopChests.getBoolean(key + ".infinite"); + + ShopUtils.addShop(new Shop(ShopChest.getInstance(), vendor, product, location, buyPrice, sellPrice, infinite)); + + } + + for (Player p : Bukkit.getOnlinePlayers()) { + Bukkit.getPluginManager().callEvent(new PlayerMoveEvent(p, p.getLocation(), p.getLocation())); + } + + } + +}