mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-23 00:49:20 +00:00
Merge pull request #325 from Catnies/dev-sk
修复Skript部分表达式在新版本冲突, 修改部分表达式;
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
package net.momirealms.craftengine.bukkit.compatibility.skript;
|
||||
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.clazz.CraftEngineClasses;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.condition.CondIsCraftEngineHasBeenLoad;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.condition.CondIsCustomBlock;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.condition.CondIsCustomItem;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.condition.CondIsFurniture;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.effect.EffPlaceCustomBlock;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.effect.EffPlaceFurniture;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.effect.EffRemoveFurniture;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.event.EvtCraftEngineReload;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.event.EvtCustomBlock;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.event.EvtCustomClick;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.event.EvtCustomFurniture;
|
||||
@@ -16,9 +18,11 @@ public class SkriptHook {
|
||||
|
||||
public static void register() {
|
||||
CraftEngineClasses.register();
|
||||
EvtCraftEngineReload.register();
|
||||
EvtCustomBlock.register();
|
||||
EvtCustomFurniture.register();
|
||||
EvtCustomClick.register();
|
||||
CondIsCraftEngineHasBeenLoad.register();
|
||||
CondIsCustomBlock.register();
|
||||
CondIsFurniture.register();
|
||||
CondIsCustomItem.register();
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package net.momirealms.craftengine.bukkit.compatibility.skript.condition;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.doc.Description;
|
||||
import ch.njol.skript.doc.Name;
|
||||
import ch.njol.skript.doc.Since;
|
||||
import ch.njol.skript.lang.Condition;
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.util.Kleenean;
|
||||
import net.momirealms.craftengine.bukkit.compatibility.skript.event.EvtCraftEngineReload;
|
||||
import org.bukkit.event.Event;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@Name("CraftEngine has been load")
|
||||
@Description({"Checks CraftEngine has been load."})
|
||||
@Since("1.0")
|
||||
public class CondIsCraftEngineHasBeenLoad extends Condition {
|
||||
|
||||
public static void register() {
|
||||
Skript.registerCondition(CondIsCraftEngineHasBeenLoad.class,
|
||||
"(ce|craft-engine) has been load[ed]",
|
||||
"(ce|craft-engine) has not been load[ed] [yet]"
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
|
||||
setNegated(matchedPattern == 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(Event event) {
|
||||
boolean beenLoad = EvtCraftEngineReload.hasBeenLoad();
|
||||
return isNegated() ? !beenLoad : beenLoad;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(@Nullable Event event, boolean debug) {
|
||||
return "craft-engine has " + (isNegated() ? "not " : "") + "been loaded";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -15,8 +15,8 @@ public class CondIsCustomBlock extends Condition {
|
||||
|
||||
public static void register() {
|
||||
Skript.registerCondition(CondIsCustomBlock.class,
|
||||
"%blocks% (is|are) custom block(s)",
|
||||
"%blocks% (is|are)(n't| not) custom block(s)");
|
||||
"%blocks% (is|are) (custom|ce|craft-engine) block(s)",
|
||||
"%blocks% (is|are)(n't| not) (custom|ce|craft-engine) block(s)");
|
||||
}
|
||||
|
||||
private Expression<Block> blocks;
|
||||
|
||||
@@ -1,41 +1,65 @@
|
||||
package net.momirealms.craftengine.bukkit.compatibility.skript.condition;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.aliases.ItemType;
|
||||
import ch.njol.skript.conditions.base.PropertyCondition;
|
||||
import ch.njol.skript.doc.Description;
|
||||
import ch.njol.skript.doc.Name;
|
||||
import ch.njol.skript.doc.Since;
|
||||
import ch.njol.skript.lang.Condition;
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.skript.util.slot.Slot;
|
||||
import ch.njol.util.Kleenean;
|
||||
import net.momirealms.craftengine.bukkit.api.CraftEngineItems;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@Name("Is CraftEngine Item")
|
||||
@Description({"Checks if the Item is CraftEngine item."})
|
||||
@Since("1.0")
|
||||
public class CondIsCustomItem extends Condition {
|
||||
|
||||
public static void register() {
|
||||
Skript.registerCondition(CondIsCustomItem.class,
|
||||
"%itemstacks% (is|are) custom item(s)",
|
||||
"%itemstacks% (is|are)(n't| not) custom item(s)");
|
||||
"%itemstack/itemtype/slot% (is [a[n]]|are) (custom|ce|craft-engine) item[s]",
|
||||
"%itemstack/itemtype/slot% (isn't|is not|aren't|are not) [a[n]] (custom|ce|craft-engine) item[s]"
|
||||
);
|
||||
}
|
||||
|
||||
private Expression<ItemStack> items;
|
||||
|
||||
@Override
|
||||
public boolean check(Event event) {
|
||||
return items.check(event, CraftEngineItems::isCustomItem, isNegated());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(@Nullable Event event, boolean debug) {
|
||||
return PropertyCondition.toString(this, PropertyCondition.PropertyType.BE, event, debug, items, "itemstack");
|
||||
}
|
||||
private Expression<?> item;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
|
||||
items = (Expression<ItemStack>) expressions[0];
|
||||
setNegated(matchedPattern > 1);
|
||||
item = expressions[0];
|
||||
setNegated(matchedPattern == 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(Event event) {
|
||||
Object single = item.getSingle(event);
|
||||
|
||||
ItemStack checkItemStack = null;
|
||||
if (single instanceof ItemType itemType) {
|
||||
checkItemStack = itemType.getTypes().getFirst().getStack();
|
||||
} else if (single instanceof ItemStack itemStack) {
|
||||
checkItemStack = itemStack;
|
||||
} else if (single instanceof Slot slot) {
|
||||
checkItemStack = slot.getItem();
|
||||
}
|
||||
|
||||
if (checkItemStack == null) return isNegated() ? true : false;
|
||||
|
||||
boolean exists = CraftEngineItems.isCustomItem(checkItemStack);
|
||||
if (!exists) return isNegated() ? true : false;
|
||||
return isNegated() ? false : true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(@Nullable Event event, boolean debug) {
|
||||
return PropertyCondition.toString(this, PropertyCondition.PropertyType.BE, event, debug, item, "itemtypes");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ public class CondIsFurniture extends Condition {
|
||||
|
||||
public static void register() {
|
||||
Skript.registerCondition(CondIsFurniture.class,
|
||||
"%entities% (is|are) furniture",
|
||||
"%entities% (is|are)(n't| not) furniture");
|
||||
"%entities% (is|are) (custom|ce|craft-engine) furniture",
|
||||
"%entities% (is|are)(n't| not) (custom|ce|craft-engine) furniture");
|
||||
}
|
||||
|
||||
private Expression<Entity> entities;
|
||||
|
||||
@@ -15,7 +15,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
public class EffPlaceCustomBlock extends Effect {
|
||||
|
||||
public static void register() {
|
||||
Skript.registerEffect(EffPlaceCustomBlock.class, "place custom block %customblockstates% [%directions% %locations%]");
|
||||
Skript.registerEffect(EffPlaceCustomBlock.class, "place (custom|ce|craft-engine) block %customblockstates% [at] [%directions% %locations%]");
|
||||
}
|
||||
|
||||
private Expression<ImmutableBlockState> blocks;
|
||||
|
||||
@@ -15,7 +15,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
public class EffPlaceFurniture extends Effect {
|
||||
|
||||
public static void register() {
|
||||
Skript.registerEffect(EffPlaceFurniture.class, "place furniture %strings% [%directions% %locations%]");
|
||||
Skript.registerEffect(EffPlaceFurniture.class, "place (custom|ce|craft-engine) furniture %strings% [at] [%directions% %locations%]");
|
||||
}
|
||||
|
||||
private Expression<String> furniture;
|
||||
|
||||
@@ -14,7 +14,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
public class EffRemoveFurniture extends Effect {
|
||||
|
||||
public static void register() {
|
||||
Skript.registerEffect(EffRemoveFurniture.class, "remove furniture %entities%");
|
||||
Skript.registerEffect(EffRemoveFurniture.class, "remove (custom|ce|craft-engine) furniture %entities%");
|
||||
}
|
||||
|
||||
private Expression<Entity> entities;
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package net.momirealms.craftengine.bukkit.compatibility.skript.event;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.doc.Description;
|
||||
import ch.njol.skript.doc.Name;
|
||||
import ch.njol.skript.doc.Since;
|
||||
import ch.njol.skript.lang.Literal;
|
||||
import ch.njol.skript.lang.SkriptEvent;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import net.momirealms.craftengine.bukkit.api.event.CraftEngineReloadEvent;
|
||||
import org.bukkit.event.Event;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@Name("On CraftEngine Reload")
|
||||
@Description({"Fires when CraftEngine reload"})
|
||||
@Since("1.0")
|
||||
public class EvtCraftEngineReload extends SkriptEvent {
|
||||
|
||||
public static void register() {
|
||||
Skript.registerEvent("CraftEngine Loaded", EvtCraftEngineReload.class, CraftEngineReloadEvent.class, "(ce|craft(engine|-engine)) [first] (load[ed]|reload)")
|
||||
.description("Called when Craft-Engine resource loaded.");
|
||||
}
|
||||
|
||||
private boolean onlyCheckFirstCall;
|
||||
private static boolean hasBeenCalled = false;
|
||||
|
||||
@Override
|
||||
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parser) {
|
||||
// 检查是否包含 "first" 关键词
|
||||
String expr = parser.expr;
|
||||
this.onlyCheckFirstCall = expr.contains("first");
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(Event event) {
|
||||
if (!(event instanceof CraftEngineReloadEvent reloadEvent)) {
|
||||
return false;
|
||||
}
|
||||
if (onlyCheckFirstCall) {
|
||||
if (hasBeenCalled) return false; // 如果 hasBeenCalled 已经为 true,代表已经调用过了, 故返回 false。
|
||||
hasBeenCalled = true;
|
||||
return true;
|
||||
}
|
||||
hasBeenCalled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(@Nullable Event event, boolean debug) {
|
||||
return onlyCheckFirstCall ? "craftengine first load" : "craftengine reload";
|
||||
}
|
||||
|
||||
public static boolean hasBeenLoad() {
|
||||
return hasBeenCalled;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package net.momirealms.craftengine.bukkit.compatibility.skript.event;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.doc.Description;
|
||||
import ch.njol.skript.doc.Name;
|
||||
import ch.njol.skript.doc.Since;
|
||||
import ch.njol.skript.lang.Literal;
|
||||
import ch.njol.skript.lang.SkriptEvent;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
@@ -16,12 +19,15 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.Arrays;
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
@Name("On Custom Block Place And Break")
|
||||
@Description({"Fires when a Custom block gets place and broken"})
|
||||
@Since("1.0")
|
||||
public class EvtCustomBlock extends SkriptEvent {
|
||||
|
||||
public static void register() {
|
||||
Skript.registerEvent("Break Custom Block", EvtCustomBlock.class, CustomBlockBreakEvent.class, "(break[ing]|1¦min(e|ing)) [[of] %-unsafeblockstatematchers%]")
|
||||
Skript.registerEvent("Break Custom Block", EvtCustomBlock.class, CustomBlockBreakEvent.class, "(break[ing]|1¦min(e|ing)) of (custom|ce|craft-engine) block [[of] %-unsafeblockstatematchers%]")
|
||||
.description("Called when a custom block is broken by a player. If you use 'on mine', only events where the broken block dropped something will call the trigger.");
|
||||
Skript.registerEvent("Place Custom Block", EvtCustomBlock.class, CustomBlockPlaceEvent.class, "(plac(e|ing)|build[ing]) [[of] %-unsafeblockstatematchers%]")
|
||||
Skript.registerEvent("Place Custom Block", EvtCustomBlock.class, CustomBlockPlaceEvent.class, "(plac(e|ing)|build[ing]) of (custom|ce|craft-engine) block [[of] %-unsafeblockstatematchers%]")
|
||||
.description("Called when a player places a custom block.");
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@ package net.momirealms.craftengine.bukkit.compatibility.skript.event;
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.aliases.ItemType;
|
||||
import ch.njol.skript.bukkitutil.ClickEventTracker;
|
||||
import ch.njol.skript.doc.Description;
|
||||
import ch.njol.skript.doc.Name;
|
||||
import ch.njol.skript.doc.Since;
|
||||
import ch.njol.skript.lang.Literal;
|
||||
import ch.njol.skript.lang.SkriptEvent;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
@@ -17,6 +20,9 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@Name("On Click with Custom Item")
|
||||
@Description({"Fires when click a custom item"})
|
||||
@Since("1.0")
|
||||
public class EvtCustomClick extends SkriptEvent {
|
||||
|
||||
private final static int RIGHT = 1, LEFT = 2, ANY = RIGHT | LEFT;
|
||||
@@ -25,8 +31,8 @@ public class EvtCustomClick extends SkriptEvent {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void register() {
|
||||
Skript.registerEvent("Interact Custom Block Furniture", EvtCustomClick.class, new Class[]{CustomBlockInteractEvent.class, FurnitureInteractEvent.class},
|
||||
"[(" + RIGHT + ":right|" + LEFT + ":left)(| |-)][mouse(| |-)]click[ing] [on %-unsafeblockstatematchers/strings%] [(with|using|holding) %-itemtype%]",
|
||||
"[(" + RIGHT + ":right|" + LEFT + ":left)(| |-)][mouse(| |-)]click[ing] (with|using|holding) %itemtype% on %unsafeblockstatematchers/strings%");
|
||||
"[(" + RIGHT + ":right|" + LEFT + ":left)(| |-)][mouse(| |-)]click[ing] of (ce|craft-engine) [on %-unsafeblockstatematchers/strings%] [(with|using|holding) %-itemtype%]",
|
||||
"[(" + RIGHT + ":right|" + LEFT + ":left)(| |-)][mouse(| |-)]click[ing] of (ce|craft-engine) (with|using|holding) %itemtype% on %unsafeblockstatematchers/strings%");
|
||||
}
|
||||
|
||||
private @Nullable Literal<?> type;
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package net.momirealms.craftengine.bukkit.compatibility.skript.event;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.doc.Description;
|
||||
import ch.njol.skript.doc.Name;
|
||||
import ch.njol.skript.doc.Since;
|
||||
import ch.njol.skript.lang.Literal;
|
||||
import ch.njol.skript.lang.SkriptEvent;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
@@ -12,12 +15,15 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.Arrays;
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
@Name("On Custom Furniture Place And Break")
|
||||
@Description({"Fires when a Custom furniture gets place and broken"})
|
||||
@Since("1.0")
|
||||
public class EvtCustomFurniture extends SkriptEvent {
|
||||
|
||||
public static void register() {
|
||||
Skript.registerEvent("Break Furniture", EvtCustomFurniture.class, FurnitureBreakEvent.class, "(break[ing]) [[of] %-strings%]")
|
||||
Skript.registerEvent("Break Furniture", EvtCustomFurniture.class, FurnitureBreakEvent.class, "(break[ing]) of (custom|ce|craft-engine) furniture[s] [[of] %-strings%]")
|
||||
.description("Called when a furniture is broken by a player.");
|
||||
Skript.registerEvent("Place Furniture", EvtCustomFurniture.class, FurniturePlaceEvent.class, "(plac(e|ing)|build[ing]) [[of] %-strings%]")
|
||||
Skript.registerEvent("Place Furniture", EvtCustomFurniture.class, FurniturePlaceEvent.class, "(plac(e|ing)|build[ing]) of (custom|ce|craft-engine) furniture[s] [[of] %-strings%]")
|
||||
.description("Called when a player places a furniture.");
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.Optional;
|
||||
public class ExprBlockCustomBlockID extends SimplePropertyExpression<Object, String> {
|
||||
|
||||
public static void register() {
|
||||
register(ExprBlockCustomBlockID.class, String.class, "custom block id", "blocks/blockdata/customblockstates");
|
||||
register(ExprBlockCustomBlockID.class, String.class, "(custom|ce|craft-engine) block [namespace] id", "blocks/blockdata/customblockstates");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,7 +10,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
public class ExprBlockCustomBlockState extends SimplePropertyExpression<Object, ImmutableBlockState> {
|
||||
|
||||
public static void register() {
|
||||
register(ExprBlockCustomBlockState.class, ImmutableBlockState.class, "custom block[ ]state", "blocks/blockdata");
|
||||
register(ExprBlockCustomBlockState.class, ImmutableBlockState.class, "(custom|ce|craft-engine) block[ ]state", "blocks/blockdata");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package net.momirealms.craftengine.bukkit.compatibility.skript.expression;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.aliases.ItemType;
|
||||
import ch.njol.skript.doc.Description;
|
||||
import ch.njol.skript.doc.Name;
|
||||
import ch.njol.skript.doc.Since;
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.ExpressionType;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
@@ -14,29 +18,44 @@ import org.bukkit.event.Event;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ExprCustomItem extends SimpleExpression<ItemStack> {
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Name("CraftEngine Item")
|
||||
@Description({"Get CraftEngine items."})
|
||||
@Since("1.0")
|
||||
public class ExprCustomItem extends SimpleExpression<ItemType> {
|
||||
|
||||
public static void register() {
|
||||
Skript.registerExpression(ExprCustomItem.class, ItemStack.class, ExpressionType.SIMPLE, "[(the|a)] custom item [with id] %string%");
|
||||
Skript.registerExpression(ExprCustomItem.class, ItemType.class, ExpressionType.SIMPLE, "[(the|a)] (custom|ce|craft-engine) item [with [namespace] id] %strings%");
|
||||
}
|
||||
|
||||
private Expression<String> itemId;
|
||||
private Expression<?> itemIds;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
|
||||
itemId = (Expression<String>) exprs[0];
|
||||
itemIds = exprs[0];
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected ItemStack[] get(Event e) {
|
||||
String itemId = this.itemId.getSingle(e);
|
||||
if (itemId == null)
|
||||
return null;
|
||||
CustomItem<ItemStack> customItem = CraftEngineItems.byId(Key.of(itemId));
|
||||
return customItem == null ? null : new ItemStack[] {customItem.buildItemStack(ItemBuildContext.EMPTY)};
|
||||
protected ItemType[] get(Event event) {
|
||||
Object[] objects = itemIds.getArray(event);
|
||||
List<ItemType> items = new ArrayList<>();
|
||||
|
||||
for (Object object : objects) {
|
||||
if (object instanceof String string) {
|
||||
CustomItem<ItemStack> customItem = CraftEngineItems.byId(Key.of(string));
|
||||
if (customItem != null) {
|
||||
ItemType itemType = new ItemType(customItem.buildItemStack(ItemBuildContext.EMPTY));
|
||||
items.add(itemType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return items.toArray(new ItemType[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -45,12 +64,12 @@ public class ExprCustomItem extends SimpleExpression<ItemStack> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<ItemStack> getReturnType() {
|
||||
return ItemStack.class;
|
||||
public Class<ItemType> getReturnType() {
|
||||
return ItemType.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(@Nullable Event e, boolean debug) {
|
||||
return "the custom item with id " + itemId.toString(e, debug);
|
||||
return "craft-engine item with id " + itemIds.toString(e, debug);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.util.Optional;
|
||||
public class ExprEntityFurnitureID extends SimplePropertyExpression<Object, String> {
|
||||
|
||||
public static void register() {
|
||||
register(ExprEntityFurnitureID.class, String.class, "furniture id", "entities");
|
||||
register(ExprEntityFurnitureID.class, String.class, "(custom|ce|craft-engine) furniture [namespace] id", "entities");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
package net.momirealms.craftengine.bukkit.compatibility.skript.expression;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.aliases.ItemType;
|
||||
import ch.njol.skript.classes.Changer;
|
||||
import ch.njol.skript.expressions.base.SimplePropertyExpression;
|
||||
import ch.njol.util.coll.CollectionUtils;
|
||||
import ch.njol.skript.doc.Description;
|
||||
import ch.njol.skript.doc.Name;
|
||||
import ch.njol.skript.doc.Since;
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.ExpressionType;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.skript.lang.util.SimpleExpression;
|
||||
import ch.njol.skript.util.slot.Slot;
|
||||
import ch.njol.util.Kleenean;
|
||||
import net.momirealms.craftengine.bukkit.api.CraftEngineItems;
|
||||
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -14,27 +20,52 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class ExprItemCustomItemID extends SimplePropertyExpression<Object, String> {
|
||||
@Name("CraftEngine Item ID")
|
||||
@Description({"Get CraftEngine item id."})
|
||||
@Since("1.0")
|
||||
public class ExprItemCustomItemID extends SimpleExpression<String> {
|
||||
|
||||
public static void register() {
|
||||
register(ExprItemCustomItemID.class, String.class, "custom item id", "itemstacks/itemtypes");
|
||||
Skript.registerExpression(ExprItemCustomItemID.class, String.class, ExpressionType.PROPERTY,
|
||||
"(custom|ce|craft-engine) item [namespace] id of %itemstack/itemtype/slot%",
|
||||
"%itemstack/itemtype/slot%'[s] (custom|ce|craft-engine) item [namespace] id"
|
||||
);
|
||||
}
|
||||
|
||||
private Expression<?> itemStackExpr;
|
||||
|
||||
@Override
|
||||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
|
||||
itemStackExpr = exprs[0];
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String convert(Object object) {
|
||||
if (object instanceof ItemStack itemStack)
|
||||
return Optional.ofNullable(CraftEngineItems.byItemStack(itemStack)).map(it -> it.id().toString()).orElse(null);
|
||||
if (object instanceof ItemType itemType) {
|
||||
ItemStack itemStack = new ItemStack(itemType.getMaterial());
|
||||
itemStack.setItemMeta(itemType.getItemMeta());
|
||||
return Optional.ofNullable(CraftEngineItems.byItemStack(itemStack)).map(it -> it.id().toString()).orElse(null);
|
||||
protected String[] get(Event event) {
|
||||
Object single = itemStackExpr.getSingle(event);
|
||||
|
||||
String result = null;
|
||||
if (single instanceof ItemStack itemStack) {
|
||||
result = Optional.of(itemStack).map(this::getCraftEngineItemId).orElse(null);
|
||||
} else if (single instanceof ItemType itemType) {
|
||||
result = Optional.ofNullable(itemType.getTypes().getFirst().getStack()).map(this::getCraftEngineItemId).orElse(null);
|
||||
} else if (single instanceof Slot slot) {
|
||||
result = Optional.ofNullable(slot.getItem()).map(this::getCraftEngineItemId).orElse(null);
|
||||
}
|
||||
return null;
|
||||
|
||||
return new String[] {result};
|
||||
}
|
||||
|
||||
|
||||
private String getCraftEngineItemId(ItemStack itemStack) {
|
||||
return Optional.ofNullable(CraftEngineItems.getCustomItemId(itemStack))
|
||||
.map(Key::asString)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPropertyName() {
|
||||
return "custom item id";
|
||||
public boolean isSingle() {
|
||||
return itemStackExpr.isSingle();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -42,23 +73,14 @@ public class ExprItemCustomItemID extends SimplePropertyExpression<Object, Strin
|
||||
return String.class;
|
||||
}
|
||||
|
||||
// 不需要处理 add, delete 等修改操作
|
||||
@Override
|
||||
public Class<?>[] acceptChange(Changer.ChangeMode mode) {
|
||||
return CollectionUtils.array(String.class);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void change(Event e, @Nullable Object[] delta, Changer.ChangeMode mode) {
|
||||
Key id = Key.of((String) delta[0]);
|
||||
for (Object item : getExpr().getArray(e)) {
|
||||
if (item instanceof ItemStack itemStack) {
|
||||
Item<ItemStack> item1 = BukkitItemManager.instance().wrap(itemStack);
|
||||
Item<ItemStack> item2 = BukkitItemManager.instance().createWrappedItem(id, null);
|
||||
item1.merge(item2);
|
||||
} else if (item instanceof ItemType itemType) {
|
||||
Item<ItemStack> item2 = BukkitItemManager.instance().createWrappedItem(id, null);
|
||||
itemType.setItemMeta(item2.getItem().getItemMeta());
|
||||
}
|
||||
}
|
||||
public String toString(@Nullable Event event, boolean debug) {
|
||||
return "craft-engine item ID of " + itemStackExpr.toString(event, debug);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user