Search in sources :

Example 61 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project SpongeCommon by SpongePowered.

the class MixinNetHandlerLoginServer method disconnectClient.

private void disconnectClient(Optional<Text> disconnectMessage) {
    ITextComponent reason = null;
    if (disconnectMessage.isPresent()) {
        reason = SpongeTexts.toComponent(disconnectMessage.get());
    } else {
        reason = new TextComponentTranslation("disconnect.disconnected");
    }
    this.closeConnection(reason);
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) ITextComponent(net.minecraft.util.text.ITextComponent)

Example 62 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project SpongeCommon by SpongePowered.

the class MixinTileEntitySign method toContainer.

@Override
public DataContainer toContainer() {
    DataContainer container = super.toContainer();
    List<String> lines = Lists.newArrayList();
    for (ITextComponent line : this.signText) {
        lines.add(ITextComponent.Serializer.componentToJson(line));
    }
    container.set(Keys.SIGN_LINES.getQuery(), lines);
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) ITextComponent(net.minecraft.util.text.ITextComponent)

Example 63 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project SpongeCommon by SpongePowered.

the class PaginationCalculator method getWidth.

/**
 * Calculates the width of a given text as the number of character
 * pixels/columns the line takes up.
 *
 * @param text The text to get the width of
 * @return The amount of character pixels/columns the text takes up
 */
@VisibleForTesting
int getWidth(Text text) {
    ITextComponent component = SpongeTexts.toComponent(text);
    Iterable<ITextComponent> children = ((IMixinTextComponent) component).withChildren();
    int total = 0;
    for (ITextComponent child : children) {
        PrimitiveIterator.OfInt i_it;
        if (child instanceof TextComponentString || child instanceof TextComponentTranslation) {
            i_it = child.getUnformattedComponentText().codePoints().iterator();
        } else {
            continue;
        }
        boolean bold = child.getStyle().getBold();
        Integer cp;
        boolean newLine = false;
        while (i_it.hasNext()) {
            cp = i_it.next();
            if (cp == '\n') {
                // if the previous character is a '\n'
                if (newLine) {
                    total += LINE_WIDTH;
                } else {
                    total = ((int) Math.ceil((double) total / LINE_WIDTH)) * LINE_WIDTH;
                    newLine = true;
                }
            } else {
                int width = getWidth(cp, bold);
                total += width;
                newLine = false;
            }
        }
    }
    return total;
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) PrimitiveIterator(java.util.PrimitiveIterator) IMixinTextComponent(org.spongepowered.common.interfaces.text.IMixinTextComponent) ITextComponent(net.minecraft.util.text.ITextComponent) TextComponentString(net.minecraft.util.text.TextComponentString) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 64 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project SpongeCommon by SpongePowered.

the class SpongeTexts method splitChatMessage.

public static Text[] splitChatMessage(TextComponentTranslation component) {
    Text source = null;
    Text body = null;
    for (Object arg : component.getFormatArgs()) {
        if (source == null) {
            if (arg instanceof ITextComponent) {
                source = SpongeTexts.toText((ITextComponent) arg);
            } else {
                source = Text.of(arg.toString());
            }
        } else {
            Text text;
            if (arg instanceof ITextComponent) {
                text = SpongeTexts.toText((ITextComponent) arg);
            } else {
                text = Text.of(arg.toString());
            }
            if (body == null) {
                body = text;
            } else {
                body = body.concat(text);
            }
        }
    }
    return new Text[] { source, body };
}
Also used : ITextComponent(net.minecraft.util.text.ITextComponent) IMixinText(org.spongepowered.common.interfaces.text.IMixinText) Text(org.spongepowered.api.text.Text)

Example 65 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project SpongeCommon by SpongePowered.

the class SpongeHoverAction method getHandle.

public static HoverEvent getHandle(HoverAction<?> action) {
    HoverEvent.Action type = getType(action);
    ITextComponent component;
    switch(type) {
        case SHOW_ENTITY:
            {
                HoverAction.ShowEntity.Ref entity = ((HoverAction.ShowEntity) action).getResult();
                NBTTagCompound nbt = new NBTTagCompound();
                nbt.setString("id", entity.getUniqueId().toString());
                if (entity.getType().isPresent()) {
                    nbt.setString("type", EntityList.getKey(((SpongeEntityType) entity.getType().get()).entityClass).toString());
                }
                nbt.setString("name", entity.getName());
                component = new TextComponentString(nbt.toString());
                break;
            }
        case SHOW_ITEM:
            {
                ItemStack item = (ItemStack) ((ItemStackSnapshot) action.getResult()).createStack();
                NBTTagCompound nbt = new NBTTagCompound();
                item.writeToNBT(nbt);
                component = new TextComponentString(nbt.toString());
                break;
            }
        case SHOW_TEXT:
            component = SpongeTexts.toComponent((Text) action.getResult());
            break;
        default:
            throw new AssertionError();
    }
    HoverEvent event = new HoverEvent(type, component);
    ((IMixinHoverEvent) event).setHandle(action);
    return event;
}
Also used : HoverEvent(net.minecraft.util.text.event.HoverEvent) IMixinHoverEvent(org.spongepowered.common.interfaces.text.IMixinHoverEvent) ITextComponent(net.minecraft.util.text.ITextComponent) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Text(org.spongepowered.api.text.Text) IMixinHoverEvent(org.spongepowered.common.interfaces.text.IMixinHoverEvent) TextComponentString(net.minecraft.util.text.TextComponentString) HoverAction(org.spongepowered.api.text.action.HoverAction) SpongeEntityType(org.spongepowered.common.entity.SpongeEntityType) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ITextComponent (net.minecraft.util.text.ITextComponent)116 TextComponentString (net.minecraft.util.text.TextComponentString)53 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)43 EntityPlayer (net.minecraft.entity.player.EntityPlayer)20 ItemStack (net.minecraft.item.ItemStack)17 Style (net.minecraft.util.text.Style)17 ClickEvent (net.minecraft.util.text.event.ClickEvent)16 HoverEvent (net.minecraft.util.text.event.HoverEvent)9 ArrayList (java.util.ArrayList)8 BlockPos (net.minecraft.util.math.BlockPos)8 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)8 Minecraft (net.minecraft.client.Minecraft)5 StringTextComponent (net.minecraft.util.text.StringTextComponent)4 Text (org.spongepowered.api.text.Text)4 MagicBook (cavern.magic.MagicBook)3 SpecialMagic (cavern.magic.SpecialMagic)3 Matcher (java.util.regex.Matcher)3 TileEntity (net.minecraft.tileentity.TileEntity)3 SoundEvent (net.minecraft.util.SoundEvent)3 TextFormatting (net.minecraft.util.text.TextFormatting)3