2018-09-27 02:35:42 +00:00
|
|
|
From ec579a0138fdc32c33ab4b1eef277e559a592b23 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-09-27 02:35:42 +00:00
|
|
|
index f64bfa1b95..16aa7f8f79 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-15 01:53:17 +00:00
|
|
|
@@ -33,7 +33,7 @@ public class Chunk implements IChunkAccess {
|
2018-08-26 18:11:49 +00:00
|
|
|
private final BiomeBase[] f;
|
|
|
|
private final boolean[] g;
|
|
|
|
private final Map<BlockPosition, NBTTagCompound> h;
|
|
|
|
- private boolean i;
|
|
|
|
+ private boolean i;public boolean isLoaded() { return i; } // Paper - OBFHELPER
|
2018-07-05 22:39:43 +00:00
|
|
|
public final World world;
|
2018-07-15 01:53:17 +00:00
|
|
|
public final Map<HeightMap.Type, HeightMap> heightMap;
|
2018-07-05 22:39:43 +00:00
|
|
|
public final int locX;
|
2018-08-26 18:11:49 +00:00
|
|
|
@@ -62,7 +62,30 @@ public class Chunk implements IChunkAccess {
|
2018-07-15 01:53:17 +00:00
|
|
|
// CraftBukkit start - Neighbor loaded cache for chunk lighting and entity ticking
|
|
|
|
private int neighbors = 0x1 << 12;
|
|
|
|
public long chunkKey;
|
2018-07-04 07:55:24 +00:00
|
|
|
+ // 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;
|
|
|
|
+ }
|
2018-07-15 01:53:17 +00:00
|
|
|
|
2018-07-04 07:55:24 +00:00
|
|
|
+ @Override
|
|
|
|
+ public TileEntity remove(Object key) {
|
|
|
|
+ TileEntity removed = super.remove(key);
|
|
|
|
+ if (removed != null) {
|
|
|
|
+ removed.setCurrentChunk(null);
|
|
|
|
+ }
|
|
|
|
+ return removed;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
2018-07-15 01:53:17 +00:00
|
|
|
public boolean areNeighborsLoaded(final int radius) {
|
|
|
|
switch (radius) {
|
|
|
|
case 2:
|
2018-08-26 18:11:49 +00:00
|
|
|
@@ -93,7 +116,7 @@ public class Chunk implements IChunkAccess {
|
|
|
|
this.g = new boolean[256];
|
|
|
|
this.h = Maps.newHashMap();
|
2018-07-15 01:53:17 +00:00
|
|
|
this.heightMap = Maps.newHashMap();
|
2018-07-04 07:55:24 +00:00
|
|
|
- this.tileEntities = Maps.newHashMap();
|
|
|
|
+ this.tileEntities = new TileEntityHashMap(); // Paper
|
2018-08-26 18:11:49 +00:00
|
|
|
this.p = Maps.newHashMap();
|
2018-07-15 01:53:17 +00:00
|
|
|
this.q = Maps.newHashMap();
|
2018-08-26 18:11:49 +00:00
|
|
|
this.r = new ShortList[16];
|
2018-08-27 15:07:22 +00:00
|
|
|
@@ -652,6 +675,9 @@ public class Chunk implements IChunkAccess {
|
2018-07-15 01:53:17 +00:00
|
|
|
entity.af = k;
|
|
|
|
entity.ag = 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 01:53:17 +00:00
|
|
|
public void a(HeightMap.Type heightmap_type, long[] along) {
|
2018-08-27 15:07:22 +00:00
|
|
|
@@ -670,8 +696,12 @@ public class Chunk implements IChunkAccess {
|
2018-07-15 01:53:17 +00:00
|
|
|
if (i >= this.entitySlices.length) {
|
|
|
|
i = this.entitySlices.length - 1;
|
2018-07-04 07:55:24 +00:00
|
|
|
}
|
2018-07-15 01:53:17 +00:00
|
|
|
-
|
|
|
|
- this.entitySlices[i].remove(entity);
|
2018-07-04 07:55:24 +00:00
|
|
|
+ // Paper start
|
2018-07-15 01:53:17 +00:00
|
|
|
+ if (!this.entitySlices[i].remove(entity)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
2018-07-04 07:55:24 +00:00
|
|
|
+ entity.setCurrentChunk(null);
|
|
|
|
+ // Paper end
|
|
|
|
}
|
|
|
|
|
2018-07-15 01:53:17 +00:00
|
|
|
public boolean c(BlockPosition blockposition) {
|
2018-09-27 02:35:42 +00:00
|
|
|
@@ -849,6 +879,7 @@ public class Chunk implements IChunkAccess {
|
2018-07-30 04:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Spigot End
|
|
|
|
+ entity.setCurrentChunk(null); // Paper
|
|
|
|
|
|
|
|
// Do not pass along players, as doing so can get them stuck outside of time.
|
|
|
|
// (which for example disables inventory icon updates and prevents block breaking)
|
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-09-27 02:35:42 +00:00
|
|
|
index 19a3ba79f2..aa9e4ef5ee 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-31 01:19:41 +00:00
|
|
|
@@ -134,7 +134,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
|
2018-07-29 17:12:39 +00:00
|
|
|
private static final DataWatcherObject<Boolean> aF = DataWatcher.a(Entity.class, DataWatcherRegistry.i);
|
|
|
|
private static final DataWatcherObject<Boolean> aG = DataWatcher.a(Entity.class, DataWatcherRegistry.i);
|
|
|
|
private static final DataWatcherObject<Boolean> aH = DataWatcher.a(Entity.class, DataWatcherRegistry.i);
|
|
|
|
- public boolean inChunk;
|
|
|
|
+ public boolean inChunk; public boolean isAddedToChunk() { return inChunk; } // Paper - OBFHELPER
|
|
|
|
public int ae; public int getChunkX() { return ae; } // Paper - OBFHELPER
|
|
|
|
public int af; public int getChunkY() { return af; } // Paper - OBFHELPER
|
|
|
|
public int ag; public int getChunkZ() { return ag; } // Paper - OBFHELPER
|
2018-09-27 02:35:42 +00:00
|
|
|
@@ -1762,6 +1762,39 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
|
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-15 01:53:17 +00:00
|
|
|
+
|
2018-07-27 04:44:53 +00:00
|
|
|
private MinecraftKey entityKey;
|
|
|
|
private String entityKeyString;
|
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-09-03 08:14:40 +00:00
|
|
|
index 7390061bf0..c69209497b 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-27 04:44:53 +00:00
|
|
|
@@ -41,6 +41,15 @@ public abstract class TileEntity implements KeyedObject { // Paper
|
|
|
|
getMinecraftKey(); // Try to load if it doesn't exists.
|
2018-07-15 01:53:17 +00:00
|
|
|
return tileEntityKeyString;
|
2018-07-04 07:55:24 +00:00
|
|
|
}
|
2018-07-15 01:53:17 +00:00
|
|
|
+
|
2018-07-04 07:55:24 +00:00
|
|
|
+ private java.lang.ref.WeakReference<Chunk> currentChunk = null;
|
|
|
|
+ public Chunk getCurrentChunk() {
|
2018-07-27 04:44:53 +00:00
|
|
|
+ final Chunk chunk = currentChunk != null ? currentChunk.get() : null;
|
2018-07-05 22:39:43 +00:00
|
|
|
+ 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-15 01:53:17 +00:00
|
|
|
// Paper end
|
2018-07-04 07:55:24 +00:00
|
|
|
|
2018-07-15 01:53:17 +00:00
|
|
|
@Nullable
|
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-09-03 08:14:40 +00:00
|
|
|
index 5cce3644d0..7a204e8edf 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;
|
2018-09-03 08:14:40 +00:00
|
|
|
@@ -42,6 +43,12 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
|
2018-07-04 07:55:24 +00:00
|
|
|
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
|
|
|
|
--
|
2018-09-27 02:35:42 +00:00
|
|
|
2.19.0
|
2018-07-04 07:55:24 +00:00
|
|
|
|