Skip to content

Commit

Permalink
expand module features
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 1, 2025
1 parent 6c4a5d0 commit a13b9b4
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 172 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package me.xginko.aef.modules.preventions;

import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
import io.papermc.paper.event.player.AsyncChatEvent;
import me.xginko.aef.modules.AEFModule;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class IllegalGameMode extends AEFModule implements Listener {

private final Map<String, GameMode> worldSettings;
private final Set<String> allowedGamemodePlayers;
private final GameMode defaultGamemode;
private final boolean shouldLog;

public IllegalGameMode() {
super("preventions.illegal-gamemode", false, """
Forces GameMode for players not in the whitelist.\s
Useful protection against past and future backdoor incidents""");
this.shouldLog = config.getBoolean(configPath+".log", false);

GameMode gameMode;
String configuredGamemode = config.getString(configPath + ".default-gamemode", GameMode.SURVIVAL.name());
try {
gameMode = GameMode.valueOf(configuredGamemode);
} catch (IllegalArgumentException e) {
notRecognized(GameMode.class, configuredGamemode);
gameMode = GameMode.SURVIVAL;
}
this.defaultGamemode = gameMode;

Map<String, Object> defaults = new HashMap<>(3);
defaults.put("world", GameMode.SURVIVAL.name());
defaults.put("world_nether", GameMode.SURVIVAL.name());
defaults.put("world_the_end", GameMode.SURVIVAL.name());

ConfigSection section = config.getConfigSection(configPath + ".world-gamemodes", defaults,
"Check the paper api for correct EntityType enums:\n" +
"https://jd.papermc.io/paper/1.20.6/org/bukkit/entity/EntityType.html\n" +
"Make sure your minecraft version is matching as well.");
List<String> worlds = section.getKeys(false);
this.worldSettings = new HashMap<>(worlds.size());
for (String world : worlds) {
try {
worldSettings.put(world, GameMode.valueOf(section.getString(world)));
} catch (NumberFormatException e) {
notRecognized(Integer.class, world);
} catch (IllegalArgumentException e) {
notRecognized(Material.class, world);
}
}

this.allowedGamemodePlayers = new HashSet<>(config.getList(configPath + ".whitelisted-players",
List.of("Notch")));
}

@Override
public void enable() {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@Override
public void disable() {
HandlerList.unregisterAll(this);
}

private void checkForIllegalGameMode(HumanEntity player) {
if (allowedGamemodePlayers.contains(player.getName())) return;

GameMode targetGamemode = worldSettings.getOrDefault(player.getWorld().getName(), defaultGamemode);

if (player.getGameMode() != targetGamemode) {
if (shouldLog) warn(player.getName() + " is GameMode " + player.getGameMode().name() +
" in world " + player.getWorld().getName() + ". Setting to " + targetGamemode.name());
player.setGameMode(targetGamemode);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onInventory(InventoryEvent event) {
checkForIllegalGameMode(event.getView().getPlayer());
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onJoin(PlayerJoinEvent event) {
checkForIllegalGameMode(event.getPlayer());
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onMove(PlayerMoveEvent event) {
checkForIllegalGameMode(event.getPlayer());
}

@EventHandler(priority = EventPriority.HIGHEST)
private void onChat(AsyncChatEvent event) {
checkForIllegalGameMode(event.getPlayer());
}

@EventHandler(priority = EventPriority.HIGHEST)
private void onCommand(PlayerCommandPreprocessEvent event) {
checkForIllegalGameMode(event.getPlayer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
import java.util.List;
import java.util.Set;

public class PreventOppedPlayers extends AEFModule implements Listener {
public class IllegalPermissions extends AEFModule implements Listener {

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

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

if (player.isOp()) {
if (shouldLog) warn(player.getName()+" is not in the operators whitelist. Removing operator status.");
if (shouldLog) warn(player.getName() + " is not in the operators whitelist. Removing operator status.");
player.setOp(false);
}

Expand Down Expand Up @@ -77,7 +78,7 @@ private void onChat(AsyncChatEvent event) {
checkForIllegalOp(event.getPlayer());
}

@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST) // Ensure this runs first
private void onCommand(PlayerCommandPreprocessEvent event) {
checkForIllegalOp(event.getPlayer());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package me.xginko.aef.modules.preventions;

import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
import me.xginko.aef.modules.AEFModule;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class IllegalGameMode extends AEFModule implements Listener {

private final Map<String, GameMode> worldSettings;
private final Set<String> allowedGamemodePlayers;
private final GameMode defaultGamemode;
private final boolean shouldLog;

public IllegalGameMode() {
super("preventions.illegal-gamemode", false,
"Forces GameMode for players not in the whitelist.\n" +
"Useful protection against past and future backdoor incidents.");
this.shouldLog = config.getBoolean(configPath + ".log", true);

GameMode gameMode;
String configuredGamemode = config.getString(configPath + ".default-gamemode", GameMode.SURVIVAL.name());
try {
gameMode = GameMode.valueOf(configuredGamemode);
} catch (IllegalArgumentException e) {
notRecognized(GameMode.class, configuredGamemode);
gameMode = GameMode.SURVIVAL;
}
this.defaultGamemode = gameMode;

Map<String, Object> defaults = new HashMap<>(3);
defaults.put("world", GameMode.SURVIVAL.name());
defaults.put("world_nether", GameMode.SURVIVAL.name());
defaults.put("world_the_end", GameMode.SURVIVAL.name());

ConfigSection section = config.getConfigSection(configPath + ".world-gamemodes", defaults,
"Check the paper api for correct EntityType enums:\n" +
"https://jd.papermc.io/paper/1.20.6/org/bukkit/entity/EntityType.html\n" +
"Make sure your minecraft version is matching as well.");
List<String> worlds = section.getKeys(false);
this.worldSettings = new HashMap<>(worlds.size());
for (String world : worlds) {
try {
worldSettings.put(world, GameMode.valueOf(section.getString(world)));
} catch (NumberFormatException e) {
notRecognized(Integer.class, world);
} catch (IllegalArgumentException e) {
notRecognized(Material.class, world);
}
}

this.allowedGamemodePlayers = new HashSet<>(config.getList(configPath + ".whitelisted-players",
Collections.singletonList("Notch")));
}

@Override
public void enable() {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@Override
public void disable() {
HandlerList.unregisterAll(this);
}

private void checkForIllegalGameMode(HumanEntity player) {
if (allowedGamemodePlayers.contains(player.getName())) return;

GameMode targetGamemode = worldSettings.getOrDefault(player.getWorld().getName(), defaultGamemode);

if (player.getGameMode() != targetGamemode) {
if (shouldLog) warn(player.getName() + " is GameMode " + player.getGameMode().name() +
" in world " + player.getWorld().getName() + ". Setting to " + targetGamemode.name());
player.setGameMode(targetGamemode);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onInventory(InventoryEvent event) {
checkForIllegalGameMode(event.getView().getPlayer());
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onJoin(PlayerJoinEvent event) {
checkForIllegalGameMode(event.getPlayer());
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onLeave(PlayerQuitEvent event) {
checkForIllegalGameMode(event.getPlayer());
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onMove(PlayerMoveEvent event) {
checkForIllegalGameMode(event.getPlayer());
}

@EventHandler(priority = EventPriority.HIGHEST)
private void onChat(AsyncPlayerChatEvent event) {
checkForIllegalGameMode(event.getPlayer());
}

@EventHandler(priority = EventPriority.HIGHEST)
private void onCommand(PlayerCommandPreprocessEvent event) {
checkForIllegalGameMode(event.getPlayer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
import java.util.HashSet;
import java.util.Set;

public class PreventOppedPlayers extends AEFModule implements Listener {
public class IllegalPermissions extends AEFModule implements Listener {

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

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

if (player.isOp()) {
if (shouldLog) warn(player.getName()+" is not in the operators whitelist. Removing operator status.");
if (shouldLog) warn(player.getName() + " is not in the operators whitelist. Removing operator status.");
player.setOp(false);
}

Expand Down Expand Up @@ -77,7 +78,7 @@ private void onChat(AsyncPlayerChatEvent event) {
checkForIllegalOp(event.getPlayer());
}

@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST) // Ensure this runs first
private void onCommand(PlayerCommandPreprocessEvent event) {
checkForIllegalOp(event.getPlayer());
}
Expand Down
Loading

0 comments on commit a13b9b4

Please sign in to comment.