2020-05-06 11:48:49 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
2018-08-17 23:55:01 +02:00
|
|
|
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
|
|
|
|
Date: Tue, 14 Aug 2018 21:42:10 -0700
|
|
|
|
Subject: [PATCH] Allow Blocks to be accessed via a long key
|
|
|
|
|
|
|
|
The key can be retrieved via methods Location#toBlockKey() and
|
|
|
|
Block#getBlockKey()
|
|
|
|
|
|
|
|
World provides lookup for blocks by long key via method World#getBlockAtKey(long)
|
|
|
|
|
|
|
|
The formatting for the key is as follows:
|
|
|
|
|
|
|
|
10 bit y|27 bit z|27 bit x
|
|
|
|
|
|
|
|
The y value is considered unsigned while z and x are considered two's complement
|
|
|
|
|
|
|
|
Y range: [0, 1023]
|
|
|
|
X, Z range: [-67 108 864, 67 108 863]
|
|
|
|
|
|
|
|
diff --git a/src/main/java/org/bukkit/Location.java b/src/main/java/org/bukkit/Location.java
|
2020-07-23 01:35:44 +02:00
|
|
|
index 369ce9ff6c8bb97a64a8e229115564412e6e7654..e700875beb76dadd55b585aca748338def286908 100644
|
2018-08-17 23:55:01 +02:00
|
|
|
--- a/src/main/java/org/bukkit/Location.java
|
|
|
|
+++ b/src/main/java/org/bukkit/Location.java
|
2019-05-06 04:58:04 +02:00
|
|
|
@@ -15,7 +15,6 @@ import org.jetbrains.annotations.Nullable;
|
2019-04-09 06:08:14 +02:00
|
|
|
|
|
|
|
// Paper start
|
|
|
|
import java.util.Collection;
|
|
|
|
-import java.util.Collections;
|
|
|
|
import java.util.function.Predicate;
|
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.LivingEntity;
|
2019-05-06 04:58:04 +02:00
|
|
|
@@ -605,6 +604,17 @@ public class Location implements Cloneable, ConfigurationSerializable {
|
2018-08-17 23:55:01 +02:00
|
|
|
blockLoc.setZ(getBlockZ());
|
|
|
|
return blockLoc;
|
|
|
|
}
|
|
|
|
+
|
|
|
|
+ // Paper Start
|
|
|
|
+ /**
|
|
|
|
+ * @return The block key for this location's block location.
|
2019-04-09 06:08:14 +02:00
|
|
|
+ * @see Block#getBlockKey(int, int, int)
|
2018-08-17 23:55:01 +02:00
|
|
|
+ */
|
|
|
|
+ public long toBlockKey() {
|
2019-04-09 06:08:14 +02:00
|
|
|
+ return Block.getBlockKey(getBlockX(), getBlockY(), getBlockZ());
|
2018-08-17 23:55:01 +02:00
|
|
|
+ }
|
|
|
|
+ // Paper End
|
|
|
|
+
|
|
|
|
/**
|
|
|
|
* @return A new location where X/Y/Z are the center of the block
|
|
|
|
*/
|
|
|
|
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
|
2020-06-27 23:37:09 +02:00
|
|
|
index eef7750fdebb4fbd156d729e4cc87f7b3d0a2ad4..656615290c3378a853ea298d0a3b5f8d28c6a177 100644
|
2018-08-17 23:55:01 +02:00
|
|
|
--- a/src/main/java/org/bukkit/World.java
|
|
|
|
+++ b/src/main/java/org/bukkit/World.java
|
2020-03-26 03:37:20 +01:00
|
|
|
@@ -90,6 +90,38 @@ public interface World extends PluginMessageRecipient, Metadatable {
|
2019-03-20 01:28:15 +01:00
|
|
|
@NotNull
|
|
|
|
public Block getBlockAt(@NotNull Location location);
|
2018-08-17 23:55:01 +02:00
|
|
|
|
2018-08-19 17:50:40 +02:00
|
|
|
+ // Paper start
|
2018-08-17 23:55:01 +02:00
|
|
|
+ /**
|
|
|
|
+ * Gets the {@link Block} at the given block key
|
|
|
|
+ *
|
|
|
|
+ * @param key The block key. See {@link Block#getBlockKey()}
|
|
|
|
+ * @return Block at the key
|
2019-04-09 06:08:14 +02:00
|
|
|
+ * @see Block#getBlockKey(int, int, int)
|
2018-08-17 23:55:01 +02:00
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-08-17 23:55:01 +02:00
|
|
|
+ public default Block getBlockAtKey(long key) {
|
2019-04-09 06:08:14 +02:00
|
|
|
+ int x = Block.getBlockKeyX(key);
|
|
|
|
+ int y = Block.getBlockKeyY(key);
|
|
|
|
+ int z = Block.getBlockKeyZ(key);
|
2018-08-17 23:55:01 +02:00
|
|
|
+ return getBlockAt(x, y, z);
|
|
|
|
+ }
|
2019-04-09 06:08:14 +02:00
|
|
|
+
|
2018-08-19 17:50:40 +02:00
|
|
|
+ /**
|
|
|
|
+ * Gets the {@link Location} at the given block key
|
|
|
|
+ *
|
|
|
|
+ * @param key The block key. See {@link Location#toBlockKey()}
|
|
|
|
+ * @return Location at the key
|
2019-04-09 06:08:14 +02:00
|
|
|
+ * @see Block#getBlockKey(int, int, int)
|
2018-08-19 17:50:40 +02:00
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-08-19 17:50:40 +02:00
|
|
|
+ public default Location getLocationAtKey(long key) {
|
2019-04-09 06:08:14 +02:00
|
|
|
+ int x = Block.getBlockKeyX(key);
|
|
|
|
+ int y = Block.getBlockKeyY(key);
|
|
|
|
+ int z = Block.getBlockKeyZ(key);
|
2018-08-19 17:50:40 +02:00
|
|
|
+ return new Location(this, x, y, z);
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
2018-08-17 23:55:01 +02:00
|
|
|
+
|
|
|
|
/**
|
2020-01-22 03:02:07 +01:00
|
|
|
* Gets the highest non-empty (impassable) coordinate at the given
|
|
|
|
* coordinates.
|
2018-08-17 23:55:01 +02:00
|
|
|
diff --git a/src/main/java/org/bukkit/block/Block.java b/src/main/java/org/bukkit/block/Block.java
|
2020-06-25 03:10:30 +02:00
|
|
|
index 633733d7c914cf6ae988f727d644c8f2be408b02..d7c7df85228c01529e266ffdd3b3bcdc9ed35774 100644
|
2018-08-17 23:55:01 +02:00
|
|
|
--- a/src/main/java/org/bukkit/block/Block.java
|
|
|
|
+++ b/src/main/java/org/bukkit/block/Block.java
|
2020-04-08 09:49:15 +02:00
|
|
|
@@ -153,6 +153,72 @@ public interface Block extends Metadatable {
|
2018-08-17 23:55:01 +02:00
|
|
|
*/
|
|
|
|
int getZ();
|
|
|
|
|
|
|
|
+ // Paper Start
|
2019-04-09 06:08:14 +02:00
|
|
|
+ /**
|
|
|
|
+ * Returns this block's coordinates packed into a long value.
|
|
|
|
+ * Computed via: {@code Block.getBlockKey(this.getX(), this.getY(), this.getZ())}
|
|
|
|
+ * @see Block#getBlockKey(int, int, int)
|
|
|
|
+ * @return This block's x, y, and z coordinates packed into a long value
|
|
|
|
+ */
|
|
|
|
+ public default long getBlockKey() {
|
|
|
|
+ return Block.getBlockKey(this.getX(), this.getY(), this.getZ());
|
|
|
|
+ }
|
2018-08-17 23:55:01 +02:00
|
|
|
+
|
|
|
|
+ /**
|
2019-04-09 06:08:14 +02:00
|
|
|
+ * Returns the specified block coordinates packed into a long value
|
2018-10-25 15:38:19 +02:00
|
|
|
+ * <p>
|
2018-08-17 23:55:01 +02:00
|
|
|
+ * The return value can be computed as follows:
|
2018-10-25 15:38:19 +02:00
|
|
|
+ * <br>
|
2019-04-09 06:08:14 +02:00
|
|
|
+ * {@code long value = ((long)x & 0x7FFFFFF) | (((long)z & 0x7FFFFFF) << 27) | ((long)y << 54);}
|
2018-10-25 15:38:19 +02:00
|
|
|
+ * </p>
|
|
|
|
+ *
|
|
|
|
+ * <p>
|
2018-08-17 23:55:01 +02:00
|
|
|
+ * And may be unpacked as follows:
|
2019-04-09 06:08:14 +02:00
|
|
|
+ * <br>
|
2018-08-17 23:55:01 +02:00
|
|
|
+ * {@code int x = (int) ((packed << 37) >> 37);}
|
2018-10-25 15:38:19 +02:00
|
|
|
+ * <br>
|
2018-08-17 23:55:01 +02:00
|
|
|
+ * {@code int y = (int) (packed >>> 54);}
|
2018-10-25 15:38:19 +02:00
|
|
|
+ * <br>
|
2018-08-17 23:55:01 +02:00
|
|
|
+ * {@code int z = (int) ((packed << 10) >> 37);}
|
2018-10-25 15:38:19 +02:00
|
|
|
+ * </p>
|
2018-08-17 23:55:01 +02:00
|
|
|
+ *
|
|
|
|
+ * @return This block's x, y, and z coordinates packed into a long value
|
|
|
|
+ */
|
2019-04-09 06:08:14 +02:00
|
|
|
+ public static long getBlockKey(int x, int y, int z) {
|
|
|
|
+ return ((long)x & 0x7FFFFFF) | (((long)z & 0x7FFFFFF) << 27) | ((long)y << 54);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Returns the x component from the packed value.
|
|
|
|
+ * @param packed The packed value, as computed by {@link Block#getBlockKey(int, int, int)}
|
|
|
|
+ * @see Block#getBlockKey(int, int, int)
|
|
|
|
+ * @return The x component from the packed value.
|
|
|
|
+ */
|
|
|
|
+ public static int getBlockKeyX(long packed) {
|
|
|
|
+ return (int) ((packed << 37) >> 37);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Returns the y component from the packed value.
|
|
|
|
+ * @param packed The packed value, as computed by {@link Block#getBlockKey(int, int, int)}
|
|
|
|
+ * @see Block#getBlockKey(int, int, int)
|
|
|
|
+ * @return The y component from the packed value.
|
|
|
|
+ */
|
|
|
|
+ public static int getBlockKeyY(long packed) {
|
|
|
|
+ return (int) (packed >>> 54);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Returns the z component from the packed value.
|
|
|
|
+ * @param packed The packed value, as computed by {@link Block#getBlockKey(int, int, int)}
|
|
|
|
+ * @see Block#getBlockKey(int, int, int)
|
|
|
|
+ * @return The z component from the packed value.
|
|
|
|
+ */
|
|
|
|
+ public static int getBlockKeyZ(long packed) {
|
|
|
|
+ return (int) ((packed << 10) >> 37);
|
2018-08-17 23:55:01 +02:00
|
|
|
+ }
|
|
|
|
+ // Paper End
|
|
|
|
+
|
|
|
|
/**
|
|
|
|
* Gets the Location of the block
|
|
|
|
*
|