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

Add worldborders #7006

Open
wants to merge 39 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
65e0efc
Initial setup
Phill310 Aug 29, 2024
a3f7f47
Fix imports
Phill310 Aug 29, 2024
000e562
Use pattern variables for instanceof
Phill310 Aug 30, 2024
28044ac
Cleaner switch statements
Phill310 Aug 30, 2024
dd4f4f4
Specify paper version
Phill310 Aug 30, 2024
593401d
Use newer timespan method
Phill310 Aug 30, 2024
97c3ffc
Require "world border" when editing a world border's attributes
Phill310 Aug 30, 2024
1588c3d
Move IsInsideWorldBorder to IsWithin condition
Phill310 Aug 30, 2024
8cb9c9d
Make variable name readable
Phill310 Aug 30, 2024
e08a74b
Clean up conditions
Phill310 Aug 30, 2024
5c125f5
Make radius/diameter optional
Phill310 Aug 31, 2024
47c2805
Make damage have a minimum of 0
Phill310 Sep 8, 2024
3ccc84c
Make warning distance have a minimum of 0
Phill310 Sep 9, 2024
1b793ca
Make damage buffer have a minimum of 0
Phill310 Sep 9, 2024
f759e46
Make warning time have a minimum of 0
Phill310 Sep 9, 2024
dfc45fc
Force the center location to stay inside the limits defined by WorldB…
Phill310 Sep 9, 2024
3134439
Tests!
Phill310 Sep 9, 2024
85a81e5
End files with a new line
Phill310 Sep 15, 2024
4636119
Apply suggestions from code review
Phill310 Sep 23, 2024
1f529b8
Correct annotation placement
Phill310 Sep 23, 2024
ce149d9
Apply suggestions from code review
Phill310 Dec 25, 2024
5296312
Phil's stuff.
Moderocky Dec 26, 2024
6c1083d
More of Phil's stuff.
Moderocky Dec 26, 2024
e708061
Whoops I imported again
Moderocky Dec 26, 2024
bd313ce
Apply suggestions from code review
Phill310 Dec 26, 2024
281395e
register properties as defaults
Phill310 Dec 26, 2024
d2e5379
Add docs
Phill310 Dec 28, 2024
a697be1
Clean up toString
Phill310 Dec 28, 2024
7486f83
Update tests
Phill310 Dec 28, 2024
7e47366
Merge branch 'dev/feature' into feature/worldborder
Phill310 Dec 28, 2024
063a0a1
Apply suggestions from code review
Phill310 Dec 28, 2024
7e97332
Fix imports
Phill310 Dec 28, 2024
b7c5ef4
Remove duplicate event registration
Phill310 Dec 28, 2024
783c5e8
Update src/main/java/ch/njol/skript/expressions/ExprSecCreateWorldBor…
Phill310 Dec 29, 2024
2e413e8
Merge branch 'dev/feature' into feature/worldborder
Phill310 Dec 29, 2024
2c89859
Merge branch 'dev/feature' into feature/worldborder
Efnilite Dec 31, 2024
6318c09
Merge branch 'dev/feature' into feature/worldborder
Phill310 Jan 6, 2025
c39a1f2
Fix typo
Phill310 Jan 6, 2025
98da501
Merge remote-tracking branch 'origin/feature/worldborder' into featur…
Phill310 Jan 6, 2025
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
43 changes: 26 additions & 17 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,7 @@
import ch.njol.util.StringUtils;
import ch.njol.yggdrasil.Fields;
import io.papermc.paper.world.MoonPhase;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Difficulty;
import org.bukkit.FireworkEffect;
import org.bukkit.GameMode;
import org.bukkit.GameRule;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Registry;
import org.bukkit.SoundCategory;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.World.Environment;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.Biome;
Expand All @@ -64,11 +53,7 @@
import org.bukkit.event.player.PlayerResourcePackStatusEvent.Status;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent.ChangeReason;
import org.bukkit.inventory.BlockInventoryHolder;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.*;
import org.bukkit.metadata.Metadatable;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
Expand Down Expand Up @@ -1568,6 +1553,30 @@ public String toVariableNameString(EnchantmentOffer eo) {
);
}

