2021-07-21 17:26:28 +08:00
|
|
|
package com.gtnewhorizon.structurelib.net;
|
2021-05-17 03:02:24 +08:00
|
|
|
|
2021-05-29 19:05:50 +08:00
|
|
|
import com.gtnewhorizon.structurelib.StructureLib;
|
2021-07-21 17:26:28 +08:00
|
|
|
import com.gtnewhorizon.structurelib.alignment.IAlignment;
|
|
|
|
|
import com.gtnewhorizon.structurelib.alignment.IAlignmentProvider;
|
2021-05-29 19:05:50 +08:00
|
|
|
import com.gtnewhorizon.structurelib.alignment.enumerable.ExtendedFacing;
|
2021-05-17 03:02:24 +08:00
|
|
|
import cpw.mods.fml.common.network.ByteBufUtils;
|
|
|
|
|
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
2021-05-29 19:05:50 +08:00
|
|
|
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
|
|
|
|
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
2021-05-17 03:02:24 +08:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2021-05-29 19:05:50 +08:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
import net.minecraftforge.common.DimensionManager;
|
2021-05-17 03:02:24 +08:00
|
|
|
|
2021-05-29 19:05:50 +08:00
|
|
|
public abstract class AlignmentMessage implements IMessage {
|
2021-05-17 03:02:24 +08:00
|
|
|
int mPosX;
|
|
|
|
|
int mPosY;
|
|
|
|
|
int mPosZ;
|
|
|
|
|
int mPosD;
|
|
|
|
|
int mAlign;
|
|
|
|
|
|
2022-08-08 17:40:12 +08:00
|
|
|
public AlignmentMessage() {}
|
2021-05-17 03:02:24 +08:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void fromBytes(ByteBuf pBuffer) {
|
|
|
|
|
NBTTagCompound tTag = ByteBufUtils.readTag(pBuffer);
|
|
|
|
|
mPosX = tTag.getInteger("posx");
|
|
|
|
|
mPosY = tTag.getInteger("posy");
|
|
|
|
|
mPosZ = tTag.getInteger("posz");
|
|
|
|
|
mPosD = tTag.getInteger("posd");
|
|
|
|
|
mAlign = tTag.getInteger("rotf");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void toBytes(ByteBuf pBuffer) {
|
|
|
|
|
NBTTagCompound tFXTag = new NBTTagCompound();
|
|
|
|
|
tFXTag.setInteger("posx", mPosX);
|
|
|
|
|
tFXTag.setInteger("posy", mPosY);
|
|
|
|
|
tFXTag.setInteger("posz", mPosZ);
|
|
|
|
|
tFXTag.setInteger("posd", mPosD);
|
|
|
|
|
tFXTag.setInteger("rotf", mAlign);
|
|
|
|
|
|
|
|
|
|
ByteBufUtils.writeTag(pBuffer, tFXTag);
|
|
|
|
|
}
|
2021-05-29 19:05:50 +08:00
|
|
|
|
|
|
|
|
private AlignmentMessage(IAlignmentProvider provider) {
|
2022-08-08 17:40:12 +08:00
|
|
|
if (!(provider instanceof TileEntity)) throw new IllegalArgumentException("Provider must be a TileEntity");
|
2021-05-29 19:05:50 +08:00
|
|
|
IAlignment alignment = provider.getAlignment();
|
2022-08-08 17:40:12 +08:00
|
|
|
if (alignment == null) throw new IllegalArgumentException("Passed in provider does not provide an alignment!");
|
2021-05-29 19:05:50 +08:00
|
|
|
TileEntity base = (TileEntity) provider;
|
|
|
|
|
mPosX = base.xCoord;
|
|
|
|
|
mPosY = base.yCoord;
|
|
|
|
|
mPosZ = base.zCoord;
|
|
|
|
|
mPosD = base.getWorldObj().provider.dimensionId;
|
|
|
|
|
mAlign = alignment.getExtendedFacing().getIndex();
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AlignmentMessage(World world, int x, int y, int z, IAlignment front) {
|
2021-05-29 19:05:50 +08:00
|
|
|
mPosX = x;
|
|
|
|
|
mPosY = y;
|
|
|
|
|
mPosZ = z;
|
|
|
|
|
mPosD = world.provider.dimensionId;
|
|
|
|
|
mAlign = front.getExtendedFacing().getIndex();
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class AlignmentQuery extends AlignmentMessage {
|
2022-08-08 17:40:12 +08:00
|
|
|
public AlignmentQuery() {}
|
2021-05-17 03:02:24 +08:00
|
|
|
|
2021-05-29 19:05:50 +08:00
|
|
|
public AlignmentQuery(IAlignmentProvider provider) {
|
|
|
|
|
super(provider);
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AlignmentQuery(World world, int x, int y, int z, IAlignment front) {
|
2021-05-29 19:05:50 +08:00
|
|
|
super(world, x, y, z, front);
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class AlignmentData extends AlignmentMessage {
|
2022-08-08 17:40:12 +08:00
|
|
|
public AlignmentData() {}
|
2021-05-17 03:02:24 +08:00
|
|
|
|
2021-05-29 19:05:50 +08:00
|
|
|
private AlignmentData(AlignmentQuery query) {
|
|
|
|
|
mPosX = query.mPosX;
|
|
|
|
|
mPosY = query.mPosY;
|
|
|
|
|
mPosZ = query.mPosZ;
|
|
|
|
|
mPosD = query.mPosD;
|
|
|
|
|
mAlign = query.mAlign;
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-29 19:05:50 +08:00
|
|
|
public AlignmentData(IAlignmentProvider provider) {
|
|
|
|
|
super(provider);
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AlignmentData(World world, int x, int y, int z, IAlignment front) {
|
2021-05-29 19:05:50 +08:00
|
|
|
super(world, x, y, z, front);
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-29 19:05:50 +08:00
|
|
|
public static class ClientHandler implements IMessageHandler<AlignmentData, IMessage> {
|
2021-05-17 03:02:24 +08:00
|
|
|
@Override
|
2021-05-29 19:05:50 +08:00
|
|
|
public IMessage onMessage(AlignmentData pMessage, MessageContext pCtx) {
|
|
|
|
|
if (StructureLib.getCurrentPlayer().worldObj.provider.dimensionId == pMessage.mPosD) {
|
2022-08-08 17:40:12 +08:00
|
|
|
TileEntity te = StructureLib.getCurrentPlayer()
|
|
|
|
|
.worldObj
|
|
|
|
|
.getTileEntity(pMessage.mPosX, pMessage.mPosY, pMessage.mPosZ);
|
2021-05-29 19:05:50 +08:00
|
|
|
if (te instanceof IAlignmentProvider) {
|
|
|
|
|
IAlignment alignment = ((IAlignmentProvider) te).getAlignment();
|
|
|
|
|
if (alignment != null) {
|
|
|
|
|
alignment.setExtendedFacing(ExtendedFacing.byIndex(pMessage.mAlign));
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-29 19:05:50 +08:00
|
|
|
public static class ServerHandler implements IMessageHandler<AlignmentQuery, AlignmentData> {
|
2021-05-17 03:02:24 +08:00
|
|
|
@Override
|
2021-05-29 19:05:50 +08:00
|
|
|
public AlignmentData onMessage(AlignmentQuery pMessage, MessageContext pCtx) {
|
|
|
|
|
World world = DimensionManager.getWorld(pMessage.mPosD);
|
|
|
|
|
if (world != null) {
|
2021-05-17 03:02:24 +08:00
|
|
|
TileEntity te = world.getTileEntity(pMessage.mPosX, pMessage.mPosY, pMessage.mPosZ);
|
2021-05-29 19:05:50 +08:00
|
|
|
if (te instanceof IAlignmentProvider) {
|
|
|
|
|
IAlignment alignment = ((IAlignmentProvider) te).getAlignment();
|
2022-08-08 17:40:12 +08:00
|
|
|
if (alignment == null) return null;
|
2021-05-29 19:05:50 +08:00
|
|
|
pMessage.mAlign = alignment.getExtendedFacing().getIndex();
|
2021-05-17 03:02:24 +08:00
|
|
|
return new AlignmentData(pMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-08 17:40:12 +08:00
|
|
|
}
|