Skip to content

Commit eeb2542

Browse files
committed
Merge remote-tracking branch 'origin/api-10' into api-11
2 parents 9e035a2 + 079c2e5 commit eeb2542

5 files changed

Lines changed: 74 additions & 51 deletions

File tree

forge/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ tasks {
364364
exclude("META-INF/INDEX.LIST", "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA", "module-info.class")
365365

366366
manifest {
367+
attributes("Automatic-Module-Name" to "spongeforge.services")
367368
attributes("Multi-Release" to true)
368369
from(forgeManifest)
369370
}
@@ -412,7 +413,7 @@ tasks {
412413
group = "build"
413414
archiveClassifier.set("universal")
414415

415-
manifest.from(forgeManifest)
416+
manifest.from(forgeServicesShadowJar.manifest)
416417

417418
from(forgeServicesShadowJar.archiveFile.map { zipTree(it) })
418419

src/main/java/org/spongepowered/common/event/tracking/PhaseContext.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -323,22 +323,14 @@ public void close() { // Should never throw an exception
323323
}
324324
final PhaseTracker instance = PhaseTracker.getInstance();
325325
instance.completePhase(this);
326-
if (!this.shouldProvideModifiers()) {
327-
if (this.usedFrame != null) {
328-
this.usedFrame.iterator().forEachRemaining(instance::popCauseFrame);
329-
}
330-
return;
331-
}
332-
if (this.usedFrame == null) {
333-
// So, this part is interesting... Since the used frame is null, that means
334-
// the cause stack manager still has the refernce of this context/phase, we have
335-
// to "pop off" the list.
336-
instance.popFrameMutator(this);
337-
}
338326
if (this.usedFrame != null) {
339327
this.usedFrame.iterator().forEachRemaining(instance::popCauseFrame);
340-
this.usedFrame.clear();
341328
this.usedFrame = null;
329+
} else if (this.shouldProvideModifiers()) {
330+
// So, this part is interesting... Since the used frame is null, that means
331+
// the cause stack manager still has the reference of this context/phase, we have
332+
// to "pop off" the list.
333+
instance.popFrameMutator(this);
342334
}
343335
this.reset();
344336
this.isCompleted = false;

src/main/java/org/spongepowered/common/event/tracking/PooledPhaseState.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,23 @@ public final C createPhaseContext(final PhaseTracker tracker) {
4343
}
4444

4545
if (tracker == PhaseTracker.SERVER) {
46-
if (this.serverCached != null && !this.serverCached.isCompleted) {
46+
if (this.serverCached != null) {
4747
final C cached = this.serverCached;
4848
this.serverCached = null;
4949
return cached;
5050
}
5151
} else if (tracker == PhaseTracker.CLIENT) {
52-
if (this.clientCached != null && !this.clientCached.isCompleted) {
52+
if (this.clientCached != null) {
5353
final C cached = this.clientCached;
5454
this.clientCached = null;
5555
return cached;
5656
}
5757
}
5858
final @Nullable C peek = tracker.getContextPoolFor(this).pollFirst();
5959
if (peek != null) {
60-
if (tracker == PhaseTracker.SERVER) {
61-
this.serverCached = peek;
62-
} else if (tracker == PhaseTracker.CLIENT) {
63-
this.clientCached = peek;
64-
}
6560
return peek;
6661
}
67-
final C maybeCached = this.createNewContext(tracker);
68-
if (tracker == PhaseTracker.SERVER) {
69-
this.serverCached = maybeCached;
70-
} else if (tracker == PhaseTracker.CLIENT) {
71-
this.clientCached = maybeCached;
72-
}
73-
return maybeCached;
62+
return this.createNewContext(tracker);
7463
}
7564

7665
final void releaseContextFromPool(final C context) {
@@ -79,25 +68,18 @@ final void releaseContextFromPool(final C context) {
7968
throw new IllegalStateException("Asynchronous Thread Access to PhaseTracker: " + createdTracker);
8069
}
8170
if (createdTracker == PhaseTracker.SERVER) {
82-
if (this.serverCached == context) {
83-
return;
84-
}
8571
if (this.serverCached == null) {
8672
this.serverCached = context;
8773
return;
8874
}
8975
}
90-
if (createdTracker == PhaseTracker.CLIENT) {
91-
if (this.clientCached == context) {
92-
return;
93-
}
76+
else if (createdTracker == PhaseTracker.CLIENT) {
9477
if (this.clientCached == null) {
9578
this.clientCached = context;
9679
return;
9780
}
9881
}
9982
createdTracker.getContextPoolFor(this).push(context);
100-
10183
}
10284

10385
protected abstract C createNewContext(PhaseTracker tracker);

src/mixins/java/org/spongepowered/common/mixin/core/commands/execution/tasks/ExecuteCommandMixin.java

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@
2828
import com.mojang.brigadier.context.CommandContext;
2929
import com.mojang.brigadier.context.ContextChain;
3030
import com.mojang.brigadier.exceptions.CommandSyntaxException;
31+
import net.minecraft.commands.CommandSourceStack;
3132
import net.minecraft.commands.execution.tasks.ExecuteCommand;
33+
import net.minecraft.server.level.ServerPlayer;
34+
import org.checkerframework.checker.nullness.qual.Nullable;
35+
import org.spongepowered.api.Sponge;
36+
import org.spongepowered.api.command.CommandCause;
3237
import org.spongepowered.api.command.CommandResult;
38+
import org.spongepowered.api.command.manager.CommandMapping;
3339
import org.spongepowered.api.event.Cause;
40+
import org.spongepowered.api.event.CauseStackManager;
3441
import org.spongepowered.api.event.EventContextKeys;
3542
import org.spongepowered.api.event.SpongeEventFactory;
3643
import org.spongepowered.asm.mixin.Final;
@@ -40,7 +47,9 @@
4047
import org.spongepowered.asm.mixin.injection.Redirect;
4148
import org.spongepowered.common.SpongeCommon;
4249
import org.spongepowered.common.bridge.commands.CommandSourceStackBridge;
43-
import org.spongepowered.common.command.brigadier.context.SpongeCommandContext;
50+
import org.spongepowered.common.event.tracking.PhaseTracker;
51+
import org.spongepowered.common.event.tracking.phase.general.CommandPhaseContext;
52+
import org.spongepowered.common.event.tracking.phase.general.GeneralPhase;
4453

4554
@Mixin(ExecuteCommand.class)
4655
public abstract class ExecuteCommandMixin {
@@ -52,26 +61,49 @@ public abstract class ExecuteCommandMixin {
5261
@Redirect(method = "execute(Lnet/minecraft/commands/ExecutionCommandSource;Lnet/minecraft/commands/execution/ExecutionContext;Lnet/minecraft/commands/execution/Frame;)V",
5362
at = @At(value = "INVOKE", target = "Lcom/mojang/brigadier/context/ContextChain;runExecutable(Lcom/mojang/brigadier/context/CommandContext;Ljava/lang/Object;Lcom/mojang/brigadier/ResultConsumer;Z)I"))
5463
private <S> int impl$onRunExecutable(final CommandContext<S> executable, final S source, final ResultConsumer<S> resultConsumer, final boolean forkedMode) throws CommandSyntaxException {
55-
final int result = ContextChain.runExecutable(executable, source, resultConsumer, forkedMode);
64+
final @Nullable ServerPlayer player = ((CommandSourceStack) source).getPlayer();
5665

5766
final Cause cause = ((CommandSourceStackBridge) source).bridge$getCause();
5867
final String originalRawCommand = cause.context().get(EventContextKeys.COMMAND).orElse(this.commandInput);
5968
final String[] origSplitArg = originalRawCommand.split(" ", 2);
6069
final String originalCommand = origSplitArg[0];
6170
final String originalArgs = origSplitArg.length == 2 ? origSplitArg[1] : "";
6271

63-
final String[] splitArg = this.commandInput.split(" ", 2);
64-
final String baseCommand = splitArg[0];
65-
final String args = splitArg.length == 2 ? splitArg[1] : "";
66-
SpongeCommon.post(SpongeEventFactory.createExecuteCommandEventPost(
67-
cause,
68-
originalArgs,
69-
args,
70-
originalCommand,
71-
baseCommand,
72-
((SpongeCommandContext) executable).cause(),
73-
CommandResult.builder().result(result).build()
74-
));
75-
return result;
72+
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
73+
final CommandSourceStackBridge sourceBridge = (CommandSourceStackBridge) source;
74+
frame.addContext(EventContextKeys.COMMAND, this.commandInput);
75+
sourceBridge.bridge$updateFrameFromCommandSource(frame);
76+
77+
final CommandCause commandCause = sourceBridge.bridge$withCurrentCause();
78+
79+
final String[] splitArg = this.commandInput.split(" ", 2);
80+
final String baseCommand = splitArg[0];
81+
final String args = splitArg.length == 2 ? splitArg[1] : "";
82+
final CommandMapping mapping = Sponge.server().commandManager().commandMapping(baseCommand).orElse(null);
83+
84+
try (final CommandPhaseContext context = GeneralPhase.State.COMMAND
85+
.createPhaseContext(PhaseTracker.getInstance())
86+
.source(source)
87+
.command(this.commandInput)
88+
.commandMapping(mapping)) {
89+
if (player != null) {
90+
context.creator(player.getUUID());
91+
context.notifier(player.getUUID());
92+
}
93+
context.buildAndSwitch();
94+
95+
final int result = ContextChain.runExecutable(executable.copyFor((S) commandCause), source, resultConsumer, forkedMode);
96+
SpongeCommon.post(SpongeEventFactory.createExecuteCommandEventPost(
97+
commandCause.cause(),
98+
originalArgs,
99+
args,
100+
originalCommand,
101+
baseCommand,
102+
commandCause,
103+
CommandResult.builder().result(result).build()
104+
));
105+
return result;
106+
}
107+
}
76108
}
77109
}

src/mixins/java/org/spongepowered/common/mixin/tracker/server/dedicated/DedicatedServerMixin_Tracker.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@
2424
*/
2525
package org.spongepowered.common.mixin.tracker.server.dedicated;
2626

27+
import net.minecraft.commands.CommandSourceStack;
28+
import net.minecraft.commands.Commands;
2729
import net.minecraft.core.BlockPos;
2830
import net.minecraft.server.dedicated.DedicatedServer;
2931
import net.minecraft.server.level.ServerLevel;
3032
import net.minecraft.world.entity.player.Player;
3133
import org.checkerframework.checker.nullness.qual.NonNull;
34+
import org.spongepowered.api.Sponge;
3235
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
36+
import org.spongepowered.api.event.CauseStackManager;
37+
import org.spongepowered.api.event.EventContextKeys;
3338
import org.spongepowered.asm.mixin.Mixin;
3439
import org.spongepowered.asm.mixin.Overwrite;
3540
import org.spongepowered.asm.mixin.Shadow;
41+
import org.spongepowered.asm.mixin.injection.At;
42+
import org.spongepowered.asm.mixin.injection.Redirect;
3643
import org.spongepowered.common.bridge.server.level.ServerLevelBridge;
3744
import org.spongepowered.common.event.ShouldFire;
3845
import org.spongepowered.common.event.SpongeCommonEventFactory;
@@ -70,4 +77,13 @@ public boolean isUnderSpawnProtection(final ServerLevel worldIn, final BlockPos
7077
&& Math.max(Math.abs(pos.getX() - spawnPoint.getX()), Math.abs(pos.getZ() - spawnPoint.getZ())) <= protectionRadius
7178
&& !((ServerPlayer) playerIn).hasPermission("minecraft.spawn-protection.override");
7279
}
80+
81+
@Redirect(method = "handleConsoleInputs", at = @At(value = "INVOKE", target = "Lnet/minecraft/commands/Commands;performPrefixedCommand(Lnet/minecraft/commands/CommandSourceStack;Ljava/lang/String;)V"))
82+
private void tracker$onPerformConsoleCommand(final Commands instance, final CommandSourceStack source, final String command) {
83+
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
84+
frame.pushCause(Sponge.systemSubject());
85+
frame.addContext(EventContextKeys.COMMAND, command);
86+
instance.performPrefixedCommand(source, command);
87+
}
88+
}
7389
}

0 commit comments

Comments
 (0)