From b13c29c3381e0cced02c52fddd54580529e080bc Mon Sep 17 00:00:00 2001 From: _tud Date: Fri, 21 Oct 2022 13:31:18 +0300 Subject: [PATCH] add environment expression (#5070) * add environment expression --- .../skript/classes/data/BukkitClasses.java | 27 +++++++++ .../expressions/ExprWorldEnvironment.java | 59 +++++++++++++++++++ src/main/resources/lang/default.lang | 7 +++ .../expressions/ExprWorldEnvironment.sk | 5 ++ 4 files changed, 98 insertions(+) create mode 100644 src/main/java/ch/njol/skript/expressions/ExprWorldEnvironment.java create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprWorldEnvironment.sk diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java index ee4be13c3cd..324cf87d59a 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -39,6 +39,7 @@ import org.bukkit.OfflinePlayer; import org.bukkit.SoundCategory; import org.bukkit.World; +import org.bukkit.World.Environment; import org.bukkit.attribute.Attribute; import org.bukkit.block.Biome; import org.bukkit.block.Block; @@ -1817,5 +1818,31 @@ public String toVariableNameString(Attribute a) { } }) .serializer(new EnumSerializer<>(Attribute.class))); + + EnumUtils environments = new EnumUtils<>(Environment.class, "environments"); + Classes.registerClass(new ClassInfo<>(Environment.class, "environment") + .user("(world ?)?environments?") + .name("World Environment") + .description("Represents the environment of a world.") + .usage(environments.getAllNames()) + .since("INSERT VERSION") + .parser(new Parser() { + @Override + @Nullable + public Environment parse(String input, ParseContext context) { + return environments.parse(input); + } + + @Override + public String toString(Environment environment, int flags) { + return environments.toString(environment, flags); + } + + @Override + public String toVariableNameString(Environment environment) { + return toString(environment, 0); + } + }) + .serializer(new EnumSerializer<>(Environment.class))); } } diff --git a/src/main/java/ch/njol/skript/expressions/ExprWorldEnvironment.java b/src/main/java/ch/njol/skript/expressions/ExprWorldEnvironment.java new file mode 100644 index 00000000000..4f57005de57 --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprWorldEnvironment.java @@ -0,0 +1,59 @@ +/** + * 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.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.expressions.base.SimplePropertyExpression; +import org.bukkit.World; +import org.bukkit.World.Environment; +import org.eclipse.jdt.annotation.Nullable; + +@Name("World Environment") +@Description("The environment of a world") +@Examples({ + "if environment of player's world is nether:", + "\tapply fire resistance to player for 10 minutes" +}) +@Since("INSERT VERSION") +public class ExprWorldEnvironment extends SimplePropertyExpression { + + static { + register(ExprWorldEnvironment.class, Environment.class, "[world] environment", "worlds"); + } + + @Override + @Nullable + public Environment convert(World world) { + return world.getEnvironment(); + } + + @Override + public Class getReturnType() { + return Environment.class; + } + + @Override + protected String getPropertyName() { + return "environment"; + } + +} diff --git a/src/main/resources/lang/default.lang b/src/main/resources/lang/default.lang index 22ef2727798..37648c76641 100644 --- a/src/main/resources/lang/default.lang +++ b/src/main/resources/lang/default.lang @@ -1856,6 +1856,13 @@ attribute types: horse_jump_strength: horse jump strength zombie_spawn_reinforcements: zombie spawn reinforcements +# -- Environments -- +environments: + normal: normal, overworld, the overworld + nether: nether, the nether + the_end: end, the end + custom: custom + # -- Boolean -- boolean: true: diff --git a/src/test/skript/tests/syntaxes/expressions/ExprWorldEnvironment.sk b/src/test/skript/tests/syntaxes/expressions/ExprWorldEnvironment.sk new file mode 100644 index 00000000000..ee17394c4f2 --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprWorldEnvironment.sk @@ -0,0 +1,5 @@ +test "world environment": + assert environment of world "world" is normal with "main overworld was not environment ""normal""" + assert environment of world "world_the_end" is the end with "world_the_end was not environment ""the end""" + set {_environment} to environment of world "world" + assert {_environment} is overworld with "environment of world didn't compare with a variable"