Java tutorial
package com.savageboy74.savagetech.network.message; /* * MessageTileEntityBase.java * Copyright (C) 2014 Savage - github.com/savageboy74 * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import com.savageboy74.savagetech.tileentity.TileEntityBase; import cpw.mods.fml.client.FMLClientHandler; 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 net.minecraft.tileentity.TileEntity; public class MessageTileEntityBase implements IMessage, IMessageHandler<MessageTileEntityBase, IMessage> { public int x, y, z; public byte orientation, state; public String customName, owner; public MessageTileEntityBase() { } public MessageTileEntityBase(TileEntityBase tileEntityBase) { this.x = tileEntityBase.xCoord; this.y = tileEntityBase.yCoord; this.z = tileEntityBase.zCoord; this.orientation = (byte) tileEntityBase.getOrientation().ordinal(); this.state = (byte) tileEntityBase.getState(); this.customName = tileEntityBase.getCustomName(); this.owner = tileEntityBase.getOwner(); } @Override public void fromBytes(ByteBuf buf) { this.x = buf.readInt(); this.y = buf.readInt(); this.z = buf.readInt(); this.orientation = buf.readByte(); this.state = buf.readByte(); int customNameLength = buf.readInt(); this.customName = new String(buf.readBytes(customNameLength).array()); int ownerLength = buf.readInt(); this.owner = new String(buf.readBytes(ownerLength).array()); } @Override public void toBytes(ByteBuf buf) { buf.writeInt(x); buf.writeInt(y); buf.writeInt(z); buf.writeByte(orientation); buf.writeByte(state); buf.writeInt(customName.length()); buf.writeBytes(customName.getBytes()); buf.writeInt(owner.length()); buf.writeBytes(owner.getBytes()); } @Override public IMessage onMessage(MessageTileEntityBase message, MessageContext ctx) { TileEntity tileEntity = FMLClientHandler.instance().getClient().theWorld.getTileEntity(message.x, message.y, message.z); if (tileEntity instanceof TileEntityBase) { ((TileEntityBase) tileEntity).setOrientation(message.orientation); ((TileEntityBase) tileEntity).setState(message.state); ((TileEntityBase) tileEntity).setCustomName(message.customName); ((TileEntityBase) tileEntity).setOwner(message.owner); } return null; } @Override public String toString() { return String.format( "MessageTileEntityBase - x:%s, y:%s, z:%s, orientation:%s, state:%s, customName:%s, owner:%s", x, y, z, orientation, state, customName, owner); } }