Files
StructureLib/src/main/java/com/gtnewhorizon/structurelib/net/AlignmentMessage.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

140 lines
4.9 KiB
Java
Raw Normal View History

package com.gtnewhorizon.structurelib.net;
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-29 19:05:50 +08:00
import com.gtnewhorizon.structurelib.StructureLib;
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;
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;
import io.netty.buffer.ByteBuf;
2021-05-29 19:05:50 +08:00
public abstract class AlignmentMessage implements IMessage {
int mPosX;
int mPosY;
int mPosZ;
int mPosD;
int mAlign;
2022-08-08 17:40:12 +08:00
public AlignmentMessage() {}
@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();
}
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();
}
public static class AlignmentQuery extends AlignmentMessage {
2022-08-08 17:40:12 +08:00
public AlignmentQuery() {}
2021-05-29 19:05:50 +08:00
public AlignmentQuery(IAlignmentProvider provider) {
super(provider);
}
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);
}
}
public static class AlignmentData extends AlignmentMessage {
2022-08-08 17:40:12 +08:00
public AlignmentData() {}
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-29 19:05:50 +08:00
public AlignmentData(IAlignmentProvider provider) {
super(provider);
}
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-29 19:05:50 +08:00
public static class ClientHandler implements IMessageHandler<AlignmentData, IMessage> {
@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));
}
}
}
return null;
}
}
2021-05-29 19:05:50 +08:00
public static class ServerHandler implements IMessageHandler<AlignmentQuery, AlignmentData> {
@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) {
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();
return new AlignmentData(pMessage);
}
}
return null;
}
}
2022-08-08 17:40:12 +08:00
}