Skip to content

Commit

Permalink
Messages.yml & Reloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Cow authored and Cow committed Feb 28, 2022
1 parent c851220 commit 09f0d4b
Show file tree
Hide file tree
Showing 22 changed files with 370 additions and 245 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<includes>
<include>plugin.yml</include>
<include>config.yml</include>
<include>messages.yml</include>
</includes>
</resource>
<resource>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/github/punishmentsx/ConfigValues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.punishmentsx;

import io.github.punishmentsx.utils.Colors;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public enum ConfigValues {
REDIS_CHANNEL("DATABASE.REDIS.CHANNEL"),
MONGO_DATABASE("DATABASE.MONGO.DB"),
MONGO_URI("DATABASE.MONGO.URI"),

CONSOLE_NAME("GENERAL.CONSOLE_NAME");

private String path;

public String format(PunishmentsX plugin) {
return Colors.convertLegacyColors(plugin.getConfig().getString(path));
}
}
24 changes: 9 additions & 15 deletions src/main/java/io/github/punishmentsx/Locale.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@

import io.github.punishmentsx.utils.Colors;
import lombok.AllArgsConstructor;
import org.bukkit.ChatColor;

import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
public enum Locale {
REDIS_CHANNEL("DATABASE.REDIS.CHANNEL"),
MONGO_DATABASE("DATABASE.MONGO.DB"),
MONGO_URI("DATABASE.MONGO.URI"),

NO_PERMISSION("MESSAGES.NO_PERMISSION"),
CONSOLE_NAME("GENERAL.CONSOLE_NAME"),

BLACKLIST_MESSAGE("MESSAGES.BLACKLIST_MESSAGE"),
BAN_MESSAGE("MESSAGES.BAN_MESSAGE"),
Expand All @@ -29,15 +22,16 @@ public enum Locale {
UNPUNISHMENT_SUCCESS("MESSAGES.UNPUNISHMENT.SUCCESS"),
UNPUNISHMENT_HOVER("MESSAGES.UNPUNISHMENT.HOVER"),

RELOAD_PERMISSION("PERMISSIONS.RELOAD"),
SILENT_PERMISSION("PERMISSIONS.SILENT_VIEW"),
HISTORY_PERMISSION("PERMISSIONS.HISTORY"),
PUNISH_PERMISSION("PERMISSIONS.PUNISH"),
UNPUNISH_PERMISSION("PERMISSIONS.UNPUNISH"),
BAN_PERMISSION("PERMISSIONS.MANUAL.BAN"),
MUTE_PERMISSION("PERMISSIONS.MANUAL.MUTE"),
WARN_PERMISSION("PERMISSIONS.MANUAL.WARN"),
KICK_PERMISSION("PERMISSIONS.MANUAL.KICK"),
BLACKLIST_PERMISSION("PERMISSIONS.MANUAL.BLACKLIST"),
BAN_PERMISSION("PERMISSIONS.MANUAL_BAN"),
MUTE_PERMISSION("PERMISSIONS.MANUAL_MUTE"),
WARN_PERMISSION("PERMISSIONS.MANUAL_WARN"),
KICK_PERMISSION("PERMISSIONS.MANUAL_KICK"),
BLACKLIST_PERMISSION("PERMISSIONS.MANUAL_BLACKLIST"),

PUNISH_TITLE("MENUS.PUNISH.TITLE"),

Expand All @@ -55,16 +49,16 @@ public enum Locale {
private String path;

public String format(PunishmentsX plugin) {
return ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString(path));
return Colors.convertLegacyColors(plugin.getMessagesFile().getString(path));
}

public List<String> formatLines(PunishmentsX plugin) {
List<String> lines = new ArrayList<>();

for (String string : plugin.getConfig().getStringList(path)) {
for (String string : plugin.getMessagesFile().getStringList(path)) {
lines.add(Colors.convertLegacyColors(string));
}

return lines;
}
}
}
46 changes: 46 additions & 0 deletions src/main/java/io/github/punishmentsx/PunishmentsX.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
import io.github.punishmentsx.punishments.Punishment;
import io.github.punishmentsx.punishments.PunishmentManager;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import redis.clients.jedis.Jedis;
import xyz.leuo.gooey.Gooey;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.logging.Level;

public class PunishmentsX extends JavaPlugin {
Expand All @@ -47,6 +53,8 @@ public class PunishmentsX extends JavaPlugin {

@Getter private Database storage;

@Getter private YamlConfiguration messagesFile;

@Override
public void onEnable() {
this.saveDefaultConfig();
Expand All @@ -68,6 +76,10 @@ public void onEnable() {
break;
}

//Creates & Loads messages file.
createMessages();
reloadMessages();

// Redis
if (getConfig().getBoolean("DATABASE.REDIS.ENABLED")) {
redisPublisher = new RedisPublisher(new Jedis(getConfig().getString("DATABASE.REDIS.HOST"), getConfig().getInt("DATABASE.REDIS.PORT")), this);
Expand Down Expand Up @@ -100,6 +112,7 @@ public void onEnable() {
new QuitListener(this);

// Commands
registerCommand(new ReloadCommand(this, "pxreload"));
registerCommand(new HistoryCommand(this, "history"));
registerCommand(new PunishCommands(this, "punishments"));
registerCommand(new PunishCommand(this, "punish"));
Expand Down Expand Up @@ -136,6 +149,39 @@ public void onDisable() {
storage.close();
}

private void createMessages() {
try {
File dataFolder = getDataFolder();
String file = dataFolder.toPath().toString() + "/messages.yml";
File messagesFile = new File(file);

if (!messagesFile.exists()) {
String[] files = file.split("/");
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(files[files.length - 1]);
File parentFile = messagesFile.getParentFile();

if (parentFile != null) parentFile.mkdirs();

if (inputStream != null) {
Files.copy(inputStream, messagesFile.toPath());
} else {
messagesFile.createNewFile();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

public void reloadMessages() {
final File dataFolder = getDataFolder();
final File file = new File(dataFolder.toPath() + "/messages.yml");

if (file.exists())
messagesFile = YamlConfiguration.loadConfiguration(file);
else messagesFile = new YamlConfiguration();
}

public void registerCommand(BaseCommand command) {
commandMap.register(command.getName(), command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected void execute(CommandSender sender, String[] args, String alias) {
return;
}

ConfigurationSection section = plugin.getConfig().getConfigurationSection(args[1]);
ConfigurationSection section = plugin.getMessagesFile().getConfigurationSection(args[1]);
if (section == null) {
sender.sendMessage("Invalid stack! (Case sensitive, try all caps!)");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@

public class PunishCommand extends BaseCommand {
private final PunishmentsX plugin;
private final Configuration config;

private String notes = null;

public PunishCommand(PunishmentsX plugin, String name) {
super(name);
this.plugin = plugin;
this.config = plugin.getConfig();
this.setAliases("p");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.punishmentsx.commands.impl;

import io.github.punishmentsx.ConfigValues;
import io.github.punishmentsx.Locale;
import io.github.punishmentsx.PunishmentsX;
import io.github.punishmentsx.commands.BaseCommand;
Expand Down Expand Up @@ -60,11 +61,8 @@ public void execute(CommandSender sender, String[] args, String alias) {
}

UUID issuer = null;
String issuerName = Locale.CONSOLE_NAME.format(plugin);
if(sender instanceof Player) {
Player player = (Player) sender;

issuerName = player.getName();
issuer = player.getUniqueId();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.punishmentsx.commands.impl;

import io.github.punishmentsx.Locale;
import io.github.punishmentsx.PunishmentsX;
import io.github.punishmentsx.commands.BaseCommand;
import io.github.punishmentsx.utils.Colors;
import io.github.punishmentsx.utils.ThreadUtil;
import org.bukkit.command.CommandSender;

public class ReloadCommand extends BaseCommand {

private final PunishmentsX plugin;

public ReloadCommand(PunishmentsX plugin, String name) {
super(name);
this.plugin = plugin;
}

@Override
public void execute(CommandSender sender, String[] args, String alias) {
if (!sender.hasPermission(Locale.RELOAD_PERMISSION.format(plugin))) {
sender.sendMessage(Locale.NO_PERMISSION.format(plugin));
return;
}

ThreadUtil.runTask(true, plugin, () -> {
plugin.reloadMessages();
sender.sendMessage(Colors.convertLegacyColors("&aReloading messages.yml file!"));
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.punishmentsx.commands.impl;

import io.github.punishmentsx.ConfigValues;
import io.github.punishmentsx.Locale;
import io.github.punishmentsx.PunishmentsX;
import io.github.punishmentsx.commands.BaseCommand;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void execute(CommandSender sender, String[] args, String alias) {
}

UUID pardoner = null;
String pardonerName = Locale.CONSOLE_NAME.format(plugin);
String pardonerName = ConfigValues.CONSOLE_NAME.format(plugin);
if(sender instanceof Player) {
Player player = (Player) sender;
pardonerName = player.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import io.github.punishmentsx.ConfigValues;
import io.github.punishmentsx.Locale;
import io.github.punishmentsx.PunishmentsX;
import io.github.punishmentsx.database.Database;
Expand All @@ -31,15 +32,15 @@ public Mongo(PunishmentsX plugin) {
MongoClient mongoClient;
if (plugin.getConfig().getBoolean("DATABASE.MONGO.LOCALHOST_NO_AUTH")) {
mongoClient = MongoClients.create(MongoClientSettings.builder().uuidRepresentation(UuidRepresentation.STANDARD).build());
mongoDatabase = mongoClient.getDatabase(Locale.MONGO_DATABASE.format(plugin));
mongoDatabase = mongoClient.getDatabase(ConfigValues.MONGO_DATABASE.format(plugin));
} else {
MongoClientSettings mcs = MongoClientSettings.builder()
.uuidRepresentation(UuidRepresentation.STANDARD)
.applyConnectionString(new ConnectionString(plugin.getConfig().getString(Locale.MONGO_URI.format(plugin))))
.applyConnectionString(new ConnectionString(plugin.getConfig().getString(ConfigValues.MONGO_URI.format(plugin))))
.build();

mongoClient = MongoClients.create(mcs);
mongoDatabase = mongoClient.getDatabase(Locale.MONGO_DATABASE.format(plugin));
mongoDatabase = mongoClient.getDatabase(ConfigValues.MONGO_DATABASE.format(plugin));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.punishmentsx.database.redis;

import com.google.gson.JsonObject;
import io.github.punishmentsx.ConfigValues;
import io.github.punishmentsx.Locale;
import io.github.punishmentsx.PunishmentsX;
import io.github.punishmentsx.profiles.Profile;
Expand All @@ -23,7 +24,7 @@ public PunishRedisMessageListener(PunishmentsX plugin) {
@Override
public void onReceive(RedisMessage redisMessage) {
JsonObject json = redisMessage.getElements();
if(redisMessage.getInternalChannel().equals(Locale.REDIS_CHANNEL.format(plugin))) {
if(redisMessage.getInternalChannel().equals(ConfigValues.REDIS_CHANNEL.format(plugin))) {
RedisAction action = RedisAction.valueOf(json.get("action").getAsString());

switch(action) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.punishmentsx.database.redis;

import io.github.punishmentsx.ConfigValues;
import io.github.punishmentsx.Locale;
import io.github.punishmentsx.PunishmentsX;
import lombok.Getter;
Expand All @@ -17,7 +18,7 @@ public RedisPublisher(Jedis jedis, PunishmentsX plugin) {
plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, ()-> {
if(!messageQueue.isEmpty()) {
RedisMessage redisMessage = messageQueue.poll();
jedis.publish(Locale.REDIS_CHANNEL.format(plugin), redisMessage.getMessage().toString());
jedis.publish(ConfigValues.REDIS_CHANNEL.format(plugin), redisMessage.getMessage().toString());
}
}, 1, 1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.punishmentsx.database.redis;

import io.github.punishmentsx.ConfigValues;
import io.github.punishmentsx.Locale;
import io.github.punishmentsx.PunishmentsX;
import lombok.Getter;
Expand All @@ -17,7 +18,7 @@ public class RedisSubscriber {
public RedisSubscriber(Jedis jedis, PunishmentsX plugin) {
this.listeners = new HashSet<>();

this.rChannel = Locale.REDIS_CHANNEL.format(plugin);
this.rChannel = ConfigValues.REDIS_CHANNEL.format(plugin);

this.jedisPubSub = new JedisPubSub() {
@Override
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/io/github/punishmentsx/database/sequel/SQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,26 @@ public Profile loadProfile(boolean async, String name, boolean store, MongoDeser

if (!rs.next()) return null;

UUID uuid = UUID.fromString(rs.getString("id"));
String currentIp = rs.getString("current_ip");


List<String> ipHistory = Arrays.asList(rs.getString("ip_history").split("\\s*,\\s*"));
List<String> punishmentsStrings = Arrays.asList(rs.getString("punishments").split("\\s*,\\s*"));

List<UUID> punishments = new ArrayList<>();
for (String string : punishmentsStrings) {
punishments.add(UUID.fromString(string));
String punishmentsString = rs.getString("punishments");
if (punishmentsString != null) {
String[] punishmentsStrings = punishmentsString.split("\\s*,\\s*");
for (String string : punishmentsStrings) {
punishments.add(UUID.fromString(string));
}
} else {
punishments = null;
}

String ipHistoryString = rs.getString("ip_history");
List<String> ipHistory = null;
if (ipHistoryString != null) {
ipHistory = Arrays.asList(ipHistoryString.split("\\s*,\\s*"));
}

UUID uuid = rs.getString("id") == null ? null : UUID.fromString(rs.getString("id"));
String currentIp = rs.getString("current_ip");

Profile profile = new Profile(plugin, uuid);
importSQL(profile, name, currentIp, ipHistory, punishments);

Expand Down
Loading

0 comments on commit 09f0d4b

Please sign in to comment.