-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
257 additions
and
172 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
...chyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/IllegalGameMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 0 additions & 77 deletions
77
...ExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/PreventNonSurvival.java
This file was deleted.
Oops, something went wrong.
125 changes: 125 additions & 0 deletions
125
...hyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/IllegalGameMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.