Search in sources :

Example 1 with ImmutableSpongeListValue

use of org.spongepowered.common.data.value.ImmutableSpongeListValue in project SpongeCommon by SpongePowered.

the class ServerGamePacketListenerImplMixin method impl$callChangeSignEvent.

@Redirect(method = "updateSignText", at = @At(value = "INVOKE", remap = false, target = "Ljava/util/List;size()I"))
private int impl$callChangeSignEvent(final List<String> list, ServerboundSignUpdatePacket p_244542_1_, List<String> p_244542_2_) {
    final SignBlockEntity blockEntity = (SignBlockEntity) this.player.level.getBlockEntity(p_244542_1_.getPos());
    final ListValue<Component> originalLinesValue = ((Sign) blockEntity).getValue(Keys.SIGN_LINES).orElseGet(() -> new ImmutableSpongeListValue<>(Keys.SIGN_LINES, ImmutableList.of()));
    final List<Component> newLines = new ArrayList<>();
    for (final String line : list) {
        // Sponge Start - While Vanilla does some strip formatting, it doesn't catch everything. This patches an exploit that allows color
        // signs to be created.
        newLines.add(Component.text(SharedConstants.filterText(line)));
    }
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(this.player);
        final ListValue.Mutable<Component> newLinesValue = ListValue.mutableOf(Keys.SIGN_LINES, newLines);
        final ChangeSignEvent event = SpongeEventFactory.createChangeSignEvent(PhaseTracker.getCauseStackManager().currentCause(), originalLinesValue.asImmutable(), newLinesValue, (Sign) blockEntity);
        final ListValue<Component> toApply = SpongeCommon.post(event) ? originalLinesValue : newLinesValue;
        ((Sign) blockEntity).offer(toApply);
    }
    return 0;
}
Also used : SignBlockEntity(net.minecraft.world.level.block.entity.SignBlockEntity) ListValue(org.spongepowered.api.data.value.ListValue) ImmutableSpongeListValue(org.spongepowered.common.data.value.ImmutableSpongeListValue) ArrayList(java.util.ArrayList) ChangeSignEvent(org.spongepowered.api.event.block.entity.ChangeSignEvent) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Sign(org.spongepowered.api.block.entity.Sign) Component(net.kyori.adventure.text.Component) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

ArrayList (java.util.ArrayList)1 Component (net.kyori.adventure.text.Component)1 SignBlockEntity (net.minecraft.world.level.block.entity.SignBlockEntity)1 Sign (org.spongepowered.api.block.entity.Sign)1 ListValue (org.spongepowered.api.data.value.ListValue)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 ChangeSignEvent (org.spongepowered.api.event.block.entity.ChangeSignEvent)1 Redirect (org.spongepowered.asm.mixin.injection.Redirect)1 ImmutableSpongeListValue (org.spongepowered.common.data.value.ImmutableSpongeListValue)1