Classes.registerClass(new ClassInfo<>(WorldBorder.class, "worldborder")
.user("world ?borders?")
.name("World Border")
.description("Represents the border of a world or player.")
.since("INSERT VERSION")
.parser(new Parser<WorldBorder>() {
@Override
public boolean canParse(ParseContext context) {
return false;
}

@Override
public String toString(WorldBorder border, int flags) {
if (border.getWorld() == null)
return "virtual world border";
return "world border of world named '" + border.getWorld().getName() + "'";
}

@Override
public String toVariableNameString(WorldBorder border) {
return toString(border, 0);
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}
})
.defaultExpression(new EventValueExpression<>(WorldBorder.class)));
}

}
26 changes: 26 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import com.destroystokyo.paper.event.player.PlayerElytraBoostEvent;
import io.papermc.paper.event.entity.EntityMoveEvent;
import io.papermc.paper.event.player.*;
import io.papermc.paper.event.world.border.WorldBorderBoundsChangeEvent;
import io.papermc.paper.event.world.border.WorldBorderBoundsChangeFinishEvent;
import io.papermc.paper.event.world.border.WorldBorderCenterChangeEvent;
import io.papermc.paper.event.world.border.WorldBorderEvent;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand Down Expand Up @@ -1880,6 +1884,28 @@ public Block get(PlayerChangeBeaconEffectEvent event) {
EventValues.registerEventValue(PlayerElytraBoostEvent.class, Entity.class, PlayerElytraBoostEvent::getFirework);
}

// === WorldBorderEvents ===
if (Skript.classExists("io.papermc.paper.event.world.border.WorldBorderEvent")) {
// WorldBorderEvent
EventValues.registerEventValue(WorldBorderEvent.class, WorldBorder.class, WorldBorderEvent::getWorldBorder);

// WorldBorderBoundsChangeEvent
EventValues.registerEventValue(WorldBorderBoundsChangeEvent.class, Number.class, WorldBorderBoundsChangeEvent::getNewSize, EventValues.TIME_NOW);
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
EventValues.registerEventValue(WorldBorderBoundsChangeEvent.class, Number.class, WorldBorderBoundsChangeEvent::getOldSize, EventValues.TIME_PAST);
EventValues.registerEventValue(WorldBorderBoundsChangeEvent.class, Timespan.class, (event) -> new Timespan(event.getDuration()));
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
EventValues.registerEventValue(WorldBorderBoundsChangeEvent.class, WorldBorder.class, WorldBorderBoundsChangeEvent::getWorldBorder);

// WorldBorderBoundsChangeFinishEvent
EventValues.registerEventValue(WorldBorderBoundsChangeFinishEvent.class, Number.class, WorldBorderBoundsChangeFinishEvent::getNewSize, EventValues.TIME_NOW);
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
EventValues.registerEventValue(WorldBorderBoundsChangeFinishEvent.class, Number.class, WorldBorderBoundsChangeFinishEvent::getOldSize, EventValues.TIME_PAST);
EventValues.registerEventValue(WorldBorderBoundsChangeFinishEvent.class, Timespan.class, (event) -> new Timespan((long) event.getDuration()));
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
EventValues.registerEventValue(WorldBorderBoundsChangeFinishEvent.class, WorldBorder.class, WorldBorderBoundsChangeFinishEvent::getWorldBorder);

// WorldBorderCenterChangeEvent
EventValues.registerEventValue(WorldBorderCenterChangeEvent.class, Location.class, WorldBorderCenterChangeEvent::getNewCenter, EventValues.TIME_NOW);
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
EventValues.registerEventValue(WorldBorderCenterChangeEvent.class, Location.class, WorldBorderCenterChangeEvent::getOldCenter, EventValues.TIME_PAST);
EventValues.registerEventValue(WorldBorderCenterChangeEvent.class, WorldBorder.class, WorldBorderCenterChangeEvent::getWorldBorder);
}
}

}
14 changes: 10 additions & 4 deletions src/main/java/ch/njol/skript/conditions/CondIsWithin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.WorldBorder;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
Expand All @@ -39,12 +40,12 @@
"\tcancel event",
"\tsend \"Back up!\" to attacker and victim",
})
@Since("2.7")
@Since("2.7, INSERT VERSION (worldborder)")
@RequiredPlugins("MC 1.17+ (within block)")
public class CondIsWithin extends Condition {

static {
String validTypes = "entity/chunk/world";
String validTypes = "entity/chunk/world/worldborder";
if (Skript.methodExists(Block.class, "getCollisionShape"))
validTypes += "/block";

Expand All @@ -71,7 +72,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
loc1 = (Expression<Location>) exprs[1];
loc2 = (Expression<Location>) exprs[2];
} else {
// within an entity/block/chunk/world
// within an entity/block/chunk/world/worldborder
withinLocations = false;
area = exprs[1];
}
Expand All @@ -90,7 +91,7 @@ public boolean check(Event event) {
return locsToCheck.check(event, box::contains, isNegated());
}

// else, within an entity/block/chunk/world
// else, within an entity/block/chunk/world/worldborder
Object area = this.area.getSingle(event);
if (area == null)
return isNegated();
Expand Down Expand Up @@ -125,6 +126,11 @@ public boolean check(Event event) {
return locsToCheck.check(event, (loc) -> loc.getWorld().equals(area), isNegated());
}

// Worldborders
if (area instanceof WorldBorder worldBorder) {
return locsToCheck.check(event, worldBorder::isInside, isNegated());
}

// fall-back
return false;
}
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffWorldBorderExpand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
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.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.SyntaxStringBuilder;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.Timespan.TimePeriod;
import ch.njol.util.Kleenean;
import ch.njol.util.Math2;
import org.bukkit.WorldBorder;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Expand/Shrink World Border")
@Description({
"Expand or shrink the size of a world border.",
"Note: Using `by` adds/subtracts from the current size of the world border.",
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
"Using `to` sets to the specified size."
})
@Examples({
"expand world border of player by 100 in 5 seconds",
"shrink world border of world \"world\" to 100 in 10 seconds"
})
@Since("INSERT VERSION")
public class EffWorldBorderExpand extends Effect {

static {
Skript.registerEffect(EffWorldBorderExpand.class,
"(expand|grow) [(diameter|:radius) of] %worldborders% (by|:to) %number% [over [a period of] %-timespan%]",
"(expand|grow) %worldborders%['s (diameter|:radius)] (by|:to) %number% [over [a period of] %-timespan%]",
"(contract|shrink) [(diameter|:radius) of] %worldborders% (by|:to) %number% [over [a period of] %-timespan%]",
"(contract|shrink) %worldborders%['s (diameter|:radius)] (by|:to) %number% [over [a period of] %-timespan%]"

Phill310 marked this conversation as resolved.
Show resolved Hide resolved
);
}

private boolean shrink;
private boolean radius;
private boolean to;
private Expression<WorldBorder> worldBorders;
private Expression<Number> number;
private @Nullable Expression<Timespan> timespan;
private static final double MAX_WORLDBORDER_SIZE = 6.0E7;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
worldBorders = (Expression<WorldBorder>) exprs[0];
number = (Expression<Number>) exprs[1];
timespan = (Expression<Timespan>) exprs[2];
shrink = matchedPattern > 1;
radius = parseResult.hasTag("radius");
to = parseResult.hasTag("to");
return true;
}

