diff --git a/src/main/java/ch/njol/skript/conditions/CondIsFireResistant.java b/src/main/java/ch/njol/skript/conditions/CondIsFireResistant.java new file mode 100644 index 00000000000..924fbd8a710 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondIsFireResistant.java @@ -0,0 +1,56 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.conditions; + +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.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.RequiredPlugins; +import ch.njol.skript.doc.Since; +import org.bukkit.inventory.meta.ItemMeta; + +@Name("Is Fire Resistant") +@Description("Checks whether an item is fire resistant.") +@Examples({ + "if player's tool is fire resistant:", + "if {_items::*} aren't resistant to fire:" +}) +@RequiredPlugins("Spigot 1.20.5+") +@Since("INSERT VERSION") +public class CondIsFireResistant extends PropertyCondition { + + static { + if (Skript.methodExists(ItemMeta.class, "isFireResistant")) + PropertyCondition.register(CondIsFireResistant.class, "(fire resistant|resistant to fire)", "itemtypes"); + } + + @Override + public boolean check(ItemType item) { + return item.getItemMeta().isFireResistant(); + } + + @Override + public String getPropertyName() { + return "fire resistant"; + } + +} diff --git a/src/main/java/ch/njol/skript/effects/EffFireResistant.java b/src/main/java/ch/njol/skript/effects/EffFireResistant.java new file mode 100644 index 00000000000..6a7bfb79f89 --- /dev/null +++ b/src/main/java/ch/njol/skript/effects/EffFireResistant.java @@ -0,0 +1,77 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.effects; + +import ch.njol.skript.Skript; +import ch.njol.skript.aliases.ItemType; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.RequiredPlugins; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Effect; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.Nullable; + +@Name("Make Fire Resistant") +@Description("Makes items fire resistant.") +@Examples({ + "make player's tool fire resistant:", + "make {_items::*} not resistant to fire" +}) +@RequiredPlugins("Spigot 1.20.5+") +@Since("INSERT VERSION") +public class EffFireResistant extends Effect { + + static { + if (Skript.methodExists(ItemMeta.class, "setFireResistant", boolean.class)) + Skript.registerEffect(EffFireResistant.class, "make %itemtypes% [:not] (fire resistant|resistant to fire)"); + } + + @SuppressWarnings("NotNullFieldNotInitialized") + private Expression items; + private boolean not; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + items = (Expression) exprs[0]; + not = parseResult.hasTag("not"); + return true; + } + + @Override + protected void execute(Event event) { + for (ItemType item : this.items.getArray(event)) { + ItemMeta meta = item.getItemMeta(); + meta.setFireResistant(!not); + item.setItemMeta(meta); + } + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return "make " + items.toString(event, debug) + (not ? " not" : "") + " fire resistant"; + } + +} diff --git a/src/main/java/ch/njol/skript/expressions/ExprWithFireResistance.java b/src/main/java/ch/njol/skript/expressions/ExprWithFireResistance.java new file mode 100644 index 00000000000..e471b4610ee --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprWithFireResistance.java @@ -0,0 +1,87 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.expressions; + +import ch.njol.skript.Skript; +import ch.njol.skript.aliases.ItemType; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.RequiredPlugins; +import ch.njol.skript.doc.Since; +import ch.njol.skript.expressions.base.PropertyExpression; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.inventory.meta.ItemMeta; +import org.eclipse.jdt.annotation.Nullable; + +@Name("With Fire Resistance") +@Description({ + "Creates a copy of an item with (or without) fire resistance." +}) +@Examples({ + "set {_x} to diamond sword with fire resistance", + "equip player with netherite helmet without fire resistance", + "drop fire resistant stone at player" +}) +@RequiredPlugins("Spigot 1.20.5+") +@Since("INSERT VERSION") +public class ExprWithFireResistance extends PropertyExpression { + + static { + if (Skript.methodExists(ItemMeta.class, "setFireResistant", boolean.class)) + Skript.registerExpression(ExprWithFireResistance.class, ItemType.class, ExpressionType.PROPERTY, + "%itemtype% with[:out] fire[ ]resistance", + "fire resistant %itemtype%"); + } + + private boolean out; + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + setExpr((Expression) exprs[0]); + out = parseResult.hasTag("out"); + return true; + } + + @Override + protected ItemType[] get(Event event, ItemType[] source) { + return get(source.clone(), item -> { + ItemMeta meta = item.getItemMeta(); + meta.setFireResistant(!out); + item.setItemMeta(meta); + return item; + }); + } + + @Override + public Class getReturnType() { + return ItemType.class; + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return getExpr().toString(event, debug) + " with fire resistance"; + } + +} diff --git a/src/test/skript/tests/syntaxes/conditions/CondIsFireResistant.sk b/src/test/skript/tests/syntaxes/conditions/CondIsFireResistant.sk new file mode 100644 index 00000000000..61555e98fae --- /dev/null +++ b/src/test/skript/tests/syntaxes/conditions/CondIsFireResistant.sk @@ -0,0 +1,41 @@ +test "is fire resistant" when running minecraft "1.20.5": + + # single item: naturally not fire resistant + set {_item} to diamond + assert {_item} is not fire resistant with "diamond is unexpectedly fire resistant" + + # TODO: enable in 1.21 (doesn't work in 1.20.5 or 1.20.6) + # single item: artificially not fire resistant + # set {_item} to netherite boots without fire resistance + # assert {_item} is not fire resistant with "netherite boots are unexpectedly fire resistant" + + # TODO: enable in 1.21 (doesn't work in 1.20.5 or 1.20.6) + # single item: naturally fire resistant + # set {_item} to netherite boots + # assert {_item} is fire resistant with "netherite boots are unexpectedly not fire resistant" + + # single item: artificially fire resistant + set {_item} to fire resistant diamond + assert {_item} is fire resistant with "fire resistant diamond is unexpectedly not fire resistant" + + # multiple items: naturally not fire resistant + set {_item} to diamond + set {_item2} to stone block + assert ({_item} and {_item2}) are not fire resistant with "{_item} and {_item2} are unexpectedly fire resistant" + + # TODO: enable in 1.21 (doesn't work in 1.20.5 or 1.20.6) + # multiple items: artificially not fire resistance + # set {_item} to netherite boots without fire resistance + # set {_item2} to netherite helmet without fire resistance + # assert ({_item} and {_item2}) are not fire resistant with "{_item} and {_item2} are unexpectedly fire resistant" + + # TODO: enable in 1.21 (doesn't work in 1.20.5 or 1.20.6) + # multiple items: naturally fire resistant + # set {_item} to netherite boots + # set {_item2} to netherite helmet + # assert ({_item} and {_item2}) are fire resistant with "{_item} and {_item2} are unexpectedly not fire resistant" + + # multiple items: artifically fire resistant + set {_item} to diamond with fire resistance + set {_item2} to fire resistant stone block + assert ({_item} and {_item2}) are fire resistant with "fire resistant {_item} and {_item2} are unexpectedly not fire resistant" diff --git a/src/test/skript/tests/syntaxes/effects/EffFireResistant.sk b/src/test/skript/tests/syntaxes/effects/EffFireResistant.sk new file mode 100644 index 00000000000..1005555ff01 --- /dev/null +++ b/src/test/skript/tests/syntaxes/effects/EffFireResistant.sk @@ -0,0 +1,14 @@ +test "apply fire resistance" when running minecraft "1.20.5": + + # single item + set {_item} to diamond + make {_item} fire resistant + assert {_item} is fire resistant with "{_item} is unexpectedly not fire resistant" + + # multiple items + set {_item} to diamond + set {_item2} to paper + make ({_item} and {_item2}) resistant to fire + assert ({_item} and {_item2}) are resistant to fire with "{_item} and {_item2} are unexpectedly not fire resistant" + + # TODO: add tests for already fire resistant items (i.e. netherite) in 1.21 (doesn't work in 1.20.5 or 1.20.6) diff --git a/src/test/skript/tests/syntaxes/expressions/ExprWithFireResistance.sk b/src/test/skript/tests/syntaxes/expressions/ExprWithFireResistance.sk new file mode 100644 index 00000000000..e7c2754a3cb --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprWithFireResistance.sk @@ -0,0 +1,12 @@ +test "item with fire resistance" when running minecraft "1.20.5": + + # single item + set {_item} to diamond with fire resistance + assert {_item} is fire resistant with "{_item} was not fire resistant" + + # multiple items + set {_item} to fire resistant diamond + set {_item2} to paper with fire resistance + assert ({_item} and {_item2}) are fire resistant with "{_item} and {_item2} are unexpectedly not fire resistant" + + # TODO: add tests for already fire resistant items (i.e. netherite) in 1.21 (doesn't work in 1.20.5 or 1.20.6)