Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev/patch' into dev/patch
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneBeee committed Jan 20, 2025
2 parents 25db3a8 + f2216dc commit b6bf491
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.bukkit.inventory.recipe.CraftingBookCategory;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -74,7 +72,7 @@ public static RecipeChoice getRecipeChoice(Object object) {
return getRecipeChoice(itemType.getRandom());
} else if (object instanceof RecipeChoice choice) {
return choice;
} else if (object instanceof Tag<?> tag && isMaterialTag(tag)) {
} else if (object instanceof Tag<?> tag && Util.isMaterialTag(tag)) {
return new MaterialChoice((Tag<Material>) tag);
}
return null;
Expand Down Expand Up @@ -251,22 +249,6 @@ private static String getFancy(RecipeChoice recipeChoice) {
return joiner.toString();
}

/**
* Check if a {@link Tag} is a Material Tag
*
* @param object Object to check
* @return True if material tag
*/
public static boolean isMaterialTag(Object object) {
if (object instanceof Tag<?> tag) {
ParameterizedType superC = (ParameterizedType) tag.getClass().getGenericSuperclass();
for (Type arg : superC.getActualTypeArguments()) {
if (arg.equals(Material.class)) return true;
}
}
return false;
}

/**
* Send an error to console prefixed with [Recipe]
*
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/shanebeestudios/skbee/api/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import com.shanebeestudios.skbee.SkBee;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -141,4 +146,32 @@ public static int[] uuidToIntArray(UUID uuid) {
return new int[]{(int) (most >> 32), (int) most, (int) (least >> 32), (int) least};
}

/**
* Check if a {@link Tag} is a {@link Material} Tag
*
* @param tag tag to check
* @return True if material tag
*/
public static boolean isMaterialTag(Tag<?> tag) {
ParameterizedType superC = (ParameterizedType) tag.getClass().getGenericSuperclass();
for (Type arg : superC.getActualTypeArguments()) {
if (arg.equals(Material.class)) return true;
}
return false;
}

