Make DisplayItem to Interface

Former-commit-id: f01134b62379387cbf353b71421070a0f7b51f0d [formerly f560df4bce3e04d29d45b67d73d405238e6fa3e3]
Former-commit-id: ee53d111616b1cfc9911400d89ab2f8e557bdecd
This commit is contained in:
Ghost_chu committed 2019-06-06 14:39:57 +08:00
1 parent 0012ecabad
commit 5f143ecef8
3 files changed
+342 -155

No files matched your search

@@ -103,7 +103,7 @@ public class ContainerShop implements Shop {
this.plugin = (QuickShop) Bukkit.getPluginManager().getPlugin("QuickShop");
this.item.setAmount(1);
if (plugin.isDisplay()) {
this.displayItem = new DisplayItem(this, this.item);
this.displayItem = new RealDisplayItem(this, this.item);
}
this.shopType = type;
this.unlimited = unlimited;
@@ -1,182 +1,190 @@
package org.maxgamer.quickshop.Shop;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.Vector;
import org.maxgamer.quickshop.QuickShop;
//import org.maxgamer.quickshop.Util.NMS;
/**
* @author Netherfoam A display item, that spawns a block above the chest and
* cannot be interacted with.
*/
public class DisplayItem {
private Shop shop;
private ItemStack iStack;
private Item item;
static QuickShop plugin = QuickShop.instance;
public interface DisplayItem {
// private Shop shop;
// private ItemStack iStack;
// private Item item;
// static QuickShop plugin = QuickShop.instance;
// private Location displayLoc;
/**
* ZZ
* Creates a new display item.
*
* @param shop The shop (See Shop)
* @param iStack The item stack to clone properties of the display item from.
*/
public DisplayItem(Shop shop, ItemStack iStack) {
this.shop = shop;
this.iStack = iStack.clone();
// /**
// * ZZ
// * Creates a new display item.
// *
// * @param shop The shop (See Shop)
// * @param iStack The item stack to clone properties of the display item from.
// */
// public DisplayItem(Shop shop, ItemStack iStack) {
// this.shop = shop;
// this.iStack = iStack.clone();
//
// // this.displayLoc = shop.getLocation().clone().add(0.5, 1.2, 0.5);
// }
// this.displayLoc = shop.getLocation().clone().add(0.5, 1.2, 0.5);
}
public abstract void spawn();
/**
* Spawns the dummy item on top of the shop.
*/
public void spawn() {
if (shop.getLocation().getWorld() == null)
return;
Location dispLoc = this.getDisplayLocation();
//Call Event for QSAPI
// /**
// * Spawns the dummy item on top of the shop.
// */
// public void spawn() {
// if (shop.getLocation().getWorld() == null)
// return;
// Location dispLoc = this.getDisplayLocation();
// //Call Event for QSAPI
//
// ShopDisplayItemSpawnEvent shopDisplayItemSpawnEvent = new ShopDisplayItemSpawnEvent(shop, iStack);
// Bukkit.getPluginManager().callEvent(shopDisplayItemSpawnEvent);
// if (shopDisplayItemSpawnEvent.isCancelled()) {
// return;
// }
// this.item = shop.getLocation().getWorld().dropItem(dispLoc, this.iStack);
// this.item.setVelocity(new Vector(0, 0.1, 0));
// try {
// this.safeGuard(this.item);
// ShopDisplayItemSpawnedEvent shopDisplayItemSpawnedEvent = new ShopDisplayItemSpawnedEvent(shop, this.item);
// Bukkit.getPluginManager().callEvent(shopDisplayItemSpawnedEvent);
// // NMS.safeGuard
// } catch (Exception e) {
// e.printStackTrace();
// plugin.getLogger().log(Level.WARNING,
// "QuickShop version mismatch! This version of QuickShop is incompatible with this version of bukkit! Try update?");
// }
// }
ShopDisplayItemSpawnEvent shopDisplayItemSpawnEvent = new ShopDisplayItemSpawnEvent(shop, iStack);
Bukkit.getPluginManager().callEvent(shopDisplayItemSpawnEvent);
if (shopDisplayItemSpawnEvent.isCancelled()) {
return;
}
this.item = shop.getLocation().getWorld().dropItem(dispLoc, this.iStack);
this.item.setVelocity(new Vector(0, 0.1, 0));
try {
this.safeGuard(this.item);
ShopDisplayItemSpawnedEvent shopDisplayItemSpawnedEvent = new ShopDisplayItemSpawnedEvent(shop, this.item);
Bukkit.getPluginManager().callEvent(shopDisplayItemSpawnedEvent);
// NMS.safeGuard
} catch (Exception e) {
e.printStackTrace();
plugin.getLogger().log(Level.WARNING,
"QuickShop version mismatch! This version of QuickShop is incompatible with this version of bukkit! Try update?");
}
}
// /**
// * Spawns the new display item. Does not remove duplicate items.
// */
// public void respawn() {
// remove();
// spawn();
// }
/**
* Spawns the new display item. Does not remove duplicate items.
*/
public void respawn() {
remove();
spawn();
}
public abstract void respawn();
/**
* Set item is QuickShop's DisplayItem and prevent them.
*
* @param item
*/
public void safeGuard(Item item) {
item.setPickupDelay(Integer.MAX_VALUE);
ItemMeta iMeta = item.getItemStack().getItemMeta();
public abstract void safeGuard(Entity entity);
// /**
// * Set item is QuickShop's DisplayItem and prevent them.
// *
// * @param item
// */
// public void safeGuard(Item item) {
// item.setPickupDelay(Integer.MAX_VALUE);
// ItemMeta iMeta = item.getItemStack().getItemMeta();
//
// if (plugin.getConfig().getBoolean("shop.display-item-use-name")) {
// item.setCustomName("QuickShop");
// iMeta.setDisplayName("QuickShop");
// }
// item.setPortalCooldown(Integer.MAX_VALUE);
// item.setSilent(true);
// item.setInvulnerable(true);
// java.util.List<String> lore = new ArrayList<String>();
// for (int i = 0; i < 21; i++) {
// lore.add("QuickShop DisplayItem"); //Create 20 lines lore to make sure no stupid plugin accident remove mark.
// }
// iMeta.setLore(lore);
// item.getItemStack().setItemMeta(iMeta);
// }
//
if (plugin.getConfig().getBoolean("shop.display-item-use-name")) {
item.setCustomName("QuickShop");
iMeta.setDisplayName("QuickShop");
}
item.setPortalCooldown(Integer.MAX_VALUE);
item.setSilent(true);
item.setInvulnerable(true);
java.util.List<String> lore = new ArrayList<String>();
for (int i = 0; i < 21; i++) {
lore.add("QuickShop DisplayItem"); //Create 20 lines lore to make sure no stupid plugin accident remove mark.
}
iMeta.setLore(lore);
item.getItemStack().setItemMeta(iMeta);
}
public abstract boolean checkShopItem(ItemStack itemStack);
/**
* Check the ItemStack is or not a DisplayItem
* @param itemStack The ItemStack you want to check.
* @return The check result.
*/
// /**
// * Check the ItemStack is or not a DisplayItem
// * @param itemStack The ItemStack you want to check.
// * @return The check result.
// */
//
// public static boolean checkShopItem(ItemStack itemStack) {
// if (itemStack == null)
// return false;
// if (!itemStack.hasItemMeta()) {
// return false;
// }
// if (itemStack.getItemMeta().hasDisplayName() && itemStack.getItemMeta().getDisplayName().contains("QuickShop")) {
// return true;
// }
// if (itemStack.getItemMeta().hasLore()) {
// List<String> lores = itemStack.getItemMeta().getLore();
// for (String singleLore : lores) {
// if (singleLore.equals("QuickShop DisplayItem") || singleLore.contains("QuickShop DisplayItem")) {
// return true;
// }
// }
// }
// return false;
// }
public static boolean checkShopItem(ItemStack itemStack) {
if (itemStack == null)
return false;
if (!itemStack.hasItemMeta()) {
return false;
}
if (itemStack.getItemMeta().hasDisplayName() && itemStack.getItemMeta().getDisplayName().contains("QuickShop")) {
return true;
}
if (itemStack.getItemMeta().hasLore()) {
List<String> lores = itemStack.getItemMeta().getLore();
for (String singleLore : lores) {
if (singleLore.equals("QuickShop DisplayItem") || singleLore.contains("QuickShop DisplayItem")) {
return true;
}
}
}
return false;
}
public abstract boolean removeDupe();
/**
* Removes all items floating ontop of the chest that aren't the display
* item.
*/
public boolean removeDupe() {
if (shop.getLocation().getWorld() == null)
return false;
Location displayLoc = shop.getLocation().getBlock().getRelative(0, 1, 0).getLocation();
boolean removed = false;
Chunk c = displayLoc.getChunk();
for (Entity e : c.getEntities()) {
if (!(e instanceof Item))
continue;
if (this.item != null && e.getEntityId() == this.item.getEntityId())
continue;
Location eLoc = e.getLocation().getBlock().getLocation();
if (eLoc.equals(displayLoc) || eLoc.equals(shop.getLocation())) {
ItemStack near = ((Item) e).getItemStack();
// if its the same its a dupe
if (this.shop.matches(near)) {
e.remove();
removed = true;
}
}
}
return removed;
// /**
// * Removes all items floating ontop of the chest that aren't the display
// * item.
// */
// public boolean removeDupe() {
// if (shop.getLocation().getWorld() == null)
// return false;
// Location displayLoc = shop.getLocation().getBlock().getRelative(0, 1, 0).getLocation();
// boolean removed = false;
// Chunk c = displayLoc.getChunk();
// for (Entity e : c.getEntities()) {
// if (!(e instanceof Item))
// continue;
// if (this.item != null && e.getEntityId() == this.item.getEntityId())
// continue;
// Location eLoc = e.getLocation().getBlock().getLocation();
// if (eLoc.equals(displayLoc) || eLoc.equals(shop.getLocation())) {
// ItemStack near = ((Item) e).getItemStack();
// // if its the same its a dupe
// if (this.shop.matches(near)) {
// e.remove();
// removed = true;
// }
// }
// }
// return removed;
//
// }
}
public abstract void remove();
/**
* Removes the display item.
*/
public void remove() {
if (this.item == null)
return;
this.item.remove();
this.item = null;
}
// /**
// * Removes the display item.
// */
// public void remove() {
// if (this.item == null)
// return;
// this.item.remove();
// this.item = null;
// }
/**
* @return Returns the exact location of the display item. (1 above shop
* block, in the center)
*/
public Location getDisplayLocation() {
return this.shop.getLocation().clone().add(0.5, 1.2, 0.5);
}
public abstract Location getDisplayLocation();
/**
* Returns the reference to this shops item. Do not modify.
*/
public Item getItem() {
return this.item;
}
// /**
// * @return Returns the exact location of the display item. (1 above shop
// * block, in the center)
// */
// public Location getDisplayLocation() {
// return this.shop.getLocation().clone().add(0.5, 1.2, 0.5);
// }
//
public abstract Item getItem();
// /**
// * Returns the reference to this shops item. Do not modify.
// */
// public Item getItem() {
// return this.item;
// }
}
@@ -0,0 +1,179 @@
package org.maxgamer.quickshop.Shop;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.Vector;
import org.maxgamer.quickshop.QuickShop;
public class RealDisplayItem implements DisplayItem {
private Shop shop;
private ItemStack iStack;
private Item item;
static QuickShop plugin = QuickShop.instance;
/**
* ZZ
* Creates a new display item.
*
* @param shop The shop (See Shop)
* @param iStack The item stack to clone properties of the display item from.
*/
public RealDisplayItem(Shop shop, ItemStack iStack) {
this.shop = shop;
this.iStack = iStack.clone();
// this.displayLoc = shop.getLocation().clone().add(0.5, 1.2, 0.5);
}
/**
* Spawns the dummy item on top of the shop.
*/
public void spawn() {
if (shop.getLocation().getWorld() == null)
return;
Location dispLoc = this.getDisplayLocation();
//Call Event for QSAPI
ShopDisplayItemSpawnEvent shopDisplayItemSpawnEvent = new ShopDisplayItemSpawnEvent(shop, iStack);
Bukkit.getPluginManager().callEvent(shopDisplayItemSpawnEvent);
if (shopDisplayItemSpawnEvent.isCancelled()) {
return;
}
this.item = shop.getLocation().getWorld().dropItem(dispLoc, this.iStack);
this.item.setVelocity(new Vector(0, 0.1, 0));
try {
this.safeGuard(this.item);
ShopDisplayItemSpawnedEvent shopDisplayItemSpawnedEvent = new ShopDisplayItemSpawnedEvent(shop, this.item);
Bukkit.getPluginManager().callEvent(shopDisplayItemSpawnedEvent);
// NMS.safeGuard
} catch (Exception e) {
e.printStackTrace();
plugin.getLogger().log(Level.WARNING,
"QuickShop version mismatch! This version of QuickShop is incompatible with this version of bukkit! Try update?");
}
}
/**
* Spawns the new display item. Does not remove duplicate items.
*/
public void respawn() {
remove();
spawn();
}
/**
* Set item is QuickShop's DisplayItem and prevent them.
*
* @param entity
*/
public void safeGuard(Entity entity) {
Item item = (Item) entity;
item.setPickupDelay(Integer.MAX_VALUE);
ItemMeta iMeta = item.getItemStack().getItemMeta();
if (plugin.getConfig().getBoolean("shop.display-item-use-name")) {
item.setCustomName("QuickShop");
iMeta.setDisplayName("QuickShop");
}
item.setPortalCooldown(Integer.MAX_VALUE);
item.setSilent(true);
item.setInvulnerable(true);
java.util.List<String> lore = new ArrayList<String>();
for (int i = 0; i < 21; i++) {
lore.add("QuickShop DisplayItem"); //Create 20 lines lore to make sure no stupid plugin accident remove mark.
}
iMeta.setLore(lore);
item.getItemStack().setItemMeta(iMeta);
}
/**
* Check the ItemStack is or not a DisplayItem
*
* @param itemStack The ItemStack you want to check.
* @return The check result.
*/
public boolean checkShopItem(ItemStack itemStack) {
if (itemStack == null)
return false;
if (!itemStack.hasItemMeta()) {
return false;
}
if (itemStack.getItemMeta().hasDisplayName() && itemStack.getItemMeta().getDisplayName().contains("QuickShop")) {
return true;
}
if (itemStack.getItemMeta().hasLore()) {
List<String> lores = itemStack.getItemMeta().getLore();
for (String singleLore : lores) {
if (singleLore.equals("QuickShop DisplayItem") || singleLore.contains("QuickShop DisplayItem")) {
return true;
}
}
}
return false;
}
/**
* Removes all items floating ontop of the chest that aren't the display
* item.
*/
public boolean removeDupe() {
if (shop.getLocation().getWorld() == null)
return false;
Location displayLoc = shop.getLocation().getBlock().getRelative(0, 1, 0).getLocation();
boolean removed = false;
Chunk c = displayLoc.getChunk();
for (Entity e : c.getEntities()) {
if (!(e instanceof Item))
continue;
if (this.item != null && e.getEntityId() == this.item.getEntityId())
continue;
Location eLoc = e.getLocation().getBlock().getLocation();
if (eLoc.equals(displayLoc) || eLoc.equals(shop.getLocation())) {
ItemStack near = ((Item) e).getItemStack();
// if its the same its a dupe
if (this.shop.matches(near)) {
e.remove();
removed = true;
}
}
}
return removed;
}
/**
* Removes the display item.
*/
public void remove() {
if (this.item == null)
return;
this.item.remove();
this.item = null;
}
/**
* @return Returns the exact location of the display item. (1 above shop
* block, in the center)
*/
public Location getDisplayLocation() {
return this.shop.getLocation().clone().add(0.5, 1.2, 0.5);
}
/**
* Returns the reference to this shops item. Do not modify.
*/
public Item getItem() {
return this.item;
}
}