Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds some fire resistant syntax #6639

Merged
merged 15 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondIsFireResistant.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
* 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<ItemType> {

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";
}

}
77 changes: 77 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffFireResistant.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
* 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<ItemType> items;
private boolean not;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
items = (Expression<ItemType>) 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";
}

}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
* 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<ItemType, ItemType> {

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<ItemType>) 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<? extends ItemType> getReturnType() {
return ItemType.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return getExpr().toString(event, debug) + " with fire resistance";
}

}
41 changes: 41 additions & 0 deletions src/test/skript/tests/syntaxes/conditions/CondIsFireResistant.sk
Original file line number Diff line number Diff line change
@@ -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"
14 changes: 14 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffFireResistant.sk
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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)