@Override
protected void execute(Event event) {
double input = number.getOptionalSingle(event).orElse(0).doubleValue();
if (radius)
input *= 2;
long speed = 0;
if (timespan != null) {
Timespan timespan = this.timespan.getSingle(event);
if (timespan != null)
speed = timespan.getAs(TimePeriod.SECOND);
}
WorldBorder[] worldBorders = this.worldBorders.getAll(event);
if (to) {
for (WorldBorder worldBorder : worldBorders)
worldBorder.setSize(Math2.fit(1, input, MAX_WORLDBORDER_SIZE), speed);
} else {
if (shrink)
input = -input;
for (WorldBorder worldBorder : worldBorders) {
double size = worldBorder.getSize();
size = Math2.fit(1, size + input, MAX_WORLDBORDER_SIZE);
worldBorder.setSize(size, speed);
}
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
builder.append(shrink ? "shrink" : "expand");
builder.append(radius ? "radius" : "diameter");
builder.append("of", worldBorders);
builder.append(to ? "to" : "by");
builder.append(number);
if (timespan != null)
builder.append("over", timespan);
return builder.toString();
}

}
75 changes: 75 additions & 0 deletions src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import io.papermc.paper.event.player.PlayerInventorySlotChangeEvent;
import io.papermc.paper.event.player.PlayerStopUsingItemEvent;
import io.papermc.paper.event.player.PlayerTradeEvent;
import io.papermc.paper.event.world.border.WorldBorderBoundsChangeEvent;
import io.papermc.paper.event.world.border.WorldBorderBoundsChangeFinishEvent;
import io.papermc.paper.event.world.border.WorldBorderCenterChangeEvent;
import org.bukkit.event.Event;
import org.bukkit.event.block.*;
import org.bukkit.event.enchantment.EnchantItemEvent;
Expand Down Expand Up @@ -701,6 +704,41 @@ public class SimpleEvents {
.requiredPlugins("Paper");
}

