|
| 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 | +} |
0 commit comments