use of org.spongepowered.common.adventure.SpongeAdventure in project SpongeCommon by SpongePowered.
the class SpongeArgumentCommandNode method suggestUsingModifier.
private CompletableFuture<Suggestions> suggestUsingModifier(final CommandContext<?> context, final SuggestionsBuilder suggestionsBuilder, final CompletableFuture<Suggestions> suggestions) {
if (this.modifier != null) {
return suggestions.thenApply(x -> {
final List<CommandCompletion> originalSuggestions = x.getList().stream().map(SpongeCommandCompletion::from).collect(Collectors.toList());
final List<CommandCompletion> modifiedSuggestions = this.modifier.modifyCompletion((org.spongepowered.api.command.parameter.CommandContext) context, suggestionsBuilder.getRemaining(), new ArrayList<>(originalSuggestions));
if (originalSuggestions.equals(modifiedSuggestions)) {
return x;
}
final SuggestionsBuilder newBuilder = suggestionsBuilder.restart();
for (final CommandCompletion suggestion : modifiedSuggestions) {
newBuilder.suggest(suggestion.completion(), suggestion.tooltip().map(SpongeAdventure::asVanilla).orElse(null));
}
return newBuilder.build();
});
}
return suggestionsBuilder.buildFuture();
}
use of org.spongepowered.common.adventure.SpongeAdventure in project SpongeCommon by SpongePowered.
the class ServerGamePacketListenerImplMixin_Vanilla method vanilla$onProcessChatMessage.
@Inject(method = "handleChat(Ljava/lang/String;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/players/PlayerList;broadcastMessage(Lnet/minecraft/network/chat/Component;Lnet/minecraft/network/chat/ChatType;Ljava/util/UUID;)V"), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD)
private void vanilla$onProcessChatMessage(String var1, CallbackInfo ci, net.minecraft.network.chat.Component component) {
ChatFormatter.formatChatComponent((net.minecraft.network.chat.TranslatableComponent) component);
final ServerPlayer player = (ServerPlayer) this.player;
final PlayerChatFormatter chatFormatter = player.chatFormatter();
final TextComponent rawMessage = Component.text(var1);
try (CauseStackManager.StackFrame frame = PhaseTracker.SERVER.pushCauseFrame()) {
frame.pushCause(this.player);
final Audience audience = (Audience) this.server;
final PlayerChatEvent event = SpongeEventFactory.createPlayerChatEvent(frame.currentCause(), audience, Optional.of(audience), chatFormatter, Optional.of(chatFormatter), rawMessage, rawMessage);
if (SpongeCommon.post(event)) {
ci.cancel();
} else {
event.chatFormatter().ifPresent(formatter -> event.audience().map(SpongeAdventure::unpackAudiences).ifPresent(targets -> {
for (Audience target : targets) {
formatter.format(player, target, event.message(), event.originalMessage()).ifPresent(formattedMessage -> target.sendMessage(player, formattedMessage));
}
}));
}
}
}
use of org.spongepowered.common.adventure.SpongeAdventure 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;
}
}
use of org.spongepowered.common.adventure.SpongeAdventure in project SpongeCommon by SpongePowered.
the class ServerPlayerMixin_API method simulateChat.
@Override
public PlayerChatEvent simulateChat(final Component message, final Cause cause) {
Objects.requireNonNull(message, "message");
Objects.requireNonNull(cause, "cause");
final PlayerChatFormatter originalRouter = this.chatFormatter();
final Audience audience = (Audience) this.server;
final PlayerChatEvent event = SpongeEventFactory.createPlayerChatEvent(cause, audience, Optional.of(audience), originalRouter, Optional.of(originalRouter), message, message);
if (!SpongeCommon.post(event)) {
event.chatFormatter().ifPresent(formatter -> event.audience().map(SpongeAdventure::unpackAudiences).ifPresent(targets -> {
for (final Audience target : targets) {
formatter.format(this, target, event.message(), event.originalMessage()).ifPresent(formattedMessage -> target.sendMessage(this, formattedMessage));
}
}));
}
return event;
}
Aggregations