Merge pull request #13 from Altitude-Devs/shop_info
Shop info, Fixes #9
This commit is contained in:
commit
06a9511874
|
|
@ -1,5 +1,8 @@
|
|||
package com.alttd.playershops.commands;
|
||||
|
||||
import com.alttd.playershops.PlayerShops;
|
||||
import com.alttd.playershops.config.Config;
|
||||
import com.alttd.playershops.config.MessageConfig;
|
||||
import com.alttd.playershops.gui.HomeGui;
|
||||
import com.alttd.playershops.gui.ShopManagementGui;
|
||||
import com.alttd.playershops.shop.PlayerShop;
|
||||
|
|
@ -24,6 +27,7 @@ public class ShopCommand implements CommandExecutor, TabCompleter {
|
|||
}
|
||||
switch (args[0].toLowerCase()) {
|
||||
case "reload":
|
||||
PlayerShops.getInstance().reloadConfigs();
|
||||
break;
|
||||
case "open":
|
||||
HomeGui gui = new HomeGui(player.getUniqueId());
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ public class MessageConfig extends AbstractConfiguration {
|
|||
version = config.getInt("config-version", 1);
|
||||
config.set("config-version", 1);
|
||||
|
||||
config.readConfig(Config.class, null);
|
||||
config.readConfig(MessageConfig.class, null);
|
||||
}
|
||||
|
||||
public static String SHOP_ALREADY_EXISTS = "<red>This block is already a Shop</red>";
|
||||
|
|
@ -26,11 +26,16 @@ public class MessageConfig extends AbstractConfiguration {
|
|||
public static String BREAK_SHOP_WHILE_CONVERSING = "<red>You can not break shop signs while editing them.</red>";
|
||||
public static String NO_PERMISSION_FOR_SHOP_TYPE = "<red>You do not have permission to use <shop_type> shops.";
|
||||
|
||||
void loadErrorMessages() {
|
||||
SHOP_ALREADY_EXISTS = getString("errors.shop-already-exists", SHOP_ALREADY_EXISTS);
|
||||
NO_SHOP_CREATE_PERMISSION = getString("errors.no-shop-create-permission", NO_SHOP_CREATE_PERMISSION);
|
||||
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);
|
||||
NO_PERMISSION_FOR_SHOP_TYPE = getString("permissions-messages.shop-type", NO_PERMISSION_FOR_SHOP_TYPE);
|
||||
private static void loadErrorMessages() {
|
||||
SHOP_ALREADY_EXISTS = config.getString("errors.shop-already-exists", SHOP_ALREADY_EXISTS);
|
||||
NO_SHOP_CREATE_PERMISSION = config.getString("errors.no-shop-create-permission", NO_SHOP_CREATE_PERMISSION);
|
||||
SHOP_LIMIT_REACHED = config.getString("errors.shop-limit-reached", SHOP_LIMIT_REACHED);
|
||||
BREAK_SHOP_WHILE_CONVERSING = config.getString("errors.break-shop-while-conversing", BREAK_SHOP_WHILE_CONVERSING);
|
||||
NO_PERMISSION_FOR_SHOP_TYPE = config.getString("permissions-messages.shop-type", NO_PERMISSION_FOR_SHOP_TYPE);
|
||||
}
|
||||
|
||||
public static String SHOP_INFO = "This shop <action> <amount> <item> for <price>.";
|
||||
private static void otherMessages() {
|
||||
SHOP_INFO = config.getString("messages.shop-info", SHOP_INFO);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,10 +11,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;
|
||||
|
|
@ -22,6 +26,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.
|
||||
|
|
@ -45,7 +50,7 @@ public class TransactionListener extends EventListener {
|
|||
return;
|
||||
|
||||
Player player = event.getPlayer();
|
||||
if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK))
|
||||
if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK) && !(event.getAction() == Action.LEFT_CLICK_BLOCK))
|
||||
return;
|
||||
|
||||
Block block = event.getClickedBlock();
|
||||
|
|
@ -79,6 +84,11 @@ public class TransactionListener extends EventListener {
|
|||
return;
|
||||
}
|
||||
|
||||
if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
|
||||
giveInfo(playerShop, player);
|
||||
return;
|
||||
}
|
||||
|
||||
if (ShopUtil.canManageShop(player, playerShop)) {
|
||||
if (player.isSneaking())
|
||||
return;
|
||||
|
|
@ -94,6 +104,29 @@ public class TransactionListener extends EventListener {
|
|||
executeTransaction(player, playerShop);
|
||||
}
|
||||
|
||||
private void giveInfo(PlayerShop playerShop, Player player) {
|
||||
if (player.isSneaking())
|
||||
return;
|
||||
if (!playerShop.isInitialized())
|
||||
return;
|
||||
|
||||
TagResolver.Single action;
|
||||
switch (playerShop.getType()) {
|
||||
case SELL -> action = Placeholder.unparsed("action", "sells");
|
||||
case BUY -> action = Placeholder.unparsed("action", "buys");
|
||||
case GAMBLE -> action = Placeholder.unparsed("action", "gambles");
|
||||
default -> action = Placeholder.unparsed("action", "UNKNOWN");
|
||||
}
|
||||
TagResolver placeholders = TagResolver.resolver(action,
|
||||
Placeholder.parsed("amount", "" + playerShop.getAmount()),
|
||||
Placeholder.component("item", ShopUtil.itemNameComponent(playerShop.getItemStack())),
|
||||
Placeholder.parsed("material", Util.capitalize(playerShop.getItemStack().getType().name())),
|
||||
Placeholder.parsed("price", "" + ShopUtil.round(playerShop.getPrice()))
|
||||
);
|
||||
|
||||
player.sendMiniMessage(MessageConfig.SHOP_INFO, placeholders);
|
||||
}
|
||||
|
||||
private void executeTransaction(Player player, PlayerShop shop) {
|
||||
if (shop == null || shop.getItemStack() == null)
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -152,9 +152,9 @@ public class PlayerShop {
|
|||
MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
TagResolver tagResolver = TagResolver.resolver(
|
||||
Placeholder.unparsed("ownername", getOwnerName()),
|
||||
Placeholder.unparsed("price", String.valueOf(getPrice())),
|
||||
Placeholder.unparsed("price", trimPrice(String.valueOf(ShopUtil.round(getPrice())))),
|
||||
Placeholder.unparsed("amount", String.valueOf(getAmount())),
|
||||
Placeholder.component("itemname", ShopUtil.itemNameComponent(getItemStack()))
|
||||
Placeholder.component("itemname", ShopUtil.trimmedItemNameComponent(getItemStack()))
|
||||
);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
signBlock.line(i, miniMessage.deserialize(signLines.get(i), tagResolver));
|
||||
|
|
@ -164,6 +164,13 @@ public class PlayerShop {
|
|||
}.runTaskLater(PlayerShops.getInstance(), 2L);
|
||||
}
|
||||
|
||||
private String trimPrice(String price) {
|
||||
if (price.length() > 15) {
|
||||
return price.substring(0, 13) + "<red>!</red>";
|
||||
}
|
||||
return price;
|
||||
}
|
||||
|
||||
public double getPricePerItem() {
|
||||
return this.getPrice() / this.getAmount();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.alttd.playershops.utils;
|
|||
import com.alttd.playershops.shop.PlayerShop;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
|
|
@ -156,17 +157,49 @@ public class ShopUtil {
|
|||
return false;
|
||||
}
|
||||
|
||||
public static Component itemNameComponent(ItemStack item) {
|
||||
Component component = Component.empty();
|
||||
public static Component trimmedItemNameComponent(ItemStack item) {
|
||||
if(item == null || item.getType().equals(Material.AIR))
|
||||
return Component.text("Nothing");
|
||||
boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName();
|
||||
if(dname) {
|
||||
component = component.append(item.getItemMeta().displayName());
|
||||
} else {
|
||||
component = component.append(Component.text(materialToName(item.getType()), NamedTextColor.WHITE));
|
||||
return materialToTrimmedName(item.getType());
|
||||
}
|
||||
|
||||
private static Component materialToTrimmedName(Material m) {
|
||||
if (m.equals(Material.TNT)) {
|
||||
return Component.text("TNT", NamedTextColor.WHITE);
|
||||
}
|
||||
String orig = m.name().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;
|
||||
}
|
||||
|
||||
if (sb.length() > 10)
|
||||
return Component.text(sb.substring(0, 8), NamedTextColor.WHITE).append(Component.text("!", NamedTextColor.RED));
|
||||
return Component.text(sb.toString(), NamedTextColor.WHITE);
|
||||
}
|
||||
|
||||
public static Component itemNameComponent(ItemStack item) {
|
||||
Component component;
|
||||
MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
if (item == null || 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;
|
||||
}
|
||||
|
||||
|
|
@ -174,7 +207,7 @@ public class ShopUtil {
|
|||
if (m.equals(Material.TNT)) {
|
||||
return "TNT";
|
||||
}
|
||||
String orig = m.name().toLowerCase();
|
||||
String orig = m.toString().toLowerCase();
|
||||
String[] splits = orig.split("_");
|
||||
StringBuilder sb = new StringBuilder(orig.length());
|
||||
int pos = 0;
|
||||
|
|
@ -193,4 +226,7 @@ public class ShopUtil {
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
public static double round(double price) {
|
||||
return (double) Math.round(price * 100) / 100;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user