From 6c73ccb02822e5d58678fa275290785037eaf3cc Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 29 May 2020 18:10:45 +0200 Subject: [PATCH] Properly fix concurrency problems with doing world stuff on the network thread --- .../unicopia/ability/EarthPonyStompAbility.java | 12 ++++-------- .../com/minelittlepony/unicopia/network/Channel.java | 11 ++++++++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java index 29ed6ac6..9383da22 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java @@ -6,7 +6,6 @@ import java.util.List; import javax.annotation.Nullable; import com.google.common.collect.Lists; -import com.minelittlepony.unicopia.AwaitTickQueue; import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.TreeTraverser; import com.minelittlepony.unicopia.TreeType; @@ -102,7 +101,6 @@ public class EarthPonyStompAbility implements Ability { @Override public void apply(Pony iplayer, Multi data) { - PlayerEntity player = iplayer.getOwner(); if (data.hitType == 0) { @@ -163,7 +161,7 @@ public class EarthPonyStompAbility implements Ability { if (harmed || player.world.random.nextInt(5) == 0) { if (!harmed || player.world.random.nextInt(30) == 0) { - AwaitTickQueue.enqueueTask(w -> TreeTraverser.removeTree(w, data.pos())); + TreeTraverser.removeTree(player.world, data.pos()); } iplayer.subtractEnergyCost(3); @@ -230,11 +228,9 @@ public class EarthPonyStompAbility implements Ability { dropApplesPart(capturedDrops, new ArrayList(), w, log, pos, 0); - AwaitTickQueue.enqueueTask(wo -> { - capturedDrops.forEach(item -> { - item.setToDefaultPickupDelay(); - wo.spawnEntity(item); - }); + capturedDrops.forEach(item -> { + item.setToDefaultPickupDelay(); + w.spawnEntity(item); }); return capturedDrops.size() / 3; diff --git a/src/main/java/com/minelittlepony/unicopia/network/Channel.java b/src/main/java/com/minelittlepony/unicopia/network/Channel.java index b23b2f73..304a6a67 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/Channel.java +++ b/src/main/java/com/minelittlepony/unicopia/network/Channel.java @@ -25,7 +25,7 @@ public interface Channel { static void bootstrap() { } static SPacketType clientToServer(Identifier id, Function factory) { - ServerSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handle(context)); + ServerSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handleOnMain(context)); return () -> id; } @@ -33,8 +33,9 @@ public interface Channel { Identifier id = new Identifier(redirect.getId().getNamespace(), "broadcast_" + redirect.getId().getPath()); ServerSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> { PlayerEntity sender = context.getPlayer(); + T p = factory.apply(buffer); - p.handle(context); + p.handleOnMain(context); sender.world.getPlayers().forEach(player -> { if (player != null) { redirect.send(player, p); @@ -45,7 +46,7 @@ public interface Channel { } static CPacketType serverToClient(Identifier id, Function factory) { - ClientSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handle(context)); + ClientSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handleOnMain(context)); return () -> id; } @@ -78,6 +79,10 @@ public interface Channel { void toBuffer(PacketByteBuf buffer); + default void handleOnMain(PacketContext context) { + context.getTaskQueue().execute(() -> handle(context)); + } + default PacketByteBuf toBuffer() { PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); toBuffer(buf);