IndustrialWires/src/main/java/malte0811/industrialWires/network/MessagePanelInteract.java

79 lines
No EOL
2.9 KiB
Java

/*
* This file is part of Industrial Wires.
* Copyright (C) 2016-2017 malte0811
*
* Industrial Wires is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Industrial Wires 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
*/
package malte0811.industrialWires.network;
import io.netty.buffer.ByteBuf;
import malte0811.industrialWires.blocks.controlpanel.TileEntityPanel;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
public class MessagePanelInteract implements IMessage {
private BlockPos pos;
//private int dimension;
private int pcId;
private Vec3d hitRelative;
public MessagePanelInteract(TileEntityPanel tile, int id, Vec3d hit) {
pos = tile.getPos();
//dimension = tile.getWorld().provider.getDimension();
pcId = id;
hitRelative = hit;
}
public MessagePanelInteract() {}
@Override
public void fromBytes(ByteBuf buf) {
this.pos = new BlockPos(buf.readInt(), buf.readInt(), buf.readInt());
//dimension = buf.readInt();
pcId = buf.readInt();
hitRelative = new Vec3d(buf.readDouble(), buf.readDouble(), buf.readDouble());
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(this.pos.getX());
buf.writeInt(this.pos.getY());
buf.writeInt(this.pos.getZ());
//buf.writeInt(dimension);
buf.writeInt(pcId);
buf.writeDouble(hitRelative.xCoord).writeDouble(hitRelative.yCoord).writeDouble(hitRelative.zCoord);
}
public static class HandlerServer implements IMessageHandler<MessagePanelInteract, IMessage> {
@Override
public IMessage onMessage(MessagePanelInteract message, MessageContext ctx) {
ctx.getServerHandler().playerEntity.getServerWorld().addScheduledTask(()->handle(message, ctx.getServerHandler().playerEntity));
return null;
}
private void handle(MessagePanelInteract msg, EntityPlayerMP player) {
if (player.getDistanceSqToCenter(msg.pos)<100) {//closer than 10 blocks TODO use player reach distance?
TileEntity te = player.worldObj.getTileEntity(msg.pos);
if (te instanceof TileEntityPanel) {
((TileEntityPanel) te).interactServer(msg.hitRelative, msg.pcId, player);
}
}
}
}
}