diff options
Diffstat (limited to 'src/main/java/kubatech/api/network')
-rw-r--r-- | src/main/java/kubatech/api/network/CustomTileEntityPacket.java | 127 | ||||
-rw-r--r-- | src/main/java/kubatech/api/network/LoadConfigPacket.java | 74 |
2 files changed, 201 insertions, 0 deletions
diff --git a/src/main/java/kubatech/api/network/CustomTileEntityPacket.java b/src/main/java/kubatech/api/network/CustomTileEntityPacket.java new file mode 100644 index 0000000000..cd7bfd3389 --- /dev/null +++ b/src/main/java/kubatech/api/network/CustomTileEntityPacket.java @@ -0,0 +1,127 @@ +package kubatech.api.network; + +import cpw.mods.fml.common.network.NetworkRegistry; +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import gregtech.api.interfaces.metatileentity.IMetaTileEntity; +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import java.nio.charset.StandardCharsets; +import kubatech.api.tileentity.CustomTileEntityPacketHandler; +import kubatech.api.utils.ModUtils; +import kubatech.kubatech; +import net.minecraft.client.Minecraft; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +public class CustomTileEntityPacket implements IMessage { + public int w, x, y, z; + public ByteBuf customdata = Unpooled.buffer(); + + @SuppressWarnings("unused") + public CustomTileEntityPacket() {} + + public CustomTileEntityPacket(TileEntity te, byte[] customdata) { + this.w = te.getWorldObj().provider.dimensionId; + this.x = te.xCoord; + this.y = te.yCoord; + this.z = te.zCoord; + if (customdata != null && customdata.length > 0) this.customdata.writeBytes(customdata); + } + + public void sendToAllAround(int range) { + kubatech.NETWORK.sendToAllAround(this, new NetworkRegistry.TargetPoint(w, x, y, z, range)); + } + + // Helper methods + + public void resetHelperData() { + customdata.clear(); + } + + public void addData(byte[] data) { + customdata.writeBytes(data); + } + + public void addData(byte data) { + customdata.writeByte(data); + } + + public void addData(int data) { + customdata.writeInt(data); + } + + public void addData(String data) { + byte[] bytes = data.getBytes(StandardCharsets.UTF_8); + addData(bytes.length); + addData(bytes); + } + + public void addData(boolean data) { + customdata.writeBoolean(data); + } + + public void getData(byte[] bytes) { + customdata.readBytes(bytes); + } + + public byte[] getData(int len) { + byte[] bytes = new byte[len]; + getData(bytes); + return bytes; + } + + public int getDataInt() { + return customdata.readInt(); + } + + public String getDataString() { + return new String(getData(getDataInt()), StandardCharsets.UTF_8); + } + + public boolean getDataBoolean() { + return customdata.readBoolean(); + } + + @Override + public void fromBytes(ByteBuf buf) { + w = buf.readInt(); + x = buf.readInt(); + y = buf.readInt(); + z = buf.readInt(); + customdata.clear(); + buf.readBytes(customdata, buf.readInt()); + } + + @Override + public void toBytes(ByteBuf buf) { + buf.writeInt(w); + buf.writeInt(x); + buf.writeInt(y); + buf.writeInt(z); + buf.writeInt(customdata.readableBytes()); + buf.writeBytes(customdata); + } + + public static class Handler implements IMessageHandler<CustomTileEntityPacket, IMessage> { + @Override + public IMessage onMessage(CustomTileEntityPacket message, MessageContext ctx) { + if (!ModUtils.isClientSided) return null; + World w = Minecraft.getMinecraft().thePlayer.getEntityWorld(); + if (message.w != w.provider.dimensionId) return null; + TileEntity e = w.getTileEntity(message.x, message.y, message.z); + if (e == null || e.isInvalid()) return null; + if (e instanceof IGregTechTileEntity && !((IGregTechTileEntity) e).isInvalidTileEntity()) { + IMetaTileEntity mte = ((IGregTechTileEntity) e).getMetaTileEntity(); + if (mte == null) return null; + if (!(mte instanceof CustomTileEntityPacketHandler)) return null; + ((CustomTileEntityPacketHandler) mte).HandleCustomPacket(message); + return null; + } else if (!(e instanceof CustomTileEntityPacketHandler)) return null; + ((CustomTileEntityPacketHandler) e).HandleCustomPacket(message); + return null; + } + } +} diff --git a/src/main/java/kubatech/api/network/LoadConfigPacket.java b/src/main/java/kubatech/api/network/LoadConfigPacket.java new file mode 100644 index 0000000000..f38293642e --- /dev/null +++ b/src/main/java/kubatech/api/network/LoadConfigPacket.java @@ -0,0 +1,74 @@ +/* + * KubaTech - Gregtech Addon + * Copyright (C) 2022 kuba6000 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see <https://www.gnu.org/licenses/>. + * + */ + +package kubatech.api.network; + +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import io.netty.buffer.ByteBuf; +import java.nio.charset.StandardCharsets; +import java.util.HashSet; +import kubatech.Config; +import kubatech.kubatech; +import kubatech.loaders.MobRecipeLoader; + +public class LoadConfigPacket implements IMessage { + + public static final LoadConfigPacket instance = new LoadConfigPacket(); + + public final HashSet<String> mobsToLoad = new HashSet<>(); + + @Override + public void fromBytes(ByteBuf buf) { + if (!buf.readBoolean()) mobsToLoad.clear(); + else { + mobsToLoad.clear(); + int mobssize = buf.readInt(); + for (int i = 0; i < mobssize; i++) { + byte[] sbytes = new byte[buf.readInt()]; + buf.readBytes(sbytes); + mobsToLoad.add(new String(sbytes, StandardCharsets.UTF_8)); + } + } + } + + @Override + public void toBytes(ByteBuf buf) { + if (!Config.mobHandlerEnabled) buf.writeBoolean(false); + else { + buf.writeBoolean(true); + buf.writeInt(mobsToLoad.size()); + mobsToLoad.forEach(s -> { + byte[] sbytes = s.getBytes(StandardCharsets.UTF_8); + buf.writeInt(sbytes.length); + buf.writeBytes(sbytes); + }); + } + } + + public static class Handler implements IMessageHandler<LoadConfigPacket, IMessage> { + @Override + public IMessage onMessage(LoadConfigPacket message, MessageContext ctx) { + kubatech.info("Received Mob Handler config, parsing"); + MobRecipeLoader.processMobRecipeMap(message.mobsToLoad); + return null; + } + } +} |