Search in sources :

Example 1 with HoverAction

use of org.spongepowered.api.text.action.HoverAction in project LanternServer by LanternPowered.

the class LanternTextHelper method raw.

public static RawAction raw(HoverAction<?> hoverAction) {
    if (hoverAction instanceof HoverAction.ShowText) {
        return new RawAction("show_text", ((HoverAction.ShowText) hoverAction).getResult());
    } else if (hoverAction instanceof HoverAction.ShowEntity) {
        final HoverAction.ShowEntity.Ref ref = ((HoverAction.ShowEntity) hoverAction).getResult();
        final DataContainer dataContainer = DataContainer.createNew().set(SHOW_ENTITY_ID, ref.getUniqueId().toString()).set(SHOW_ENTITY_NAME, ref.getName());
        ref.getType().ifPresent(type -> dataContainer.set(SHOW_ENTITY_TYPE, type.getId()));
        try {
            return new RawAction("show_entity", JsonDataFormat.writeAsString(dataContainer));
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    } else if (hoverAction instanceof HoverAction.ShowItem) {
        final ItemStackSnapshot itemStackSnapshot = ((HoverAction.ShowItem) hoverAction).getResult();
        final LanternItemStack itemStack = (LanternItemStack) itemStackSnapshot.createStack();
        final DataView dataView = ItemStackStore.INSTANCE.serialize(itemStack);
        try {
            return new RawAction("show_item", JsonDataFormat.writeAsString(dataView));
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    } else {
        throw new IllegalArgumentException("Unknown hover action type: " + hoverAction.getClass().getName());
    }
}
Also used : JsonParseException(com.google.gson.JsonParseException) ClickAction(org.spongepowered.api.text.action.ClickAction) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStackStore(org.lanternpowered.server.data.io.store.item.ItemStackStore) DataQuery(org.spongepowered.api.data.DataQuery) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Matcher(java.util.regex.Matcher) Text(org.spongepowered.api.text.Text) Gson(com.google.gson.Gson) HoverAction(org.spongepowered.api.text.action.HoverAction) URI(java.net.URI) LanternClickActionCallbacks(org.lanternpowered.server.text.action.LanternClickActionCallbacks) Nullable(javax.annotation.Nullable) TextActions(org.spongepowered.api.text.action.TextActions) CommandSource(org.spongepowered.api.command.CommandSource) MalformedURLException(java.net.MalformedURLException) Sponge(org.spongepowered.api.Sponge) DataContainer(org.spongepowered.api.data.DataContainer) IOException(java.io.IOException) UUID(java.util.UUID) JsonDataFormat(org.lanternpowered.server.data.persistence.json.JsonDataFormat) File(java.io.File) Consumer(java.util.function.Consumer) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) DataView(org.spongepowered.api.data.DataView) Coerce(org.spongepowered.api.util.Coerce) EntityType(org.spongepowered.api.entity.EntityType) Optional(java.util.Optional) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) IOException(java.io.IOException) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) HoverAction(org.spongepowered.api.text.action.HoverAction) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

Example 2 with HoverAction

use of org.spongepowered.api.text.action.HoverAction 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)

Example 3 with HoverAction

use of org.spongepowered.api.text.action.HoverAction in project SpongeAPI by SpongePowered.

the class Text method of.

/**
 * Builds a {@link Text} from a given array of objects.
 *
 * <p>For instance, you can use this like
 * <code>Text.of(TextColors.DARK_AQUA, "Hi", TextColors.AQUA, "Bye")</code>
 * </p>
 *
 * <p>This will create the correct {@link Text} instance if the input object
 * is the input for one of the {@link Text} types or convert the object to a
 * string otherwise.</p>
 *
 * <p>For instances of type {@link TextRepresentable} (e.g. {@link Text},
 * {@link Builder}, ...) the formatting of appended text has priority over
 * the current formatting in the method, e.g. the following results in a
 * green, then yellow and at the end green again {@link Text}:</p>
 *
 * <code>Text.of(TextColors.GREEN, "Hello ", Text.of(TextColors.YELLOW,
 * "Spongie"), '!');</code>
 *
 * @param objects The object array
 * @return The built text object
 */
