mirror of
https://github.com/kaboomserver/extras.git
synced 2025-08-04 03:26:06 +00:00
feat: use maxCommandForkCount gamerule for limiting execute
refactor: clean up gamerule limiter
This commit is contained in:
parent
39feb26c93
commit
e4000b8881
4 changed files with 42 additions and 25 deletions
2
pom.xml
2
pom.xml
|
@ -15,7 +15,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.papermc.paper</groupId>
|
<groupId>io.papermc.paper</groupId>
|
||||||
<artifactId>paper-api</artifactId>
|
<artifactId>paper-api</artifactId>
|
||||||
<version>1.19.4-R0.1-SNAPSHOT</version>
|
<version>1.20.4-R0.1-SNAPSHOT</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
|
@ -37,7 +37,7 @@ public final class EntitySpawn implements Listener {
|
||||||
private static final FileConfiguration CONFIG = JavaPlugin.getPlugin(Main.class).getConfig();
|
private static final FileConfiguration CONFIG = JavaPlugin.getPlugin(Main.class).getConfig();
|
||||||
|
|
||||||
private static final int MAX_ENTITIES_PER_CHUNK = CONFIG.getInt("maxEntitiesPerChunk");
|
private static final int MAX_ENTITIES_PER_CHUNK = CONFIG.getInt("maxEntitiesPerChunk");
|
||||||
private static final int MAX_ENTITIES_PER_WORLD = CONFIG.getInt("maxEntitiesPerWorld");
|
public static final int MAX_ENTITIES_PER_WORLD = CONFIG.getInt("maxEntitiesPerWorld");
|
||||||
private static final int MAX_TNTS_PER_WORLD = CONFIG.getInt("maxTntsPerWorld");
|
private static final int MAX_TNTS_PER_WORLD = CONFIG.getInt("maxTntsPerWorld");
|
||||||
|
|
||||||
private void applyEntityChanges(final Entity entity) {
|
private void applyEntityChanges(final Entity entity) {
|
||||||
|
|
|
@ -18,15 +18,13 @@ import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public final class ServerCommand implements Listener {
|
public final class ServerCommand implements Listener {
|
||||||
private static final Pattern AS_AT_PATTERN = Pattern.compile(
|
|
||||||
"\\b(as|at|facing entity) @[ae]\\b");
|
|
||||||
private static final Pattern SELECTOR_PATTERN = Pattern.compile("(?>\\s)*@[aepsr](?>\\s)*");
|
private static final Pattern SELECTOR_PATTERN = Pattern.compile("(?>\\s)*@[aepsr](?>\\s)*");
|
||||||
private static final Logger LOGGER = JavaPlugin.getPlugin(Main.class).getLogger();
|
private static final Logger LOGGER = JavaPlugin.getPlugin(Main.class).getLogger();
|
||||||
|
|
||||||
private static final String[] COMMANDS = { "execute", "clone", "datapack", "fill",
|
private static final String[] COMMANDS = {"clone", "datapack", "fill", "forceload",
|
||||||
"forceload", "give", "kick", "locate", "locatebiome", "me", "msg", "reload",
|
"give", "kick", "locate", "locatebiome", "me", "msg", "reload", "save-all",
|
||||||
"save-all", "say", "spawnpoint", "spreadplayers", "stop", "summon", "teammsg",
|
"say", "spawnpoint", "spreadplayers", "stop", "summon", "teammsg",
|
||||||
"teleport", "tell", "tellraw", "tm", "tp", "w", "place", "fillbiome", "ride" ,
|
"teleport", "tell", "tellraw", "tm", "tp", "w", "place", "fillbiome", "ride",
|
||||||
"tick", "jfr"};
|
"tick", "jfr"};
|
||||||
|
|
||||||
public static boolean checkExecuteCommand(final String cmd) {
|
public static boolean checkExecuteCommand(final String cmd) {
|
||||||
|
@ -81,15 +79,6 @@ public final class ServerCommand implements Listener {
|
||||||
switch (commandName) {
|
switch (commandName) {
|
||||||
case "/minecraft:execute", "/execute" -> {
|
case "/minecraft:execute", "/execute" -> {
|
||||||
if (arr.length >= 2) {
|
if (arr.length >= 2) {
|
||||||
int asAtCount = 0;
|
|
||||||
Matcher asAtMatcher = AS_AT_PATTERN.matcher(command.toLowerCase());
|
|
||||||
while (asAtMatcher.find()) {
|
|
||||||
asAtCount++;
|
|
||||||
}
|
|
||||||
if (asAtCount >= 2) {
|
|
||||||
return "cancel";
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 1; i < arr.length; i++) {
|
for (int i = 1; i < arr.length; i++) {
|
||||||
if ("summon".equalsIgnoreCase(arr[i])) {
|
if ("summon".equalsIgnoreCase(arr[i])) {
|
||||||
return "cancel";
|
return "cancel";
|
||||||
|
|
|
@ -8,31 +8,59 @@ import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.scheduler.BukkitScheduler;
|
import org.bukkit.scheduler.BukkitScheduler;
|
||||||
import pw.kaboom.extras.Main;
|
import pw.kaboom.extras.Main;
|
||||||
|
import pw.kaboom.extras.modules.entity.EntitySpawn;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public final class ServerGameRule implements Listener {
|
public final class ServerGameRule implements Listener {
|
||||||
|
private static final Map<GameRule<Integer>, Integer> GAMERULE_LIMITS = Map.of(
|
||||||
|
GameRule.RANDOM_TICK_SPEED, 6,
|
||||||
|
GameRule.SPAWN_RADIUS, 100,
|
||||||
|
GameRule.COMMAND_MODIFICATION_BLOCK_LIMIT, 32768,
|
||||||
|
GameRule.MAX_COMMAND_FORK_COUNT, EntitySpawn.MAX_ENTITIES_PER_WORLD
|
||||||
|
);
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
void onGameRuleChange(final WorldGameRuleChangeEvent event) {
|
void onGameRuleChange(final WorldGameRuleChangeEvent event) {
|
||||||
final GameRule<?> gameRule = event.getGameRule();
|
final GameRule<?> gameRule = event.getGameRule();
|
||||||
|
|
||||||
if ((gameRule == GameRule.RANDOM_TICK_SPEED
|
final Integer limit = GAMERULE_LIMITS.get(gameRule);
|
||||||
&& Integer.parseInt(event.getValue()) > 6)
|
if (limit == null) {
|
||||||
|| (event.getGameRule() == GameRule.SPAWN_RADIUS
|
return;
|
||||||
&& Integer.parseInt(event.getValue()) > 100)
|
}
|
||||||
|| (event.getGameRule() == GameRule.COMMAND_MODIFICATION_BLOCK_LIMIT
|
|
||||||
&& Integer.parseInt(event.getValue()) > 32768)) {
|
final int value = Integer.parseInt(event.getValue());
|
||||||
event.setCancelled(true);
|
if (value > limit) {
|
||||||
|
event.setValue(limit.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void enableAutoSave() {
|
private static void enableAutoSave() {
|
||||||
for (final World world: Bukkit.getWorlds()) {
|
for (final World world : Bukkit.getWorlds()) {
|
||||||
world.setAutoSave(true);
|
world.setAutoSave(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void fixGameRules() {
|
||||||
|
for (final World world : Bukkit.getWorlds()) {
|
||||||
|
for (final var entry : GAMERULE_LIMITS.entrySet()) {
|
||||||
|
final GameRule<Integer> gameRule = entry.getKey();
|
||||||
|
final int limit = entry.getValue();
|
||||||
|
|
||||||
|
final Integer value = world.getGameRuleValue(gameRule) != null
|
||||||
|
? world.getGameRuleValue(gameRule) : world.getGameRuleDefault(gameRule);
|
||||||
|
|
||||||
|
if (value == null || value > limit) {
|
||||||
|
world.setGameRule(gameRule, limit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void init(final Main main) {
|
public static void init(final Main main) {
|
||||||
final BukkitScheduler scheduler = Bukkit.getScheduler();
|
final BukkitScheduler scheduler = Bukkit.getScheduler();
|
||||||
|
|
||||||
|
scheduler.runTask(main, ServerGameRule::fixGameRules); // Right before server fully starts
|
||||||
scheduler.runTaskTimer(main, ServerGameRule::enableAutoSave, 0L, 600L); // 30 seconds
|
scheduler.runTaskTimer(main, ServerGameRule::enableAutoSave, 0L, 600L); // 30 seconds
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue