perf: 优化代码
This commit is contained in:
@@ -0,0 +1,82 @@
|
|||||||
|
package cn.hamster3.mc.plugin.core.bukkit.command.lore;
|
||||||
|
|
||||||
|
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
|
||||||
|
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;
|
||||||
|
import cn.hamster3.mc.plugin.core.bukkit.util.BukkitUtils;
|
||||||
|
import net.kyori.adventure.text.TextReplacementConfig;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class LoreCustomModelDataCommand extends ChildCommand {
|
||||||
|
public static final LoreCustomModelDataCommand INSTANCE = new LoreCustomModelDataCommand();
|
||||||
|
|
||||||
|
private LoreCustomModelDataCommand() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull String getName() {
|
||||||
|
return "cmd";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull String getUsage() {
|
||||||
|
return "cmd [data]";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasPermission(@NotNull CommandSender sender) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull String getDescription() {
|
||||||
|
return "设置手持物品的自定义模型数据";
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
@Override
|
||||||
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||||
|
if (!(sender instanceof Player player)) {
|
||||||
|
CoreMessage.COMMAND_MUST_USED_BY_PLAYER.show(sender);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
ItemStack stack = player.getItemInHand();
|
||||||
|
if (BukkitUtils.isEmptyItemStack(stack)) {
|
||||||
|
CoreMessage.COMMAND_LORE_HAND_EMPTY.show(player);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
ItemMeta meta = stack.getItemMeta();
|
||||||
|
if (meta == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (args.length < 1) {
|
||||||
|
meta.setCustomModelData(null);
|
||||||
|
stack.setItemMeta(meta);
|
||||||
|
CoreMessage.COMMAND_LORE_CMD_CLEAR_SUCCESS.show(sender);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
int data = Integer.parseInt(args[0]);
|
||||||
|
meta.setCustomModelData(data);
|
||||||
|
stack.setItemMeta(meta);
|
||||||
|
CoreMessage.COMMAND_LORE_CMD_SET_SUCCESS.show(sender, TextReplacementConfig.builder()
|
||||||
|
.matchLiteral("%data%").replacement(args[0])
|
||||||
|
.build());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
CoreMessage.COMMAND_LORE_CMD_SET_INPUT_ERROR.show(sender);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@@ -17,6 +17,7 @@ public final class ParentLoreCommand extends ParentCommand {
|
|||||||
addChildCommand(LoreSetCommand.INSTANCE);
|
addChildCommand(LoreSetCommand.INSTANCE);
|
||||||
addChildCommand(LoreClearCommand.INSTANCE);
|
addChildCommand(LoreClearCommand.INSTANCE);
|
||||||
addChildCommand(LoreNameCommand.INSTANCE);
|
addChildCommand(LoreNameCommand.INSTANCE);
|
||||||
|
addChildCommand(LoreCustomModelDataCommand.INSTANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -21,6 +21,9 @@ public enum CoreMessage {
|
|||||||
COMMAND_LORE_ADD_SUCCESS,
|
COMMAND_LORE_ADD_SUCCESS,
|
||||||
COMMAND_LORE_CLEAR_NOTHING,
|
COMMAND_LORE_CLEAR_NOTHING,
|
||||||
COMMAND_LORE_CLEAR_SUCCESS,
|
COMMAND_LORE_CLEAR_SUCCESS,
|
||||||
|
COMMAND_LORE_CMD_CLEAR_SUCCESS,
|
||||||
|
COMMAND_LORE_CMD_SET_SUCCESS,
|
||||||
|
COMMAND_LORE_CMD_SET_INPUT_ERROR,
|
||||||
COMMAND_LORE_NAME_SUCCESS,
|
COMMAND_LORE_NAME_SUCCESS,
|
||||||
COMMAND_LORE_REMOVE_NOT_INPUT_NUMBER,
|
COMMAND_LORE_REMOVE_NOT_INPUT_NUMBER,
|
||||||
COMMAND_LORE_REMOVE_INPUT_NUMBER_ERROR,
|
COMMAND_LORE_REMOVE_INPUT_NUMBER_ERROR,
|
||||||
|
@@ -1,15 +1,12 @@
|
|||||||
package cn.hamster3.mc.plugin.core.bukkit.page;
|
package cn.hamster3.mc.plugin.core.bukkit.page;
|
||||||
|
|
||||||
import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
|
import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
|
||||||
|
import cn.hamster3.mc.plugin.core.bukkit.util.BukkitUtils;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class PageManager {
|
public class PageManager {
|
||||||
@@ -33,26 +30,9 @@ public class PageManager {
|
|||||||
if (pageFolder.mkdirs()) {
|
if (pageFolder.mkdirs()) {
|
||||||
HamsterCorePlugin.getInstance().getLogger().info("为 " + pluginName + " 创建页面配置文件夹...");
|
HamsterCorePlugin.getInstance().getLogger().info("为 " + pluginName + " 创建页面配置文件夹...");
|
||||||
}
|
}
|
||||||
String pageFileName = clazz.getSimpleName() + ".yml";
|
String filename = clazz.getSimpleName() + ".yml";
|
||||||
File pageFile = new File(pageFolder, pageFileName);
|
YamlConfiguration config = BukkitUtils.getPluginConfig(plugin, filename);
|
||||||
if (pageFile.exists()) {
|
pageConfig = new PageConfig(plugin, config);
|
||||||
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(pageFile);
|
|
||||||
pageConfig = new PageConfig(plugin, configuration);
|
|
||||||
PAGE_CONFIG.put(clazz.getName(), pageConfig);
|
|
||||||
return pageConfig;
|
|
||||||
}
|
|
||||||
InputStream resource = plugin.getResource("/" + pageFileName);
|
|
||||||
if (resource == null) {
|
|
||||||
throw new IllegalArgumentException("在插件 " + pluginName + " 的文件内部未找到 " + pageFileName + " !");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Files.copy(resource, pageFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
resource.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(pageFile);
|
|
||||||
pageConfig = new PageConfig(plugin, configuration);
|
|
||||||
PAGE_CONFIG.put(clazz.getName(), pageConfig);
|
PAGE_CONFIG.put(clazz.getName(), pageConfig);
|
||||||
return pageConfig;
|
return pageConfig;
|
||||||
}
|
}
|
||||||
|
@@ -1,18 +1,26 @@
|
|||||||
package cn.hamster3.mc.plugin.core.bukkit.util;
|
package cn.hamster3.mc.plugin.core.bukkit.util;
|
||||||
|
|
||||||
|
import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
|
||||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.entity.HumanEntity;
|
import org.bukkit.entity.HumanEntity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.bukkit.inventory.meta.SkullMeta;
|
import org.bukkit.inventory.meta.SkullMeta;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
@@ -207,4 +215,24 @@ public final class BukkitUtils {
|
|||||||
}
|
}
|
||||||
return displayMessage;
|
return displayMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static YamlConfiguration getPluginConfig(@NotNull Plugin plugin, @NotNull String filename) {
|
||||||
|
File dataFolder = plugin.getDataFolder();
|
||||||
|
if (dataFolder.mkdirs()) {
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().info("已为插件 %s 生成存档文件夹...");
|
||||||
|
}
|
||||||
|
File file = new File(dataFolder, filename);
|
||||||
|
if (!file.exists()) {
|
||||||
|
try (InputStream stream = plugin.getClass().getResourceAsStream("/" + filename)) {
|
||||||
|
if (stream == null) {
|
||||||
|
throw new IllegalArgumentException("在插件 " + plugin.getName() + " 的文件内部未找到 " + filename + " !");
|
||||||
|
}
|
||||||
|
Files.copy(stream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalArgumentException("在插件 " + plugin.getName() + " 内部读取文件 " + filename + " 时发生错误!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return YamlConfiguration.loadConfiguration(file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,17 +4,30 @@ import net.md_5.bungee.api.plugin.Plugin;
|
|||||||
import net.md_5.bungee.config.Configuration;
|
import net.md_5.bungee.config.Configuration;
|
||||||
import net.md_5.bungee.config.ConfigurationProvider;
|
import net.md_5.bungee.config.ConfigurationProvider;
|
||||||
import net.md_5.bungee.config.YamlConfiguration;
|
import net.md_5.bungee.config.YamlConfiguration;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public final class BungeeCordUtils {
|
public final class BungeeCordUtils {
|
||||||
private BungeeCordUtils() {
|
private BungeeCordUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Configuration getPluginConfig(Plugin plugin) {
|
public static Configuration getConfig(@NotNull File file) {
|
||||||
|
if (!file.exists()) {
|
||||||
|
throw new IllegalArgumentException("文件不存在!");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Configuration getPluginConfig(@NotNull Plugin plugin) {
|
||||||
File configFile = new File(plugin.getDataFolder(), "config.yml");
|
File configFile = new File(plugin.getDataFolder(), "config.yml");
|
||||||
if (!configFile.exists()) {
|
if (!configFile.exists()) {
|
||||||
return saveDefaultConfig(plugin);
|
return saveDefaultConfig(plugin);
|
||||||
@@ -23,20 +36,22 @@ public final class BungeeCordUtils {
|
|||||||
try {
|
try {
|
||||||
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Configuration saveDefaultConfig(Plugin plugin) {
|
public static Configuration saveDefaultConfig(@NotNull Plugin plugin) {
|
||||||
|
if (plugin.getDataFolder().mkdir()) {
|
||||||
|
plugin.getLogger().info("创建插件文件夹...");
|
||||||
|
}
|
||||||
|
File configFile = new File(plugin.getDataFolder(), "config.yml");
|
||||||
try {
|
try {
|
||||||
if (plugin.getDataFolder().mkdir()) {
|
|
||||||
plugin.getLogger().info("创建插件文件夹...");
|
|
||||||
}
|
|
||||||
File configFile = new File(plugin.getDataFolder(), "config.yml");
|
|
||||||
InputStream in = plugin.getResourceAsStream("config.yml");
|
InputStream in = plugin.getResourceAsStream("config.yml");
|
||||||
Files.copy(in, configFile.toPath());
|
Files.copy(in, configFile.toPath());
|
||||||
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
||||||
} catch (Exception ignored) {
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@@ -18,9 +18,9 @@ public final class CommonUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void zipCompressionFolder(@NotNull File folder, @NotNull File zipFile) throws IOException {
|
public static void zipCompressionFolder(@NotNull File folder, @NotNull File zipFile) throws IOException {
|
||||||
ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()));
|
try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()))) {
|
||||||
putFileToZipStream(stream, "", folder);
|
putFileToZipStream(stream, "", folder);
|
||||||
stream.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putFileToZipStream(@NotNull ZipOutputStream stream, @NotNull String path, @NotNull File file) throws IOException {
|
public static void putFileToZipStream(@NotNull ZipOutputStream stream, @NotNull String path, @NotNull File file) throws IOException {
|
||||||
|
Reference in New Issue
Block a user