9
0
mirror of https://github.com/BX-Team/DivineMC.git synced 2025-12-19 14:59:25 +00:00
Files
DivineMC/divinemc-server/minecraft-patches/features/0039-Command-block-parse-results-caching.patch
2025-10-01 01:38:10 +03:00

84 lines
5.1 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Sat, 15 Mar 2025 22:01:08 +0300
Subject: [PATCH] Command block parse results caching
diff --git a/net/minecraft/world/level/BaseCommandBlock.java b/net/minecraft/world/level/BaseCommandBlock.java
index 13950339598f4ec705c54275342fa17ff2e74ca9..5791910babe010d3bdc00a5dd4486f00358df14f 100644
--- a/net/minecraft/world/level/BaseCommandBlock.java
+++ b/net/minecraft/world/level/BaseCommandBlock.java
@@ -34,6 +34,10 @@ public abstract class BaseCommandBlock implements CommandSource {
private String command = "";
@Nullable
private Component customName;
+ // DivineMC start - Caching command block parse results
+ private String lastExecutedCommand;
+ private com.mojang.brigadier.ParseResults<CommandSourceStack> parseResultsCache;
+ // DivineMC end - Caching command block parse results
// CraftBukkit start
@Override
public abstract org.bukkit.command.CommandSender getBukkitSender(CommandSourceStack wrapper);
@@ -113,13 +117,41 @@ public abstract class BaseCommandBlock implements CommandSource {
this.successCount++;
}
});
- // Paper start - ServerCommandEvent
- org.bukkit.event.server.ServerCommandEvent event = new org.bukkit.event.server.ServerCommandEvent(commandSourceStack.getBukkitSender(), net.minecraft.commands.Commands.trimOptionalPrefix(this.command));
- if (!event.callEvent()) {
- return true;
+ // DivineMC start - Command block parse results caching
+ if (org.bxteam.divinemc.config.DivineConfig.PerformanceCategory.commandBlockParseResultsCaching) {
+ String commandCache = this.command;
+ // noinspection DuplicatedCode
+ com.google.common.base.Joiner joiner = com.google.common.base.Joiner.on(" ");
+
+ if (commandCache.startsWith("/")) {
+ commandCache = commandCache.substring(1);
+ }
+
+ org.bukkit.event.server.ServerCommandEvent event = new org.bukkit.event.server.ServerCommandEvent(commandSourceStack.getBukkitSender(), commandCache);
+ org.bukkit.Bukkit.getPluginManager().callEvent(event);
+ if (!event.isCancelled()) {
+ commandCache = event.getCommand();
+ String[] args = commandCache.split(" ");
+
+ if (args.length != 0) {
+ String newCommand = joiner.join(args);
+ if (!newCommand.equals(lastExecutedCommand) || parseResultsCache == null) {
+ MinecraftServer.LOGGER.info("Recompiling parse results cache for command block at ({}, {}, {})", this.getPosition().x, this.getPosition().y, this.getPosition().z);
+ this.cache(server.getCommands().getDispatcher(), commandSourceStack, newCommand);
+ }
+ server.getCommands().performCommand(parseResultsCache, newCommand);
+ }
+ }
+ } else {
+ // Paper start - ServerCommandEvent
+ org.bukkit.event.server.ServerCommandEvent event = new org.bukkit.event.server.ServerCommandEvent(commandSourceStack.getBukkitSender(), net.minecraft.commands.Commands.trimOptionalPrefix(this.command));
+ if (!event.callEvent()) {
+ return true;
+ }
+ server.getCommands().performPrefixedCommand(commandSourceStack, event.getCommand());
+ // Paper end - ServerCommandEvent
}
- server.getCommands().performPrefixedCommand(commandSourceStack, event.getCommand());
- // Paper end - ServerCommandEvent
+ // DivineMC end - Command block parse results caching
} catch (Throwable var6) {
CrashReport crashReport = CrashReport.forThrowable(var6, "Executing command block");
CrashReportCategory crashReportCategory = crashReport.addCategory("Command to be executed");
@@ -139,6 +171,13 @@ public abstract class BaseCommandBlock implements CommandSource {
}
}
+ // DivineMC start - Command block parse results caching
+ private void cache(com.mojang.brigadier.CommandDispatcher<CommandSourceStack> dispatcher, CommandSourceStack commandSourceStack, String commandCache) {
+ this.parseResultsCache = dispatcher.parse(commandCache, commandSourceStack);
+ this.lastExecutedCommand = commandCache;
+ }
+ // DivineMC end - Command block parse results caching
+
public Component getName() {
return this.customName != null ? this.customName : DEFAULT_NAME;
}