293 行
9.4 KiB
Java
293 行
9.4 KiB
Java
/*
|
|
* @author ucchy
|
|
* @license GPLv3
|
|
* @copyright Copyright ucchy 2013
|
|
*/
|
|
package com.github.ucchyocean.lc;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.entity.Player;
|
|
|
|
/**
|
|
* @author ucchy
|
|
* チャンネルマネージャー
|
|
*/
|
|
public class ChannelManager {
|
|
|
|
private static final String LIST_FIRSTLINE = Resources.get("listFirstLine");
|
|
private static final String LIST_ENDLINE = Resources.get("listEndLine");
|
|
private static final String LIST_FORMAT = Resources.get("listFormat");
|
|
|
|
private static final String DEFAULT_FORMAT = Resources.get("defaultFormat");
|
|
|
|
private static final String FILE_NAME = "channels.yml";
|
|
|
|
private static final String KEY_DESC = "desc";
|
|
private static final String KEY_FORMAT = "format";
|
|
private static final String KEY_MEMBERS = "members";
|
|
private static final String KEY_BANNED = "banned";
|
|
|
|
private File file;
|
|
private HashMap<String, Channel> channels;
|
|
private HashMap<String, String> defaultChannels;
|
|
|
|
/**
|
|
* コンストラクタ
|
|
*/
|
|
public ChannelManager() {
|
|
load();
|
|
}
|
|
|
|
/**
|
|
* 読み込みする
|
|
*/
|
|
private void load() {
|
|
|
|
file = new File(
|
|
LunaChat.instance.getDataFolder() +
|
|
File.separator + FILE_NAME);
|
|
|
|
if ( !file.exists() ) {
|
|
YamlConfiguration conf = new YamlConfiguration();
|
|
try {
|
|
conf.save(file);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
YamlConfiguration config =
|
|
YamlConfiguration.loadConfiguration(file);
|
|
|
|
// チャンネル設定のロード
|
|
if ( channels == null ) {
|
|
channels = new HashMap<String, Channel>();
|
|
} else {
|
|
channels.clear();
|
|
}
|
|
|
|
if ( config.contains("channels") ) {
|
|
ConfigurationSection section = config.getConfigurationSection("channels");
|
|
Set<String> keyset = section.getValues(false).keySet();
|
|
for ( String key : keyset ) {
|
|
ConfigurationSection sec = section.getConfigurationSection(key);
|
|
Channel channel = getChannelFromSection(sec);
|
|
channels.put(key, channel);
|
|
}
|
|
}
|
|
|
|
// デフォルト設定のロード
|
|
if ( defaultChannels == null ) {
|
|
defaultChannels = new HashMap<String, String>();
|
|
} else {
|
|
defaultChannels.clear();
|
|
}
|
|
|
|
if ( config.contains("defaults") ) {
|
|
ConfigurationSection section = config.getConfigurationSection("defaults");
|
|
Set<String> keyset = section.getValues(false).keySet();
|
|
for ( String key : keyset ) {
|
|
defaultChannels.put(key, section.getString(key));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* セクションからChannelクラスを生成して返す
|
|
* @param section セクション
|
|
* @return Channel
|
|
*/
|
|
private Channel getChannelFromSection(ConfigurationSection section) {
|
|
|
|
String name = section.getName();
|
|
String desc = section.getString(KEY_DESC, "");
|
|
String format = section.getString(KEY_FORMAT, DEFAULT_FORMAT);
|
|
List<String> members = section.getStringList(KEY_MEMBERS);
|
|
List<String> banned = section.getStringList(KEY_BANNED);
|
|
if ( members == null ) {
|
|
members = new ArrayList<String>();
|
|
}
|
|
|
|
Channel channel = new Channel(name, desc, members);
|
|
channel.format = format;
|
|
channel.banned = banned;
|
|
return channel;
|
|
}
|
|
|
|
/**
|
|
* 保存する
|
|
*/
|
|
protected boolean save() {
|
|
|
|
try {
|
|
YamlConfiguration config =
|
|
YamlConfiguration.loadConfiguration(file);
|
|
|
|
for ( String key : channels.keySet() ) {
|
|
Channel channel = channels.get(key);
|
|
config.set("channels." + key + "." + KEY_DESC, channel.description);
|
|
config.set("channels." + key + "." + KEY_FORMAT, channel.format);
|
|
config.set("channels." + key + "." + KEY_MEMBERS, channel.members);
|
|
config.set("channels." + key + "." + KEY_BANNED, channel.banned);
|
|
}
|
|
|
|
for ( String key : defaultChannels.keySet() ) {
|
|
config.set("defaults." + key, defaultChannels.get(key));
|
|
}
|
|
|
|
config.save(file);
|
|
return true;
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* チャンネルの名前リストを返す
|
|
* @return チャンネルの名前
|
|
*/
|
|
protected ArrayList<String> getNames() {
|
|
|
|
ArrayList<String> names = new ArrayList<String>();
|
|
for ( String k : channels.keySet() ) {
|
|
names.add(k);
|
|
}
|
|
return names;
|
|
}
|
|
|
|
/**
|
|
* リスト表示用のリストを返す
|
|
* @param player プレイヤー、指定しない場合はnullにする
|
|
* @return リスト
|
|
*/
|
|
protected ArrayList<String> getList(Player player) {
|
|
|
|
ArrayList<String> items = new ArrayList<String>();
|
|
String dchannel = "";
|
|
if ( player != null ) {
|
|
dchannel = defaultChannels.get(player.getName());
|
|
if ( dchannel == null ) {
|
|
dchannel = "";
|
|
}
|
|
}
|
|
|
|
items.add(Utility.replaceColorCode(LIST_FIRSTLINE));
|
|
for ( String key : channels.keySet() ) {
|
|
Channel channel = channels.get(key);
|
|
String disp = key;
|
|
if ( key.equals(dchannel) ) {
|
|
disp = ChatColor.RED + key;
|
|
}
|
|
String desc = channel.description;
|
|
int onlineNum = 0;
|
|
for ( String pname : channel.members ) {
|
|
Player p = LunaChat.getPlayerExact(pname);
|
|
if ( p != null && p.isOnline() ) {
|
|
onlineNum++;
|
|
}
|
|
}
|
|
int memberNum = channel.members.size();
|
|
String item = String.format(
|
|
Utility.replaceColorCode(LIST_FORMAT),
|
|
disp, onlineNum, memberNum, desc);
|
|
items.add(item);
|
|
}
|
|
items.add(Utility.replaceColorCode(LIST_ENDLINE));
|
|
|
|
return items;
|
|
}
|
|
|
|
/**
|
|
* プレイヤーが参加しているチャンネルを返す
|
|
* @param player プレイヤー
|
|
* @return チャンネル
|
|
*/
|
|
protected ArrayList<Channel> getChannelByPlayer(Player player) {
|
|
|
|
ArrayList<Channel> result = new ArrayList<Channel>();
|
|
String name = player.getName();
|
|
for ( String key : channels.keySet() ) {
|
|
Channel channel = channels.get(key);
|
|
if ( channel.members.contains(name) ) {
|
|
result.add(channel);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* プレイヤーが参加しているデフォルトのチャンネルを返す
|
|
* @param player プレイヤー
|
|
* @return チャンネル
|
|
*/
|
|
protected Channel getDefaultChannelByPlayer(Player player) {
|
|
|
|
String name = defaultChannels.get(player.getName());
|
|
if ( name == null || !channels.containsKey(name) ) {
|
|
return null;
|
|
}
|
|
return channels.get(name);
|
|
}
|
|
|
|
/**
|
|
* プレイヤーのデフォルトチャンネルを設定する
|
|
* @param player プレイヤー
|
|
* @param cname チャンネル名
|
|
*/
|
|
protected void setDefaultChannel(Player player, String cname) {
|
|
defaultChannels.put(player.getName(), cname);
|
|
save();
|
|
}
|
|
|
|
/**
|
|
* チャンネルを取得する
|
|
* @param name チャンネル名
|
|
* @return チャンネル
|
|
*/
|
|
protected Channel getChannel(String name) {
|
|
return channels.get(name);
|
|
}
|
|
|
|
/**
|
|
* 新しいチャンネルを作成する
|
|
* @param name チャンネル名
|
|
* @param desc チャンネルの説明文
|
|
* @return 作成されたチャンネル
|
|
*/
|
|
protected Channel createChannel(String name, String desc) {
|
|
Channel channel = new Channel(name, desc);
|
|
channels.put(name, channel);
|
|
save();
|
|
return channel;
|
|
}
|
|
|
|
protected void removeChannel(String name) {
|
|
if ( channels.containsKey(name) ) {
|
|
channels.remove(name);
|
|
}
|
|
Channel channel = getChannel(name);
|
|
if ( channel != null ) {
|
|
// チャンネルのメンバーを強制解散させる
|
|
String message = String.format(Utility.replaceColorCode(
|
|
Resources.get("breakupMessage")), name);
|
|
for ( String pname : channel.members ) {
|
|
Player player = LunaChat.getPlayerExact(pname);
|
|
if ( player != null ) {
|
|
player.sendMessage(message);
|
|
}
|
|
}
|
|
}
|
|
save();
|
|
}
|
|
}
|