/**
* Check if a {@link Tag} is an {@link EntityType} Tag
*
* @param tag tag to check
* @return True if EntityType tag
*/
public static boolean isEntityTypeTag(Tag<?> tag) {
ParameterizedType superC = (ParameterizedType) tag.getClass().getGenericSuperclass();
for (Type arg : superC.getActualTypeArguments()) {
if (arg.equals(EntityType.class)) return true;
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,63 @@
package com.shanebeestudios.skbee.elements.other.type;

import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.bukkitutil.EntityUtils;
import ch.njol.skript.entity.EntityData;
import com.shanebeestudios.skbee.api.util.Util;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.skriptlang.skript.lang.comparator.Comparators;
import org.skriptlang.skript.lang.comparator.Relation;

@SuppressWarnings("unchecked")
public class Comps {

static {
Comparators.registerComparator(NamespacedKey.class, String.class, (o1, o2) ->
Relation.get(o1.toString().equalsIgnoreCase(o2)));

if (!Comparators.exactComparatorExists(ItemType.class, Tag.class)) {
// Add this comparator until Skript adds it!
Comparators.registerComparator(ItemType.class, Tag.class, (itemType, tag) -> {
if (Util.isMaterialTag(tag)) {
return Relation.get(((Tag<Material>) tag).isTagged(itemType.getMaterial()));
}
return Relation.NOT_EQUAL;
});
}

if (!Comparators.exactComparatorExists(BlockData.class, Tag.class)) {
// Add this comparator until Skript adds it!
Comparators.registerComparator(BlockData.class, Tag.class, (blockData, tag) -> {
if (Util.isMaterialTag(tag)) {
return Relation.get(((Tag<Material>) tag).isTagged(blockData.getMaterial()));
}
return Relation.NOT_EQUAL;
});
}

if (!Comparators.exactComparatorExists(EntityData.class, Tag.class)) {
// Add this comparator until Skript adds it!
Comparators.registerComparator(EntityData.class, Tag.class, (entityData, tag) -> {
if (Util.isEntityTypeTag(tag)) {
EntityType bukkitEntityType = EntityUtils.toBukkitEntityType(entityData);
return Relation.get(((Tag<EntityType>) tag).isTagged(bukkitEntityType));
}
return Relation.NOT_EQUAL;
});
}

if (!Comparators.exactComparatorExists(EntityType.class, Tag.class)) {
Comparators.registerComparator(EntityType.class, Tag.class, (entityType, tag) -> {
if (Util.isEntityTypeTag(tag)) {
return Relation.get(((Tag<EntityType>) tag).isTagged(entityType));
}
return Relation.NOT_EQUAL;
});
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import com.shanebeestudios.skbee.api.recipe.RecipeUtil;
import com.shanebeestudios.skbee.api.util.Util;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.event.Event;
Expand Down Expand Up @@ -59,8 +59,8 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
if (!materials.contains(material) && material.isItem() && !material.isAir())
materials.add(material);
});
} else if (RecipeUtil.isMaterialTag(object)) {
MaterialChoice materialChoice = new MaterialChoice((Tag<Material>) object);
} else if (object instanceof Tag<?> tag && Util.isMaterialTag(tag)) {
MaterialChoice materialChoice = new MaterialChoice((Tag<Material>) tag);
materialChoice.getChoices().forEach(material -> {
if (!materials.contains(material)) materials.add(material);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import ch.njol.skript.registrations.Classes;
import ch.njol.util.StringUtils;
import com.shanebeestudios.skbee.api.recipe.RecipeType;
import com.shanebeestudios.skbee.api.recipe.RecipeUtil;
import com.shanebeestudios.skbee.api.util.Util;
import com.shanebeestudios.skbee.api.wrapper.EnumWrapper;
import org.bukkit.Material;
import org.bukkit.Tag;
Expand Down Expand Up @@ -80,7 +80,7 @@ public boolean canParse(ParseContext context) {
@SuppressWarnings("unchecked")
@Override
public @Nullable RecipeChoice convert(Tag from) {
if (RecipeUtil.isMaterialTag(from)) {
if (Util.isMaterialTag(from)) {
return new MaterialChoice((Tag<Material>) from);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@
"\t\tdefault -> 1 of stick",
"\tgive {_item} to attacker",})
@Since("3.8.0")
public class EffCaseReturn extends Effect {
public class EffCase extends Effect {

static {
Skript.registerEffect(EffCaseReturn.class,
Skript.registerEffect(EffCase.class,
"case %objects% -> <.+>",
"default -> <.+>");
}
Expand Down Expand Up @@ -160,7 +160,7 @@ protected void execute(Event event) {
protected @Nullable TriggerItem walk(Event event) {
if (event instanceof SwitchReturnEvent switchReturnEvent) {
if (this.defaultCase || SecCase.compare(this.caseObject.getArray(event), switchReturnEvent.getSwitchedObject())) {
Object returnObject = this.returnObject.getSingle(event);
Object returnObject = this.returnObject.getSingle(switchReturnEvent.getParentEvent());
if (returnObject != null) {
switchReturnEvent.setReturnedObject(returnObject);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@

@SuppressWarnings("UnstableApiUsage")
@Name("SwitchCase - Switched Value")
@Description("Represents the object that was 'switched' in a switch section/expression.")
@Examples({"function getBiome(b: biome) :: string:",
"\treturn switch return {_b}:",
"\t\tcase plains, sunflower plains, beach -> \"&a%switched value%\"",
"\t\tcase desert, savanna, badlands -> \"&e%switched value%\"",
"\t\tcase snowy beach, frozen peaks, grove -> \"&b%switched value%\"",
"\t\tdefault -> \"&7%switched value%\""})
@Description({"Represents the object that was 'switched' in a switch section/expression.",
"**DEPRECATED** - This is no longer needed (was originally a bandaid for an issue with event-values)."})
@Examples("")
@Since("3.8.0")
public class ExprSwitchedObject extends SimpleExpression<Object> {

Expand All @@ -40,6 +36,7 @@ public class ExprSwitchedObject extends SimpleExpression<Object> {

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
Skript.warning("Deprecated");
if (getParser().getCurrentStructure() instanceof SectionSkriptEvent skriptEvent) {
if (skriptEvent.getSection() instanceof SecSwitch secSwitch) {
this.switchSection = secSwitch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class SecCase extends Section implements ReturnHandler<Object> {
private Expression<Object> caseObject;
private boolean defaultCase;
private ReturnableTrigger<?> caseSection;
private Expression<?> returnObject;
private Object returnObject;

@SuppressWarnings("UnstableApiUsage")
@Override
Expand Down Expand Up @@ -116,12 +116,11 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
if (this.defaultCase || compare(this.caseObject.getArray(event), switchEvent.getSwitchedObject())) {
Trigger.walk(this.caseSection, switchEvent.getParentEvent());
if (event instanceof SwitchReturnEvent switchReturnEvent) {
Object returnObject = this.returnObject.getSingle(event);
if (returnObject != null) switchReturnEvent.setReturnedObject(returnObject);
if (this.returnObject != null) switchReturnEvent.setReturnedObject(this.returnObject);
}
// TODO somehow handle functions?!?!
return null;
} else if (getActualNext() instanceof SecCase) {
} else if (getActualNext() != null) {
return super.walk(event, false);
}
}
Expand Down Expand Up @@ -154,7 +153,7 @@ public static boolean compare(Object[] objects1, Object o2) {

@Override
public void returnValues(Event event, Expression<?> value) {
this.returnObject = value;
this.returnObject = value.getSingle(event);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.shanebeestudios.skbee.elements.switchcase.sections;

import ch.njol.skript.Skript;
import ch.njol.skript.config.Node;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
Expand Down Expand Up @@ -61,18 +62,29 @@ public class SecExprSwitchReturn extends SectionExpression<Object> {
}

private Expression<?> switchedObject;
private Trigger section;
private Trigger caseSection;

@SuppressWarnings({"unchecked", "DataFlowIssue"})
@Override
public boolean init(Expression<?>[] exprs, int pattern, Kleenean delayed, ParseResult result, @Nullable SectionNode node, @Nullable List<TriggerItem> triggerItems) {
public boolean init(Expression<?>[] exprs, int pattern, Kleenean delayed, ParseResult result, @Nullable SectionNode sectionNode, @Nullable List<TriggerItem> triggerItems) {
this.switchedObject = LiteralUtils.defendExpression(exprs[0]);

Class<? extends Event>[] currentEvents = getParser().getCurrentEvents();
Class<? extends Event>[] events = new Class[currentEvents.length + 1];
System.arraycopy(currentEvents, 0, events, 0, currentEvents.length);
events[currentEvents.length] = SwitchSecEvent.class;
this.section = loadCode(node, "switch section expression", null, events);
this.caseSection = loadCode(sectionNode, "switch section expression", null, events);

// Search through the section and see if the lines are cases
for (Node node : sectionNode) {
String key = node.getKey();
if (!key.startsWith("case") && !key.startsWith("default")) {
Skript.error("Only cases can be used in a switch section but found this:");
this.caseSection = null;
break;
}
}

return LiteralUtils.canInitSafely(this.switchedObject);
}

Expand All @@ -84,7 +96,7 @@ public boolean init(Expression<?>[] exprs, int pattern, Kleenean delayed, ParseR
Object variables = Variables.copyLocalVariables(event);
SwitchReturnEvent returnEvent = new SwitchReturnEvent(object, event);
Variables.setLocalVariables(returnEvent, variables);
Trigger.walk(this.section, returnEvent);
Trigger.walk(this.caseSection, returnEvent);
Variables.setLocalVariables(event, Variables.copyLocalVariables(returnEvent));

return new Object[]{returnEvent.getReturnedObject()};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.shanebeestudios.skbee.elements.switchcase.sections;

import ch.njol.skript.Skript;
import ch.njol.skript.config.Node;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
Expand Down Expand Up @@ -55,7 +56,6 @@ public class SecSwitch extends Section {

private Expression<?> switchedObject;
private Trigger caseSection;
public TriggerItem dirty;

@SuppressWarnings({"unchecked", "DataFlowIssue"})
@Override
Expand All @@ -68,9 +68,14 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

this.caseSection = loadCode(sectionNode, "switch case", events);

if (this.dirty != null) {
Skript.error("Only cases can be used in a switch section but found this: \n ==> '" + this.dirty + "'");
return false;
// Search through the section and see if the lines are cases
for (Node node : sectionNode) {
String key = node.getKey();
if (!key.startsWith("case") && !key.startsWith("default")) {
Skript.error("Only cases can be used in a switch section but found this:");
this.caseSection = null;
break;
}
}

return LiteralUtils.canInitSafely(this.switchedObject);
Expand Down
Loading

0 comments on commit b6bf491

Please sign in to comment.