Skip to content

Commit

Permalink
Implement client-side world borders
Browse files Browse the repository at this point in the history
  • Loading branch information
mergu committed Nov 10, 2018
1 parent 96253ff commit 225f527
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 7 deletions.
Expand Up @@ -15,6 +15,10 @@

public interface PacketHelper {

void resetWorldBorder(Player player);

void setWorldBorder(Player player, Location center, double size, double currSize, long time, int warningDistance, int warningTime);

void setSlot(Player player, int slot, ItemStack itemStack, boolean playerOnly);

void setFieldOfView(Player player, float fov);
Expand Down
Expand Up @@ -4410,15 +4410,15 @@ public void registerCoreMembers() {

// <--[command]
// @Name WorldBorder
// @Syntax worldborder [<world>] (center:<location>) (size:<#.#>) (damage:<#.#>) (damagebuffer:<#.#>) (warningdistance:<#>) (warningtime:<duration>) (duration:<duration>) (reset)
// @Syntax worldborder [<world>/<player>|...] (center:<location>) (size:<#.#>) (curr_size:<#.#>) (damage:<#.#>) (damagebuffer:<#.#>) (warningdistance:<#>) (warningtime:<duration>) (duration:<duration>) (reset)
// @Required 2
// @Stable stable
// @Short Modifies a world border.
// @Author BlackCoyote
// @Group world
//
// @Description
// Modifies the world border of a specified world.
// Modifies the world border of a specified world or a list of players.
// Optionally, you can specify a duration with the size to change
// the size over the course of the duration.
//
Expand All @@ -4438,9 +4438,13 @@ public void registerCoreMembers() {
// @Usage
// Use to update a world border's center, and then the size over the course of 10 seconds.
// - worldborder <def[world]> center:<def[world].spawn_location> size:100 duration:10s
//
// @Usage
// Use to show a client-side world border to the attached player.
// - worldborder <player> center:<player.location> size:10
// -->
registerCoreMember(WorldBorderCommand.class,
"WORLDBORDER", "worldborder [<world>] (center:<location>) (size:<#.#>) (damage:<#.#>) (damagebuffer:<#.#>) (warningdistance:<#>) (warningtime:<duration>) (duration:<duration>)", 2);
"WORLDBORDER", "worldborder [<world>/<player>|...] (center:<location>) (size:<#.#>) (curr_size:<#.#>) (damage:<#.#>) (damagebuffer:<#.#>) (warningdistance:<#>) (warningtime:<duration>) (duration:<duration>) (reset)", 2);


// <--[command]
Expand Down
@@ -1,17 +1,22 @@
package net.aufdemrand.denizen.scripts.commands.world;

import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Duration;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.WorldBorder;

import java.util.List;

public class WorldBorderCommand extends AbstractCommand {

@Override
Expand Down Expand Up @@ -39,6 +44,11 @@ else if (!scriptEntry.hasObject("size")
&& arg.matchesPrefix("size")) {
scriptEntry.addObject("size", arg.asElement());
}
else if (!scriptEntry.hasObject("curr_size")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)
&& arg.matchesPrefix("curr_size")) {
scriptEntry.addObject("curr_size", arg.asElement());
}
else if (!scriptEntry.hasObject("duration")
&& arg.matchesArgumentType(Duration.class)
&& arg.matchesPrefix("duration")) {
Expand All @@ -58,6 +68,10 @@ else if (!scriptEntry.hasObject("world")
&& arg.matchesArgumentType(dWorld.class)) {
scriptEntry.addObject("world", arg.asType(dWorld.class));
}
else if (!scriptEntry.hasObject("players")
&& arg.matchesArgumentList(dPlayer.class)) {
scriptEntry.addObject("players", arg.asType(dList.class).filter(dPlayer.class));
}
else if (!scriptEntry.hasObject("reset")
&& arg.matches("reset")) {
scriptEntry.addObject("reset", new Element("true"));
Expand All @@ -69,8 +83,8 @@ else if (!scriptEntry.hasObject("reset")

// Check to make sure required arguments have been filled

if (!scriptEntry.hasObject("world")) {
throw new InvalidArgumentsException("Must specify a world!");
if (!scriptEntry.hasObject("world") && !scriptEntry.hasObject("players") ) {
throw new InvalidArgumentsException("Must specify a world or players!");
}

if (!scriptEntry.hasObject("center") && !scriptEntry.hasObject("size")
Expand All @@ -90,8 +104,10 @@ else if (!scriptEntry.hasObject("reset")
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

dWorld world = (dWorld) scriptEntry.getObject("world");
List<dPlayer> players = (List<dPlayer>) scriptEntry.getObject("players");
dLocation center = (dLocation) scriptEntry.getObject("center");
Element size = scriptEntry.getElement("size");
Element currSize = scriptEntry.getElement("curr_size");
Element damage = scriptEntry.getElement("damage");
Element damagebuffer = scriptEntry.getElement("damagebuffer");
Duration duration = scriptEntry.getdObject("duration");
Expand All @@ -101,9 +117,11 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

if (scriptEntry.dbCallShouldDebug()) {

dB.report(scriptEntry, getName(), world.debug()
dB.report(scriptEntry, getName(), (world != null ? world.debug() : "")
+ (players != null ? aH.debugList("Player(s)", players) : "")
+ (center != null ? center.debug() : "")
+ (size != null ? size.debug() : "")
+ (currSize != null ? currSize.debug() : "")
+ (damage != null ? damage.debug() : "")
+ (damagebuffer != null ? damagebuffer.debug() : "")
+ (warningdistance != null ? warningdistance.debug() : "")
Expand All @@ -112,6 +130,30 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

}

// Handle client-side world borders
if (players != null) {
if (reset.asBoolean()) {
for (dPlayer player : players) {
NMSHandler.getInstance().getPacketHelper().resetWorldBorder(player.getPlayerEntity());
}
return;
}

WorldBorder wb;
for (dPlayer player : players) {
wb = player.getWorld().getWorldBorder();
NMSHandler.getInstance().getPacketHelper().setWorldBorder(
player.getPlayerEntity(),
(center != null ? center : wb.getCenter()),
(size != null ? size.asDouble() : wb.getSize()),
(currSize != null ? currSize.asDouble() : wb.getSize()),
duration.getMillis(),
(warningdistance != null ? warningdistance.asInt() : wb.getWarningDistance()),
(warningtime != null ? warningtime.getSecondsAsInt() : wb.getWarningTime()));
}
return;
}

WorldBorder worldborder = world.getWorld().getWorldBorder();

if (reset.asBoolean()) {
Expand All @@ -124,6 +166,9 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
}

if (size != null) {
if (currSize != null) {
worldborder.setSize(currSize.asDouble());
}
worldborder.setSize(size.asDouble(), duration.getSecondsAsInt());
}

Expand Down
Expand Up @@ -33,6 +33,31 @@

public class PacketHelper_v1_10_R1 implements PacketHelper {

@Override
public void resetWorldBorder(Player player) {
WorldBorder wb = ((CraftWorld) player.getWorld()).getHandle().getWorldBorder();
sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setWorldBorder(Player player, Location center, double size, double currSize, long time, int warningDistance, int warningTime) {
WorldBorder wb = new WorldBorder();

wb.world = ((CraftWorld) player.getWorld()).getHandle();
wb.setCenter(center.getX(), center.getZ());
wb.setWarningDistance(warningDistance);
wb.setWarningTime(warningTime);

if (time > 0) {
wb.transitionSizeBetween(currSize, size, time);
}
else {
wb.setSize(size);
}

sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setSlot(Player player, int slot, ItemStack itemStack, boolean playerOnly) {
int windowId = playerOnly ? 0 : ((CraftPlayer) player).getHandle().activeContainer.windowId;
Expand Down
Expand Up @@ -33,6 +33,31 @@

public class PacketHelper_v1_11_R1 implements PacketHelper {

@Override
public void resetWorldBorder(Player player) {
WorldBorder wb = ((CraftWorld) player.getWorld()).getHandle().getWorldBorder();
sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setWorldBorder(Player player, Location center, double size, double currSize, long time, int warningDistance, int warningTime) {
WorldBorder wb = new WorldBorder();

wb.world = ((CraftWorld) player.getWorld()).getHandle();
wb.setCenter(center.getX(), center.getZ());
wb.setWarningDistance(warningDistance);
wb.setWarningTime(warningTime);

if (time > 0) {
wb.transitionSizeBetween(currSize, size, time);
}
else {
wb.setSize(size);
}

sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setSlot(Player player, int slot, ItemStack itemStack, boolean playerOnly) {
int windowId = playerOnly ? 0 : ((CraftPlayer) player).getHandle().activeContainer.windowId;
Expand Down
Expand Up @@ -33,6 +33,31 @@

public class PacketHelper_v1_12_R1 implements PacketHelper {

@Override
public void resetWorldBorder(Player player) {
WorldBorder wb = ((CraftWorld) player.getWorld()).getHandle().getWorldBorder();
sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setWorldBorder(Player player, Location center, double size, double currSize, long time, int warningDistance, int warningTime) {
WorldBorder wb = new WorldBorder();

wb.world = ((CraftWorld) player.getWorld()).getHandle();
wb.setCenter(center.getX(), center.getZ());
wb.setWarningDistance(warningDistance);
wb.setWarningTime(warningTime);

if (time > 0) {
wb.transitionSizeBetween(currSize, size, time);
}
else {
wb.setSize(size);
}

sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setSlot(Player player, int slot, ItemStack itemStack, boolean playerOnly) {
int windowId = playerOnly ? 0 : ((CraftPlayer) player).getHandle().activeContainer.windowId;
Expand Down
Expand Up @@ -4,7 +4,6 @@
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.impl.jnbt.CompoundTag_v1_13_R2;
import net.aufdemrand.denizen.nms.interfaces.PacketHelper;
import net.aufdemrand.denizen.nms.util.ReflectionHelper;
import net.aufdemrand.denizen.nms.util.jnbt.CompoundTag;
import net.aufdemrand.denizen.nms.util.jnbt.ListTag;
import net.aufdemrand.denizen.nms.util.jnbt.Tag;
Expand Down Expand Up @@ -35,6 +34,31 @@

public class PacketHelper_v1_13_R2 implements PacketHelper {

@Override
public void resetWorldBorder(Player player) {
WorldBorder wb = ((CraftWorld) player.getWorld()).getHandle().getWorldBorder();
sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setWorldBorder(Player player, Location center, double size, double currSize, long time, int warningDistance, int warningTime) {
WorldBorder wb = new WorldBorder();

wb.world = ((CraftWorld) player.getWorld()).getHandle();
wb.setCenter(center.getX(), center.getZ());
wb.setWarningDistance(warningDistance);
wb.setWarningTime(warningTime);

if (time > 0) {
wb.transitionSizeBetween(currSize, size, time);
}
else {
wb.setSize(size);
}

sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setSlot(Player player, int slot, ItemStack itemStack, boolean playerOnly) {
int windowId = playerOnly ? 0 : ((CraftPlayer) player).getHandle().activeContainer.windowId;
Expand Down
Expand Up @@ -33,6 +33,31 @@

public class PacketHelper_v1_8_R3 implements PacketHelper {

@Override
public void resetWorldBorder(Player player) {
WorldBorder wb = ((CraftWorld) player.getWorld()).getHandle().getWorldBorder();
sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setWorldBorder(Player player, Location center, double size, double currSize, long time, int warningDistance, int warningTime) {
WorldBorder wb = new WorldBorder();

wb.world = ((CraftWorld) player.getWorld()).getHandle();
wb.setCenter(center.getX(), center.getZ());
wb.setWarningDistance(warningDistance);
wb.setWarningTime(warningTime);

if (time > 0) {
wb.transitionSizeBetween(currSize, size, time);
}
else {
wb.setSize(size);
}

sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setSlot(Player player, int slot, ItemStack itemStack, boolean playerOnly) {
int windowId = playerOnly ? 0 : ((CraftPlayer) player).getHandle().activeContainer.windowId;
Expand Down
Expand Up @@ -35,6 +35,31 @@

public class PacketHelper_v1_9_R2 implements PacketHelper {

@Override
public void resetWorldBorder(Player player) {
WorldBorder wb = ((CraftWorld) player.getWorld()).getHandle().getWorldBorder();
sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setWorldBorder(Player player, Location center, double size, double currSize, long time, int warningDistance, int warningTime) {
WorldBorder wb = new WorldBorder();

wb.world = ((CraftWorld) player.getWorld()).getHandle();
wb.setCenter(center.getX(), center.getZ());
wb.setWarningDistance(warningDistance);
wb.setWarningTime(warningTime);

if (time > 0) {
wb.transitionSizeBetween(currSize, size, time);
}
else {
wb.setSize(size);
}

sendPacket(player, new PacketPlayOutWorldBorder(wb, PacketPlayOutWorldBorder.EnumWorldBorderAction.INITIALIZE));
}

@Override
public void setSlot(Player player, int slot, ItemStack itemStack, boolean playerOnly) {
int windowId = playerOnly ? 0 : ((CraftPlayer) player).getHandle().activeContainer.windowId;
Expand Down

0 comments on commit 225f527

Please sign in to comment.