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

New Expression: Loaded Chunks of World #5662

Merged
106 changes: 66 additions & 40 deletions src/main/java/ch/njol/skript/expressions/ExprChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,83 +18,109 @@
*/
package ch.njol.skript.expressions;

import java.util.Arrays;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
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.PropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.Direction;
import ch.njol.util.Kleenean;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

/**
* @author Peter Güttinger
*/
@Name("Chunk")
@Description("The <a href='./classes.html#chunk'>chunk</a> a block, location or entity is in.")
@Examples("add the chunk at the player to {protected chunks::*}")
@Since("2.0")
public class ExprChunk extends PropertyExpression<Location, Chunk> {
@Description("Returns the <a href='./classes.html#chunk'>chunk</a> of a block, location or entity is in, or a list of the loaded chunks of a world.")
@Examples({
"add the chunk at the player to {protected chunks::*}",
"set {_chunks::*} to the loaded chunks of the player's world"
})
@Since("2.0, INSERT VERSION (loaded chunks)")
public class ExprChunk extends SimpleExpression<Chunk> {

static {
Skript.registerExpression(ExprChunk.class, Chunk.class, ExpressionType.PROPERTY, "[the] chunk[s] (of|%-directions%) %locations%", "%locations%'[s] chunk[s]");
Skript.registerExpression(ExprChunk.class, Chunk.class, ExpressionType.COMBINED,
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
"[all [of]] [the] chunk[s] (of|%-directions%) %locations%",
"%locations%'[s] chunk[s]",
"[all [of]] [the] loaded chunks (of|in) %worlds%"
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
);
}
@SuppressWarnings("null")

private int pattern;
private Expression<Location> locations;

@SuppressWarnings({"unchecked", "null"})
private Expression<World> worlds;

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
if (matchedPattern == 0) {
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
pattern = matchedPattern;
if (pattern == 0) {
locations = (Expression<Location>) exprs[1];
if (exprs[0] != null)
if (exprs[0] != null) {
locations = Direction.combine((Expression<? extends Direction>) exprs[0], locations);
} else {
}
} else if (pattern == 1) {
locations = (Expression<Location>) exprs[0];
} else {
worlds = ((Expression<World>) exprs[0]);
}
setExpr(locations);
return true;
}

@Override
protected Chunk[] get(final Event e, final Location[] source) {
return get(source, Location::getChunk);
}

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


@Override
public String toString(final @Nullable Event e, final boolean debug) {
return "the chunk at " + locations.toString(e, debug);
protected Chunk[] get(Event event) {
if (pattern != 2) {
return locations.stream(event)
.map(Location::getChunk)
.toArray(Chunk[]::new);
}
return worlds.stream(event)
.flatMap(world -> Arrays.stream(world.getLoadedChunks()))
.toArray(Chunk[]::new);
}

@Override
@Nullable
public Class<?>[] acceptChange(final ChangeMode mode) {
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.RESET)
return new Class[0];
return null;
}

@Override
public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) {
@SuppressWarnings("deprecation")
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
assert mode == ChangeMode.RESET;

final Chunk[] cs = getArray(e);
for (final Chunk c : cs)
c.getWorld().regenerateChunk(c.getX(), c.getZ());
for (Chunk chunk : get(event))
chunk.getWorld().regenerateChunk(chunk.getX(), chunk.getZ());
}

@Override
public boolean isSingle() {
if (pattern == 2)
return false;
return locations.isSingle();
}

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

@Override
public String toString(@Nullable Event event, boolean debug) {
if (pattern == 2)
return "loaded chunks of " + worlds.toString(event, debug);
return "chunk at " + locations.toString(event, debug);
}

}