Fix potential Deadlock when using custom Item entities. (#7532)

Be careful when adding Entities during Chunk Load (#7519)
This commit is contained in:
AterAnimAvis authored and GitHub committed 2021-03-12 10:50:05 -08:00
1 parent 19f9b309a6
commit 72d05d17a9
3 files changed
+8 -1

No files matched your search

@@ -24,6 +24,8 @@ import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.concurrent.ThreadTaskExecutor;
import net.minecraft.util.concurrent.TickDelayedTask;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.loot.LootModifierManager;
import net.minecraftforge.common.util.FakePlayerFactory;
@@ -40,6 +42,8 @@ import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.TickEvent.ClientTickEvent;
import net.minecraftforge.event.TickEvent.Phase;
import net.minecraftforge.event.TickEvent.ServerTickEvent;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.LogicalSidedProvider;
import net.minecraftforge.server.command.ForgeCommand;
import net.minecraftforge.server.command.ConfigCommand;
@@ -60,7 +64,8 @@ public class ForgeInternalHandler
{
entity.remove();
event.setCanceled(true);
event.getWorld().addFreshEntity(newEntity);
ThreadTaskExecutor<Runnable> executor = LogicalSidedProvider.WORKQUEUE.get(event.getWorld().isClientSide ? LogicalSide.CLIENT : LogicalSide.SERVER);
executor.tell(new TickDelayedTask(0, () -> event.getWorld().addFreshEntity(newEntity)));
}
}
}
@@ -30,6 +30,7 @@ import java.util.Collection;
* EntityJoinWorldEvent is fired when an Entity joins the world. <br>
* This event is fired whenever an Entity is added to the world in
* {@link World#loadEntities(Collection)}, {@link net.minecraft.world.ServerWorld#loadEntities(Collection)} {@link World#joinEntityInSurroundings(Entity)}, and {@link World#spawnEntity(Entity)}. <br>
* <strong>Note:</strong> This event may be called before the underlying {@link net.minecraft.world.chunk.Chunk} is promoted to {@link net.minecraft.world.chunk.ChunkStatus#FULL}. You will cause chunk loading deadlocks if you don't delay your world interactions.<br>
* <br>
* {@link #world} contains the world in which the entity is to join.<br>
* <br>
@@ -58,6 +58,7 @@ public class ChunkEvent extends WorldEvent
* This event is fired during chunk loading in <br>
* {@link ChunkProviderClient#loadChunk(int, int)}, <br>
* Chunk.onChunkLoad(). <br>
* <strong>Note:</strong> This event may be called before the underlying {@link net.minecraft.world.chunk.Chunk} is promoted to {@link net.minecraft.world.chunk.ChunkStatus#FULL}. You will cause chunk loading deadlocks if you don't delay your world interactions.<br>
* <br>
* This event is not {@link net.minecraftforge.eventbus.api.Cancelable}.<br>
* <br>