use of com.robomwm.prettysimpleshop.event.ShopSelectEvent in project PrettySimpleShop by MLG-Fortress.
the class ShopListener method selectShop.
public boolean selectShop(Player player, Block block, boolean wantToBuy) {
if (!config.isShopBlock(block.getType()))
return false;
Container container = (Container) block.getState();
if (!shopAPI.isShop(container))
return false;
ItemStack item = shopAPI.getItemStack(container);
double price = shopAPI.getPrice(container);
if (price < 0) {
config.sendMessage(player, "noPrice");
return true;
} else if (item == null) {
config.sendMessage(player, "noStock");
config.sendTip(player, "noStock");
return true;
}
ShopInfo shopInfo = new ShopInfo(shopAPI.getLocation(container), item, price);
ShopSelectEvent shopSelectEvent = new ShopSelectEvent(player, shopInfo, shopInfo.equals(selectedShop.get(player)) || wantToBuy);
selectedShop.put(player, shopInfo);
// Refactor: put this in the ShopInfo constructor instead
String textToSend = config.getString("saleInfo", PrettySimpleShop.getItemName(item), economy.format(price), Integer.toString(item.getAmount()));
String json;
item.setAmount(1);
try {
// CraftItemStack#asNMSCopy(itemStack); //nms version of the ItemStack
Object nmsItemStack = asNMSCopy.invoke(null, item);
// new NBTTagCompoundClazz(); //get a new NBTTagCompound, which will contain the nmsItemStack.
Object nbtTagCompound = NBTTagCompoundClazz.newInstance();
// nmsItemStack#save(nbtTagCompound); //saves nmsItemStack into our new NBTTagCompound
nbtTagCompound = saveNMSItemStack.invoke(nmsItemStack, nbtTagCompound);
json = nbtTagCompound.toString();
} catch (Throwable rock) {
// print stacktrace in debug
// https://howtodoinjava.com/java/string/convert-stacktrace-to-string/
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
rock.printStackTrace(pw);
PrettySimpleShop.debug(sw.toString());
player.sendMessage(textToSend);
instance.getServer().getPluginManager().callEvent(shopSelectEvent);
return true;
}
BaseComponent[] hoverEventComponents = new BaseComponent[] { new TextComponent(json) };
HoverEvent hover = new HoverEvent(HoverEvent.Action.SHOW_ITEM, hoverEventComponents);
TextComponent text = new TextComponent(textToSend);
text.setHoverEvent(hover);
text.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/buy " + container.getLocation().getWorld().getName() + " " + container.getLocation().getX() + " " + container.getLocation().getBlockY() + " " + container.getLocation().getBlockZ()));
player.spigot().sendMessage(text);
shopInfo.setHoverableText(text);
config.sendTip(player, "saleInfo");
instance.getServer().getPluginManager().callEvent(shopSelectEvent);
return true;
}
Aggregations