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;
}
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;
}
Aggregations