com.mrcrayfish.furniture.network.message.MessageDishwasher.java Source code

Java tutorial

Introduction

Here is the source code for com.mrcrayfish.furniture.network.message.MessageDishwasher.java

Source

/**
 * MrCrayfish's Furniture Mod
 * Copyright (C) 2014  MrCrayfish (http://www.mrcrayfish.com/)
 * 
 * This program 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.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.mrcrayfish.furniture.network.message;

import io.netty.buffer.ByteBuf;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import com.mrcrayfish.furniture.tileentity.TileEntityDishwasher;

public class MessageDishwasher implements IMessage, IMessageHandler<MessageDishwasher, IMessage> {
    private int type;
    private int x, y, z;

    public MessageDishwasher() {
    }

    public MessageDishwasher(int type, int x, int y, int z) {
        this.type = type;
        this.x = x;
        this.y = y;
        this.z = z;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        this.type = buf.readInt();
        this.x = buf.readInt();
        this.y = buf.readInt();
        this.z = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(type);
        buf.writeInt(x);
        buf.writeInt(y);
        buf.writeInt(z);
    }

    @Override
    public IMessage onMessage(MessageDishwasher message, MessageContext ctx) {
        World world = ctx.getServerHandler().playerEntity.worldObj;
        TileEntity tileEntity = world.getTileEntity(new BlockPos(message.x, message.y, message.z));
        if (tileEntity instanceof TileEntityDishwasher) {
            TileEntityDishwasher tileEntityDishwasher = (TileEntityDishwasher) tileEntity;
            if (message.type == 0) {
                tileEntityDishwasher.startWashing();
            }
            if (message.type == 1) {
                tileEntityDishwasher.stopWashing();
            }
            world.markBlockForUpdate(new BlockPos(message.x, message.y, message.z));
        }
        return null;
    }
}