Add static encode/decode for block long keys (#1712)

Should make this API easier to use.
This commit is contained in:
Spottedleaf
2019-04-08 21:08:14 -07:00
parent acde3bb1da
commit 5040cddd28
4 changed files with 78 additions and 29 deletions

View File

@@ -5,7 +5,7 @@ Subject: [PATCH] Add sun related API
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index 6b91635f..3d8ff98a 100644 index 38dae649..107f4173 100644
--- a/src/main/java/org/bukkit/World.java --- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java +++ b/src/main/java/org/bukkit/World.java
@@ -0,0 +0,0 @@ public interface World extends PluginMessageRecipient, Metadatable { @@ -0,0 +0,0 @@ public interface World extends PluginMessageRecipient, Metadatable {

View File

@@ -18,22 +18,29 @@ Y range: [0, 1023]
X, Z range: [-67 108 864, 67 108 863] 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 diff --git a/src/main/java/org/bukkit/Location.java b/src/main/java/org/bukkit/Location.java
index 84b7d93cf..334e31350 100644 index 4c2a269d..2719eecb 100644
--- a/src/main/java/org/bukkit/Location.java --- a/src/main/java/org/bukkit/Location.java
+++ b/src/main/java/org/bukkit/Location.java +++ b/src/main/java/org/bukkit/Location.java
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
// 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;
@@ -0,0 +0,0 @@ public class Location implements Cloneable, ConfigurationSerializable { @@ -0,0 +0,0 @@ public class Location implements Cloneable, ConfigurationSerializable {
blockLoc.setZ(getBlockZ()); blockLoc.setZ(getBlockZ());
return blockLoc; return blockLoc;
} }
+ +
+ // Paper Start + // Paper Start
+
+ /** + /**
+ * @return The block key for this location's block location. + * @return The block key for this location's block location.
+ * @see Block#getBlockKey() + * @see Block#getBlockKey(int, int, int)
+ */ + */
+ public long toBlockKey() { + public long toBlockKey() {
+ return ((long)getBlockX() & 0x7FFFFFF) | (((long)getBlockZ() & 0x7FFFFFF) << 27) | ((long)getBlockY() << 54); + return Block.getBlockKey(getBlockX(), getBlockY(), getBlockZ());
+ } + }
+ // Paper End + // Paper End
+ +
@@ -41,7 +48,7 @@ index 84b7d93cf..334e31350 100644
* @return A new location where X/Y/Z are the center of the block * @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 diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index 1b0744ed9..158917492 100644 index 1b0744ed..fa736b07 100644
--- a/src/main/java/org/bukkit/World.java --- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java +++ b/src/main/java/org/bukkit/World.java
@@ -0,0 +0,0 @@ public interface World extends PluginMessageRecipient, Metadatable { @@ -0,0 +0,0 @@ public interface World extends PluginMessageRecipient, Metadatable {
@@ -54,29 +61,28 @@ index 1b0744ed9..158917492 100644
+ * + *
+ * @param key The block key. See {@link Block#getBlockKey()} + * @param key The block key. See {@link Block#getBlockKey()}
+ * @return Block at the key + * @return Block at the key
+ * @see Location#toBlockKey() + * @see Block#getBlockKey(int, int, int)
+ * @see Block#getBlockKey()
+ */ + */
+ @NotNull + @NotNull
+ public default Block getBlockAtKey(long key) { + public default Block getBlockAtKey(long key) {
+ int x = (int) ((key << 37) >> 37); + int x = Block.getBlockKeyX(key);
+ int y = (int) (key >>> 54); + int y = Block.getBlockKeyY(key);
+ int z = (int) ((key << 10) >> 37); + int z = Block.getBlockKeyZ(key);
+ return getBlockAt(x, y, z); + return getBlockAt(x, y, z);
+ } + }
+
+ /** + /**
+ * Gets the {@link Location} at the given block key + * Gets the {@link Location} at the given block key
+ * + *
+ * @param key The block key. See {@link Location#toBlockKey()} + * @param key The block key. See {@link Location#toBlockKey()}
+ * @return Location at the key + * @return Location at the key
+ * @see Location#toBlockKey() + * @see Block#getBlockKey(int, int, int)
+ * @see Block#getBlockKey()
+ */ + */
+ @NotNull + @NotNull
+ public default Location getLocationAtKey(long key) { + public default Location getLocationAtKey(long key) {
+ int x = (int) ((key << 37) >> 37); + int x = Block.getBlockKeyX(key);
+ int y = (int) (key >>> 54); + int y = Block.getBlockKeyY(key);
+ int z = (int) ((key << 10) >> 37); + int z = Block.getBlockKeyZ(key);
+ return new Location(this, x, y, z); + return new Location(this, x, y, z);
+ } + }
+ // Paper end + // Paper end
@@ -85,7 +91,7 @@ index 1b0744ed9..158917492 100644
* Gets the y coordinate of the lowest block at this position such that the * Gets the y coordinate of the lowest block at this position such that the
* block and all blocks above it are transparent for lighting purposes. * block and all blocks above it are transparent for lighting purposes.
diff --git a/src/main/java/org/bukkit/block/Block.java b/src/main/java/org/bukkit/block/Block.java diff --git a/src/main/java/org/bukkit/block/Block.java b/src/main/java/org/bukkit/block/Block.java
index 708288e99..42f31db29 100644 index 708288e9..c20f903a 100644
--- a/src/main/java/org/bukkit/block/Block.java --- a/src/main/java/org/bukkit/block/Block.java
+++ b/src/main/java/org/bukkit/block/Block.java +++ b/src/main/java/org/bukkit/block/Block.java
@@ -0,0 +0,0 @@ public interface Block extends Metadatable { @@ -0,0 +0,0 @@ public interface Block extends Metadatable {
@@ -93,29 +99,72 @@ index 708288e99..42f31db29 100644
int getZ(); int getZ();
+ // Paper Start + // Paper Start
+ /**
+ * 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());
+ }
+ +
+ /** + /**
+ * Returns this block's coordinates packed into a long value + * Returns the specified block coordinates packed into a long value
+ * <p> + * <p>
+ * The return value can be computed as follows: + * The return value can be computed as follows:
+ * <br> + * <br>
+ * {@code long value = ((long)getX() & 0x7FFFFFF) | (((long)getZ() & 0x7FFFFFF) << 27) | ((long)getY() << 54);} + * {@code long value = ((long)x & 0x7FFFFFF) | (((long)z & 0x7FFFFFF) << 27) | ((long)y << 54);}
+ * </br>
+ * </p> + * </p>
+ * + *
+ * <p> + * <p>
+ * And may be unpacked as follows: + * And may be unpacked as follows:
+ * <br> + * <br>
+ * {@code int x = (int) ((packed << 37) >> 37);} + * {@code int x = (int) ((packed << 37) >> 37);}
+ * </br>
+ * <br> + * <br>
+ * {@code int y = (int) (packed >>> 54);} + * {@code int y = (int) (packed >>> 54);}
+ * </br>
+ * <br> + * <br>
+ * {@code int z = (int) ((packed << 10) >> 37);} + * {@code int z = (int) ((packed << 10) >> 37);}
+ * </br>
+ * </p> + * </p>
+ * + *
+ * @return This block's x, y, and z coordinates packed into a long value + * @return This block's x, y, and z coordinates packed into a long value
+ */ + */
+ public default long getBlockKey() { + public static long getBlockKey(int x, int y, int z) {
+ return ((long)getX() & 0x7FFFFFF) | (((long)getZ() & 0x7FFFFFF) << 27) | ((long)getY() << 54); + 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);
+ } + }
+ // Paper End + // Paper End
+ +

View File

@@ -9,7 +9,7 @@ a ton of noise to plugin developers.
These do not help plugin developers if they bring moise noise than value. These do not help plugin developers if they bring moise noise than value.
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index 6b0a09067..5ed9726c8 100644 index 6b0a0906..5ed9726c 100644
--- a/src/main/java/org/bukkit/Bukkit.java --- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java +++ b/src/main/java/org/bukkit/Bukkit.java
@@ -0,0 +0,0 @@ public final class Bukkit { @@ -0,0 +0,0 @@ public final class Bukkit {
@@ -31,7 +31,7 @@ index 6b0a09067..5ed9726c8 100644
return server.getTag(registry, tag, clazz); return server.getTag(registry, tag, clazz);
} }
diff --git a/src/main/java/org/bukkit/Location.java b/src/main/java/org/bukkit/Location.java diff --git a/src/main/java/org/bukkit/Location.java b/src/main/java/org/bukkit/Location.java
index 57ce443a5..fcb9059d5 100644 index a90f78d5..8352b77c 100644
--- a/src/main/java/org/bukkit/Location.java --- a/src/main/java/org/bukkit/Location.java
+++ b/src/main/java/org/bukkit/Location.java +++ b/src/main/java/org/bukkit/Location.java
@@ -0,0 +0,0 @@ public class Location implements Cloneable, ConfigurationSerializable { @@ -0,0 +0,0 @@ public class Location implements Cloneable, ConfigurationSerializable {
@@ -62,7 +62,7 @@ index 57ce443a5..fcb9059d5 100644
return world; return world;
} }
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
index 66d22ba79..eb23417b7 100644 index 66d22ba7..eb23417b 100644
--- a/src/main/java/org/bukkit/Server.java --- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java +++ b/src/main/java/org/bukkit/Server.java
@@ -0,0 +0,0 @@ public interface Server extends PluginMessageRecipient { @@ -0,0 +0,0 @@ public interface Server extends PluginMessageRecipient {
@@ -84,7 +84,7 @@ index 66d22ba79..eb23417b7 100644
/** /**
diff --git a/src/main/java/org/bukkit/inventory/ItemFactory.java b/src/main/java/org/bukkit/inventory/ItemFactory.java diff --git a/src/main/java/org/bukkit/inventory/ItemFactory.java b/src/main/java/org/bukkit/inventory/ItemFactory.java
index dca77bbaf..56734f8ee 100644 index dca77bba..56734f8e 100644
--- a/src/main/java/org/bukkit/inventory/ItemFactory.java --- a/src/main/java/org/bukkit/inventory/ItemFactory.java
+++ b/src/main/java/org/bukkit/inventory/ItemFactory.java +++ b/src/main/java/org/bukkit/inventory/ItemFactory.java
@@ -0,0 +0,0 @@ package org.bukkit.inventory; @@ -0,0 +0,0 @@ package org.bukkit.inventory;
@@ -105,7 +105,7 @@ index dca77bbaf..56734f8ee 100644
/** /**
diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java
index 1b19f8215..1d3b0a312 100644 index 1b19f821..1d3b0a31 100644
--- a/src/main/java/org/bukkit/inventory/ItemStack.java --- a/src/main/java/org/bukkit/inventory/ItemStack.java
+++ b/src/main/java/org/bukkit/inventory/ItemStack.java +++ b/src/main/java/org/bukkit/inventory/ItemStack.java
@@ -0,0 +0,0 @@ import java.util.Set; @@ -0,0 +0,0 @@ import java.util.Set;

View File

@@ -5,7 +5,7 @@ Subject: [PATCH] isChunkGenerated API
diff --git a/src/main/java/org/bukkit/Location.java b/src/main/java/org/bukkit/Location.java diff --git a/src/main/java/org/bukkit/Location.java b/src/main/java/org/bukkit/Location.java
index 334e31350..57ce443a5 100644 index 2719eecb..a90f78d5 100644
--- a/src/main/java/org/bukkit/Location.java --- a/src/main/java/org/bukkit/Location.java
+++ b/src/main/java/org/bukkit/Location.java +++ b/src/main/java/org/bukkit/Location.java
@@ -0,0 +0,0 @@ @@ -0,0 +0,0 @@
@@ -32,7 +32,7 @@ index 334e31350..57ce443a5 100644
/** /**
* Sets the position of this Location and returns itself * Sets the position of this Location and returns itself
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index 158917492..6b91635fe 100644 index fa736b07..38dae649 100644
--- a/src/main/java/org/bukkit/World.java --- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java +++ b/src/main/java/org/bukkit/World.java
@@ -0,0 +0,0 @@ public interface World extends PluginMessageRecipient, Metadatable { @@ -0,0 +0,0 @@ public interface World extends PluginMessageRecipient, Metadatable {