Skip to content

Commit a13b9b4

Browse files
committed
expand module features
1 parent 6c4a5d0 commit a13b9b4

File tree

6 files changed

+257
-172
lines changed

6 files changed

+257
-172
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package me.xginko.aef.modules.preventions;
2+
3+
import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
4+
import io.papermc.paper.event.player.AsyncChatEvent;
5+
import me.xginko.aef.modules.AEFModule;
6+
import org.bukkit.GameMode;
7+
import org.bukkit.Material;
8+
import org.bukkit.entity.HumanEntity;
9+
import org.bukkit.event.EventHandler;
10+
import org.bukkit.event.EventPriority;
11+
import org.bukkit.event.HandlerList;
12+
import org.bukkit.event.Listener;
13+
import org.bukkit.event.inventory.InventoryEvent;
14+
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
15+
import org.bukkit.event.player.PlayerJoinEvent;
16+
import org.bukkit.event.player.PlayerMoveEvent;
17+
18+
import java.util.HashMap;
19+
import java.util.HashSet;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.Set;
23+
24+
public class IllegalGameMode extends AEFModule implements Listener {
25+
26+
private final Map<String, GameMode> worldSettings;
27+
private final Set<String> allowedGamemodePlayers;
28+
private final GameMode defaultGamemode;
29+
private final boolean shouldLog;
30+
31+
public IllegalGameMode() {
32+
super("preventions.illegal-gamemode", false, """
33+
Forces GameMode for players not in the whitelist.\s
34+
Useful protection against past and future backdoor incidents""");
35+
this.shouldLog = config.getBoolean(configPath+".log", false);
36+
37+
GameMode gameMode;
38+
String configuredGamemode = config.getString(configPath + ".default-gamemode", GameMode.SURVIVAL.name());
39+
try {
40+
gameMode = GameMode.valueOf(configuredGamemode);
41+
} catch (IllegalArgumentException e) {
42+
notRecognized(GameMode.class, configuredGamemode);
43+
gameMode = GameMode.SURVIVAL;
44+
}
45+
this.defaultGamemode = gameMode;
46+
47+
Map<String, Object> defaults = new HashMap<>(3);
48+
defaults.put("world", GameMode.SURVIVAL.name());
49+
defaults.put("world_nether", GameMode.SURVIVAL.name());
50+
defaults.put("world_the_end", GameMode.SURVIVAL.name());
51+
52+
ConfigSection section = config.getConfigSection(configPath + ".world-gamemodes", defaults,
53+
"Check the paper api for correct EntityType enums:\n" +
54+
"https://jd.papermc.io/paper/1.20.6/org/bukkit/entity/EntityType.html\n" +
55+
"Make sure your minecraft version is matching as well.");
56+
List<String> worlds = section.getKeys(false);
57+
this.worldSettings = new HashMap<>(worlds.size());
58+
for (String world : worlds) {
59+
try {
60+
worldSettings.put(world, GameMode.valueOf(section.getString(world)));
61+
} catch (NumberFormatException e) {
62+
notRecognized(Integer.class, world);
63+
} catch (IllegalArgumentException e) {
64+
notRecognized(Material.class, world);
65+
}
66+
}
67+
68+
this.allowedGamemodePlayers = new HashSet<>(config.getList(configPath + ".whitelisted-players",
69+
List.of("Notch")));
70+
}
71+
72+
@Override
73+
public void enable() {
74+
plugin.getServer().getPluginManager().registerEvents(this, plugin);
75+
}
76+
77+
@Override
78+
public void disable() {
79+
HandlerList.unregisterAll(this);
80+
}
81+
82+
private void checkForIllegalGameMode(HumanEntity player) {
83+
if (allowedGamemodePlayers.contains(player.getName())) return;
84+
85+
GameMode targetGamemode = worldSettings.getOrDefault(player.getWorld().getName(), defaultGamemode);
86+
87+
if (player.getGameMode() != targetGamemode) {
88+
if (shouldLog) warn(player.getName() + " is GameMode " + player.getGameMode().name() +
89+
" in world " + player.getWorld().getName() + ". Setting to " + targetGamemode.name());
90+
player.setGameMode(targetGamemode);
91+
}
92+
}
93+
94+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
95+
private void onInventory(InventoryEvent event) {
96+
checkForIllegalGameMode(event.getView().getPlayer());
97+
}
98+
99+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
100+
private void onJoin(PlayerJoinEvent event) {
101+
checkForIllegalGameMode(event.getPlayer());
102+
}
103+
104+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
105+
private void onMove(PlayerMoveEvent event) {
106+
checkForIllegalGameMode(event.getPlayer());
107+
}
108+
109+
@EventHandler(priority = EventPriority.HIGHEST)
110+
private void onChat(AsyncChatEvent event) {
111+
checkForIllegalGameMode(event.getPlayer());
112+
}
113+
114+
@EventHandler(priority = EventPriority.HIGHEST)
115+
private void onCommand(PlayerCommandPreprocessEvent event) {
116+
checkForIllegalGameMode(event.getPlayer());
117+
}
118+
}

AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/PreventOppedPlayers.java renamed to AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/IllegalPermissions.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
import java.util.List;
1919
import java.util.Set;
2020

21-
public class PreventOppedPlayers extends AEFModule implements Listener {
21+
public class IllegalPermissions extends AEFModule implements Listener {
2222

2323
private final Set<String> allowedOperators, blacklistedPermissions;
2424
private final boolean shouldLog;
2525

26-
public PreventOppedPlayers() {
27-
super("preventions.prevent-opped-players", false,
28-
"Useful if you suspect a backdoor has happened.");
26+
public IllegalPermissions() {
27+
super("preventions.illegal-permissions", false, """
28+
Strips/prevents certain permissions being used by unauthorized players.
29+
Useful protection against past and future backdoor incidents""");
2930
this.shouldLog = config.getBoolean(configPath+".log", false);
3031
this.allowedOperators = new HashSet<>(config.getList(configPath + ".whitelisted-players", List.of("Notch")));
3132
this.blacklistedPermissions = new HashSet<>(config.getList(configPath + ".blacklisted-permissions", List.of("*")));
@@ -45,7 +46,7 @@ private void checkForIllegalOp(Player player) {
4546
if (allowedOperators.contains(player.getName())) return;
4647

4748
if (player.isOp()) {
48-
if (shouldLog) warn(player.getName()+" is not in the operators whitelist. Removing operator status.");
49+
if (shouldLog) warn(player.getName() + " is not in the operators whitelist. Removing operator status.");
4950
player.setOp(false);
5051
}
5152

@@ -77,7 +78,7 @@ private void onChat(AsyncChatEvent event) {
7778
checkForIllegalOp(event.getPlayer());
7879
}
7980

80-
@EventHandler(priority = EventPriority.HIGHEST)
81+
@EventHandler(priority = EventPriority.LOWEST) // Ensure this runs first
8182
private void onCommand(PlayerCommandPreprocessEvent event) {
8283
checkForIllegalOp(event.getPlayer());
8384
}

AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/PreventNonSurvival.java

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package me.xginko.aef.modules.preventions;
2+
3+
import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
4+
import me.xginko.aef.modules.AEFModule;
5+
import org.bukkit.GameMode;
6+
import org.bukkit.Material;
7+
import org.bukkit.entity.HumanEntity;
8+
import org.bukkit.event.EventHandler;
9+
import org.bukkit.event.EventPriority;
10+
import org.bukkit.event.HandlerList;
11+
import org.bukkit.event.Listener;
12+
import org.bukkit.event.inventory.InventoryEvent;
13+
import org.bukkit.event.player.AsyncPlayerChatEvent;
14+
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
15+
import org.bukkit.event.player.PlayerJoinEvent;
16+
import org.bukkit.event.player.PlayerMoveEvent;
17+
import org.bukkit.event.player.PlayerQuitEvent;
18+
19+
import java.util.Collections;
20+
import java.util.HashMap;
21+
import java.util.HashSet;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.Set;
25+
26+
public class IllegalGameMode extends AEFModule implements Listener {
27+
28+
private final Map<String, GameMode> worldSettings;
29+
private final Set<String> allowedGamemodePlayers;
30+
private final GameMode defaultGamemode;
31+
private final boolean shouldLog;
32+
33+
public IllegalGameMode() {
34+
super("preventions.illegal-gamemode", false,
35+
"Forces GameMode for players not in the whitelist.\n" +
36+
"Useful protection against past and future backdoor incidents.");
37+
this.shouldLog = config.getBoolean(configPath + ".log", true);
38+
39+
GameMode gameMode;
40+
String configuredGamemode = config.getString(configPath + ".default-gamemode", GameMode.SURVIVAL.name());
41+
try {
42+
gameMode = GameMode.valueOf(configuredGamemode);
43+
} catch (IllegalArgumentException e) {
44+
notRecognized(GameMode.class, configuredGamemode);
45+
gameMode = GameMode.SURVIVAL;
46+
}
47+
this.defaultGamemode = gameMode;
48+
49+
Map<String, Object> defaults = new HashMap<>(3);
50+
defaults.put("world", GameMode.SURVIVAL.name());
51+
defaults.put("world_nether", GameMode.SURVIVAL.name());
52+
defaults.put("world_the_end", GameMode.SURVIVAL.name());
53+
54+
ConfigSection section = config.getConfigSection(configPath + ".world-gamemodes", defaults,
55+
"Check the paper api for correct EntityType enums:\n" +
56+
"https://jd.papermc.io/paper/1.20.6/org/bukkit/entity/EntityType.html\n" +
57+
"Make sure your minecraft version is matching as well.");
58+
List<String> worlds = section.getKeys(false);
59+
this.worldSettings = new HashMap<>(worlds.size());
60+
for (String world : worlds) {
61+
try {
62+
worldSettings.put(world, GameMode.valueOf(section.getString(world)));
63+
} catch (NumberFormatException e) {
64+
notRecognized(Integer.class, world);
65+
} catch (IllegalArgumentException e) {
66+
notRecognized(Material.class, world);
67+
}
68+
}
69+
70+
this.allowedGamemodePlayers = new HashSet<>(config.getList(configPath + ".whitelisted-players",
71+
Collections.singletonList("Notch")));
72+
}
73+
74+
@Override
75+
public void enable() {
76+
plugin.getServer().getPluginManager().registerEvents(this, plugin);
77+
}
78+
79+
@Override
80+
public void disable() {
81+
HandlerList.unregisterAll(this);
82+
}
83+
84+
private void checkForIllegalGameMode(HumanEntity player) {
85+
if (allowedGamemodePlayers.contains(player.getName())) return;
86+
87+
GameMode targetGamemode = worldSettings.getOrDefault(player.getWorld().getName(), defaultGamemode);
88+
89+
if (player.getGameMode() != targetGamemode) {
90+
if (shouldLog) warn(player.getName() + " is GameMode " + player.getGameMode().name() +
91+
" in world " + player.getWorld().getName() + ". Setting to " + targetGamemode.name());
92+
player.setGameMode(targetGamemode);
93+
}
94+
}
95+
96+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
97+
private void onInventory(InventoryEvent event) {
98+
checkForIllegalGameMode(event.getView().getPlayer());
99+
}
100+
101+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
102+
private void onJoin(PlayerJoinEvent event) {
103+
checkForIllegalGameMode(event.getPlayer());
104+
}
105+
106+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
107+
private void onLeave(PlayerQuitEvent event) {
108+
checkForIllegalGameMode(event.getPlayer());
109+
}
110+
111+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
112+
private void onMove(PlayerMoveEvent event) {
113+
checkForIllegalGameMode(event.getPlayer());
114+
}
115+
116+
@EventHandler(priority = EventPriority.HIGHEST)
117+
private void onChat(AsyncPlayerChatEvent event) {
118+
checkForIllegalGameMode(event.getPlayer());
119+
}
120+
121+
@EventHandler(priority = EventPriority.HIGHEST)
122+
private void onCommand(PlayerCommandPreprocessEvent event) {
123+
checkForIllegalGameMode(event.getPlayer());
124+
}
125+
}

AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/PreventOppedPlayers.java renamed to AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/IllegalPermissions.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
import java.util.HashSet;
1919
import java.util.Set;
2020

21-
public class PreventOppedPlayers extends AEFModule implements Listener {
21+
public class IllegalPermissions extends AEFModule implements Listener {
2222

2323
private final Set<String> allowedOperators, blacklistedPermissions;
2424
private final boolean shouldLog;
2525

26-
public PreventOppedPlayers() {
27-
super("preventions.prevent-opped-players", false,
28-
"Useful if you suspect a backdoor has happened.");
26+
public IllegalPermissions() {
27+
super("preventions.illegal-permissions", false,
28+
"Strips/prevents certain permissions being used by unauthorized players.\n" +
29+
"Useful protection against past and future backdoor incidents");
2930
this.shouldLog = config.getBoolean(configPath + ".log", true);
3031
this.allowedOperators = new HashSet<>(config.getList(configPath + ".whitelisted-players", Collections.singletonList("Notch")));
3132
this.blacklistedPermissions = new HashSet<>(config.getList(configPath + ".blacklisted-permissions", Collections.singletonList("*")));
@@ -45,7 +46,7 @@ private void checkForIllegalOp(Player player) {
4546
if (allowedOperators.contains(player.getName())) return;
4647

4748
if (player.isOp()) {
48-
if (shouldLog) warn(player.getName()+" is not in the operators whitelist. Removing operator status.");
49+
if (shouldLog) warn(player.getName() + " is not in the operators whitelist. Removing operator status.");
4950
player.setOp(false);
5051
}
5152

@@ -77,7 +78,7 @@ private void onChat(AsyncPlayerChatEvent event) {
7778
checkForIllegalOp(event.getPlayer());
7879
}
7980

80-
@EventHandler(priority = EventPriority.HIGHEST)
81+
@EventHandler(priority = EventPriority.LOWEST) // Ensure this runs first
8182
private void onCommand(PlayerCommandPreprocessEvent event) {
8283
checkForIllegalOp(event.getPlayer());
8384
}

0 commit comments

Comments
 (0)