Skip to content

Commit 6c25066

Browse files
committed
fix message formatting and make them configurable
1 parent 5256a7d commit 6c25066

8 files changed

Lines changed: 168 additions & 15 deletions

File tree

src/main/java/me/isoham/imageframe/mde/ImageFrameMDE.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package me.isoham.imageframe.mde;
22

3+
import me.isoham.imageframe.mde.config.Config;
34
import me.isoham.imageframe.mde.discord.DiscordService;
45
import me.isoham.imageframe.mde.listeners.CommandInterceptor;
6+
import me.isoham.imageframe.mde.listeners.ReloadCommand;
57
import me.isoham.imageframe.mde.moderation.ModerationManager;
68
import me.isoham.imageframe.mde.storage.DatabaseManager;
79
import me.isoham.imageframe.mde.storage.RequestRepository;
@@ -20,7 +22,8 @@ public void onEnable() {
2022

2123
String token = getConfig().getString("discord.token");
2224
long channelId = getConfig().getLong("discord.channel-id");
23-
int maxPendingRequests = getConfig().getInt("moderation.max-pending-requests-per-player", 3);
25+
26+
Config config = new Config(this);
2427

2528
try {
2629
// Database Service
@@ -30,7 +33,7 @@ public void onEnable() {
3033
getLogger().info("Database initialized");
3134

3235
// Moderation Manager
33-
ModerationManager moderationManager = new ModerationManager(this, urlRepository, requestRepository, maxPendingRequests);
36+
ModerationManager moderationManager = new ModerationManager(this, urlRepository, requestRepository, config);
3437
moderationManager.loadStartupCaches();
3538

3639
long cutoff = System.currentTimeMillis() - Duration.ofDays(30).toMillis(); // delete pending requests 30 days old
@@ -42,7 +45,9 @@ public void onEnable() {
4245

4346
// Register event listeners for new/updated images
4447
// getServer().getPluginManager().registerEvents(new ImageMapListener(), this);
45-
getServer().getPluginManager().registerEvents(new CommandInterceptor(moderationManager, discordService), this);
48+
getServer().getPluginManager().registerEvents(new CommandInterceptor(moderationManager, discordService, config), this);
49+
50+
getCommand("mdereload").setExecutor(new ReloadCommand(config));
4651
} catch (Exception e) {
4752
getLogger().severe("Failed to start Plugin: " + e.getLocalizedMessage());
4853
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package me.isoham.imageframe.mde.config;
2+
3+
import org.bukkit.plugin.java.JavaPlugin;
4+
5+
public class Config {
6+
7+
private final JavaPlugin plugin;
8+
private final MessageConfig messages;
9+
10+
public Config(JavaPlugin plugin) {
11+
this.plugin = plugin;
12+
this.messages = new MessageConfig(plugin);
13+
14+
load();
15+
}
16+
17+
private void load() {
18+
// FileConfiguration config = plugin.getConfig();
19+
20+
// Nothing right now :)
21+
}
22+
23+
public void reload() {
24+
plugin.reloadConfig();
25+
load();
26+
}
27+
28+
public MessageConfig getMessages() {
29+
return messages;
30+
}
31+
32+
public int getMaxPendingRequestsPerPlayer() {
33+
return plugin.getConfig().getInt("moderation.max-pending-requests-per-player");
34+
}
35+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package me.isoham.imageframe.mde.config;
2+
3+
import org.bukkit.ChatColor;
4+
import org.bukkit.plugin.java.JavaPlugin;
5+
6+
public class MessageConfig {
7+
8+
private final JavaPlugin plugin;
9+
10+
public MessageConfig(JavaPlugin plugin) {
11+
this.plugin = plugin;
12+
}
13+
14+
public String get(Message message) {
15+
String raw = plugin.getConfig().getString(message.path, message.defaultMessage);
16+
return ChatColor.translateAlternateColorCodes('&', raw);
17+
}
18+
19+
public enum Message {
20+
21+
REQUEST_SUBMITTED(
22+
"messages.request.submitted",
23+
"&eYour image request was sent for moderation."
24+
),
25+
26+
REQUEST_ALREADY_PENDING(
27+
"messages.request.already_pending",
28+
"&6This image is already awaiting moderation. Please wait."
29+
),
30+
31+
REQUEST_LIMIT_REACHED(
32+
"messages.request.limit_reached",
33+
"&cYou have too many pending moderation requests."
34+
),
35+
36+
URL_REJECTED(
37+
"messages.url.rejected",
38+
"&cThis image URL has been rejected by moderators."
39+
),
40+
41+
MODERATION_APPROVED(
42+
"messages.moderation.approved",
43+
"&aYour image request has been approved by a moderator."
44+
),
45+
46+
MODERATION_REJECTED(
47+
"messages.moderation.rejected",
48+
"&cYour image request was rejected by a moderator."
49+
);
50+
51+
public final String path;
52+
public final String defaultMessage;
53+
54+
Message(String path, String defaultMessage) {
55+
this.path = path;
56+
this.defaultMessage = defaultMessage;
57+
}
58+
}
59+
}

src/main/java/me/isoham/imageframe/mde/listeners/CommandInterceptor.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package me.isoham.imageframe.mde.listeners;
22

3+
import me.isoham.imageframe.mde.config.Config;
4+
import me.isoham.imageframe.mde.config.MessageConfig;
35
import me.isoham.imageframe.mde.discord.DiscordService;
46
import me.isoham.imageframe.mde.moderation.ModerationManager;
57
import me.isoham.imageframe.mde.storage.URLStatus;
68

7-
import org.bukkit.Color;
89
import org.bukkit.entity.Player;
910
import org.bukkit.event.EventHandler;
1011
import org.bukkit.event.Listener;
@@ -31,10 +32,12 @@
3132
public class CommandInterceptor implements Listener {
3233
private final ModerationManager moderationManager;
3334
private final DiscordService discordService;
35+
private final Config config;
3436

35-
public CommandInterceptor(ModerationManager moderationManager, DiscordService discordService) {
37+
public CommandInterceptor(ModerationManager moderationManager, DiscordService discordService, Config config) {
3638
this.moderationManager = moderationManager;
3739
this.discordService = discordService;
40+
this.config = config;
3841
}
3942

4043
@EventHandler
@@ -93,12 +96,12 @@ public void onCommand(PlayerCommandPreprocessEvent event) {
9396

9497
case REJECTED -> {
9598
event.setCancelled(true);
96-
player.sendMessage(Color.RED + "This image URL has been rejected by moderators.");
99+
player.sendMessage(config.getMessages().get(MessageConfig.Message.URL_REJECTED));
97100
}
98101

99102
case PENDING -> {
100103
event.setCancelled(true);
101-
player.sendMessage(Color.YELLOW + "This image is already awaiting moderation. Please wait for a moderator to review it.");
104+
player.sendMessage(config.getMessages().get(MessageConfig.Message.REQUEST_ALREADY_PENDING));
102105
}
103106

104107
case UNKNOWN -> {
@@ -112,7 +115,7 @@ public void onCommand(PlayerCommandPreprocessEvent event) {
112115
);
113116

114117
if (requestId == null) {
115-
player.sendMessage(Color.RED + "You have too many pending moderation requests.");
118+
player.sendMessage(config.getMessages().get(MessageConfig.Message.REQUEST_LIMIT_REACHED));
116119
return;
117120
}
118121

@@ -123,7 +126,7 @@ public void onCommand(PlayerCommandPreprocessEvent event) {
123126
url
124127
);
125128

126-
player.sendMessage(Color.YELLOW + "Your image request was sent for moderation.");
129+
player.sendMessage(config.getMessages().get(MessageConfig.Message.REQUEST_SUBMITTED));
127130
}
128131
}
129132
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package me.isoham.imageframe.mde.listeners;
2+
3+
import me.isoham.imageframe.mde.config.Config;
4+
import org.bukkit.ChatColor;
5+
import org.bukkit.command.Command;
6+
import org.bukkit.command.CommandExecutor;
7+
import org.bukkit.command.CommandSender;
8+
import org.jspecify.annotations.NonNull;
9+
10+
public class ReloadCommand implements CommandExecutor {
11+
12+
private final Config config;
13+
14+
public ReloadCommand(Config config) {
15+
this.config = config;
16+
}
17+
18+
@Override
19+
public boolean onCommand(@NonNull CommandSender sender, @NonNull Command command, @NonNull String label, @NonNull String[] args) {
20+
if (!sender.hasPermission("imageframemde.reload")) {
21+
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cYou do not have permission to run this command."));
22+
return true;
23+
}
24+
25+
config.reload();
26+
27+
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&aImageFrameMDE configuration reloaded."));
28+
29+
return true;
30+
}
31+
}

src/main/java/me/isoham/imageframe/mde/moderation/ModerationManager.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.isoham.imageframe.mde.moderation;
22

3+
import me.isoham.imageframe.mde.config.Config;
4+
import me.isoham.imageframe.mde.config.MessageConfig;
35
import me.isoham.imageframe.mde.storage.*;
46

57
import org.bukkit.Bukkit;
@@ -17,17 +19,17 @@ public class ModerationManager {
1719
private final JavaPlugin plugin;
1820
private final URLRepository urlRepository;
1921
private final RequestRepository requestRepository;
20-
private final int maxPendingRequests;
22+
private final Config config;
2123

2224
private final Map<String, URLStatus> cache = new ConcurrentHashMap<>();
2325
private final Set<String> pendingHashes = ConcurrentHashMap.newKeySet();
2426
private final Map<UUID, Integer> pendingRequests = new ConcurrentHashMap<>();
2527

26-
public ModerationManager(JavaPlugin plugin, URLRepository urlRepository, RequestRepository requestRepository, int maxPendingRequests) {
28+
public ModerationManager(JavaPlugin plugin, URLRepository urlRepository, RequestRepository requestRepository, Config config) {
2729
this.plugin = plugin;
2830
this.urlRepository = urlRepository;
2931
this.requestRepository = requestRepository;
30-
this.maxPendingRequests = maxPendingRequests;
32+
this.config = config;
3133
}
3234

3335
public URLStatus check(String hash) {
@@ -109,7 +111,7 @@ public boolean approve(String requestId, String moderator) {
109111

110112
// TODO: Store this and send it to player when they join!
111113
if (player != null) {
112-
player.sendMessage(Color.GREEN + "Your image request has been approved by a moderator.");
114+
player.sendMessage(config.getMessages().get(MessageConfig.Message.MODERATION_APPROVED));
113115
Bukkit.getScheduler().runTask(plugin, () ->
114116
Bukkit.dispatchCommand(player, req.command().substring(1))
115117
);
@@ -147,7 +149,7 @@ public boolean reject(String requestId, String moderator) {
147149

148150
Player player = Bukkit.getPlayer(uuid);
149151
if (player != null) {
150-
player.sendMessage(Color.RED + "Your image request was rejected by a moderator.");
152+
player.sendMessage(config.getMessages().get(MessageConfig.Message.MODERATION_REJECTED));
151153
}
152154

153155
pendingRequests.computeIfPresent(uuid, (k, v) -> Math.max(0, v - 1));
@@ -179,6 +181,6 @@ public void loadStartupCaches() {
179181
public boolean isRateLimited(Player player) {
180182
UUID uuid = player.getUniqueId();
181183
int pending = pendingRequests.getOrDefault(uuid, 0);
182-
return pending >= maxPendingRequests;
184+
return pending >= config.getMaxPendingRequestsPerPlayer();
183185
}
184186
}

src/main/resources/config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@ moderation:
66
max-pending-requests-per-player: 3
77

88
messages:
9+
request:
10+
submitted: "&eYour image request was sent for moderation."
11+
already_pending: "&6This image is already awaiting moderation. Please wait."
12+
limit_reached: "&cYou have too many pending moderation requests."
913

14+
url:
15+
rejected: "&cThis image URL has been rejected by moderators."
16+
17+
moderation:
18+
approved: "&aYour image request has been approved by a moderator."
19+
rejected: "&cYour image request was rejected by a moderator."

src/main/resources/plugin.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ authors: [ iSoham ]
66
description:
77
"ImageFrame Extension for Moderating Images on Discord (MDE: Moderating on Discord Extension)"
88
depend: [ ImageFrame ]
9+
commands:
10+
mdereload:
11+
description: Reload ImageFrameMDE configuration
12+
permission: imageframemde.reload
13+
14+
permissions:
15+
imageframemde.reload:
16+
default: op

0 commit comments

Comments
 (0)