Search in sources :

Example 1 with IMixinTextComponent

use of org.spongepowered.common.interfaces.text.IMixinTextComponent in project SpongeCommon by SpongePowered.

the class MixinHoverEvent method getHandle.

@Override
public HoverAction<?> getHandle() {
    if (!this.initialized) {
        try {
            // This is inefficient, but at least we only need to do it once
            switch(this.action) {
                case SHOW_TEXT:
                    setHandle(TextActions.showText(((IMixinTextComponent) this.value).toText()));
                    break;
                case SHOW_ITEM:
                    setHandle(TextActions.showItem(ItemStackUtil.snapshotOf(new net.minecraft.item.ItemStack(loadNbt()))));
                    break;
                case SHOW_ENTITY:
                    NBTTagCompound nbt = loadNbt();
                    String name = nbt.getString("name");
                    EntityType type = null;
                    if (nbt.hasKey("type", NbtDataUtil.TAG_STRING)) {
                        type = SpongeImpl.getGame().getRegistry().getType(EntityType.class, name).orElse(null);
                    }
                    UUID uniqueId = UUID.fromString(nbt.getString("id"));
                    setHandle(TextActions.showEntity(uniqueId, name, type));
                    break;
                default:
            }
        } finally {
            this.initialized = true;
        }
    }
    return this.handle;
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) IMixinTextComponent(org.spongepowered.common.interfaces.text.IMixinTextComponent) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) UUID(java.util.UUID)

Example 2 with IMixinTextComponent

use of org.spongepowered.common.interfaces.text.IMixinTextComponent 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)

Aggregations

IMixinTextComponent (org.spongepowered.common.interfaces.text.IMixinTextComponent)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 PrimitiveIterator (java.util.PrimitiveIterator)1 UUID (java.util.UUID)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 ITextComponent (net.minecraft.util.text.ITextComponent)1 TextComponentString (net.minecraft.util.text.TextComponentString)1 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)1 EntityType (org.spongepowered.api.entity.EntityType)1