configuration file
This commit is contained in:
63
src/main/java/ru/dmitriymx/minecraft/globalchat/Config.java
Normal file
63
src/main/java/ru/dmitriymx/minecraft/globalchat/Config.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package ru.dmitriymx.minecraft.globalchat;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
@AllArgsConstructor
|
||||
class Config {
|
||||
|
||||
private JavaPlugin plugin;
|
||||
private FileConfiguration config;
|
||||
|
||||
String getHosts() {
|
||||
String hosts;
|
||||
List<String> hostList = config.getStringList("kafka.hosts");
|
||||
if (hostList.size() == 0) {
|
||||
Bukkit.getServer().getPluginManager().disablePlugin(plugin);
|
||||
throw new RuntimeException("Empty field 'kafka.hosts'!");
|
||||
} else if (hostList.size() == 1) {
|
||||
hosts = hostList.get(0);
|
||||
} else {
|
||||
StringJoiner sj = new StringJoiner(",");
|
||||
hostList.forEach(sj::add);
|
||||
hosts = sj.toString();
|
||||
}
|
||||
|
||||
return hosts;
|
||||
}
|
||||
|
||||
String getTopic() {
|
||||
String topic = config.getString("kafka.topic");
|
||||
if (topic == null || topic.trim().isEmpty()) {
|
||||
Bukkit.getServer().getPluginManager().disablePlugin(plugin);
|
||||
throw new RuntimeException("Empty field 'kafka.topic'!");
|
||||
}
|
||||
|
||||
return topic;
|
||||
}
|
||||
|
||||
long getDuration() {
|
||||
long duration = config.getLong("kafka.duration");
|
||||
if (duration == 0L) {
|
||||
plugin.getLogger().warning("Field 'kafka.duration' is verry low. Set default value 1000.");
|
||||
duration = 1000L;
|
||||
}
|
||||
|
||||
return duration;
|
||||
}
|
||||
|
||||
String getFormat() {
|
||||
String format = config.getString("message_format");
|
||||
if (format.trim().isEmpty()) {
|
||||
plugin.getLogger().warning("Field 'message_format' is empty. Set default value '{0}: {1}'.");
|
||||
format = "{0}: {1}";
|
||||
}
|
||||
|
||||
return format;
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,17 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import ru.dmitriymx.minecraft.globalchat.mq.KafkaService;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
|
||||
public class MainPlugin extends JavaPlugin {
|
||||
|
||||
private Config config;
|
||||
private KafkaService service;
|
||||
private Thread mqThread;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
initConfig();
|
||||
initKafkaService();
|
||||
getServer().getPluginManager().registerEvents(new ChatListener(service), this);
|
||||
}
|
||||
@@ -20,19 +24,22 @@ public class MainPlugin extends JavaPlugin {
|
||||
mqThread.interrupt();
|
||||
}
|
||||
|
||||
private void initConfig() {
|
||||
saveDefaultConfig();
|
||||
config = new Config(this, getConfig());
|
||||
}
|
||||
|
||||
private void initKafkaService() {
|
||||
ClassLoader originalContext = Thread.currentThread().getContextClassLoader();
|
||||
Thread.currentThread().setContextClassLoader(null);
|
||||
//FIXME перенести в конфигурацию
|
||||
service = new KafkaService("127.0.0.1:9092", "global-chat", 1000);
|
||||
service = new KafkaService(config.getHosts(), config.getTopic(), config.getDuration());
|
||||
Thread.currentThread().setContextClassLoader(originalContext);
|
||||
|
||||
mqThread = new Thread(() -> {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
service.get().forEach(messageData -> {
|
||||
//FIXME формат сообщений должен браться из конфига
|
||||
Bukkit.getServer().broadcastMessage(String.format(
|
||||
"%s: %s",
|
||||
Bukkit.getServer().broadcastMessage(MessageFormat.format(
|
||||
config.getFormat(),
|
||||
messageData.getPlayerName(),
|
||||
messageData.getMessage()
|
||||
));
|
||||
|
||||
@@ -59,6 +59,7 @@ public class KafkaService {
|
||||
ChatMessageData data = GSON.fromJson(record.value(), ChatMessageData.class);
|
||||
list.add(data);
|
||||
} catch (Exception ignore) {
|
||||
//FIXME
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
9
src/main/resources/config.yml
Normal file
9
src/main/resources/config.yml
Normal file
@@ -0,0 +1,9 @@
|
||||
kafka:
|
||||
hosts: [ '127.0.0.1:9022' ]
|
||||
topic: 'global-chat'
|
||||
duration: 1000
|
||||
|
||||
# Global message format.
|
||||
# {0} - player name
|
||||
# {1} - message
|
||||
message_format: '{0}: {1}'
|
||||
Reference in New Issue
Block a user