public static Text of(Object... objects) {
    // Shortcut for lonely TextRepresentables
    if (objects.length == 1 && objects[0] instanceof TextRepresentable) {
        return ((TextRepresentable) objects[0]).toText();
    }
    final Text.Builder builder = builder();
    TextFormat format = TextFormat.NONE;
    HoverAction<?> hoverAction = null;
    ClickAction<?> clickAction = null;
    ShiftClickAction<?> shiftClickAction = null;
    boolean changedFormat = false;
    for (Object obj : objects) {
        // Text formatting + actions
        if (obj instanceof TextFormat) {
            changedFormat = true;
            format = (TextFormat) obj;
        } else if (obj instanceof TextColor) {
            changedFormat = true;
            format = format.color((TextColor) obj);
        } else if (obj instanceof TextStyle) {
            changedFormat = true;
            format = format.style(obj.equals(TextStyles.RESET) ? TextStyles.NONE : format.getStyle().and((TextStyle) obj));
        } else if (obj instanceof TextAction) {
            changedFormat = true;
            if (obj instanceof HoverAction) {
                hoverAction = (HoverAction<?>) obj;
            } else if (obj instanceof ClickAction) {
                clickAction = (ClickAction<?>) obj;
            } else if (obj instanceof ShiftClickAction) {
                shiftClickAction = (ShiftClickAction<?>) obj;
            } else {
            // Unsupported TextAction
            }
        } else if (obj instanceof TextRepresentable) {
            // Special content
            changedFormat = false;
            Text.Builder childBuilder = ((TextRepresentable) obj).toText().toBuilder();
            // Merge format (existing format has priority)
            childBuilder.format(format.merge(childBuilder.format));
            // Overwrite text actions if *NOT* present
            if (childBuilder.clickAction == null) {
                childBuilder.clickAction = clickAction;
            }
            if (childBuilder.hoverAction == null) {
                childBuilder.hoverAction = hoverAction;
            }
            if (childBuilder.shiftClickAction == null) {
                childBuilder.shiftClickAction = shiftClickAction;
            }
            builder.append(childBuilder.build());
        } else {
            // Simple content
            changedFormat = false;
            Text.Builder childBuilder;
            if (obj instanceof String) {
                childBuilder = builder((String) obj);
            } else if (obj instanceof Translation) {
                childBuilder = builder((Translation) obj);
            } else if (obj instanceof Translatable) {
                childBuilder = builder(((Translatable) obj).getTranslation());
            } else if (obj instanceof Selector) {
                childBuilder = builder((Selector) obj);
            } else if (obj instanceof Score) {
                childBuilder = builder((Score) obj);
            } else {
                childBuilder = builder(String.valueOf(obj));
            }
            if (hoverAction != null) {
                childBuilder.onHover(hoverAction);
            }
            if (clickAction != null) {
                childBuilder.onClick(clickAction);
            }
            if (shiftClickAction != null) {
                childBuilder.onShiftClick(shiftClickAction);
            }
            builder.append(childBuilder.format(format).build());
        }
    }
    if (changedFormat) {
        // Did the formatting change without being applied to something?
        // Then just append an empty text with that formatting
        final Text.Builder childBuilder = builder();
        if (hoverAction != null) {
            childBuilder.onHover(hoverAction);
        }
        if (clickAction != null) {
            childBuilder.onClick(clickAction);
        }
        if (shiftClickAction != null) {
            childBuilder.onShiftClick(shiftClickAction);
        }
        builder.append(childBuilder.format(format).build());
    }
    if (builder.children.size() == 1) {
        // Single content, reduce Text depth
        return builder.children.get(0);
    }
    return builder.build();
}
Also used : Translation(org.spongepowered.api.text.translation.Translation) Translatable(org.spongepowered.api.text.translation.Translatable) TextAction(org.spongepowered.api.text.action.TextAction) Score(org.spongepowered.api.scoreboard.Score) HoverAction(org.spongepowered.api.text.action.HoverAction) TextStyle(org.spongepowered.api.text.format.TextStyle) ClickAction(org.spongepowered.api.text.action.ClickAction) ShiftClickAction(org.spongepowered.api.text.action.ShiftClickAction) ShiftClickAction(org.spongepowered.api.text.action.ShiftClickAction) TextFormat(org.spongepowered.api.text.format.TextFormat) TextColor(org.spongepowered.api.text.format.TextColor) Selector(org.spongepowered.api.text.selector.Selector)

Aggregations

HoverAction (org.spongepowered.api.text.action.HoverAction)3 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)2 Text (org.spongepowered.api.text.Text)2 ClickAction (org.spongepowered.api.text.action.ClickAction)2 Gson (com.google.gson.Gson)1 JsonParseException (com.google.gson.JsonParseException)1 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 Consumer (java.util.function.Consumer)1 Matcher (java.util.regex.Matcher)1 Nullable (javax.annotation.Nullable)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 ITextComponent (net.minecraft.util.text.ITextComponent)1