From 8e91d8a6adb2d17e8a39fa424556d7f0c75f9e57 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Fri, 21 Oct 2016 00:45:40 +1100 Subject: [PATCH] Potential fix for crash --- .gitignore | 3 +- .../fawe/bukkit/v1_10/BukkitChunk_1_10.java | 146 +++++++++-------- .../fawe/bukkit/v1_7/BukkitChunk_1_7.java | 117 ++++++++------ .../fawe/bukkit/v1_8/BukkitChunk_1_8.java | 117 ++++++++------ .../fawe/bukkit/v1_9/BukkitChunk_1_9.java | 147 +++++++++--------- .../java/com/boydti/fawe/util/FaweTimer.java | 2 + .../java/com/boydti/fawe/util/SetQueue.java | 11 +- 7 files changed, 286 insertions(+), 257 deletions(-) diff --git a/.gitignore b/.gitignore index 9f79b0b1..9ae1bb7f 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,5 @@ gradle.log /bukkit19/build /bukkit18/build build -/mvn \ No newline at end of file +/mvn +spigot-1.10 \ No newline at end of file diff --git a/bukkit110/src/main/java/com/boydti/fawe/bukkit/v1_10/BukkitChunk_1_10.java b/bukkit110/src/main/java/com/boydti/fawe/bukkit/v1_10/BukkitChunk_1_10.java index 0fc62c25..edcab622 100644 --- a/bukkit110/src/main/java/com/boydti/fawe/bukkit/v1_10/BukkitChunk_1_10.java +++ b/bukkit110/src/main/java/com/boydti/fawe/bukkit/v1_10/BukkitChunk_1_10.java @@ -2,6 +2,7 @@ package com.boydti.fawe.bukkit.v1_10; import com.boydti.fawe.Fawe; import com.boydti.fawe.FaweCache; +import com.boydti.fawe.bukkit.v0.BukkitQueue_0; import com.boydti.fawe.example.CharFaweChunk; import com.boydti.fawe.object.BytePair; import com.boydti.fawe.object.FaweChunk; @@ -9,35 +10,12 @@ import com.boydti.fawe.object.FaweQueue; import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.ReflectionUtils; -import com.sk89q.jnbt.CompoundTag; -import com.sk89q.jnbt.ListTag; -import com.sk89q.jnbt.LongTag; -import com.sk89q.jnbt.StringTag; -import com.sk89q.jnbt.Tag; +import com.sk89q.jnbt.*; import com.sk89q.worldedit.internal.Constants; import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; -import java.util.UUID; -import net.minecraft.server.v1_10_R1.Block; -import net.minecraft.server.v1_10_R1.BlockPosition; -import net.minecraft.server.v1_10_R1.ChunkSection; -import net.minecraft.server.v1_10_R1.DataBits; -import net.minecraft.server.v1_10_R1.DataPalette; -import net.minecraft.server.v1_10_R1.DataPaletteBlock; -import net.minecraft.server.v1_10_R1.DataPaletteGlobal; -import net.minecraft.server.v1_10_R1.Entity; -import net.minecraft.server.v1_10_R1.EntityPlayer; -import net.minecraft.server.v1_10_R1.EntityTypes; -import net.minecraft.server.v1_10_R1.IBlockData; -import net.minecraft.server.v1_10_R1.NBTTagCompound; -import net.minecraft.server.v1_10_R1.TileEntity; +import java.util.*; +import net.minecraft.server.v1_10_R1.*; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.craftbukkit.v1_10_R1.CraftChunk; @@ -180,70 +158,86 @@ public class BukkitChunk_1_10 extends CharFaweChunk { if (count == 0) { continue; } else if (count >= 4096) { - entities[i].clear(); + Collection ents = entities[i]; + if (!ents.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + ents.clear(); + } + } } else { - char[] array = this.getIdArray(i); - Collection ents = new ArrayList<>(entities[i]); - for (Entity entity : ents) { - if (entity instanceof EntityPlayer) { - continue; - } - int x = ((int) Math.round(entity.locX) & 15); - int z = ((int) Math.round(entity.locZ) & 15); - int y = (int) Math.round(entity.locY); - if (array == null || y < 0 || y > 255) { - continue; - } - if (y < 0 || y > 255 || array[FaweCache.CACHE_J[y][z][x]] != 0) { - nmsWorld.removeEntity(entity); + Collection ents = entities[i]; + if (!ents.isEmpty()) { + char[] array = this.getIdArray(i); + ents = new ArrayList<>(entities[i]); + synchronized (BukkitQueue_0.adapter) { + for (Entity entity : ents) { + if (entity instanceof EntityPlayer) { + continue; + } + int x = ((int) Math.round(entity.locX) & 15); + int z = ((int) Math.round(entity.locZ) & 15); + int y = (int) Math.round(entity.locY); + if (array == null || y < 0 || y > 255) { + continue; + } + if (y < 0 || y > 255 || array[FaweCache.CACHE_J[y][z][x]] != 0) { + nmsWorld.removeEntity(entity); + } + } } } } } HashSet entsToRemove = this.getEntityRemoves(); - if (entsToRemove.size() > 0) { - for (int i = 0; i < entities.length; i++) { - Collection ents = new ArrayList<>(entities[i]); - for (Entity entity : ents) { - if (entsToRemove.contains(entity.getUniqueID())) { - nmsWorld.removeEntity(entity); + if (!entsToRemove.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + for (int i = 0; i < entities.length; i++) { + Collection ents = new ArrayList<>(entities[i]); + for (Entity entity : ents) { + if (entsToRemove.contains(entity.getUniqueID())) { + nmsWorld.removeEntity(entity); + } } } } } // Set entities - Set createdEntities = new HashSet<>(); Set entitiesToSpawn = this.getEntities(); - for (CompoundTag nativeTag : entitiesToSpawn) { - Map entityTagMap = ReflectionUtils.getMap(nativeTag.getValue()); - StringTag idTag = (StringTag) entityTagMap.get("Id"); - ListTag posTag = (ListTag) entityTagMap.get("Pos"); - ListTag rotTag = (ListTag) entityTagMap.get("Rotation"); - if (idTag == null || posTag == null || rotTag == null) { - Fawe.debug("Unknown entity tag: " + nativeTag); - continue; - } - double x = posTag.getDouble(0); - double y = posTag.getDouble(1); - double z = posTag.getDouble(2); - float yaw = rotTag.getFloat(0); - float pitch = rotTag.getFloat(1); - String id = idTag.getValue(); - Entity entity = EntityTypes.createEntityByName(id, nmsWorld); - if (entity != null) { - UUID uuid = entity.getUniqueID(); - entityTagMap.put("UUIDMost", new LongTag(uuid.getMostSignificantBits())); - entityTagMap.put("UUIDLeast", new LongTag(uuid.getLeastSignificantBits())); - if (nativeTag != null) { - NBTTagCompound tag = (NBTTagCompound) BukkitQueue_1_10.methodFromNative.invoke(BukkitQueue_1_10.adapter, nativeTag); - for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { - tag.remove(name); + Set createdEntities = new HashSet<>(); + if (!entitiesToSpawn.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + for (CompoundTag nativeTag : entitiesToSpawn) { + Map entityTagMap = ReflectionUtils.getMap(nativeTag.getValue()); + StringTag idTag = (StringTag) entityTagMap.get("Id"); + ListTag posTag = (ListTag) entityTagMap.get("Pos"); + ListTag rotTag = (ListTag) entityTagMap.get("Rotation"); + if (idTag == null || posTag == null || rotTag == null) { + Fawe.debug("Unknown entity tag: " + nativeTag); + continue; + } + double x = posTag.getDouble(0); + double y = posTag.getDouble(1); + double z = posTag.getDouble(2); + float yaw = rotTag.getFloat(0); + float pitch = rotTag.getFloat(1); + String id = idTag.getValue(); + Entity entity = EntityTypes.createEntityByName(id, nmsWorld); + if (entity != null) { + UUID uuid = entity.getUniqueID(); + entityTagMap.put("UUIDMost", new LongTag(uuid.getMostSignificantBits())); + entityTagMap.put("UUIDLeast", new LongTag(uuid.getLeastSignificantBits())); + if (nativeTag != null) { + NBTTagCompound tag = (NBTTagCompound) BukkitQueue_1_10.methodFromNative.invoke(BukkitQueue_1_10.adapter, nativeTag); + for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { + tag.remove(name); + } + entity.f(tag); + } + entity.setLocation(x, y, z, yaw, pitch); + nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM); + createdEntities.add(entity.getUniqueID()); } - entity.f(tag); } - entity.setLocation(x, y, z, yaw, pitch); - nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM); - createdEntities.add(entity.getUniqueID()); } } // Change task? diff --git a/bukkit1710/src/main/java/com/boydti/fawe/bukkit/v1_7/BukkitChunk_1_7.java b/bukkit1710/src/main/java/com/boydti/fawe/bukkit/v1_7/BukkitChunk_1_7.java index b35183d1..d0099353 100644 --- a/bukkit1710/src/main/java/com/boydti/fawe/bukkit/v1_7/BukkitChunk_1_7.java +++ b/bukkit1710/src/main/java/com/boydti/fawe/bukkit/v1_7/BukkitChunk_1_7.java @@ -2,6 +2,7 @@ package com.boydti.fawe.bukkit.v1_7; import com.boydti.fawe.Fawe; import com.boydti.fawe.FaweCache; +import com.boydti.fawe.bukkit.v0.BukkitQueue_0; import com.boydti.fawe.example.CharFaweChunk; import com.boydti.fawe.object.BytePair; import com.boydti.fawe.object.FaweChunk; @@ -161,22 +162,45 @@ public class BukkitChunk_1_7 extends CharFaweChunk { if (count == 0) { continue; } else if (count >= 4096) { - entities[i].clear(); + Collection ents = entities[i]; + if (!ents.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + ents.clear(); + } + } } else { - char[] array = this.getIdArray(i); - Collection ents = new ArrayList<>(entities[i]); - for (Entity entity : ents) { - if (entity instanceof EntityPlayer) { - continue; + Collection ents = entities[i]; + if (!ents.isEmpty()) { + char[] array = this.getIdArray(i); + ents = new ArrayList<>(entities[i]); + synchronized (BukkitQueue_0.adapter) { + for (Entity entity : ents) { + if (entity instanceof EntityPlayer) { + continue; + } + int x = ((int) Math.round(entity.locX) & 15); + int z = ((int) Math.round(entity.locZ) & 15); + int y = (int) Math.round(entity.locY); + if (array == null || y < 0 || y > 255) { + continue; + } + if (y < 0 || y > 255 || array[FaweCache.CACHE_J[y][z][x]] != 0) { + nmsWorld.removeEntity(entity); + } + } } - int x = ((int) Math.round(entity.locX) & 15); - int z = ((int) Math.round(entity.locZ) & 15); - int y = (int) Math.round(entity.locY); - if (array == null) { - continue; - } - if (y < 0 || y > 255 || array[FaweCache.CACHE_J[y][z][x]] != 0) { - nmsWorld.removeEntity(entity); + } + } + } + HashSet entsToRemove = this.getEntityRemoves(); + if (!entsToRemove.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + for (int i = 0; i < entities.length; i++) { + Collection ents = new ArrayList<>(entities[i]); + for (Entity entity : ents) { + if (entsToRemove.contains(entity.getUniqueID())) { + nmsWorld.removeEntity(entity); + } } } } @@ -184,33 +208,37 @@ public class BukkitChunk_1_7 extends CharFaweChunk { // Set entities Set createdEntities = new HashSet<>(); Set entitiesToSpawn = this.getEntities(); - for (CompoundTag nativeTag : entitiesToSpawn) { - Map entityTagMap = nativeTag.getValue(); - StringTag idTag = (StringTag) entityTagMap.get("Id"); - ListTag posTag = (ListTag) entityTagMap.get("Pos"); - ListTag rotTag = (ListTag) entityTagMap.get("Rotation"); - if (idTag == null || posTag == null || rotTag == null) { - Fawe.debug("Unknown entity tag: " + nativeTag); - continue; - } - double x = posTag.getDouble(0); - double y = posTag.getDouble(1); - double z = posTag.getDouble(2); - float yaw = rotTag.getFloat(0); - float pitch = rotTag.getFloat(1); - String id = idTag.getValue(); - Entity entity = EntityTypes.createEntityByName(id, nmsWorld); - if (entity != null) { - if (nativeTag != null) { - NBTTagCompound tag = (NBTTagCompound) BukkitQueue17.methodFromNative.invoke(BukkitQueue17.adapter, nativeTag); - for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { - tag.remove(name); + if (!entitiesToSpawn.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + for (CompoundTag nativeTag : entitiesToSpawn) { + Map entityTagMap = nativeTag.getValue(); + StringTag idTag = (StringTag) entityTagMap.get("Id"); + ListTag posTag = (ListTag) entityTagMap.get("Pos"); + ListTag rotTag = (ListTag) entityTagMap.get("Rotation"); + if (idTag == null || posTag == null || rotTag == null) { + Fawe.debug("Unknown entity tag: " + nativeTag); + continue; + } + double x = posTag.getDouble(0); + double y = posTag.getDouble(1); + double z = posTag.getDouble(2); + float yaw = rotTag.getFloat(0); + float pitch = rotTag.getFloat(1); + String id = idTag.getValue(); + Entity entity = EntityTypes.createEntityByName(id, nmsWorld); + if (entity != null) { + if (nativeTag != null) { + NBTTagCompound tag = (NBTTagCompound) BukkitQueue17.methodFromNative.invoke(BukkitQueue17.adapter, nativeTag); + for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { + tag.remove(name); + } + entity.f(tag); + } + entity.setLocation(x, y, z, yaw, pitch); + nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM); + createdEntities.add(entity.getUniqueID()); } - entity.f(tag); } - entity.setLocation(x, y, z, yaw, pitch); - nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM); - createdEntities.add(entity.getUniqueID()); } } // Run change task if applicable @@ -251,17 +279,6 @@ public class BukkitChunk_1_7 extends CharFaweChunk { } } - HashSet entsToRemove = this.getEntityRemoves(); - if (entsToRemove.size() > 0) { - for (int i = 0; i < entities.length; i++) { - Collection ents = new ArrayList<>(entities[i]); - for (Entity entity : ents) { - if (entsToRemove.contains(entity.getUniqueID())) { - nmsWorld.removeEntity(entity); - } - } - } - } // Set blocks for (int j = 0; j < sections.length; j++) { if (this.getCount(j) == 0) { diff --git a/bukkit18/src/main/java/com/boydti/fawe/bukkit/v1_8/BukkitChunk_1_8.java b/bukkit18/src/main/java/com/boydti/fawe/bukkit/v1_8/BukkitChunk_1_8.java index 0cb84beb..4090a068 100644 --- a/bukkit18/src/main/java/com/boydti/fawe/bukkit/v1_8/BukkitChunk_1_8.java +++ b/bukkit18/src/main/java/com/boydti/fawe/bukkit/v1_8/BukkitChunk_1_8.java @@ -2,6 +2,7 @@ package com.boydti.fawe.bukkit.v1_8; import com.boydti.fawe.Fawe; import com.boydti.fawe.FaweCache; +import com.boydti.fawe.bukkit.v0.BukkitQueue_0; import com.boydti.fawe.example.CharFaweChunk; import com.boydti.fawe.object.BytePair; import com.boydti.fawe.object.FaweChunk; @@ -75,22 +76,45 @@ public class BukkitChunk_1_8 extends CharFaweChunk { if (count == 0) { continue; } else if (count >= 4096) { - entities[i].clear(); + Collection ents = entities[i]; + if (!ents.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + ents.clear(); + } + } } else { - char[] array = this.getIdArray(i); - Collection ents = new ArrayList<>(entities[i]); - for (Entity entity : ents) { - if (entity instanceof EntityPlayer) { - continue; + Collection ents = entities[i]; + if (!ents.isEmpty()) { + char[] array = this.getIdArray(i); + ents = new ArrayList<>(entities[i]); + synchronized (BukkitQueue_0.adapter) { + for (Entity entity : ents) { + if (entity instanceof EntityPlayer) { + continue; + } + int x = ((int) Math.round(entity.locX) & 15); + int z = ((int) Math.round(entity.locZ) & 15); + int y = (int) Math.round(entity.locY); + if (array == null || y < 0 || y > 255) { + continue; + } + if (y < 0 || y > 255 || array[FaweCache.CACHE_J[y][z][x]] != 0) { + nmsWorld.removeEntity(entity); + } + } } - int x = ((int) Math.round(entity.locX) & 15); - int z = ((int) Math.round(entity.locZ) & 15); - int y = (int) Math.round(entity.locY); - if (array == null) { - continue; - } - if (y < 0 || y > 255 || array[FaweCache.CACHE_J[y][z][x]] != 0) { - nmsWorld.removeEntity(entity); + } + } + } + HashSet entsToRemove = this.getEntityRemoves(); + if (!entsToRemove.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + for (int i = 0; i < entities.length; i++) { + Collection ents = new ArrayList<>(entities[i]); + for (Entity entity : ents) { + if (entsToRemove.contains(entity.getUniqueID())) { + nmsWorld.removeEntity(entity); + } } } } @@ -98,33 +122,37 @@ public class BukkitChunk_1_8 extends CharFaweChunk { // Set entities Set createdEntities = new HashSet<>(); Set entitiesToSpawn = this.getEntities(); - for (CompoundTag nativeTag : entitiesToSpawn) { - Map entityTagMap = nativeTag.getValue(); - StringTag idTag = (StringTag) entityTagMap.get("Id"); - ListTag posTag = (ListTag) entityTagMap.get("Pos"); - ListTag rotTag = (ListTag) entityTagMap.get("Rotation"); - if (idTag == null || posTag == null || rotTag == null) { - Fawe.debug("Unknown entity tag: " + nativeTag); - continue; - } - double x = posTag.getDouble(0); - double y = posTag.getDouble(1); - double z = posTag.getDouble(2); - float yaw = rotTag.getFloat(0); - float pitch = rotTag.getFloat(1); - String id = idTag.getValue(); - Entity entity = EntityTypes.createEntityByName(id, nmsWorld); - if (entity != null) { - if (nativeTag != null) { - NBTTagCompound tag = (NBTTagCompound)BukkitQueue18R3.methodFromNative.invoke(BukkitQueue18R3.adapter, nativeTag); - for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { - tag.remove(name); + if (!entitiesToSpawn.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + for (CompoundTag nativeTag : entitiesToSpawn) { + Map entityTagMap = nativeTag.getValue(); + StringTag idTag = (StringTag) entityTagMap.get("Id"); + ListTag posTag = (ListTag) entityTagMap.get("Pos"); + ListTag rotTag = (ListTag) entityTagMap.get("Rotation"); + if (idTag == null || posTag == null || rotTag == null) { + Fawe.debug("Unknown entity tag: " + nativeTag); + continue; + } + double x = posTag.getDouble(0); + double y = posTag.getDouble(1); + double z = posTag.getDouble(2); + float yaw = rotTag.getFloat(0); + float pitch = rotTag.getFloat(1); + String id = idTag.getValue(); + Entity entity = EntityTypes.createEntityByName(id, nmsWorld); + if (entity != null) { + if (nativeTag != null) { + NBTTagCompound tag = (NBTTagCompound) BukkitQueue18R3.methodFromNative.invoke(BukkitQueue18R3.adapter, nativeTag); + for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { + tag.remove(name); + } + entity.f(tag); + } + entity.setLocation(x, y, z, yaw, pitch); + nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM); + createdEntities.add(entity.getUniqueID()); } - entity.f(tag); } - entity.setLocation(x, y, z, yaw, pitch); - nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM); - createdEntities.add(entity.getUniqueID()); } } // Run change task if applicable @@ -165,17 +193,6 @@ public class BukkitChunk_1_8 extends CharFaweChunk { } } - HashSet entsToRemove = this.getEntityRemoves(); - if (entsToRemove.size() > 0) { - for (int i = 0; i < entities.length; i++) { - Collection ents = new ArrayList<>(entities[i]); - for (Entity entity : ents) { - if (entsToRemove.contains(entity.getUniqueID())) { - nmsWorld.removeEntity(entity); - } - } - } - } // Set blocks for (int j = 0; j < sections.length; j++) { if (this.getCount(j) == 0) { diff --git a/bukkit19/src/main/java/com/boydti/fawe/bukkit/v1_9/BukkitChunk_1_9.java b/bukkit19/src/main/java/com/boydti/fawe/bukkit/v1_9/BukkitChunk_1_9.java index 57192c2b..6a1f6f21 100644 --- a/bukkit19/src/main/java/com/boydti/fawe/bukkit/v1_9/BukkitChunk_1_9.java +++ b/bukkit19/src/main/java/com/boydti/fawe/bukkit/v1_9/BukkitChunk_1_9.java @@ -2,6 +2,7 @@ package com.boydti.fawe.bukkit.v1_9; import com.boydti.fawe.Fawe; import com.boydti.fawe.FaweCache; +import com.boydti.fawe.bukkit.v0.BukkitQueue_0; import com.boydti.fawe.example.CharFaweChunk; import com.boydti.fawe.object.BytePair; import com.boydti.fawe.object.FaweChunk; @@ -9,36 +10,12 @@ import com.boydti.fawe.object.FaweQueue; import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.ReflectionUtils; -import com.sk89q.jnbt.CompoundTag; -import com.sk89q.jnbt.ListTag; -import com.sk89q.jnbt.LongTag; -import com.sk89q.jnbt.StringTag; -import com.sk89q.jnbt.Tag; +import com.sk89q.jnbt.*; import com.sk89q.worldedit.internal.Constants; import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; -import java.util.UUID; -import net.minecraft.server.v1_9_R2.Block; -import net.minecraft.server.v1_9_R2.BlockPosition; -import net.minecraft.server.v1_9_R2.Blocks; -import net.minecraft.server.v1_9_R2.ChunkSection; -import net.minecraft.server.v1_9_R2.DataBits; -import net.minecraft.server.v1_9_R2.DataPalette; -import net.minecraft.server.v1_9_R2.DataPaletteBlock; -import net.minecraft.server.v1_9_R2.DataPaletteGlobal; -import net.minecraft.server.v1_9_R2.Entity; -import net.minecraft.server.v1_9_R2.EntityPlayer; -import net.minecraft.server.v1_9_R2.EntityTypes; -import net.minecraft.server.v1_9_R2.IBlockData; -import net.minecraft.server.v1_9_R2.NBTTagCompound; -import net.minecraft.server.v1_9_R2.TileEntity; +import java.util.*; +import net.minecraft.server.v1_9_R2.*; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.block.Biome; @@ -183,70 +160,86 @@ public class BukkitChunk_1_9 extends CharFaweChunk { if (count == 0) { continue; } else if (count >= 4096) { - entities[i].clear(); + Collection ents = entities[i]; + if (!ents.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + ents.clear(); + } + } } else { - char[] array = this.getIdArray(i); - Collection ents = new ArrayList<>(entities[i]); - for (Entity entity : ents) { - if (entity instanceof EntityPlayer) { - continue; - } - int x = ((int) Math.round(entity.locX) & 15); - int z = ((int) Math.round(entity.locZ) & 15); - int y = (int) Math.round(entity.locY); - if (array == null || y < 0 || y > 255) { - continue; - } - if (y < 0 || y > 255 || array[FaweCache.CACHE_J[y][z][x]] != 0) { - nmsWorld.removeEntity(entity); + Collection ents = entities[i]; + if (!ents.isEmpty()) { + char[] array = this.getIdArray(i); + ents = new ArrayList<>(entities[i]); + synchronized (BukkitQueue_0.adapter) { + for (Entity entity : ents) { + if (entity instanceof EntityPlayer) { + continue; + } + int x = ((int) Math.round(entity.locX) & 15); + int z = ((int) Math.round(entity.locZ) & 15); + int y = (int) Math.round(entity.locY); + if (array == null || y < 0 || y > 255) { + continue; + } + if (y < 0 || y > 255 || array[FaweCache.CACHE_J[y][z][x]] != 0) { + nmsWorld.removeEntity(entity); + } + } } } } } HashSet entsToRemove = this.getEntityRemoves(); - if (entsToRemove.size() > 0) { - for (int i = 0; i < entities.length; i++) { - Collection ents = new ArrayList<>(entities[i]); - for (Entity entity : ents) { - if (entsToRemove.contains(entity.getUniqueID())) { - nmsWorld.removeEntity(entity); + if (!entsToRemove.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + for (int i = 0; i < entities.length; i++) { + Collection ents = new ArrayList<>(entities[i]); + for (Entity entity : ents) { + if (entsToRemove.contains(entity.getUniqueID())) { + nmsWorld.removeEntity(entity); + } } } } } // Set entities - Set createdEntities = new HashSet<>(); Set entitiesToSpawn = this.getEntities(); - for (CompoundTag nativeTag : entitiesToSpawn) { - Map entityTagMap = ReflectionUtils.getMap(nativeTag.getValue()); - StringTag idTag = (StringTag) entityTagMap.get("Id"); - ListTag posTag = (ListTag) entityTagMap.get("Pos"); - ListTag rotTag = (ListTag) entityTagMap.get("Rotation"); - if (idTag == null || posTag == null || rotTag == null) { - Fawe.debug("Unknown entity tag: " + nativeTag); - continue; - } - double x = posTag.getDouble(0); - double y = posTag.getDouble(1); - double z = posTag.getDouble(2); - float yaw = rotTag.getFloat(0); - float pitch = rotTag.getFloat(1); - String id = idTag.getValue(); - Entity entity = EntityTypes.createEntityByName(id, nmsWorld); - if (entity != null) { - UUID uuid = entity.getUniqueID(); - entityTagMap.put("UUIDMost", new LongTag(uuid.getMostSignificantBits())); - entityTagMap.put("UUIDLeast", new LongTag(uuid.getLeastSignificantBits())); - if (nativeTag != null) { - NBTTagCompound tag = (NBTTagCompound) BukkitQueue_1_9_R1.methodFromNative.invoke(BukkitQueue_1_9_R1.adapter, nativeTag); - for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { - tag.remove(name); + Set createdEntities = new HashSet<>(); + if (!entitiesToSpawn.isEmpty()) { + synchronized (BukkitQueue_0.adapter) { + for (CompoundTag nativeTag : entitiesToSpawn) { + Map entityTagMap = ReflectionUtils.getMap(nativeTag.getValue()); + StringTag idTag = (StringTag) entityTagMap.get("Id"); + ListTag posTag = (ListTag) entityTagMap.get("Pos"); + ListTag rotTag = (ListTag) entityTagMap.get("Rotation"); + if (idTag == null || posTag == null || rotTag == null) { + Fawe.debug("Unknown entity tag: " + nativeTag); + continue; + } + double x = posTag.getDouble(0); + double y = posTag.getDouble(1); + double z = posTag.getDouble(2); + float yaw = rotTag.getFloat(0); + float pitch = rotTag.getFloat(1); + String id = idTag.getValue(); + Entity entity = EntityTypes.createEntityByName(id, nmsWorld); + if (entity != null) { + UUID uuid = entity.getUniqueID(); + entityTagMap.put("UUIDMost", new LongTag(uuid.getMostSignificantBits())); + entityTagMap.put("UUIDLeast", new LongTag(uuid.getLeastSignificantBits())); + if (nativeTag != null) { + NBTTagCompound tag = (NBTTagCompound) BukkitQueue_1_9_R1.methodFromNative.invoke(BukkitQueue_1_9_R1.adapter, nativeTag); + for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { + tag.remove(name); + } + entity.f(tag); + } + entity.setLocation(x, y, z, yaw, pitch); + nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM); + createdEntities.add(entity.getUniqueID()); } - entity.f(tag); } - entity.setLocation(x, y, z, yaw, pitch); - nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM); - createdEntities.add(entity.getUniqueID()); } } // Change task? diff --git a/core/src/main/java/com/boydti/fawe/util/FaweTimer.java b/core/src/main/java/com/boydti/fawe/util/FaweTimer.java index 264d689b..e3876541 100644 --- a/core/src/main/java/com/boydti/fawe/util/FaweTimer.java +++ b/core/src/main/java/com/boydti/fawe/util/FaweTimer.java @@ -67,6 +67,8 @@ public class FaweTimer implements Runnable { if (skip > 0) { if (skipTick != tick) { skip--; + skipTick = tick; + return true; // Run once per tick } return false; } diff --git a/core/src/main/java/com/boydti/fawe/util/SetQueue.java b/core/src/main/java/com/boydti/fawe/util/SetQueue.java index 767b1ca8..6a97c4c7 100644 --- a/core/src/main/java/com/boydti/fawe/util/SetQueue.java +++ b/core/src/main/java/com/boydti/fawe/util/SetQueue.java @@ -61,9 +61,14 @@ public class SetQueue { public void run() { try { double targetTPS = 18 - Math.max(Settings.QUEUE.EXTRA_TIME_MS * 0.05, 0); - while (!tasks.isEmpty() && Fawe.get().getTimer().isAbove(targetTPS)) { - tasks.poll().run(); - } + do { + Runnable task = tasks.poll(); + if (task != null) { + task.run(); + } else { + break; + } + } while (Fawe.get().getTimer().isAbove(targetTPS)); if (inactiveQueues.isEmpty() && activeQueues.isEmpty()) { lastSuccess = System.currentTimeMillis(); runEmptyTasks();