use of net.minecraft.commands.CommandSourceStack in project SpongeCommon by SpongePowered.
the class CommandsMixin method impl$preventPutIntoMapIfNodeIsComplex.
@SuppressWarnings("unchecked")
@Redirect(method = "fillUsableCommands", at = @At(value = "INVOKE", target = "Ljava/util/Map;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", remap = false))
private <K, V> V impl$preventPutIntoMapIfNodeIsComplex(final Map<K, V> map, final K key, final V value, final CommandNode<CommandSourceStack> rootCommandSource, final CommandNode<SharedSuggestionProvider> rootSuggestion, final CommandSourceStack source, final Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> commandNodeToSuggestionNode) {
if (!map.containsKey(key)) {
// done here because this check is applicable
final ServerPlayer e = (ServerPlayer) source.getEntity();
final Map<CommandNode<CommandSourceStack>, List<CommandNode<SharedSuggestionProvider>>> playerNodes = this.impl$playerNodeCache.get(e);
if (!playerNodes.containsKey(key)) {
final List<CommandNode<SharedSuggestionProvider>> children = new ArrayList<>();
children.add((CommandNode<SharedSuggestionProvider>) value);
playerNodes.put((CommandNode<CommandSourceStack>) key, children);
}
// we need to swap it out.
if (value instanceof ArgumentCommandNode && CommandUtil.checkForCustomSuggestions(rootSuggestion)) {
rootSuggestion.addChild(this.impl$cloneArgumentCommandNodeWithoutSuggestions((ArgumentCommandNode<SharedSuggestionProvider, ?>) value));
} else {
rootSuggestion.addChild((CommandNode<SharedSuggestionProvider>) value);
}
return map.put(key, value);
}
// it's ignored anyway.
return null;
}
use of net.minecraft.commands.CommandSourceStack in project SpongeCommon by SpongePowered.
the class CommandSourceStackMixin method impl$updateCauseOnWithPosition.
@Inject(method = "withPosition", at = @At("RETURN"))
private void impl$updateCauseOnWithPosition(final Vec3 pos, final CallbackInfoReturnable<CommandSourceStack> cir) {
if (cir.getReturnValue() != (Object) this) {
final org.spongepowered.math.vector.Vector3d position = VecHelper.toVector3d(pos);
final ServerLocation location = this.impl$cause.context().get(EventContextKeys.LOCATION).map(x -> ServerLocation.of(x.world(), position)).orElseGet(() -> ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) cir.getReturnValue().getLevel(), position));
((CommandSourceStackBridge) cir.getReturnValue()).bridge$setCause(this.impl$applyToCause(EventContextKeys.LOCATION, location));
}
}
use of net.minecraft.commands.CommandSourceStack in project SpongeCommon by SpongePowered.
the class DifficultyCommandMixin method setDifficulty.
// @formatter:on
/**
* @author Zidane
* @reason Only apply difficulty for the world the command was ran in (as in Sponge, all worlds have difficulties)
*/
@Overwrite
public static int setDifficulty(CommandSourceStack source, Difficulty difficulty) throws CommandSyntaxException {
if (source.getLevel().getDifficulty() == difficulty) {
throw DifficultyCommandMixin.ERROR_ALREADY_DIFFICULT.create(difficulty.getKey());
} else {
final LevelData levelData = source.getLevel().getLevelData();
((ServerWorldProperties) levelData).setDifficulty((org.spongepowered.api.world.difficulty.Difficulty) (Object) difficulty);
source.getLevel().setSpawnSettings(((MinecraftServerAccessor) SpongeCommon.server()).invoker$isSpawningMonsters(), SpongeCommon.server().isSpawningAnimals());
source.getLevel().getPlayers(p -> true).forEach(p -> p.connection.send(new ClientboundChangeDifficultyPacket(levelData.getDifficulty(), levelData.isDifficultyLocked())));
source.sendSuccess(new TranslatableComponent("commands.difficulty.success", difficulty.getDisplayName()), true);
return 0;
}
}
use of net.minecraft.commands.CommandSourceStack in project SpongeCommon by SpongePowered.
the class ForgeCommandManager method processCommand.
@Override
protected CommandResult processCommand(final CommandCause cause, final CommandMapping mapping, final String original, final String command, final String args) throws Throwable {
final CommandRegistrar<?> registrar = mapping.registrar();
final boolean isBrig = registrar instanceof BrigadierBasedRegistrar;
final ParseResults<CommandSourceStack> parseResults;
if (isBrig) {
parseResults = this.getDispatcher().parse(original, (CommandSourceStack) cause);
} else {
// We have a non Brig registrar, so we just create a dummy result for mods to inspect.
final CommandContextBuilder<CommandSourceStack> contextBuilder = new CommandContextBuilder<>(this.getDispatcher(), (CommandSourceStack) cause, this.getDispatcher().getRoot(), 0);
contextBuilder.withCommand(ctx -> 1);
if (!args.isEmpty()) {
contextBuilder.withArgument("parsed", new ParsedArgument<>(command.length(), original.length(), args));
}
parseResults = new ParseResults<>(contextBuilder, new SpongeStringReader(original), Collections.emptyMap());
}
// Relocated from Commands (injection short circuits it there)
final CommandEvent event = new CommandEvent(parseResults);
if (MinecraftForge.EVENT_BUS.post(event)) {
if (event.getException() != null) {
Throwables.throwIfUnchecked(event.getException());
}
// As per Forge, we just treat it as a zero success, and do nothing with it.
return CommandResult.builder().result(0).build();
}
if (isBrig) {
return CommandResult.builder().result(this.getDispatcher().execute(parseResults)).build();
} else {
return mapping.registrar().process(cause, mapping, command, args);
}
}
use of net.minecraft.commands.CommandSourceStack in project SpongeCommon by SpongePowered.
the class SpongeCommandCauseFactory method create.
@Override
@NonNull
public CommandCause create() {
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
final Cause cause = frame.currentCause();
final CommandSource iCommandSource = cause.first(CommandSource.class).orElseGet(() -> SpongeCommon.game().systemSubject());
final CommandSourceStack commandSource;
if (iCommandSource instanceof CommandSourceProviderBridge) {
// We know about this one so we can create it using the factory method on the source.
commandSource = ((CommandSourceProviderBridge) iCommandSource).bridge$getCommandSource(cause);
} else {
// try to create a command cause from the given ICommandSource, but as Mojang did not see fit to
// put any identifying characteristics on the object, we have to go it alone...
final EventContext context = cause.context();
@Nullable final Locatable locatable = iCommandSource instanceof Locatable ? (Locatable) iCommandSource : null;
final Component displayName;
if (iCommandSource instanceof Entity) {
displayName = ((Entity) iCommandSource).get(Keys.DISPLAY_NAME).map(SpongeAdventure::asVanilla).orElseGet(() -> new TextComponent(iCommandSource instanceof Nameable ? ((Nameable) iCommandSource).name() : iCommandSource.getClass().getSimpleName()));
} else {
displayName = new TextComponent(iCommandSource instanceof Nameable ? ((Nameable) iCommandSource).name() : iCommandSource.getClass().getSimpleName());
}
final String name = displayName.getString();
commandSource = new CommandSourceStack(iCommandSource, context.get(EventContextKeys.LOCATION).map(x -> VecHelper.toVanillaVector3d(x.position())).orElseGet(() -> locatable == null ? Vec3.ZERO : VecHelper.toVanillaVector3d(locatable.location().position())), context.get(EventContextKeys.ROTATION).map(rot -> new Vec2((float) rot.x(), (float) rot.y())).orElse(Vec2.ZERO), context.get(EventContextKeys.LOCATION).map(x -> (ServerLevel) x.world()).orElseGet(() -> locatable == null ? SpongeCommon.server().getLevel(Level.OVERWORLD) : (ServerLevel) locatable.serverLocation().world()), 4, name, displayName, SpongeCommon.server(), iCommandSource instanceof Entity ? (net.minecraft.world.entity.Entity) iCommandSource : null);
}
// We don't want the command source to have altered the cause here (unless there is the special case of the
// server), so we reset it back to what it was (in the ctor of CommandSource, it will add the current source
// to the cause - that's for if the source is created elsewhere, not here)
((CommandSourceStackBridge) commandSource).bridge$setCause(frame.currentCause());
return (CommandCause) commandSource;
}
}
Aggregations