if (Skript.classExists("io.papermc.paper.event.world.border.WorldBorderEvent")) {
Skript.registerEvent("World Border Bounds Change", SimpleEvent.class, WorldBorderBoundsChangeEvent.class, "[world[ ]]border [bounds] chang(e|ing)")
.description(
"Called when a world border changes its bounds, either over time, or instantly.",
"Note: event-number will be the diameter of the world border."
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
)
.requiredPlugins("Paper 1.16+")
.examples(
"on border bounds change:",
"\tbroadcast \"You better get moving!\""
)
.since("INSERT VERSION");

Skript.registerEvent("World Border Bounds Finish Change", SimpleEvent.class, WorldBorderBoundsChangeFinishEvent.class, "[world[ ]]border [bounds] finish chang(e|ing)")
.description(
"Called when a moving world border has finished its move.",
"Note: event-number will be the diameter of the world border."
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
)
.requiredPlugins("Paper 1.16+")
.examples(
"on border bounds finish change:",
"\tbroadcast \"Get inside the borders!\""
)
.since("INSERT VERSION");

Skript.registerEvent("World Border Center Change", SimpleEvent.class, WorldBorderCenterChangeEvent.class, "[world[ ]]border center chang(e|ing)")
.description("Called when a world border's center has changed.")
.requiredPlugins("Paper 1.16+")
.examples(
"on border center change:",
"\tbroadcast \"The center has moved\""
)
.since("INSERT VERSION");
}

if (Skript.classExists("io.papermc.paper.event.player.PlayerChangeBeaconEffectEvent")) {
Skript.registerEvent("Beacon Change Effect", SimpleEvent.class, PlayerChangeBeaconEffectEvent.class,
"beacon change effect", "beacon effect change", "player chang(e[s]|ing) [of] beacon effect")
Expand Down Expand Up @@ -763,6 +801,43 @@ public class SimpleEvents {
.since("INSERT VERSION");
}

// WorldBorder Events
if (Skript.classExists("io.papermc.paper.event.world.border.WorldBorderEvent")) {
Skript.registerEvent("World Border Bounds Change", SimpleEvent.class, WorldBorderBoundsChangeEvent.class, "world[ ]border [bounds] chang(e|ing)")
.description(
"Called when a world border changes its bounds, either over time, or instantly.",
"Note: Does not get called for virtual borders"
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
)
.requiredPlugins("Paper 1.16+")
Moderocky marked this conversation as resolved.
Show resolved Hide resolved
.examples(
"on worldborder bounds change:",
"\tbroadcast \"The border is changing from a diameter of %past event-number% to %event-number% over %event-timespan%\""
)
.since("INSERT VERSION");

Skript.registerEvent("World Border Bounds Finish Change", SimpleEvent.class, WorldBorderBoundsChangeFinishEvent.class, "world[ ]border [bounds] finish chang(e|ing)")
.description(
"Called when a moving world border has finished its move.",
"Note: Does not get called for virtual borders"
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
)
.requiredPlugins("Paper 1.16+")
.examples(
"on worldborder bounds finish change:",
"\tbroadcast \"Over the past %event-timespan%, the diameter of %event-worldborder% went from %past event-number% to %event-number%\""
)
.since("INSERT VERSION");

Skript.registerEvent("World Border Center Change", SimpleEvent.class, WorldBorderCenterChangeEvent.class, "world[ ]border center chang(e|ing)")
.description("Called when a world border's center has changed.",
"Note: Does not get called for virtual borders"
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
)
.requiredPlugins("Paper 1.16+")
Moderocky marked this conversation as resolved.
Show resolved Hide resolved
.examples(
"on worldborder center change:",
"\tbroadcast \"The center of %event-worldborder% has moved from %past event-location% to %event-location%\""
)
.since("INSERT VERSION");
}
}

}
Loading
Loading