Skip to content

Commit

Permalink
优化GUI创建配方物品识别逻辑,现在会自动识别其他插件的物品名字而不是一股脑存在自己这边
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiyodaXiaoYi committed Nov 14, 2023
1 parent 22cb28f commit 0e84706
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 59 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {
}

group = "com.github.yufiriamazenta"
version = "1.4.6-dev1"
version = "1.4.6-dev2"
var pluginVersion: String = version.toString() + "-" + SimpleDateFormat("yyyyMMdd").format(System.currentTimeMillis())
java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void updateConfig() {
}
config.set("version", Craftorithm.getInstance().getDescription().getVersion());
Craftorithm.getInstance().saveConfig();
Craftorithm.getInstance().reloadConfig();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.yufiriamazenta.craftorithm.util.ContainerUtil;
import com.github.yufiriamazenta.craftorithm.util.FileUtil;
import com.github.yufiriamazenta.craftorithm.util.LangUtil;
import com.github.yufiriamazenta.craftorithm.util.PluginHookUtil;
import crypticlib.config.impl.YamlConfigWrapper;
import crypticlib.nms.item.Item;
import crypticlib.nms.item.ItemFactory;
Expand Down Expand Up @@ -128,13 +129,13 @@ public static ItemStack matchItem(String itemStr) {
item = getCraftorithmItem(key);
break;
case "items_adder":
item = getItemsAdderItem(key);
item = PluginHookUtil.getItemsAdderItem(key);
break;
case "oraxen":
item = getOraxenItem(key);
item = PluginHookUtil.getOraxenItem(key);
break;
case "mythic_mobs":
item = getMythicMobsItem(key);
item = PluginHookUtil.getMythicMobsItem(key);
break;
default:
throw new IllegalArgumentException(namespace + " is not a valid item namespace");
Expand Down Expand Up @@ -183,30 +184,4 @@ public static String getItemName(ItemStack item, boolean ignoreAmount, boolean r
return null;
}

public static ItemStack getItemsAdderItem(String itemStr) {
CustomStack customStack = CustomStack.getInstance(itemStr);
if (customStack == null) {
throw new IllegalArgumentException(itemStr + " is a not exist ItemsAdder item");
}
return customStack.getItemStack();
}

public static ItemStack getOraxenItem(String itemStr) {
if (!OraxenItems.exists(itemStr)) {
throw new IllegalArgumentException(itemStr + " is a not exist Oraxen item");
}
return OraxenItems.getItemById(itemStr).build();
}

public static ItemStack getMythicMobsItem(String itemStr) {
ItemExecutor executor = MythicBukkit.inst().getItemManager();
Optional<MythicItem> itemOptional = executor.getItem(itemStr);
if (!itemOptional.isPresent()) {
throw new IllegalArgumentException(itemStr + " is not a valid MythicMobs item");
}
MythicItem mythicItem = itemOptional.get();
int amount = mythicItem.getAmount();
return BukkitAdapter.adapt(itemOptional.get().generateItemStack(amount));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.github.yufiriamazenta.craftorithm.util.ContainerUtil;
import com.github.yufiriamazenta.craftorithm.util.FileUtil;
import com.github.yufiriamazenta.craftorithm.util.LangUtil;
import com.github.yufiriamazenta.craftorithm.util.PluginHookUtil;
import crypticlib.CrypticLib;
import crypticlib.config.impl.YamlConfigWrapper;
import crypticlib.util.ItemUtil;
Expand Down Expand Up @@ -451,7 +452,9 @@ private String getItemName(ItemStack item, boolean ignoreAmount) {
if (ItemUtil.isItemInvalidate(item)) {
return null;
}
String itemName;
String itemName = checkIsOtherPluginName(item);
if (itemName != null)
return itemName;
if (item.hasItemMeta()) {
itemName = ItemManager.getItemName(item, ignoreAmount, true, "gui_items", UUID.randomUUID().toString());
itemName = "items:" + itemName;
Expand All @@ -461,6 +464,25 @@ private String getItemName(ItemStack item, boolean ignoreAmount) {
return itemName;
}

private String checkIsOtherPluginName(ItemStack item) {
//识别是否是ItemsAdder的物品
String itemsAdderName = PluginHookUtil.getItemsAdderName(item);
if (itemsAdderName != null)
return "items_adder:" + itemsAdderName;

//识别是否是Oraxen的物品
String oraxenName = PluginHookUtil.getOraxenName(item);
if (oraxenName != null) {
return "oraxen:" + oraxenName;
}

//识别是否是MythicMobs的物品
String mythicName = PluginHookUtil.getMythicMobsName(item);
if (mythicName != null)
return "mythic_mobs:" + mythicName;
return null;
}

private void sendSuccessMsgAndReloadMap(HumanEntity entity) {
Map<String, String> replaceMap = ContainerUtil.newHashMap("<recipe_type>", recipeType.name().toLowerCase(),
"<recipe_name>", recipeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public static void sendMsg(CommandSender sender, String msgKey, Map<String, Stri
}
formatMap.putAll(defaultFormatMap);
String message = langConfigFile.config().getString(msgKey, msgKey);

if (!langConfigFile.config().contains(msgKey)) {
langConfigFile.config().set(msgKey, msgKey);
langConfigFile.saveConfig();
langConfigFile.reloadConfig();
}

for (String formatStr : formatMap.keySet()) {
message = message.replace(formatStr, formatMap.get(formatStr));
}
Expand Down Expand Up @@ -83,4 +90,8 @@ public static String placeholder(Player player, String source) {
return source;
}

public static YamlConfigWrapper getLangConfigFile() {
return langConfigFile;
}

}
Original file line number Diff line number Diff line change
@@ -1,57 +1,159 @@
package com.github.yufiriamazenta.craftorithm.util;

import dev.lone.itemsadder.api.CustomStack;
import io.lumine.mythic.bukkit.BukkitAdapter;
import io.lumine.mythic.bukkit.MythicBukkit;
import io.lumine.mythic.core.items.ItemExecutor;
import io.lumine.mythic.core.items.MythicItem;
import io.th0rgal.oraxen.api.OraxenItems;
import net.milkbowl.vault.economy.Economy;
import org.black_ixx.playerpoints.PlayerPoints;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.RegisteredServiceProvider;

import java.util.Optional;

public class PluginHookUtil {

private static Economy economy;
private static PlayerPoints playerPoints;
private static boolean economyLoaded, pointsLoaded;
private static boolean economyLoaded, pointsLoaded, itemsAdderLoaded, oraxenLoaded, mythicLoaded;

public static void hookPlugins() {
hookVault();
hookPlayerPoints();
hookItemsAdder();
hookOraxen();
hookMythicMobs();
}


private static boolean hookVault() {
if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
return false;
private static void hookVault() {
if (Bukkit.getPluginManager().isPluginEnabled("Vault")) {
economyLoaded = false;
LangUtil.info("load.vault_not_exist");
return;
}
RegisteredServiceProvider<Economy> vaultRsp = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
if (vaultRsp == null) {
return false;
economyLoaded = false;
LangUtil.info("load.vault_not_exist");
return;
}
economy = vaultRsp.getProvider();
return true;
}

private static boolean hookPlayerPoints() {
playerPoints = (PlayerPoints) Bukkit.getPluginManager().getPlugin("PlayerPoints");
return playerPoints != null;
LangUtil.info("load.vault_hook");
economyLoaded = true;
economy = vaultRsp.getProvider();
}

public static boolean isEconomyLoaded() { return economyLoaded; }

public static Economy getEconomy() {
return economy;
}

private static void hookPlayerPoints() {
pointsLoaded = Bukkit.getPluginManager().isPluginEnabled("PlayerPoints");
if (pointsLoaded) {
playerPoints = (PlayerPoints) Bukkit.getPluginManager().getPlugin("PlayerPoints");
LangUtil.info("load.points_hook");
} else
LangUtil.info("load.points_not_exist");
}

public static boolean isPlayerPointsLoaded() { return pointsLoaded; }

public static PlayerPoints getPlayerPoints() {
return playerPoints;
}

public static boolean isEconomyLoaded() { return economyLoaded; }
private static void hookItemsAdder() {
itemsAdderLoaded = Bukkit.getPluginManager().isPluginEnabled("ItemsAdder");
if (itemsAdderLoaded)
LangUtil.info("load.items_adder_hook");
else
LangUtil.info("load.items_adder_not_exist");
}

public static boolean isPlayerPointsLoaded() { return pointsLoaded; }
public static ItemStack getItemsAdderItem(String itemStr) {
if (!itemsAdderLoaded)
throw new UnsupportedOperationException("Can not found ItemsAdder plugin");
CustomStack customStack = CustomStack.getInstance(itemStr);
if (customStack == null) {
throw new IllegalArgumentException(itemStr + " is a not exist ItemsAdder item");
}
return customStack.getItemStack();
}

public static void hookPlugins() {
economyLoaded = hookVault();
String messageKey = "load.vault_success";
if (!economyLoaded)
messageKey = "load.vault_failed";
LangUtil.info(messageKey);
pointsLoaded = hookPlayerPoints();
messageKey = "load.points_success";
if (!pointsLoaded)
messageKey = "load.points_failed";
LangUtil.info(messageKey);
public static String getItemsAdderName(ItemStack itemStack) {
if (!itemsAdderLoaded)
return null;
CustomStack customStack = CustomStack.byItemStack(itemStack);
if (customStack == null)
return null;
return customStack.getId();
}

private static void hookOraxen() {
oraxenLoaded = Bukkit.getPluginManager().isPluginEnabled("Oraxen");
if (oraxenLoaded)
LangUtil.info("load.oraxen_hook");
else
LangUtil.info("load.oraxen_not_exist");
}

public static ItemStack getOraxenItem(String itemStr) {
if (!oraxenLoaded)
throw new UnsupportedOperationException("Can not found Oraxen plugin");
if (!OraxenItems.exists(itemStr)) {
throw new IllegalArgumentException(itemStr + " is a not exist Oraxen item");
}
return OraxenItems.getItemById(itemStr).build();
}

public static String getOraxenName(ItemStack itemStack) {
if (!oraxenLoaded)
return null;
if (!OraxenItems.exists(itemStack))
return null;
return OraxenItems.getIdByItem(itemStack);
}

private static void hookMythicMobs() {
mythicLoaded = Bukkit.getPluginManager().isPluginEnabled("MythicMobs");
if (mythicLoaded)
LangUtil.info("load.mythic_mobs_hook");
else
LangUtil.info("load.mythic_mobs_not_exist");
}

public static ItemStack getMythicMobsItem(String itemStr) {
if (!mythicLoaded)
throw new UnsupportedOperationException("Can not found MythicMobs plugin");
ItemExecutor executor = MythicBukkit.inst().getItemManager();
Optional<MythicItem> itemOptional = executor.getItem(itemStr);
if (!itemOptional.isPresent()) {
throw new IllegalArgumentException(itemStr + " is not a valid MythicMobs item");
}
MythicItem mythicItem = itemOptional.get();
int amount = mythicItem.getAmount();
return BukkitAdapter.adapt(itemOptional.get().generateItemStack(amount));
}

public static String getMythicMobsName(ItemStack itemStack) {
if (!mythicLoaded)
return null;
ItemExecutor itemExecutor = MythicBukkit.inst().getItemManager();
if (!itemExecutor.isMythicItem(itemStack))
return null;
return itemExecutor.getMythicTypeFromItem(itemStack);
}

public ItemExecutor getMythicMobsItemExecutor() {
if (!mythicLoaded)
throw new UnsupportedOperationException("Can not found MythicMobs plugin");
return MythicBukkit.inst().getItemManager();
}

}
14 changes: 10 additions & 4 deletions src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ load:
finish: '<prefix> &a插件加载完毕'
recipe_load_exception: '<prefix> &c加载配方<recipe_name>时出现错误'
item_load_exception: '<prefix> &c加载物品<item_name>时出现错误'
vault_success: '<prefix> &a发现经济插件,已挂钩'
vault_failed: '<prefix> &c未发现经济插件,金钱检查将不会生效'
points_success: '<prefix> &a发现点券插件,已挂钩'
points_failed: '<prefix> &c未发现点券插件,点券检查将不会生效'
vault_hook: '<prefix> &a发现经济插件,已挂钩'
vault_not_exist: '<prefix> &c未发现经济插件,金钱检查将不会生效'
points_hook: '<prefix> &a发现点券插件,已挂钩'
points_not_exist: '<prefix> &c未发现点券插件,点券检查将不会生效'
items_adder_hook: '<prefix> &c发现ItemsAdder,已挂钩'
items_adder_not_exist: '<prefix> &c未发现ItemsAdder'
oraxen_hook: '<prefix> &c发现Oraxen,已挂钩'
oraxen_not_exist: '<prefix> &c未发现Oraxen'
mythic_mobs_hook: '<prefix> &c发现MythicMobs,已挂钩'
mythic_mobs_not_exist: '<prefix> &c未发现MythicMobs'

0 comments on commit 0e84706

Please sign in to comment.