Skip to content

Commit 0ffc2da

Browse files
committed
Add GameMode update event and config
1 parent 686d295 commit 0ffc2da

4 files changed

Lines changed: 92 additions & 0 deletions

File tree

src/main/java/pw/chew/listenernotifier/ListenerNotifier.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
package pw.chew.listenernotifier;
22

3+
import org.bukkit.configuration.ConfigurationSection;
4+
import org.bukkit.configuration.file.FileConfiguration;
5+
import org.bukkit.plugin.java.JavaPlugin;
6+
import pw.chew.listenernotifier.events.GameModeChangeHandler;
7+
38
public final class ListenerNotifier extends JavaPlugin {
49

510
@Override
611
public void onEnable() {
12+
FileConfiguration config = this.getConfig();
13+
config.options().copyDefaults(true);
14+
saveDefaultConfig();
715
// Plugin startup logic
16+
ConfigurationSection gameMode = config.getConfigurationSection("PlayerGameModeChangeEvent");
17+
if (gameMode == null) {
18+
getLogger().warning("Config is outdated! Please regenerate thanks.");
19+
} else {
20+
if (gameMode.getBoolean("enabled"))
21+
getServer().getPluginManager().registerEvents(new GameModeChangeHandler(gameMode), this);
22+
}
823
}
924

1025
@Override
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package pw.chew.listenernotifier.events;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.configuration.ConfigurationSection;
5+
import org.bukkit.entity.Player;
6+
import org.bukkit.event.EventHandler;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.player.PlayerGameModeChangeEvent;
9+
import pw.chew.listenernotifier.util.ColorConverter;
10+
11+
public class GameModeChangeHandler implements Listener {
12+
private final ConfigurationSection config;
13+
14+
public GameModeChangeHandler(ConfigurationSection config) {
15+
this.config = config;
16+
}
17+
18+
@EventHandler
19+
public void handleGamemode(PlayerGameModeChangeEvent event) {
20+
for(Player player : Bukkit.getOnlinePlayers()) {
21+
if(player.hasPermission(getPermissionNode())) {
22+
String parsed = ColorConverter.convertColorCodesToChatColor(getMessage());
23+
parsed = parsed.replace("{name}", event.getPlayer().getName()).replace("{gamemode}", event.getNewGameMode().name());
24+
player.sendMessage(parsed);
25+
}
26+
}
27+
}
28+
29+
private String getPermissionNode() {
30+
if(config.getString("node") == null) {
31+
return "notify.on.gamemode";
32+
} else {
33+
return "notify.on." + config.getString("node");
34+
}
35+
}
36+
37+
private String getMessage() {
38+
if(config.getString("message") == null) {
39+
return "&bListenerNotifier &7> &a{name} &bhas switched to &a{gamemode}&b!";
40+
} else {
41+
return config.getString("message");
42+
}
43+
}
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package pw.chew.listenernotifier.util;
2+
3+
import org.bukkit.ChatColor;
4+
5+
public class ColorConverter {
6+
public static String convertColorCodesToChatColor(String input) {
7+
String[] colorchars = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "l", "m", "n", "o", "r"};
8+
String[] bruh = input.split("&");
9+
StringBuilder output = new StringBuilder();
10+
for(String moment : bruh) {
11+
for(String code : colorchars) {
12+
if(moment.startsWith(code)) {
13+
moment = ChatColor.getByChar(code) + moment.substring(1);
14+
}
15+
}
16+
output.append(moment);
17+
}
18+
return output.toString();
19+
}
20+
}

src/main/resources/config.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ListenerNotifier Config
2+
3+
# Called when the GameMode of the player is changed.
4+
PlayerGameModeChangeEvent:
5+
enabled: true
6+
# Node is notify.on.[what you put below]
7+
# e.g. notify.on.gamemode
8+
node: gamemode
9+
# Message to send, &x color codes supported. RGB codes not supported.
10+
# Supported tags:
11+
# {name} - Name of player
12+
# {gamemode} - The new game-mode
13+
message: "&bListenerNotifier &7> &a{name} &bhas switched to &a{gamemode}&b!"

0 commit comments

Comments
 (0)