Skip to content

Commit

Permalink
1.4.2 支持 PlayerCurrency 多货币
Browse files Browse the repository at this point in the history
  • Loading branch information
handy-git committed Dec 2, 2024
1 parent 17b8c1c commit 5f5d4ae
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 170 deletions.
13 changes: 10 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
<groupId>cn.handyplus.menu</groupId>
<name>PlayerMenu</name>
<artifactId>PlayerMenu</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
<description>一个有点好用的玩家菜单插件</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spigot-api.vesion>1.21.3-R0.1-SNAPSHOT</spigot-api.vesion>
<placeholderapi.version>2.11.6</placeholderapi.version>
<lombok.version>1.18.34</lombok.version>
<HandyLib.version>3.13.5</HandyLib.version>
<lombok.version>1.18.36</lombok.version>
<HandyLib.version>3.13.8</HandyLib.version>
<VaultAPI.vesion>1.7</VaultAPI.vesion>
<PlayerPoints.version>3.2.7</PlayerPoints.version>
<PlayerGuild.version>1.14.5</PlayerGuild.version>
<PlayerCurrency.version>1.0.3</PlayerCurrency.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -63,6 +64,12 @@
<version>${PlayerGuild.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cn.handyplus.currency</groupId>
<artifactId>PlayerCurrency</artifactId>
<version>${PlayerCurrency.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/cn/handyplus/menu/PlayerMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class PlayerMenu extends JavaPlugin {
public static Economy ECON;
public static PlayerPoints PLAYER_POINTS;
public static boolean USE_GUILD;
public static boolean USE_PLY;

@Override
public void onEnable() {
Expand All @@ -46,6 +47,8 @@ public void onEnable() {
}
// 加载PlayerGuild
USE_GUILD = BaseUtil.hook("PlayerGuild", "playerGuildSucceedMsg", "playerGuildFailureMsg");
// 加载PlayerCurrency
USE_PLY = BaseUtil.hook("PlayerCurrency", "playerCurrencySucceedMsg", "playerCurrencyFailureMsg");
// 打印logo
List<String> lordList = Arrays.asList(
"",
Expand Down
62 changes: 43 additions & 19 deletions src/main/java/cn/handyplus/menu/core/MenuCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import cn.handyplus.menu.constants.MenuConstants;
import cn.handyplus.menu.enter.MenuLimit;
import cn.handyplus.menu.hook.PlaceholderApiUtil;
import cn.handyplus.menu.hook.PlayerCurrencyUtil;
import cn.handyplus.menu.hook.PlayerPointsUtil;
import cn.handyplus.menu.hook.VaultUtil;
import cn.handyplus.menu.inventory.MenuGui;
Expand All @@ -28,6 +29,7 @@

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
Expand Down Expand Up @@ -288,33 +290,25 @@ private static boolean shopCheck(Player player, MenuButtonParam menuButtonParam)
}
// 金币处理
String input = MenuConstants.PLAYER_INPUT_MAP.getOrDefault(player.getUniqueId(), "");
String shopMoneyStr = menuButtonParam.getShopMoney();
int shopMoney = 0;
if (StrUtil.isNotEmpty(shopMoneyStr)) {
if (NumberUtil.isNumericToInt(shopMoneyStr) != null) {
shopMoney = NumberUtil.isNumericToInt(shopMoneyStr);
} else {
shopMoney = FormulaUtil.evaluateFormulaToInt(shopMoneyStr, MapUtil.of("input", input));
}
}
int shopMoney = getShopPrice(menuButtonParam.getShopMoney(), input);
// 点券处理
String shopPointStr = menuButtonParam.getShopPoint();
int shopPoint = 0;
if (StrUtil.isNotEmpty(shopPointStr)) {
if (NumberUtil.isNumericToInt(shopPointStr) != null) {
shopPoint = NumberUtil.isNumericToInt(shopPointStr);
} else {
shopPoint = FormulaUtil.evaluateFormulaToInt(shopPointStr, MapUtil.of("input", input));
}
int shopPoint = getShopPrice(menuButtonParam.getShopPoint(), input);
// 多货币处理
int currencyPrice = 0;
String currencyType = null;
if (StrUtil.isNotEmpty(menuButtonParam.getShopCurrency())) {
List<String> shopCurrencyList = StrUtil.strToStrList(menuButtonParam.getShopCurrency(), ":");
currencyType = shopCurrencyList.get(0).trim();
currencyPrice = getShopPrice(shopCurrencyList.get(1).trim(), input);
}
// 玩家购买物品
if ("buy".equalsIgnoreCase(shopType)) {
// 判断点击金钱是否满足
// 金钱是否满足
if (shopMoney > 0 && VaultUtil.getPlayerVault(player) < shopMoney) {
MessageUtil.sendMessage(player, BaseUtil.getMsgNotColor("noMoney"));
return true;
}
// 判断点击点券是否满足
// 点券是否满足
if (shopPoint > 0 && PlayerPointsUtil.getPlayerPoints(player) < shopPoint) {
MessageUtil.sendMessage(player, BaseUtil.getMsgNotColor("noPoint"));
return true;
Expand All @@ -333,6 +327,14 @@ private static boolean shopCheck(Player player, MenuButtonParam menuButtonParam)
return true;
}
}
// 多经济处理
if (currencyPrice > 0) {
if (!PlayerCurrencyUtil.buy(player, currencyType, currencyPrice)) {
HashMap<String, String> map = MapUtil.of("${type}", PlayerCurrencyUtil.getDesc(currencyType));
MessageUtil.sendMessage(player, BaseUtil.getMsgNotColor("noBalance", map));
return true;
}
}
// 发送物品
String[] shopMaterialStr = shopMaterial.split(":");
String material = shopMaterialStr[0];
Expand All @@ -355,11 +357,33 @@ private static boolean shopCheck(Player player, MenuButtonParam menuButtonParam)
}
VaultUtil.give(player, shopMoney);
PlayerPointsUtil.give(player, shopPoint);
PlayerCurrencyUtil.give(player, currencyType, currencyPrice);
MessageUtil.sendMessage(player, BaseUtil.getMsgNotColor("sellMsg"));
}
return false;
}

/**
* 获取商品价格
*
* @param shopPriceStr 商品价格
* @param input 输入值
* @return 价格
*/
private static int getShopPrice(String shopPriceStr, String input) {
int shopPrice = 0;
if (StrUtil.isEmpty(shopPriceStr)) {
return shopPrice;
}
// 价格格式处理
if (NumberUtil.isNumericToInt(shopPriceStr) != null) {
shopPrice = NumberUtil.isNumericToInt(shopPriceStr);
} else {
shopPrice = FormulaUtil.evaluateFormulaToInt(shopPriceStr, MapUtil.of("input", input));
}
return shopPrice;
}

/**
* 判断点击自定义条件是否满足
*
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/cn/handyplus/menu/hook/PlayerCurrencyUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cn.handyplus.menu.hook;

import cn.handyplus.currency.api.PlayerCurrencyApi;
import cn.handyplus.lib.core.StrUtil;
import cn.handyplus.lib.util.BaseUtil;
import cn.handyplus.lib.util.MessageUtil;
import cn.handyplus.menu.PlayerMenu;
import org.bukkit.entity.Player;

/**
* 玩家多货币插件 PlayerCurrency 兼容
*
* @author handy
* @since 1.4.2
*/
public class PlayerCurrencyUtil {

/**
* 点击购买
*
* @param player 玩家
* @param type 类型
* @param price 价格
*/
public static boolean buy(Player player, String type, long price) {
// 多经济是否加载
if (!PlayerMenu.USE_PLY) {
MessageUtil.sendMessage(player, BaseUtil.getMsgNotColor("playerCurrencyFailureMsg"));
return false;
}
// 扣除点券
return PlayerCurrencyApi.take(player.getUniqueId(), type, price);
}

/**
* 点击给予
*
* @param player 玩家
* @param type 类型
* @param price 价格
*/
public static boolean give(Player player, String type, long price) {
if (price == 0) {
return false;
}
// 多经济是否加载
if (!PlayerMenu.USE_PLY) {
MessageUtil.sendMessage(player, BaseUtil.getMsgNotColor("playerCurrencyFailureMsg"));
return false;
}
// 给予点券
return PlayerCurrencyApi.give(player.getUniqueId(), type, price);
}

/**
* 查询玩家余额
*
* @param player 玩家
* @return 玩家余额
*/
public static long getPlayerBalance(Player player, String type) {
if (!PlayerMenu.USE_PLY || player == null || StrUtil.isEmpty(type)) {
return 0;
}
return PlayerCurrencyApi.look(player.getUniqueId(), type);
}

/**
* 获取货币描述
*
* @param type 类型
* @return 描述
*/
public static String getDesc(String type) {
if (!PlayerMenu.USE_PLY || StrUtil.isEmpty(type)) {
return type;
}
return PlayerCurrencyApi.getDesc(type);
}

}
2 changes: 2 additions & 0 deletions src/main/java/cn/handyplus/menu/inventory/MenuGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public static MenuButtonParam getMenuButtonParam(MemorySection memorySection, Of
String shopMaterial = memorySection.getString("shopMaterial");
String shopMoney = memorySection.getString("shopMoney");
String shopPoint = memorySection.getString("shopPoint");
String shopCurrency = memorySection.getString("shopCurrency");
int amount = memorySection.getInt("amount");
String dynamicAmount = memorySection.getString("dynamicAmount");
String input = memorySection.getString("input");
Expand Down Expand Up @@ -239,6 +240,7 @@ public static MenuButtonParam getMenuButtonParam(MemorySection memorySection, Of
menuButtonParam.setShopMaterial(shopMaterial);
menuButtonParam.setShopMoney(shopMoney);
menuButtonParam.setShopPoint(shopPoint);
menuButtonParam.setShopCurrency(shopCurrency);
menuButtonParam.setInput(input);
return menuButtonParam;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ public void onEvent(InventoryCloseEvent event) throws IOException {
if (StrUtil.isNotEmpty(shopPoint)) {
createMenuItem.put("shopPoint", shopPoint);
}
String shopCurrency = menuButtonParam.getShopCurrency();
if (StrUtil.isNotEmpty(shopCurrency)) {
createMenuItem.put("shopCurrency", shopCurrency);
}
int amount = menuButtonParam.getAmount();
if (amount > 0) {
createMenuItem.put("amount", amount);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/cn/handyplus/menu/param/MenuButtonParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ public class MenuButtonParam {
*/
private String shopPoint;

/**
* 商店多经济价格
*
* @since 1.4.2
*/
private String shopCurrency;

/**
* 显示数量
*
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/handyplus/menu/util/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ private static void upConfig() {
HandyConfigUtil.setPathIsNotContains(LANG_CONFIG, "playerNotOnline", "&8[&c✘&8] &7玩家 &a${player} &7不在线", null, language);
// 1.3.7
HandyConfigUtil.setPathIsNotContains(LANG_CONFIG, "noNumber", "&8[&c✘&8] &7请输入数字", null, language);
// 1.4.2
HandyConfigUtil.setPathIsNotContains(LANG_CONFIG, "playerCurrencySucceedMsg.", "&a已成功加载PlayerCurrency 启用多货币经济兼容", null, language);
HandyConfigUtil.setPathIsNotContains(LANG_CONFIG, "playerCurrencyFailureMsg", "&7你的服务端没有安装PlayerCurrency 未启用多货币经济兼容", null, language);
HandyConfigUtil.setPathIsNotContains(LANG_CONFIG, "noBalance", "&8[&c✘&8] &7${type}&7不足,无法使用", null, language);
LANG_CONFIG = HandyConfigUtil.loadLangConfig(CONFIG.getString("language"), true);
}

Expand Down
Loading

0 comments on commit 5f5d4ae

Please sign in to comment.