Skip to content

Commit

Permalink
add environment expression (#5070)
Browse files Browse the repository at this point in the history
* add environment expression
  • Loading branch information
UnderscoreTud authored Oct 21, 2022
1 parent 0e1455b commit b13c29c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1817,5 +1818,31 @@ public String toVariableNameString(Attribute a) {
}
})
.serializer(new EnumSerializer<>(Attribute.class)));

EnumUtils<Environment> 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<Environment>() {
@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)));
}
}
59 changes: 59 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprWorldEnvironment.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
* 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<World, Environment> {

static {
register(ExprWorldEnvironment.class, Environment.class, "[world] environment", "worlds");
}

@Override
@Nullable
public Environment convert(World world) {
return world.getEnvironment();
}

@Override
public Class<? extends Environment> getReturnType() {
return Environment.class;
}

@Override
protected String getPropertyName() {
return "environment";
}

}
7 changes: 7 additions & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit b13c29c

Please sign in to comment.