Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Add FTT.with_color and .with_styles
Browse files Browse the repository at this point in the history
Also added method for getting catalog types without needing to put "minecraft:" or "sponge:" as a prefix.
  • Loading branch information
Xenmai committed Feb 2, 2018
1 parent 254f782 commit 688527c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 35 deletions.
Expand Up @@ -6,12 +6,10 @@
import com.denizenscript.denizen2core.tags.objects.IntegerTag;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import org.spongepowered.api.Sponge;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.effect.particle.ParticleEffect;
import org.spongepowered.api.effect.particle.ParticleType;

import java.util.Optional;

public class PlayEffectCommand extends AbstractCommand {

// <--[explanation]
Expand Down Expand Up @@ -43,7 +41,7 @@ public class PlayEffectCommand extends AbstractCommand {
// TODO: Explain more!
// @Example
// # This example plays the 'heart' effect around the player.
// - playeffect <player.location> minecraft:heart --count 50 --offset 1,1,1
// - playeffect <player.location> heart --count 50 --offset 1,1,1
// -->

@Override
Expand Down Expand Up @@ -71,12 +69,12 @@ public void execute(CommandQueue queue, CommandEntry entry) {
LocationTag loc = LocationTag.getFor(queue.error, entry.getArgumentObject(queue, 0));
String effectName = entry.getArgumentObject(queue, 1).toString();
ParticleEffect.Builder build = ParticleEffect.builder();
Optional<ParticleType> type = Sponge.getRegistry().getType(ParticleType.class, effectName);
if (!type.isPresent()) {
Object type = Utilities.getTypeWithDefaultPrefix(ParticleType.class, effectName);
if (type == null) {
queue.handleError(entry, "Invalid particle effect type: '" + effectName + "'!");
return;
}
build.type(type.get());
build.type((ParticleType) type);
if (entry.namedArgs.containsKey("count")) {
IntegerTag count = IntegerTag.getFor(queue.error, entry.getNamedArgumentObject(queue, "count"));
build.quantity((int) count.getInternal());
Expand All @@ -99,7 +97,7 @@ public void execute(CommandQueue queue, CommandEntry entry) {
}
if (queue.shouldShowGood()) {
queue.outGood("Successfully played the particle effect of type '" +
ColorSet.emphasis + type.get().getId() + ColorSet.good + "' at location " +
ColorSet.emphasis + ((ParticleType) type).getId() + ColorSet.good + "' at location " +
ColorSet.emphasis + loc.debug() + ColorSet.good + "!");
}
}
Expand Down
Expand Up @@ -3,21 +3,25 @@
import com.denizenscript.denizen2core.tags.AbstractTagBase;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.tags.TagData;
import com.denizenscript.denizen2core.tags.objects.*;
import com.denizenscript.denizen2core.tags.objects.ListTag;
import com.denizenscript.denizen2core.tags.objects.MapTag;
import com.denizenscript.denizen2core.tags.objects.TextTag;
import com.denizenscript.denizen2core.utilities.CoreUtilities;
import com.denizenscript.denizen2core.utilities.Function2;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.tags.objects.FormattedTextTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.action.TextActions;
import org.spongepowered.api.text.format.TextColor;
import org.spongepowered.api.text.format.TextStyles;
import org.spongepowered.api.text.format.TextStyle;
import org.spongepowered.api.text.serializer.TextSerializers;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Optional;

public class TextsTagBase extends AbstractTagBase {

Expand Down Expand Up @@ -67,45 +71,32 @@ public String getName() {
// text:hello -> the base text will be 'hello'.
// color:blue -> the color will be blue.
// style:italics|bold -> the style will be bold-italic. Also allowed: obfuscated, reset, underline, strike.
// hover_text:<FormattedTextTag> -> hovering over the the text will display 'hello_world'
// hover_text:<FormattedTextTag> -> hovering over the text will display 'hello_world'.
// click_type:suggest -> clicking will suggest a command. Also allowed: execute, open_url.
// click_data:/dance -> clicking will use the command '/dance'.
// @Example <texts.for_input[text:hello|color:blue|style:<escape[bold|italics]>|hover_text:<texts.for_plain[hi]>|click_type:suggest|click_data:/dance]>
// -->
handlers.put("for_input", (dat, obj) -> {
MapTag map = MapTag.getFor(dat.error, dat.getNextModifier());
if (!map.getInternal().containsKey("text")) {
dat.error.run("Missing TEXT setter in for_input tag, cannot created FormattedTextTag!");
dat.error.run("Missing TEXT setter in for_input tag, cannot create FormattedTextTag!");
}
Text.Builder build = Text.builder(map.getInternal().get("text").toString());
if (map.getInternal().containsKey("color")) {
build.color(Sponge.getRegistry().getType(TextColor.class, map.getInternal().get("color").toString().toUpperCase()).get());
Optional<TextColor> color = Sponge.getRegistry().getType(TextColor.class, map.getInternal().get("color").toString());
if (!color.isPresent()) {
dat.error.run("The color specified in for_input tag is invalid, cannot create FormattedTextTag!");
}
build.color(color.get());
}
if (map.getInternal().containsKey("style")) {
ListTag reqs = ListTag.getFor(dat.error, map.getInternal().get("style"));
boolean bold = false;
boolean italics = false;
boolean obfu = false;
for (AbstractTagObject ato : reqs.getInternal()) {
String str = ato.toString();
if (str.equals("bold")) {
build.style(TextStyles.BOLD);
}
else if (str.equals("italics")) {
build.style(TextStyles.ITALIC);
}
else if (str.equals("obfuscated")) {
build.style(TextStyles.OBFUSCATED);
}
else if (str.equals("reset")) {
build.style(TextStyles.RESET);
}
else if (str.equals("underline")) {
build.style(TextStyles.UNDERLINE);
}
else if (str.equals("strike")) {
build.style(TextStyles.STRIKETHROUGH);
Object style = Utilities.getTypeWithDefaultPrefix(TextStyle.Base.class, ato.toString());
if (style == null) {
dat.error.run("The style specified in for_input tag is invalid, cannot create FormattedTextTag!");
}
build.style((TextStyle.Base) style);
}
}
if (map.getInternal().containsKey("hover_text")) {
Expand Down
Expand Up @@ -2,15 +2,21 @@

import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.tags.TagData;
import com.denizenscript.denizen2core.tags.objects.ListTag;
import com.denizenscript.denizen2core.tags.objects.TextTag;
import com.denizenscript.denizen2core.utilities.Action;
import com.denizenscript.denizen2core.utilities.Function2;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColor;
import org.spongepowered.api.text.format.TextStyle;
import org.spongepowered.api.text.serializer.TextParseException;
import org.spongepowered.api.text.serializer.TextSerializers;

import java.util.HashMap;
import java.util.Optional;

public class FormattedTextTag extends AbstractTagObject {

Expand Down Expand Up @@ -40,7 +46,7 @@ public Text getInternal() {
// @Name FormattedTextTag.append[<FormattedTextTag>]
// @Updated 2016/09/21
// @Group Modification
// @ReturnType TextTag
// @ReturnType FormattedTextTag
// @Returns the text followed by another piece of text.
// -->
handlers.put("append", (dat, obj) -> new FormattedTextTag(Text.join(((FormattedTextTag) obj).internal,
Expand All @@ -64,6 +70,43 @@ public Text getInternal() {
// @Returns a plain version of this formatted text.
// -->
handlers.put("plain", (dat, obj) -> new TextTag(((FormattedTextTag) obj).internal.toPlain()));
// <--[tag]
// @Since 0.4.0
// @Name FormattedTextTag.with_color[<TextTag>]
// @Updated 2018/02/01
// @Group Modification
// @ReturnType FormattedTextTag
// @Returns the text with the specified color applied to it.
// -->
handlers.put("with_color", (dat, obj) -> {
Text.Builder build = ((FormattedTextTag) obj).internal.toBuilder();
Optional<TextColor> color = Sponge.getRegistry().getType(TextColor.class, dat.getNextModifier().toString());
if (!color.isPresent()) {
dat.error.run("The color specified in with_color tag is invalid, cannot be applied to FormattedTextTag!");
}
build.color(color.get());
return new FormattedTextTag(build.build());
});
// <--[tag]
// @Since 0.4.0
// @Name FormattedTextTag.with_styles[<ListTag>]
// @Updated 2018/02/01
// @Group Modification
// @ReturnType FormattedTextTag
// @Returns the text with the specified styles applied to it.
// -->
handlers.put("with_styles", (dat, obj) -> {
Text.Builder build = ((FormattedTextTag) obj).internal.toBuilder();
ListTag styles = ListTag.getFor(dat.error, dat.getNextModifier());
for (AbstractTagObject ato : styles.getInternal()) {
Object style = Utilities.getTypeWithDefaultPrefix(TextStyle.Base.class, ato.toString());
if (style == null) {
dat.error.run("The style specified in with_styles tag is invalid, cannot be applied to FormattedTextTag!");
}
build.style((TextStyle.Base) style);
}
return new FormattedTextTag(build.build());
});
}

public static FormattedTextTag getFor(Action<String> error, String text) {
Expand Down
Expand Up @@ -4,12 +4,14 @@
import com.denizenscript.denizen2core.tags.objects.TimeTag;
import com.denizenscript.denizen2core.utilities.Action;
import com.denizenscript.denizen2core.utilities.CoreUtilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Optional;

public class Utilities {

Expand Down Expand Up @@ -40,4 +42,17 @@ public static boolean flagIsValidAndNotExpired(Action<String> error, MapTag flag
public static String getIdWithoutDefaultPrefix(String id) {
return id.startsWith("minecraft:") ? id.substring("minecraft:".length()) : id;
}

public static Object getTypeWithDefaultPrefix(Class clazz, String name) {
Optional<?> opt = Sponge.getRegistry().getType(clazz, name);
if (opt.isPresent()) {
return opt.get();
}
opt = Sponge.getRegistry().getType(clazz, "minecraft:" + name);
if (opt.isPresent()) {
return opt.get();
}
opt = Sponge.getRegistry().getType(clazz, "sponge:" + name);
return opt.orElse(null);
}
}

0 comments on commit 688527c

Please sign in to comment.