|
| 1 | +package supersymmetry.mixins.gregtech; |
| 2 | + |
| 3 | +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
| 4 | +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; |
| 5 | +import gregtech.api.block.BlockStateTileEntity; |
| 6 | +import gregtech.api.metatileentity.SyncedTileEntityBase; |
| 7 | +import gregtech.api.network.PacketDataList; |
| 8 | +import net.minecraft.world.WorldServer; |
| 9 | +import org.jetbrains.annotations.ApiStatus.ScheduledForRemoval; |
| 10 | +import org.spongepowered.asm.mixin.Final; |
| 11 | +import org.spongepowered.asm.mixin.Mixin; |
| 12 | +import org.spongepowered.asm.mixin.Shadow; |
| 13 | +import org.spongepowered.asm.mixin.Unique; |
| 14 | +import org.spongepowered.asm.mixin.injection.At; |
| 15 | + |
| 16 | +/// Ported from [CEu#2823](https://github.com/GregTechCEu/GregTech/pull/2823) |
| 17 | +/// and [CEu#2839](https://github.com/GregTechCEu/GregTech/pull/2839) |
| 18 | +@Deprecated |
| 19 | +@ScheduledForRemoval(inVersion = "Next CEu update") |
| 20 | +@Mixin(value = SyncedTileEntityBase.class, remap = false) |
| 21 | +public abstract class SyncedTileEntityBaseMixin extends BlockStateTileEntity { |
| 22 | + |
| 23 | + @Unique |
| 24 | + private static final int SIZE_THRESHOLD = 10; |
| 25 | + @Shadow |
| 26 | + @Final |
| 27 | + private PacketDataList updates; |
| 28 | + |
| 29 | + @WrapOperation(method = "writeCustomData", |
| 30 | + at = @At(value = "INVOKE", |
| 31 | + target = "Lgregtech/api/metatileentity/SyncedTileEntityBase;notifyWorld()V")) |
| 32 | + private void checkCanNotifyWorld(SyncedTileEntityBase self, Operation<Void> method) { |
| 33 | + // Short circuit with packet size to avoid too many hash lookups and instanceof casts |
| 34 | + if (updates.size() > SIZE_THRESHOLD && getWorld() instanceof WorldServer server) { |
| 35 | + int x = getPos().getX() >> 4; |
| 36 | + int z = getPos().getZ() >> 4; |
| 37 | + if (server.getPlayerChunkMap().contains(x, z)) { |
| 38 | + method.call(self); |
| 39 | + } else { |
| 40 | + // Cannot send it, so clear |
| 41 | + updates.clear(); |
| 42 | + } |
| 43 | + return; |
| 44 | + } |
| 45 | + // Assume we can send data regardless |
| 46 | + method.call(self); |
| 47 | + } |
| 48 | +} |
0 commit comments