2018-07-29 16:04:09 +00:00
|
|
|
From ab050c94d0a74f405ae49fce43faf3ee265d9a39 Mon Sep 17 00:00:00 2001
|
2018-07-04 07:55:24 +00:00
|
|
|
From: Aikar <aikar@aikar.co>
|
|
|
|
Date: Wed, 4 Jul 2018 02:10:36 -0400
|
|
|
|
Subject: [PATCH] Store reference to current Chunk for Entity and Block
|
|
|
|
Entities
|
|
|
|
|
|
|
|
This enables us a fast reference to the entities current chunk instead
|
|
|
|
of having to look it up by hashmap lookups.
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
|
2018-07-29 16:04:09 +00:00
|
|
|
index 4bbebb25a..ea167a17b 100644
|
2018-07-04 07:55:24 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/Chunk.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/Chunk.java
|
2018-07-05 22:39:43 +00:00
|
|
|
@@ -25,7 +25,7 @@ public class Chunk {
|
|
|
|
private final byte[] g;
|
|
|
|
private final int[] h;
|
|
|
|
private final boolean[] i;
|
|
|
|
- private boolean j;
|
|
|
|
+ private boolean j; public boolean isLoaded() { return j; } // Paper - OBFHELPER
|
|
|
|
public final World world;
|
|
|
|
public final int[] heightMap;
|
|
|
|
public final int locX;
|
2018-07-04 07:55:24 +00:00
|
|
|
@@ -33,6 +33,30 @@ public class Chunk {
|
|
|
|
private boolean m;
|
|
|
|
public final Map<BlockPosition, TileEntity> tileEntities;
|
|
|
|
public final List<Entity>[] entitySlices; // Spigot
|
|
|
|
+ // Paper start
|
|
|
|
+ private class TileEntityHashMap extends java.util.HashMap<BlockPosition, TileEntity> {
|
|
|
|
+ @Override
|
|
|
|
+ public TileEntity put(BlockPosition key, TileEntity value) {
|
|
|
|
+ TileEntity replaced = super.put(key, value);
|
|
|
|
+ if (replaced != null) {
|
|
|
|
+ replaced.setCurrentChunk(null);
|
|
|
|
+ }
|
|
|
|
+ if (value != null) {
|
|
|
|
+ value.setCurrentChunk(Chunk.this);
|
|
|
|
+ }
|
|
|
|
+ return replaced;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public TileEntity remove(Object key) {
|
|
|
|
+ TileEntity removed = super.remove(key);
|
|
|
|
+ if (removed != null) {
|
|
|
|
+ removed.setCurrentChunk(null);
|
|
|
|
+ }
|
|
|
|
+ return removed;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
private boolean done;
|
|
|
|
private boolean lit;
|
|
|
|
private boolean r;
|
|
|
|
@@ -80,7 +104,7 @@ public class Chunk {
|
|
|
|
this.g = new byte[256];
|
|
|
|
this.h = new int[256];
|
|
|
|
this.i = new boolean[256];
|
|
|
|
- this.tileEntities = Maps.newHashMap();
|
|
|
|
+ this.tileEntities = new TileEntityHashMap(); // Paper
|
|
|
|
this.x = 4096;
|
|
|
|
this.y = Queues.newConcurrentLinkedQueue();
|
|
|
|
this.entitySlices = (List[]) (new List[16]); // Spigot
|
2018-07-15 06:44:37 +00:00
|
|
|
@@ -609,6 +633,9 @@ public class Chunk {
|
|
|
|
entity.ac = k;
|
|
|
|
entity.ad = this.locZ;
|
|
|
|
this.entitySlices[k].add(entity);
|
2018-07-04 07:55:24 +00:00
|
|
|
+ // Paper start
|
|
|
|
+ entity.setCurrentChunk(this);
|
|
|
|
+ // Paper end
|
2018-07-15 06:44:37 +00:00
|
|
|
// Spigot start - increment creature type count
|
|
|
|
// Keep this synced up with World.a(Class)
|
|
|
|
if (entity instanceof EntityInsentient) {
|
|
|
|
@@ -641,6 +668,9 @@ public class Chunk {
|
2018-07-04 07:55:24 +00:00
|
|
|
}
|
2018-07-15 06:44:37 +00:00
|
|
|
|
|
|
|
this.entitySlices[i].remove(entity);
|
2018-07-04 07:55:24 +00:00
|
|
|
+ // Paper start
|
|
|
|
+ entity.setCurrentChunk(null);
|
|
|
|
+ // Paper end
|
2018-07-15 06:44:37 +00:00
|
|
|
// Spigot start - decrement creature type count
|
|
|
|
// Keep this synced up with World.a(Class)
|
|
|
|
if (entity instanceof EntityInsentient) {
|
2018-07-04 07:55:24 +00:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
|
2018-07-29 16:04:09 +00:00
|
|
|
index 06c72b95f..0e3a94ab8 100644
|
2018-07-04 07:55:24 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/Entity.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/Entity.java
|
2018-07-29 16:04:09 +00:00
|
|
|
@@ -121,7 +121,7 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
|
|
|
|
private static final DataWatcherObject<Boolean> aC = DataWatcher.a(Entity.class, DataWatcherRegistry.h);
|
|
|
|
private static final DataWatcherObject<Boolean> aD = DataWatcher.a(Entity.class, DataWatcherRegistry.h);
|
|
|
|
private static final DataWatcherObject<Boolean> aE = DataWatcher.a(Entity.class, DataWatcherRegistry.h);
|
|
|
|
- public boolean aa;
|
|
|
|
+ public boolean aa; public boolean isAddedToChunk() { return aa; } // Paper - OBFHELPER
|
|
|
|
public int ab; public int getChunkX() { return ab; } // Paper - OBFHELPER
|
|
|
|
public int ac; public int getChunkY() { return ac; } // Paper - OBFHELPER
|
|
|
|
public int ad; public int getChunkZ() { return ad; } // Paper - OBFHELPER
|
2018-07-14 04:12:42 +00:00
|
|
|
@@ -1703,6 +1703,38 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
|
2018-07-04 07:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Paper start
|
|
|
|
+ private java.lang.ref.WeakReference<Chunk> currentChunk = null;
|
2018-07-14 04:12:42 +00:00
|
|
|
+
|
|
|
|
+ public void setCurrentChunk(Chunk chunk) {
|
|
|
|
+ this.currentChunk = chunk != null ? new java.lang.ref.WeakReference<>(chunk) : null;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * Returns the entities current registered chunk. If the entity is not added to a chunk yet, it will return null
|
|
|
|
+ */
|
2018-07-04 07:55:24 +00:00
|
|
|
+ public Chunk getCurrentChunk() {
|
2018-07-09 02:39:46 +00:00
|
|
|
+ final Chunk chunk = currentChunk != null ? currentChunk.get() : null;
|
2018-07-29 16:04:09 +00:00
|
|
|
+ return chunk != null && chunk.isLoaded() ? chunk : (isAddedToChunk() ? world.getChunkIfLoaded(getChunkX(), getChunkZ()) : null);
|
2018-07-04 07:55:24 +00:00
|
|
|
+ }
|
2018-07-14 04:12:42 +00:00
|
|
|
+ /**
|
|
|
|
+ * Returns the chunk at the location, using the entities local cache if avail
|
|
|
|
+ * Will only return null if the location specified is not loaded
|
|
|
|
+ */
|
2018-07-12 20:42:56 +00:00
|
|
|
+ public Chunk getCurrentChunkAt(int x, int z) {
|
|
|
|
+ if (getChunkX() == x && getChunkZ() == z) {
|
2018-07-14 04:12:42 +00:00
|
|
|
+ Chunk chunk = getCurrentChunk();
|
|
|
|
+ if (chunk != null) {
|
|
|
|
+ return chunk;
|
|
|
|
+ }
|
2018-07-12 20:42:56 +00:00
|
|
|
+ }
|
2018-07-14 04:12:42 +00:00
|
|
|
+ return world.getChunkIfLoaded(x, z);
|
2018-07-12 20:42:56 +00:00
|
|
|
+ }
|
2018-07-14 04:12:42 +00:00
|
|
|
+ /**
|
|
|
|
+ * Returns the chunk at the entities current location, using the entities local cache if avail
|
|
|
|
+ * Will only return null if the location specified is not loaded
|
|
|
|
+ */
|
2018-07-12 20:42:56 +00:00
|
|
|
+ public Chunk getChunkAtLocation() {
|
|
|
|
+ return getCurrentChunkAt((int)Math.floor(locX) >> 4, (int)Math.floor(locZ) >> 4);
|
2018-07-04 07:55:24 +00:00
|
|
|
+ }
|
2018-07-27 03:57:31 +00:00
|
|
|
private String entityKeyString = null;
|
|
|
|
private MinecraftKey entityKey = getMinecraftKey();
|
2018-07-04 07:55:24 +00:00
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/TileEntity.java b/src/main/java/net/minecraft/server/TileEntity.java
|
2018-07-29 16:04:09 +00:00
|
|
|
index 0176ca530..29069b753 100644
|
2018-07-04 07:55:24 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/TileEntity.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/TileEntity.java
|
2018-07-05 22:39:43 +00:00
|
|
|
@@ -28,6 +28,14 @@ public abstract class TileEntity implements KeyedObject {
|
2018-07-04 07:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Paper start
|
|
|
|
+ private java.lang.ref.WeakReference<Chunk> currentChunk = null;
|
|
|
|
+ public Chunk getCurrentChunk() {
|
2018-07-05 22:39:43 +00:00
|
|
|
+ final Chunk chunk = currentChunk != null ? currentChunk.get() : world.getChunkIfLoaded(position.getX() >> 4, position.getZ() >> 4);
|
|
|
|
+ return chunk != null && chunk.isLoaded() ? chunk : null;
|
2018-07-04 07:55:24 +00:00
|
|
|
+ }
|
|
|
|
+ public void setCurrentChunk(Chunk chunk) {
|
|
|
|
+ this.currentChunk = chunk != null ? new java.lang.ref.WeakReference<>(chunk) : null;
|
|
|
|
+ }
|
2018-07-27 03:57:31 +00:00
|
|
|
private String tileEntityKeyString = null;
|
|
|
|
private MinecraftKey tileEntityKey = getMinecraftKey();
|
2018-07-04 07:55:24 +00:00
|
|
|
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
|
2018-07-29 16:04:09 +00:00
|
|
|
index c5a194ffe..833e3111d 100644
|
2018-07-04 07:55:24 +00:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
|
|
|
|
@@ -9,6 +9,7 @@ import java.util.UUID;
|
|
|
|
|
|
|
|
import net.minecraft.server.*;
|
|
|
|
|
|
|
|
+import org.bukkit.Chunk;
|
|
|
|
import org.bukkit.EntityEffect;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Server;
|
|
|
|
@@ -39,6 +40,12 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
|
|
|
|
this.entity = entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public Chunk getChunk() {
|
|
|
|
+ net.minecraft.server.Chunk currentChunk = entity.getCurrentChunk();
|
|
|
|
+ return currentChunk != null ? currentChunk.bukkitChunk : getLocation().getChunk();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
public static CraftEntity getEntity(CraftServer server, Entity entity) {
|
|
|
|
/**
|
|
|
|
* Order is *EXTREMELY* important -- keep it right! =D
|
|
|
|
--
|
|
|
|
2.18.0
|
|
|
|
|