|
| 1 | +/* |
| 2 | + * ChunkAPI |
| 3 | + * |
| 4 | + * Copyright (C) 2023-2025 FalsePattern, The MEGA Team, LegacyModdingMC contributors |
| 5 | + * All Rights Reserved |
| 6 | + * |
| 7 | + * The above copyright notice and this permission notice shall be included |
| 8 | + * in all copies or substantial portions of the Software. |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Lesser General Public License as published by |
| 12 | + * the Free Software Foundation, only version 3 of the License. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public License |
| 20 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | + |
| 23 | +package com.falsepattern.chunk.internal.mixin.mixins.common.lookingglass; |
| 24 | + |
| 25 | +import com.xcompwiz.lookingglass.log.LoggerUtils; |
| 26 | +import com.xcompwiz.lookingglass.network.LookingGlassPacketManager; |
| 27 | +import com.xcompwiz.lookingglass.network.packet.PacketChunkInfo; |
| 28 | +import com.xcompwiz.lookingglass.network.packet.PacketHandlerBase; |
| 29 | +import com.xcompwiz.lookingglass.network.packet.PacketRequestChunk; |
| 30 | +import io.netty.buffer.ByteBuf; |
| 31 | +import org.spongepowered.asm.mixin.Mixin; |
| 32 | +import org.spongepowered.asm.mixin.Overwrite; |
| 33 | +import org.spongepowered.asm.mixin.Shadow; |
| 34 | + |
| 35 | +import net.minecraft.entity.player.EntityPlayer; |
| 36 | +import net.minecraft.network.play.server.S21PacketChunkData; |
| 37 | +import net.minecraft.world.chunk.Chunk; |
| 38 | +import cpw.mods.fml.common.network.internal.FMLProxyPacket; |
| 39 | + |
| 40 | +import java.util.concurrent.Semaphore; |
| 41 | + |
| 42 | +@Mixin(PacketChunkInfo.class) |
| 43 | +public abstract class PacketChunkInfoMixin extends PacketHandlerBase { |
| 44 | + @Shadow(remap = false) private static Semaphore deflateGate; |
| 45 | + |
| 46 | + @Shadow(remap = false) |
| 47 | + private static int deflate(byte[] chunkData, byte[] compressedChunkData) { |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + @Shadow(remap = false) public abstract void handle(EntityPlayer player, byte[] chunkData, int dim, int xPos, int zPos, boolean reqinit, short yPos, short yMSBPos); |
| 52 | + |
| 53 | + @Shadow(remap = false) protected abstract byte[] inflateChunkData(ByteBuf in, int compressedsize, int uncompressedsize); |
| 54 | + |
| 55 | + /** |
| 56 | + * @author FalsePattern |
| 57 | + * @reason This class was just copied vanilla code. |
| 58 | + */ |
| 59 | + @Overwrite(remap = false) |
| 60 | + public static S21PacketChunkData.Extracted getMapChunkData(Chunk chunk, boolean forceUpdate, int subChunkMask) { |
| 61 | + return S21PacketChunkData.func_149269_a(chunk, forceUpdate, subChunkMask); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @author FalsePattern |
| 66 | + * @reason Wired up to ChunkAPI |
| 67 | + */ |
| 68 | + @Overwrite(remap = false) |
| 69 | + public static FMLProxyPacket createPacket(Chunk chunk, boolean forceUpdate, int subChunkMask, int dim) { |
| 70 | + int xPos = chunk.xPosition; |
| 71 | + int zPos = chunk.zPosition; |
| 72 | + S21PacketChunkData.Extracted extracted = getMapChunkData(chunk, forceUpdate, subChunkMask); |
| 73 | + int realSubChunkMask = extracted.field_150280_b; |
| 74 | + byte[] chunkData = extracted.field_150282_a; |
| 75 | + deflateGate.acquireUninterruptibly(); |
| 76 | + byte[] compressedChunkData = new byte[chunkData.length]; |
| 77 | + int len = deflate(chunkData, compressedChunkData); |
| 78 | + deflateGate.release(); |
| 79 | + ByteBuf data = PacketHandlerBase.createDataBuffer(PacketChunkInfo.class); |
| 80 | + data.writeInt(dim); |
| 81 | + data.writeInt(xPos); |
| 82 | + data.writeInt(zPos); |
| 83 | + data.writeBoolean(forceUpdate); |
| 84 | + data.writeShort((short)(realSubChunkMask & 0xFFFF)); |
| 85 | + data.writeInt(len); |
| 86 | + data.writeInt(chunkData.length); |
| 87 | + data.ensureWritable(len); |
| 88 | + data.writeBytes(compressedChunkData, 0, len); |
| 89 | + return buildPacket(data); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @author FalsePattern |
| 94 | + * @reason Wired up to ChunkAPI |
| 95 | + */ |
| 96 | + @Overwrite(remap = false) |
| 97 | + public void handle(ByteBuf in, EntityPlayer player) { |
| 98 | + int dim = in.readInt(); |
| 99 | + int xPos = in.readInt(); |
| 100 | + int zPos = in.readInt(); |
| 101 | + boolean forceUpdate = in.readBoolean(); |
| 102 | + int subChunkMask = in.readShort() & 0xFFFF; |
| 103 | + int compressedSize = in.readInt(); |
| 104 | + int uncompressedSize = in.readInt(); |
| 105 | + byte[] chunkData = this.inflateChunkData(in, compressedSize, uncompressedSize); |
| 106 | + if (chunkData == null) { |
| 107 | + LookingGlassPacketManager.bus.sendToServer(PacketRequestChunk.createPacket(xPos, subChunkMask, zPos, dim)); |
| 108 | + LoggerUtils.error("Chunk decompression failed: \t%d\t\t%d : %d\n", subChunkMask, compressedSize, uncompressedSize); |
| 109 | + } else { |
| 110 | + this.handle(player, chunkData, dim, xPos, zPos, forceUpdate, (short) subChunkMask, (short)0); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments