2019-04-24 01:00:24 +00:00
|
|
|
From 117ce0bb3764d81b944e8a9455f9da22c01d732c 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
|
2019-04-24 01:00:24 +00:00
|
|
|
index 0f031e862b..7fa141f52c 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
|
2019-01-01 03:15:55 +00:00
|
|
|
@@ -36,7 +36,7 @@ public class Chunk implements IChunkAccess {
|
2019-04-24 01:00:24 +00:00
|
|
|
private final ChunkSection[] sections;
|
|
|
|
private final BiomeBase[] d;
|
|
|
|
private final Map<BlockPosition, NBTTagCompound> e;
|
|
|
|
- public boolean loaded;
|
|
|
|
+ public boolean loaded; public boolean isLoaded() { return loaded; } // 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;
|
2019-04-24 01:00:24 +00:00
|
|
|
private final ChunkConverter i;
|
|
|
|
@@ -58,11 +58,39 @@ public class Chunk implements IChunkAccess {
|
|
|
|
private final ChunkCoordIntPair loc;
|
|
|
|
private volatile boolean x;
|
|
|
|
|
|
|
|
+ // 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;
|
|
|
|
+ }
|
2019-04-24 01:00:24 +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
|
2019-04-24 01:00:24 +00:00
|
|
|
+
|
|
|
|
public Chunk(World world, ChunkCoordIntPair chunkcoordintpair, BiomeBase[] abiomebase, ChunkConverter chunkconverter, TickList<Block> ticklist, TickList<FluidType> ticklist1, long i, @Nullable ChunkSection[] achunksection, @Nullable Consumer<Chunk> consumer) {
|
|
|
|
this.sections = new ChunkSection[16];
|
|
|
|
this.e = Maps.newHashMap();
|
2018-10-22 23:16:21 +00:00
|
|
|
this.heightMap = Maps.newEnumMap(HeightMap.Type.class);
|
2018-07-04 07:55:24 +00:00
|
|
|
- this.tileEntities = Maps.newHashMap();
|
|
|
|
+ this.tileEntities = new TileEntityHashMap(); // Paper
|
2019-04-24 01:00:24 +00:00
|
|
|
this.l = Maps.newHashMap();
|
|
|
|
this.m = Maps.newHashMap();
|
|
|
|
this.n = new ShortList[16];
|
|
|
|
@@ -357,6 +385,7 @@ public class Chunk implements IChunkAccess {
|
2019-04-06 03:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entity.inChunk = true;
|
|
|
|
+ entity.setCurrentChunk(this); // Paper
|
2019-04-24 01:00:24 +00:00
|
|
|
entity.chunkX = this.loc.x;
|
2018-12-17 05:18:06 +00:00
|
|
|
entity.chunkY = k;
|
2019-04-24 01:00:24 +00:00
|
|
|
entity.chunkZ = this.loc.z;
|
|
|
|
@@ -368,6 +397,7 @@ public class Chunk implements IChunkAccess {
|
2019-04-06 03:08:45 +00:00
|
|
|
((HeightMap) this.heightMap.get(heightmap_type)).a(along);
|
2018-07-04 07:55:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 03:08:45 +00:00
|
|
|
+ public void removeEntity(Entity entity) { this.b(entity); } // Paper - OBFHELPER
|
|
|
|
public void b(Entity entity) {
|
|
|
|
this.a(entity, entity.chunkY);
|
|
|
|
}
|
2019-04-24 01:00:24 +00:00
|
|
|
@@ -380,8 +410,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
|
2019-04-06 03:08:45 +00:00
|
|
|
+ if (entity.currentChunk != null && entity.currentChunk.get() == this) entity.setCurrentChunk(null);
|
2018-07-15 01:53:17 +00:00
|
|
|
+ if (!this.entitySlices[i].remove(entity)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
2018-07-04 07:55:24 +00:00
|
|
|
+ // Paper end
|
|
|
|
}
|
|
|
|
|
2019-04-24 01:00:24 +00:00
|
|
|
@Override
|
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
|
2019-04-24 01:00:24 +00:00
|
|
|
index d636965800..e2b37ed1a0 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
|
2019-04-24 01:00:24 +00:00
|
|
|
@@ -131,7 +131,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
|
|
|
|
private static final DataWatcherObject<Boolean> aB = DataWatcher.a(Entity.class, DataWatcherRegistry.i);
|
|
|
|
private static final DataWatcherObject<Boolean> aC = DataWatcher.a(Entity.class, DataWatcherRegistry.i);
|
|
|
|
protected static final DataWatcherObject<EntityPose> X = DataWatcher.a(Entity.class, DataWatcherRegistry.s);
|
2018-07-29 17:12:39 +00:00
|
|
|
- public boolean inChunk;
|
|
|
|
+ public boolean inChunk; public boolean isAddedToChunk() { return inChunk; } // Paper - OBFHELPER
|
2018-12-17 05:18:06 +00:00
|
|
|
public int chunkX; public int getChunkX() { return chunkX; } // Paper - OBFHELPER
|
|
|
|
public int chunkY; public int getChunkY() { return chunkY; } // Paper - OBFHELPER
|
|
|
|
public int chunkZ; public int getChunkZ() { return chunkZ; } // Paper - OBFHELPER
|
2019-04-24 01:00:24 +00:00
|
|
|
@@ -1665,6 +1665,39 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
|
2018-07-04 07:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Paper start
|
2019-04-06 03:08:45 +00:00
|
|
|
+ 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
|
2019-04-24 01:00:24 +00:00
|
|
|
index 319be67501..12f359a6ba 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
|
2019-04-24 01:00:24 +00:00
|
|
|
@@ -42,6 +42,15 @@ public abstract class TileEntity implements KeyedObject { // Paper
|
2018-07-27 04:44:53 +00:00
|
|
|
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
|
2019-04-24 01:00:24 +00:00
|
|
|
index ee34d42160..c1ad2626a7 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
|
2019-04-24 01:00:24 +00:00
|
|
|
@@ -6,6 +6,7 @@ import com.google.common.collect.Lists;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.UUID;
|
|
|
|
+<<<<<<< HEAD
|
|
|
|
import net.minecraft.server.AxisAlignedBB;
|
|
|
|
import net.minecraft.server.BlockPosition;
|
|
|
|
import net.minecraft.server.DamageSource;
|
|
|
|
@@ -137,6 +138,7 @@ import net.minecraft.server.EntityZombieVillager;
|
|
|
|
import net.minecraft.server.EnumChatFormat;
|
|
|
|
import net.minecraft.server.IChatBaseComponent;
|
|
|
|
import net.minecraft.server.NBTTagCompound;
|
|
|
|
+import org.bukkit.Chunk; // Paper
|
2018-07-04 07:55:24 +00:00
|
|
|
import org.bukkit.EntityEffect;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Server;
|
2019-04-24 01:00:24 +00:00
|
|
|
@@ -173,6 +175,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
|
|
|
|
--
|
2019-04-03 08:51:39 +00:00
|
|
|
2.21.0
|
2018-07-04 07:55:24 +00:00
|
|
|
|