Search in sources :

Example 1 with MessageSyncFluidAmount

use of com.infinityraider.agricraft.network.MessageSyncFluidAmount in project AgriCraft by AgriCraft.

the class TileEntityChannel method update.

@Override
public void update() {
    // Push down.
    if (this.connections.get(EnumFacing.DOWN) > 0) {
        // Get the component.
        final IAgriFluidComponent component = WorldHelper.getTile(world, pos.offset(EnumFacing.DOWN), IAgriFluidComponent.class).orElse(null);
        // Push all fluid.
        if (component != null) {
            int newFluidAmount = component.acceptFluid(1000, fluidAmount, true, false);
            if (newFluidAmount < 0) {
                throw new AssertionError("A component acccepted too much fluid!");
            }
            this.fluidAmount = newFluidAmount;
        }
    }
    // Push Out.
    for (EnumFacing side : EnumFacing.HORIZONTALS) {
        // Check if connected.
        if (this.connections.get(side) > 0) {
            // Get the component and attempt to push.
            final IAgriFluidComponent component = WorldHelper.getTile(world, pos.offset(side), IAgriFluidComponent.class).orElse(null);
            // If present, do the thing.
            if (component != null) {
                this.pushToComponent(component);
            }
        }
    }
    // If the fluid amount changed then need to do an update?
    if (Math.abs(this.oldFluidAmount - this.fluidAmount) > this.fluidSyncThreshold) {
        // Check the time.
        long currentTime = System.currentTimeMillis();
        // If time is greater than delta, then do sync.
        if (currentTime - this.last_update > fluidSyncTimeout) {
            // Update the old amount.
            this.oldFluidAmount = this.fluidAmount;
            // If on server side, need to tell client.
            if (!this.world.isRemote) {
                new MessageSyncFluidAmount(world, pos, fluidAmount).sendToAll();
            }
        }
    }
    // If the component fluid amount changed, mark this as dirty.
    if (this.fluidAmount != this.oldFluidAmount) {
        this.world.markChunkDirty(pos, this);
    }
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) MessageSyncFluidAmount(com.infinityraider.agricraft.network.MessageSyncFluidAmount) IAgriFluidComponent(com.infinityraider.agricraft.api.v1.misc.IAgriFluidComponent)

Aggregations

IAgriFluidComponent (com.infinityraider.agricraft.api.v1.misc.IAgriFluidComponent)1 MessageSyncFluidAmount (com.infinityraider.agricraft.network.MessageSyncFluidAmount)1 EnumFacing (net.minecraft.util.EnumFacing)1