Changed shop info to show the actual item in the shop when hovering over its name
This commit is contained in:
parent
581d03d79b
commit
387a56b1d3
|
|
@ -31,4 +31,9 @@ public class MessageConfig extends AbstractConfiguration {
|
|||
SHOP_LIMIT_REACHED = getString("errors.shop-limit-reached", SHOP_LIMIT_REACHED);
|
||||
BREAK_SHOP_WHILE_CONVERSING = getString("errors.break-shop-while-conversing", BREAK_SHOP_WHILE_CONVERSING);
|
||||
}
|
||||
|
||||
public static String SHOP_INFO = "This shop <action> <amount> <blue><item></blue> for <price>.";
|
||||
void otherMessages() {
|
||||
SHOP_INFO = getString("messages.shop-info", SHOP_INFO);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.alttd.playershops.listener;
|
||||
|
||||
import com.alttd.playershops.PlayerShops;
|
||||
import com.alttd.playershops.config.MessageConfig;
|
||||
import com.alttd.playershops.gui.ShopManagementGui;
|
||||
import com.alttd.playershops.handler.ShopHandler;
|
||||
import com.alttd.playershops.hook.WorldGuardHook;
|
||||
|
|
@ -9,10 +10,14 @@ import com.alttd.playershops.shop.TransactionError;
|
|||
import com.alttd.playershops.utils.Logger;
|
||||
import com.alttd.playershops.utils.ShopUtil;
|
||||
import com.alttd.playershops.utils.Util;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Tag;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
|
|
@ -20,6 +25,7 @@ import org.bukkit.event.EventHandler;
|
|||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
/**
|
||||
* Dedicated class to listen to transactions for shops.
|
||||
|
|
@ -102,8 +108,6 @@ public class TransactionListener extends EventListener {
|
|||
if (!playerShop.isInitialized())
|
||||
return;
|
||||
|
||||
String message = "This shop <action> <amount> <item_name> (<material>) for <price>";
|
||||
|
||||
TagResolver.Single action;
|
||||
switch (playerShop.getType()) {
|
||||
case SELL -> action = Placeholder.unparsed("action", "sells");
|
||||
|
|
@ -113,12 +117,50 @@ public class TransactionListener extends EventListener {
|
|||
}
|
||||
TagResolver placeholders = TagResolver.resolver(action,
|
||||
Placeholder.parsed("amount", "" + playerShop.getAmount()),
|
||||
Placeholder.component("item", playerShop.getItemStack().displayName()),
|
||||
Placeholder.component("item", itemComponent(playerShop.getItemStack())),
|
||||
Placeholder.parsed("material", Util.capitalize(playerShop.getItemStack().getType().name())),
|
||||
Placeholder.parsed("price", "" + playerShop.getPrice())
|
||||
);
|
||||
|
||||
player.sendMiniMessage(message, placeholders);
|
||||
player.sendMiniMessage(MessageConfig.SHOP_INFO, placeholders);
|
||||
}
|
||||
|
||||
private Component itemComponent(ItemStack item) {
|
||||
Component component;
|
||||
MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
if (item.getType().equals(Material.AIR))
|
||||
return miniMessage.deserialize("NONE");
|
||||
boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName();
|
||||
if (dname) {
|
||||
component = item.getItemMeta().displayName();
|
||||
} else {
|
||||
component = Component.text(materialToName(item.getType()), NamedTextColor.WHITE);
|
||||
}
|
||||
component = component.hoverEvent(item.asHoverEvent());
|
||||
return component;
|
||||
}
|
||||
|
||||
private String materialToName(Material m) {
|
||||
if (m.equals(Material.TNT)) {
|
||||
return "TNT";
|
||||
}
|
||||
String orig = m.toString().toLowerCase();
|
||||
String[] splits = orig.split("_");
|
||||
StringBuilder sb = new StringBuilder(orig.length());
|
||||
int pos = 0;
|
||||
for (String split : splits) {
|
||||
sb.append(split);
|
||||
int loc = sb.lastIndexOf(split);
|
||||
char charLoc = sb.charAt(loc);
|
||||
if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") ||
|
||||
split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on")))
|
||||
sb.setCharAt(loc, Character.toUpperCase(charLoc));
|
||||
if (pos != splits.length - 1)
|
||||
sb.append(' ');
|
||||
++pos;
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void executeTransaction(Player player, PlayerShop shop) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user