Files
AuMyItems/src/main/java/com/io/yaohun/myitems/util/MessageUtil.java
2026-07-11 22:13:14 +08:00

72 lines
2.4 KiB
Java

package com.io.yaohun.myitems.util;
import com.io.yaohun.myitems.MyItems;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import java.io.File;
import java.util.HashMap;
public class MessageUtil {
private static HashMap<String,String> messageMap = new HashMap<>();
public static void init(MyItems plugin) {
messageMap.clear();
File file = new File(plugin.getDataFolder(),"lang.yml");
// 如果没有这个文件就新建一个~
if(!file.exists()){
plugin.saveResource("lang.yml",false);
}
FileConfiguration yml = YamlConfiguration.loadConfiguration(file);
for (String ymlKey : yml.getKeys(false)){
ConfigurationSection section = yml.getConfigurationSection(ymlKey);
for (String key : section.getKeys(false)){
String message = DemonAPI.convertHexColor(section.getString(key));
messageMap.put(key,message.replace("&","§"));
}
}
}
public static String getMessage(String key){
if(messageMap.containsKey(key)){
return messageMap.get(key);
}
return "§c未找到节点.§7#"+key;
}
public static void broadcast(String msg) {
Bukkit.getServer().broadcastMessage( msg.replace("&","§"));
}
public static void sendMessage(CommandSender sender, String message){
sender.sendMessage(message);
}
public static void sendMessageKey(CommandSender sender, String key, Sound sound){
sender.sendMessage(getMessage(key));
if(sender instanceof Player player){
player.playSound(player.getLocation(),sound,1,1);
}
}
public static void sendMessage(CommandSender sender, String message, Sound sound){
sender.sendMessage(message);
if(sender instanceof Player player){
player.playSound(player.getLocation(),sound,1,1);
}
}
public static void sendDescMessage(CommandSender sender, String message, Sound sound){
sender.sendMessage("§f[§c消息§f] §a正确用法: §e"+message);
if(sender instanceof Player player){
player.playSound(player.getLocation(),sound,1,1);
}
}
}