Move detector data to description packet

This commit is contained in:
Raoul Van den Berge
2016-05-20 19:57:13 +02:00
parent 798ae88957
commit d9ff0cac43
3 changed files with 18 additions and 73 deletions

View File

@@ -1,66 +0,0 @@
package refinedstorage.network;
import io.netty.buffer.ByteBuf;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.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 refinedstorage.RefinedStorageUtils;
import refinedstorage.proxy.ClientProxy;
import refinedstorage.tile.TileDetector;
public class MessageDetectorPoweredUpdate implements IMessage, IMessageHandler<MessageDetectorPoweredUpdate, IMessage> {
private int x;
private int y;
private int z;
private boolean powered;
public MessageDetectorPoweredUpdate() {
}
public MessageDetectorPoweredUpdate(TileDetector detector) {
this.x = detector.getPos().getX();
this.y = detector.getPos().getY();
this.z = detector.getPos().getZ();
this.powered = detector.isPowered();
}
@Override
public void fromBytes(ByteBuf buf) {
x = buf.readInt();
y = buf.readInt();
z = buf.readInt();
powered = buf.readBoolean();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(x);
buf.writeInt(y);
buf.writeInt(z);
buf.writeBoolean(powered);
}
@Override
public IMessage onMessage(MessageDetectorPoweredUpdate message, MessageContext ctx) {
BlockPos pos = new BlockPos(message.x, message.y, message.z);
World world = ClientProxy.getWorld();
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileDetector) {
boolean wasPowered = ((TileDetector) tile).isPowered();
((TileDetector) tile).setPowered(message.powered);
if (wasPowered != message.powered) {
RefinedStorageUtils.updateBlock(world, pos);
}
}
return null;
}
}

View File

@@ -37,7 +37,6 @@ public class CommonProxy {
RefinedStorage.NETWORK.registerMessage(MessageTileContainerUpdate.class, MessageTileContainerUpdate.class, id++, Side.CLIENT);
RefinedStorage.NETWORK.registerMessage(MessageControllerEnergyUpdate.class, MessageControllerEnergyUpdate.class, id++, Side.CLIENT);
RefinedStorage.NETWORK.registerMessage(MessageDetectorPoweredUpdate.class, MessageDetectorPoweredUpdate.class, id++, Side.CLIENT);
RefinedStorage.NETWORK.registerMessage(MessageRedstoneModeUpdate.class, MessageRedstoneModeUpdate.class, id++, Side.SERVER);
RefinedStorage.NETWORK.registerMessage(MessageGridStoragePush.class, MessageGridStoragePush.class, id++, Side.SERVER);
RefinedStorage.NETWORK.registerMessage(MessageGridStoragePull.class, MessageGridStoragePull.class, id++, Side.SERVER);

View File

@@ -10,7 +10,6 @@ import refinedstorage.RefinedStorageBlocks;
import refinedstorage.RefinedStorageUtils;
import refinedstorage.container.ContainerDetector;
import refinedstorage.inventory.InventorySimple;
import refinedstorage.network.MessageDetectorPoweredUpdate;
import refinedstorage.storage.ItemGroup;
import refinedstorage.tile.config.ICompareConfig;
import refinedstorage.tile.config.RedstoneMode;
@@ -23,6 +22,7 @@ public class TileDetector extends TileMachine implements ICompareConfig {
public static final String NBT_COMPARE = "Compare";
public static final String NBT_MODE = "Mode";
public static final String NBT_AMOUNT = "Amount";
public static final String NBT_DESC_POWERED = "Powered";
private InventorySimple inventory = new InventorySimple("detector", 1, this);
@@ -49,7 +49,7 @@ public class TileDetector extends TileMachine implements ICompareConfig {
if (ticks % 5 == 0) {
ItemStack slot = inventory.getStackInSlot(0);
boolean lastPowered = powered;
boolean wasPowered = powered;
if (slot != null) {
boolean foundAny = false;
@@ -87,12 +87,10 @@ public class TileDetector extends TileMachine implements ICompareConfig {
powered = false;
}
if (powered != lastPowered) {
if (powered != wasPowered) {
worldObj.notifyNeighborsOfStateChange(pos, RefinedStorageBlocks.DETECTOR);
}
if (ticks % 4 == 0) {
RefinedStorageUtils.sendToAllAround(worldObj, pos, new MessageDetectorPoweredUpdate(this));
RefinedStorageUtils.updateBlock(worldObj, pos);
}
}
}
@@ -167,6 +165,20 @@ public class TileDetector extends TileMachine implements ICompareConfig {
RefinedStorageUtils.saveInventory(inventory, 0, nbt);
}
@Override
public void readFromDescriptionPacketNBT(NBTTagCompound tag) {
super.readFromDescriptionPacketNBT(tag);
powered = tag.getBoolean(NBT_DESC_POWERED);
}
@Override
public void writeToDescriptionPacketNBT(NBTTagCompound tag) {
super.writeToDescriptionPacketNBT(tag);
tag.setBoolean(NBT_DESC_POWERED, powered);
}
@Override
public void sendContainerData(ByteBuf buf) {
super.sendContainerData(buf);