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