Move direction of network nodes to the network node itself instead of the tile.

This commit is contained in:
raoulvdberge
2018-06-14 16:40:54 +02:00
parent 5387bd00e3
commit 67f07a8f5d
7 changed files with 103 additions and 29 deletions

View File

@@ -4,7 +4,6 @@ import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.api.network.INetworkNodeVisitor; import com.raoulvdberge.refinedstorage.api.network.INetworkNodeVisitor;
import com.raoulvdberge.refinedstorage.api.network.node.INetworkNode; import com.raoulvdberge.refinedstorage.api.network.node.INetworkNode;
import com.raoulvdberge.refinedstorage.apiimpl.API; import com.raoulvdberge.refinedstorage.apiimpl.API;
import com.raoulvdberge.refinedstorage.tile.TileBase;
import com.raoulvdberge.refinedstorage.tile.config.RedstoneMode; import com.raoulvdberge.refinedstorage.tile.config.RedstoneMode;
import com.raoulvdberge.refinedstorage.util.WorldUtils; import com.raoulvdberge.refinedstorage.util.WorldUtils;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
@@ -23,6 +22,7 @@ import java.util.UUID;
public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor { public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor {
private static final String NBT_OWNER = "Owner"; private static final String NBT_OWNER = "Owner";
private static final String NBT_DIRECTION = "Direction";
@Nullable @Nullable
protected INetwork network; protected INetwork network;
@@ -33,7 +33,7 @@ public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor {
@Nullable @Nullable
protected UUID owner; protected UUID owner;
private EnumFacing direction; private EnumFacing direction = EnumFacing.NORTH;
private boolean throttlingDisabled; private boolean throttlingDisabled;
private boolean couldUpdate; private boolean couldUpdate;
@@ -147,6 +147,8 @@ public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor {
tag.setUniqueId(NBT_OWNER, owner); tag.setUniqueId(NBT_OWNER, owner);
} }
tag.setInteger(NBT_DIRECTION, direction.ordinal());
writeConfiguration(tag); writeConfiguration(tag);
return tag; return tag;
@@ -163,6 +165,10 @@ public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor {
owner = tag.getUniqueId(NBT_OWNER); owner = tag.getUniqueId(NBT_OWNER);
} }
if (tag.hasKey(NBT_DIRECTION)) {
direction = EnumFacing.getFront(tag.getInteger(NBT_DIRECTION));
}
readConfiguration(tag); readConfiguration(tag);
} }
@@ -205,22 +211,15 @@ public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor {
} }
public EnumFacing getDirection() { public EnumFacing getDirection() {
if (direction == null) {
loadDirection();
}
return direction; return direction;
} }
// TODO: Move to network node. public void setDirection(EnumFacing direction) {
public void loadDirection() { this.direction = direction;
EnumFacing direction = ((TileBase) world.getTileEntity(pos)).getDirection();
if (!direction.equals(this.direction)) { onDirectionChanged();
this.direction = direction;
onDirectionChanged(); markDirty();
}
} }
protected void onDirectionChanged() { protected void onDirectionChanged() {

View File

@@ -64,7 +64,7 @@ public class NetworkNodeCrafter extends NetworkNode implements ICraftingPatternC
private ItemHandlerUpgrade upgrades = new ItemHandlerUpgrade(4, new ItemHandlerListenerNetworkNode(this), ItemUpgrade.TYPE_SPEED); private ItemHandlerUpgrade upgrades = new ItemHandlerUpgrade(4, new ItemHandlerListenerNetworkNode(this), ItemUpgrade.TYPE_SPEED);
// Used to prevent infinite recursion on getRootContainer() when there's eg. two crafters facing each other. // Used to prevent infinite recursion on getRootContainer() when there's e.g. two crafters facing each other.
private boolean visited = false; private boolean visited = false;
@Nullable @Nullable

View File

@@ -0,0 +1,32 @@
package com.raoulvdberge.refinedstorage.tile;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
public class DirectionHandlerNetworkNode implements IDirectionHandler {
private TileNode tile;
public DirectionHandlerNetworkNode(TileNode tile) {
this.tile = tile;
}
@Override
public void setDirection(EnumFacing direction) {
tile.getNode().setDirection(direction);
}
@Override
public EnumFacing getDirection() {
return tile.getNode().getDirection();
}
@Override
public void writeToTileNbt(NBTTagCompound tag) {
// NO OP
}
@Override
public void readFromTileNbt(NBTTagCompound tag) {
// NO OP
}
}

View File

@@ -0,0 +1,32 @@
package com.raoulvdberge.refinedstorage.tile;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
public class DirectionHandlerTile implements IDirectionHandler {
public static final String NBT_DIRECTION = "Direction";
private EnumFacing direction;
@Override
public void setDirection(EnumFacing direction) {
this.direction = direction;
}
@Override
public EnumFacing getDirection() {
return direction;
}
@Override
public void writeToTileNbt(NBTTagCompound tag) {
tag.setInteger(NBT_DIRECTION, direction.ordinal());
}
@Override
public void readFromTileNbt(NBTTagCompound tag) {
if (tag.hasKey(NBT_DIRECTION)) {
direction = EnumFacing.getFront(tag.getInteger(NBT_DIRECTION));
}
}
}

View File

@@ -0,0 +1,14 @@
package com.raoulvdberge.refinedstorage.tile;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
public interface IDirectionHandler {
void setDirection(EnumFacing direction);
EnumFacing getDirection();
void writeToTileNbt(NBTTagCompound tag);
void readFromTileNbt(NBTTagCompound tag);
}

View File

@@ -17,12 +17,14 @@ import javax.annotation.Nullable;
public abstract class TileBase extends TileEntity { public abstract class TileBase extends TileEntity {
protected static final String NBT_DIRECTION = "Direction"; protected static final String NBT_DIRECTION = "Direction";
private EnumFacing direction = EnumFacing.NORTH; private EnumFacing clientDirection = EnumFacing.NORTH;
protected IDirectionHandler directionHandler = new DirectionHandlerTile();
protected TileDataManager dataManager = new TileDataManager(this); protected TileDataManager dataManager = new TileDataManager(this);
public void setDirection(EnumFacing direction) { public void setDirection(EnumFacing direction) {
this.direction = direction; clientDirection = direction;
directionHandler.setDirection(direction);
world.notifyNeighborsOfStateChange(pos, world.getBlockState(pos).getBlock(), true); world.notifyNeighborsOfStateChange(pos, world.getBlockState(pos).getBlock(), true);
@@ -30,7 +32,7 @@ public abstract class TileBase extends TileEntity {
} }
public EnumFacing getDirection() { public EnumFacing getDirection() {
return direction; return world.isRemote ? clientDirection : directionHandler.getDirection();
} }
public TileDataManager getDataManager() { public TileDataManager getDataManager() {
@@ -38,25 +40,25 @@ public abstract class TileBase extends TileEntity {
} }
public NBTTagCompound write(NBTTagCompound tag) { public NBTTagCompound write(NBTTagCompound tag) {
tag.setInteger(NBT_DIRECTION, direction.ordinal()); directionHandler.writeToTileNbt(tag);
return tag; return tag;
} }
public NBTTagCompound writeUpdate(NBTTagCompound tag) { public NBTTagCompound writeUpdate(NBTTagCompound tag) {
tag.setInteger(NBT_DIRECTION, direction.ordinal()); tag.setInteger(NBT_DIRECTION, directionHandler.getDirection().ordinal());
return tag; return tag;
} }
public void read(NBTTagCompound tag) { public void read(NBTTagCompound tag) {
direction = EnumFacing.getFront(tag.getInteger(NBT_DIRECTION)); directionHandler.readFromTileNbt(tag);
} }
public void readUpdate(NBTTagCompound tag) { public void readUpdate(NBTTagCompound tag) {
boolean doRender = canCauseRenderUpdate(tag); boolean doRender = canCauseRenderUpdate(tag);
direction = EnumFacing.getFront(tag.getInteger(NBT_DIRECTION)); clientDirection = EnumFacing.getFront(tag.getInteger(NBT_DIRECTION));
if (doRender) { if (doRender) {
WorldUtils.updateBlock(world, pos); WorldUtils.updateBlock(world, pos);

View File

@@ -27,6 +27,8 @@ public abstract class TileNode<N extends NetworkNode> extends TileBase implement
private N clientNode; private N clientNode;
public TileNode() { public TileNode() {
directionHandler = new DirectionHandlerNetworkNode(this);
dataManager.addWatchedParameter(REDSTONE_MODE); dataManager.addWatchedParameter(REDSTONE_MODE);
} }
@@ -49,13 +51,6 @@ public abstract class TileNode<N extends NetworkNode> extends TileBase implement
getNode().markDirty(); getNode().markDirty();
} }
@Override
public void setDirection(EnumFacing direction) {
super.setDirection(direction);
getNode().loadDirection();
}
public NBTTagCompound writeUpdate(NBTTagCompound tag) { public NBTTagCompound writeUpdate(NBTTagCompound tag) {
super.writeUpdate(tag); super.writeUpdate(tag);