Skip to content

Commit

Permalink
Merge pull request #666 from ShaneBeee/dev/patch
Browse files Browse the repository at this point in the history
Dev/Patch - future release
  • Loading branch information
ShaneBeee authored Jul 2, 2024
2 parents db861e4 + ca940d2 commit fef67e7
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public static ComponentWrapper fromRawText(String text) {
*/
@SuppressWarnings("NullableProblems")
public static ComponentWrapper fromMiniMessage(@NotNull String text, @Nullable TagResolver... resolvers) {
String string = text;
// Skript 2.9+ no longer requires double hash, so let's manage that
String string = text.replaceAll("##(\\w{6})", "#$1");
// MiniMessage doesn't like these
if (text.contains("&")) {
TextComponent deserialize = LegacyComponentSerializer.legacyAmpersand().deserialize(string);
Expand Down Expand Up @@ -167,6 +168,17 @@ public static ComponentWrapper fromTranslate(String translate, Object... objects
return new ComponentWrapper(Component.translatable(translate, comps));
}

/**
* Deserialize a json string into a component
*
* @param json Json string to deserialize
* @return Component from json string
*/
public static ComponentWrapper fromJson(String json) {
Component deserialize = JSONComponentSerializer.json().deserialize(json);
return new ComponentWrapper(deserialize);
}

@Nullable
private static Component getItem(Object object) {
ItemStack itemStack = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
NBTCompound compound = this.compound.getSingle(e);
if (tag == null || compound == null) return null;

return new NBTCustomType[]{NBTApi.getTagType(compound, tag)};
NBTCustomType tagType = NBTApi.getTagType(compound, tag);
if (tagType == NBTCustomType.NBTTagEnd) return null;
return new NBTCustomType[]{tagType};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,58 @@
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.TriggerSection;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.util.Kleenean;
import com.shanebeestudios.skbee.elements.other.sections.SecRunTaskLater;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.scheduler.BukkitScheduler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

@Name("Task - Cancel Task")
@Description("Stop tasks.")
@Description({"Stop tasks.",
"`stop all tasks` = Will stop all currently running tasks created with a task section.",
"`stop current task` = Will stop the task section this effect is in.",
"`stop task with id` = Will stop any task from an ID."})
@Examples({"run 0 ticks later repeating every second:",
"\tset {-id} to current task id",
"\tadd 1 to {_a}",
"\tif {_a} > 10:",
"\t\tstop current task",
"",
"on unload:",
"\tstop all tasks",
"",
"on break:",
"\tstop task with id {-id}"})
"\tset {-id} to current task id",
"\tadd 1 to {_a}",
"\tif {_a} > 10:",
"\t\tstop current task",
"",
"on unload:",
"\tstop all tasks",
"",
"on break:",
"\tstop task with id {-id}"})
@Since("3.3.0")
public class EffTaskStop extends Effect {

private static final BukkitScheduler SCHEDULER = Bukkit.getScheduler();

static {
Skript.registerEffect(EffTaskStop.class,
"stop all tasks", "stop current task", "stop task[s] with id[s] %numbers%");
"(stop|cancel) all tasks", "(stop|cancel) current task", "(stop|cancel) task[s] with id[s] %numbers%");
}

private int pattern;
private Expression<Number> ids;
private SecRunTaskLater currentTask;

@SuppressWarnings({"NullableProblems", "unchecked"})
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
this.pattern = matchedPattern;
if (matchedPattern == 2) {
this.ids = (Expression<Number>) exprs[0];
}
if (matchedPattern == 1) {
for (TriggerSection currentSection : ParserInstance.get().getCurrentSections()) {
if (currentSection instanceof SecRunTaskLater) return true;
} else if (matchedPattern == 1) {
List<SecRunTaskLater> currentSections = getParser().getCurrentSections(SecRunTaskLater.class);
if (currentSections.isEmpty()) {
Skript.error("'" + parseResult.expr + "' can only be used in a run task section.");
return false;
}
Skript.error("'stop current task' can only be used in a run task section.");
return false;
this.currentTask = currentSections.getLast();
}
return true;
}
Expand All @@ -66,16 +69,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
protected void execute(Event event) {
switch (this.pattern) {
case 0 -> SecRunTaskLater.cancelTasks();
case 1 -> {
TriggerSection parent = getParent();
while (parent != null) {
if (parent instanceof SecRunTaskLater runTaskLater) {
runTaskLater.stopCurrentTask();
return;
}
parent = parent.getParent();
}
}
case 1 -> this.currentTask.stopCurrentTask();
case 2 -> {
for (Number id : this.ids.getArray(event)) {
SCHEDULER.cancelTask(id.intValue());
Expand All @@ -84,9 +78,8 @@ protected void execute(Event event) {
}
}

@SuppressWarnings("DataFlowIssue")
@Override
public @NotNull String toString(@Nullable Event e, boolean d) {
public @NotNull String toString(Event e, boolean d) {
return switch (this.pattern) {
case 1 -> "stop current task";
case 2 -> "stop task[s] with id[s] " + this.ids.toString(e, d);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ExprSuspiciousBlock extends SimplePropertyExpression<Block, ItemTyp
}
}

@SuppressWarnings({"deprecation"})
@SuppressWarnings({"deprecation", "removal"})
@Override
public @Nullable ItemType convert(Block block) {
BlockState state = block.getState();
Expand All @@ -57,7 +57,7 @@ public class ExprSuspiciousBlock extends SimplePropertyExpression<Block, ItemTyp
return null;
}

@SuppressWarnings({"NullableProblems", "ConstantValue", "deprecation"})
@SuppressWarnings({"NullableProblems", "ConstantValue", "deprecation", "removal"})
@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
ItemStack itemStack = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,49 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import com.shanebeestudios.skbee.elements.other.sections.SecRunTaskLater;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

@Name("Task - ID")
@Description({"Get the current task ID. This will only be tasks created with the task section.",
"THIS IS BROKEN... DO NOT USE!"})
@Description({"Get the current task ID. This will only be tasks created with the task section."})
@Examples({"set {_id} to current task id",
"",
"run 0 ticks later repeating every second:",
"\tset {-id} to current task id",
"\tadd 1 to {_a}",
"\tif {_a} > 10:",
"\t\tstop current task",
"",
"on unload:",
"\tstop all tasks",
"",
"on break:",
"\tstop task with id {-id}"})
"",
"run 0 ticks later repeating every second:",
"\tset {-id} to current task id",
"\tadd 1 to {_a}",
"\tif {_a} > 10:",
"\t\tstop current task",
"",
"on unload:",
"\tstop all tasks",
"",
"on break:",
"\tstop task with id {-id}"})
@Since("3.3.0")
public class ExprTaskID extends SimpleExpression<Number> {

static {
Skript.registerExpression(ExprTaskID.class, Number.class, ExpressionType.SIMPLE, "current task id");
}

private SecRunTaskLater task;

@SuppressWarnings("NullableProblems")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
Skript.error("'" + parseResult.expr + "' is currently broken!");
return false;
List<SecRunTaskLater> currentSections = getParser().getCurrentSections(SecRunTaskLater.class);
this.task = currentSections.getLast();
return true;
}

@SuppressWarnings("NullableProblems")
@Override
protected Number @Nullable [] get(Event event) {
return null;
return new Number[]{this.task.getCurrentTaskId()};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ public void stopCurrentTask() {
Bukkit.getScheduler().cancelTask(this.currentTaskId);
}

public int getCurrentTaskId() {
return this.currentTaskId;
}

@SuppressWarnings("DataFlowIssue")
@Override
public @NotNull String toString(@Nullable Event e, boolean d) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,41 @@
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

@Name("Text Component - New Text Component")
@Description({"Create a new text component. Can have hover and click events added to it. You can also create a translate component, ",
"this will send to the client, and the client will translate based on their language. You can use either an item type or a ",
"translate string, you can find these in your Minecraft jar 'assets/minecraft/lang/<lang file>.json'.",
"As of Paper 1.17.1, several more objects can translate including GameRules, PotionEffectTypes, Attributes, Difficulty, Enchantments, ",
"FireworkEffectTypes, Entities and Blocks. KeyBind components will be replaced with the actual key the client is using.",
"Some components have extra objects, you can use strings or other text components here."})
@Name("TextComponent - New Text Component")
@Description({"Create a new text component. Can add hover/click events to it.",
"",
"**Types:**",
"Text: Just a plain old text component from a string.",
"Rawtext: Same as text, but color codes will be visible.",
"Keybind: Will use Minecraft's keybind system.",
"Translate: Will use Minecraft's lang file keys. ",
" - You can find these in your Minecraft jar 'assets/minecraft/lang/<lang file>.json'.",
" - Also supports getting translations for objects such as ItemTypes, Entities and PotionEffectTypes.",
" - When sent to the client, the client will translate based on the lang they've picked.",
" - Some lang file entries take in other objects, thats what the optional `using %objects%` is for.",
"Json: Will deserialize a json string back into a component.",
" - Minecraft stores components in NBT as json components (ex: name of a held item)."})
@Examples({"set {_comp::1} to text component from \"hi player \"",
"set {_comp::2} to text component of \"hover over me for a special message!\"",
"add hover event showing \"OoO look ma I'm hovering!\" to {_comp::2}",
"send component {_comp::*} to player", "",
"set {_t} to translate component from player's tool",
"set {_t} to translate component from \"item.minecraft.milk_bucket\"",
"set {_death} to translate component from \"death.fell.accident.ladder\" using player's name",
"set {_assist} to translate component from \"death.fell.assist\" using victim's name and attacker's name",
"set {_key} to keybind component of \"key.jump\""})
"set {_comp::2} to text component of \"hover over me for a special message!\"",
"add hover event showing \"OoO look ma I'm hovering!\" to {_comp::2}",
"send component {_comp::*} to player", "",
"set {_t} to translate component from player's tool",
"set {_t} to translate component from \"item.minecraft.milk_bucket\"",
"set {_death} to translate component from \"death.fell.accident.ladder\" using player's name",
"set {_assist} to translate component from \"death.fell.assist\" using victim's name and attacker's name",
"set {_key} to keybind component of \"key.jump\"",
"set {_name} to json component from (string tag \"custom_name\" of nbt of target block)"})
@Since("1.5.0")
public class ExprTextComponent extends SimpleExpression<ComponentWrapper> {
static {
Skript.registerExpression(ExprTextComponent.class, ComponentWrapper.class, ExpressionType.COMBINED,
"[a] [new] (text|:rawtext) component[s] (from|of) %strings%",
"[a] [new] key[ ]bind component[s] (from|of) %strings%",
"[a] [new] translate component[s] (from|of) %objects% [(with|using) %-objects%]");
"[a] [new] (text|:rawtext) component[s] (from|of) %strings%",
"[a] [new] key[ ]bind component[s] (from|of) %strings%",
"[a] [new] translate component[s] (from|of) %objects% [(with|using) %-objects%]",
"[a] [new] json component (from|of) %strings%");
}

private int pattern;
Expand All @@ -52,14 +60,14 @@ public class ExprTextComponent extends SimpleExpression<ComponentWrapper> {

@Override
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
pattern = matchedPattern;
translation = LiteralUtils.defendExpression(exprs[0]);
objects = pattern == 2 ? LiteralUtils.defendExpression(exprs[1]) : null;
raw = parseResult.hasTag("rawtext");
if (objects != null) {
return LiteralUtils.canInitSafely(translation, objects);
this.pattern = matchedPattern;
this.translation = LiteralUtils.defendExpression(exprs[0]);
this.objects = pattern == 2 ? LiteralUtils.defendExpression(exprs[1]) : null;
this.raw = parseResult.hasTag("rawtext");
if (this.objects != null) {
return LiteralUtils.canInitSafely(this.translation, this.objects);
}
return LiteralUtils.canInitSafely(translation);
return LiteralUtils.canInitSafely(this.translation);
}

@SuppressWarnings("NullableProblems")
Expand All @@ -68,21 +76,22 @@ protected ComponentWrapper[] get(@NotNull Event e) {
List<ComponentWrapper> components = new ArrayList<>();

for (Object object : this.translation.getArray(e)) {
if (pattern == 0) {
if (raw) components.add(ComponentWrapper.fromRawText((String) object));
if (this.pattern == 0) {
if (this.raw) components.add(ComponentWrapper.fromRawText((String) object));
else components.add(ComponentWrapper.fromText((String) object));
} else if (pattern == 1) {
} else if (this.pattern == 1) {
components.add(ComponentWrapper.fromKeybind((String) object));
} else if (pattern == 2) {
} else if (this.pattern == 2) {
String translate = ChatUtil.getTranslation(object);
if (translate != null) {
if (this.objects != null) {
components.add(ComponentWrapper.fromTranslate(translate, this.objects.getArray(e)));
} else {
components.add(ComponentWrapper.fromTranslate(translate));
}

}
} else if (this.pattern == 3) {
components.add(ComponentWrapper.fromJson((String) object));
}
}
return components.toArray(new ComponentWrapper[0]);
Expand All @@ -98,12 +107,16 @@ public boolean isSingle() {
return ComponentWrapper.class;
}

@SuppressWarnings("DataFlowIssue")
@Override
public @NotNull String toString(@Nullable Event e, boolean d) {
String comp = pattern == 0 ? "text" : pattern == 1 ? "keybind" : "translate";
String trans = translation.toString(e, d);
String obj = objects != null ? "using " + objects.toString(e, d) : "";
public @NotNull String toString(Event e, boolean d) {
String comp = switch (this.pattern) {
case 1 -> "keybind";
case 2 -> "translate";
case 3 -> "json";
default -> this.raw ? "rawtext" : "text";
};
String trans = this.translation.toString(e, d);
String obj = this.objects != null ? "using " + this.objects.toString(e, d) : "";
return String.format("a new %s component from %s %s", comp, trans, obj);
}

Expand Down

0 comments on commit fef67e7

Please sign in to comment.