Search in sources :

Example 1 with ShopSelectEvent

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;
}
Also used : TextComponent(net.md_5.bungee.api.chat.TextComponent) HoverEvent(net.md_5.bungee.api.chat.HoverEvent) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) ShopSelectEvent(com.robomwm.prettysimpleshop.event.ShopSelectEvent) Container(org.bukkit.block.Container) StringWriter(java.io.StringWriter) ItemStack(org.bukkit.inventory.ItemStack) PrintWriter(java.io.PrintWriter)

Aggregations

ShopSelectEvent (com.robomwm.prettysimpleshop.event.ShopSelectEvent)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 BaseComponent (net.md_5.bungee.api.chat.BaseComponent)1 ClickEvent (net.md_5.bungee.api.chat.ClickEvent)1 HoverEvent (net.md_5.bungee.api.chat.HoverEvent)1 TextComponent (net.md_5.bungee.api.chat.TextComponent)1 Container (org.bukkit.block.Container)1 ItemStack (org.bukkit.inventory.ItemStack)1