Remove connectable API in favor of capabilities

This commit is contained in:
Raoul Van den Berge
2016-11-30 17:12:13 +01:00
parent d04ac11bde
commit aa27de7618
8 changed files with 9 additions and 278 deletions

View File

@@ -11,13 +11,9 @@ import com.raoulvdberge.refinedstorage.api.solderer.ISoldererRegistry;
import com.raoulvdberge.refinedstorage.api.util.IComparer; import com.raoulvdberge.refinedstorage.api.util.IComparer;
import com.raoulvdberge.refinedstorage.api.util.IStackList; import com.raoulvdberge.refinedstorage.api.util.IStackList;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidStack;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Set;
import java.util.function.BiPredicate;
/** /**
* Represents a Refined Storage API implementation. * Represents a Refined Storage API implementation.
@@ -92,9 +88,4 @@ public interface IRSAPI {
* @return a hashcode for the given stack * @return a hashcode for the given stack
*/ */
int getFluidStackHashCode(FluidStack stack); int getFluidStackHashCode(FluidStack stack);
/**
* @return a set with the predicates to check if a block is connectable
*/
Set<BiPredicate<TileEntity, EnumFacing>> getConnectableConditions();
} }

View File

@@ -137,9 +137,4 @@ public class API implements IRSAPI {
public int getFluidStackHashCode(FluidStack stack) { public int getFluidStackHashCode(FluidStack stack) {
return stack.getFluid().hashCode() * (stack.tag != null ? stack.tag.hashCode() : 1); return stack.getFluid().hashCode() * (stack.tag != null ? stack.tag.hashCode() : 1);
} }
@Override
public Set<BiPredicate<TileEntity, EnumFacing>> getConnectableConditions() {
return connectableConditions;
}
} }

View File

@@ -1,257 +0,0 @@
package com.raoulvdberge.refinedstorage.apiimpl.storage.fluid;
import com.raoulvdberge.refinedstorage.RSUtils;
import com.raoulvdberge.refinedstorage.api.storage.AccessType;
import com.raoulvdberge.refinedstorage.api.storage.fluid.IFluidStorage;
import com.raoulvdberge.refinedstorage.apiimpl.API;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fluids.FluidStack;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* A implementation of {@link IFluidStorage} that stores storage fluids in NBT.
*/
public abstract class FluidStorageNBT implements IFluidStorage {
/**
* The current save protocol that is used. It's set to every {@link FluidStorageNBT} to allow for
* safe backwards compatibility breaks.
*/
private static final int PROTOCOL = 1;
private static final String NBT_PROTOCOL = "Protocol";
private static final String NBT_FLUIDS = "Fluids";
private static final String NBT_STORED = "Stored";
private NBTTagCompound tag;
private int capacity;
private TileEntity tile;
private NonNullList<FluidStack> stacks = NonNullList.create();
/**
* @param tag The NBT tag we are reading from and writing the amount stored to, has to be initialized with {@link FluidStorageNBT#createNBT()} if it doesn't exist yet
* @param capacity The capacity of this storage, -1 for infinite capacity
* @param tile A {@link TileEntity} that the NBT storage is in, will be marked dirty when the storage changes
*/
public FluidStorageNBT(NBTTagCompound tag, int capacity, @Nullable TileEntity tile) {
this.tag = tag;
this.capacity = capacity;
this.tile = tile;
readFromNBT();
}
private void readFromNBT() {
NBTTagList list = (NBTTagList) tag.getTag(NBT_FLUIDS);
for (int i = 0; i < list.tagCount(); ++i) {
FluidStack stack = FluidStack.loadFluidStackFromNBT(list.getCompoundTagAt(i));
if (stack != null) {
stacks.add(stack);
}
}
}
/**
* Writes the items to the NBT tag.
*/
public void writeToNBT() {
NBTTagList list = new NBTTagList();
for (FluidStack stack : stacks) {
list.appendTag(stack.writeToNBT(new NBTTagCompound()));
}
tag.setTag(NBT_FLUIDS, list);
tag.setInteger(NBT_PROTOCOL, PROTOCOL);
}
@Override
public NonNullList<FluidStack> getStacks() {
return stacks;
}
@Override
public synchronized FluidStack insertFluid(@Nonnull FluidStack stack, int size, boolean simulate) {
for (FluidStack otherStack : stacks) {
if (otherStack.isFluidEqual(stack)) {
if (getCapacity() != -1 && getStored() + size > getCapacity()) {
int remainingSpace = getCapacity() - getStored();
if (remainingSpace <= 0) {
if (isVoiding()) {
return null;
}
return RSUtils.copyStackWithSize(stack, size);
}
if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + remainingSpace);
otherStack.amount += remainingSpace;
onStorageChanged();
}
return isVoiding() ? null : RSUtils.copyStackWithSize(otherStack, size - remainingSpace);
} else {
if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + size);
otherStack.amount += size;
onStorageChanged();
}
return null;
}
}
}
if (getCapacity() != -1 && getStored() + size > getCapacity()) {
int remainingSpace = getCapacity() - getStored();
if (remainingSpace <= 0) {
if (isVoiding()) {
return null;
}
return RSUtils.copyStackWithSize(stack, size);
}
if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + remainingSpace);
stacks.add(RSUtils.copyStackWithSize(stack, remainingSpace));
onStorageChanged();
}
return isVoiding() ? null : RSUtils.copyStackWithSize(stack, size - remainingSpace);
} else {
if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + size);
stacks.add(RSUtils.copyStackWithSize(stack, size));
onStorageChanged();
}
return null;
}
}
@Override
public synchronized FluidStack extractFluid(@Nonnull FluidStack stack, int size, int flags, boolean simulate) {
for (FluidStack otherStack : stacks) {
if (API.instance().getComparer().isEqual(otherStack, stack, flags)) {
if (size > otherStack.amount) {
size = otherStack.amount;
}
if (!simulate) {
if (otherStack.amount - size == 0) {
stacks.remove(otherStack);
} else {
otherStack.amount -= size;
}
tag.setInteger(NBT_STORED, getStored() - size);
onStorageChanged();
}
return RSUtils.copyStackWithSize(otherStack, size);
}
}
return null;
}
public void onStorageChanged() {
if (tile != null) {
tile.markDirty();
}
}
@Override
public int getStored() {
return getStoredFromNBT(tag);
}
public int getCapacity() {
return capacity;
}
protected boolean isVoiding() {
return false;
}
@Override
public int getCacheDelta(int storedPreInsertion, int size, @Nullable FluidStack remainder) {
if (getAccessType() == AccessType.INSERT) {
return 0;
}
int inserted = remainder == null ? size : (size - remainder.amount);
if (isVoiding() && storedPreInsertion + inserted > getCapacity()) {
inserted = getCapacity() - storedPreInsertion;
}
return inserted;
}
public NBTTagCompound getTag() {
return tag;
}
public static int getStoredFromNBT(NBTTagCompound tag) {
return tag.getInteger(NBT_STORED);
}
public static NBTTagCompound getNBTShareTag(NBTTagCompound tag) {
NBTTagCompound otherTag = new NBTTagCompound();
otherTag.setInteger(NBT_STORED, getStoredFromNBT(tag));
otherTag.setTag(NBT_FLUIDS, new NBTTagList()); // To circumvent not being able to insert disks in Disk Drives (see FluidStorageNBT#isValid(ItemStack)).
return otherTag;
}
/*
* @return A NBT tag initialized with the fields that {@link NBTStorage} uses
*/
public static NBTTagCompound createNBT() {
NBTTagCompound tag = new NBTTagCompound();
tag.setTag(NBT_FLUIDS, new NBTTagList());
tag.setInteger(NBT_STORED, 0);
tag.setInteger(NBT_PROTOCOL, PROTOCOL);
return tag;
}
public static boolean isValid(ItemStack stack) {
return stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_FLUIDS) && stack.getTagCompound().hasKey(NBT_STORED);
}
/**
* @param stack The {@link ItemStack} to populate with the NBT tags from {@link FluidStorageNBT#createNBT()}
* @return The provided {@link ItemStack} with NBT tags from {@link FluidStorageNBT#createNBT()}
*/
public static ItemStack createStackWithNBT(ItemStack stack) {
stack.setTagCompound(createNBT());
return stack;
}
}

View File

@@ -1,7 +1,7 @@
package com.raoulvdberge.refinedstorage.block; package com.raoulvdberge.refinedstorage.block;
import com.raoulvdberge.refinedstorage.RSUtils; import com.raoulvdberge.refinedstorage.RSUtils;
import com.raoulvdberge.refinedstorage.apiimpl.API; import com.raoulvdberge.refinedstorage.proxy.CapabilityNetworkNode;
import com.raoulvdberge.refinedstorage.tile.TileCable; import com.raoulvdberge.refinedstorage.tile.TileCable;
import net.minecraft.block.properties.PropertyBool; import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.BlockStateContainer;
@@ -93,7 +93,7 @@ public class BlockCable extends BlockNode {
TileEntity otherTile = world.getTileEntity(pos.offset(direction)); TileEntity otherTile = world.getTileEntity(pos.offset(direction));
EnumFacing otherTileSide = direction.getOpposite(); EnumFacing otherTileSide = direction.getOpposite();
return API.instance().getConnectableConditions().stream().anyMatch(p -> p.test(otherTile, otherTileSide)); return otherTile.hasCapability(CapabilityNetworkNode.NETWORK_NODE_CAPABILITY, otherTileSide);
} }
private boolean isInAABB(AxisAlignedBB aabb, float hitX, float hitY, float hitZ) { private boolean isInAABB(AxisAlignedBB aabb, float hitX, float hitY, float hitZ) {
@@ -158,6 +158,7 @@ public class BlockCable extends BlockNode {
} }
@Override @Override
@SuppressWarnings("deprecation")
public void addCollisionBoxToList(IBlockState state, World world, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> collidingBoxes, Entity entityIn) { public void addCollisionBoxToList(IBlockState state, World world, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> collidingBoxes, Entity entityIn) {
for (AxisAlignedBB aabb : getCollisionBoxes(this.getActualState(state, world, pos))) { for (AxisAlignedBB aabb : getCollisionBoxes(this.getActualState(state, world, pos))) {
addCollisionBoxToList(pos, entityBox, collidingBoxes, aabb); addCollisionBoxToList(pos, entityBox, collidingBoxes, aabb);
@@ -165,6 +166,7 @@ public class BlockCable extends BlockNode {
} }
@Override @Override
@SuppressWarnings("deprecation")
public RayTraceResult collisionRayTrace(IBlockState state, World world, BlockPos pos, Vec3d start, Vec3d end) { public RayTraceResult collisionRayTrace(IBlockState state, World world, BlockPos pos, Vec3d start, Vec3d end) {
RSUtils.AdvancedRayTraceResult result = RSUtils.collisionRayTrace(pos, start, end, getCollisionBoxes(this.getActualState(state, world, pos))); RSUtils.AdvancedRayTraceResult result = RSUtils.collisionRayTrace(pos, start, end, getCollisionBoxes(this.getActualState(state, world, pos)));
@@ -172,11 +174,13 @@ public class BlockCable extends BlockNode {
} }
@Override @Override
@SuppressWarnings("deprecation")
public boolean isOpaqueCube(IBlockState state) { public boolean isOpaqueCube(IBlockState state) {
return false; return false;
} }
@Override @Override
@SuppressWarnings("deprecation")
public boolean isFullCube(IBlockState state) { public boolean isFullCube(IBlockState state) {
return false; return false;
} }

View File

@@ -81,6 +81,7 @@ public class BlockExternalStorage extends BlockCable {
} }
@Override @Override
@SuppressWarnings("deprecation")
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block, BlockPos fromPos) { public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block, BlockPos fromPos) {
super.neighborChanged(state, world, pos, block, fromPos); super.neighborChanged(state, world, pos, block, fromPos);

View File

@@ -61,6 +61,7 @@ public class BlockWriter extends BlockCable {
} }
@Override @Override
@SuppressWarnings("deprecation")
public boolean canProvidePower(IBlockState state) { public boolean canProvidePower(IBlockState state) {
return true; return true;
} }

View File

@@ -24,7 +24,7 @@ public class CapabilityNetworkNode {
} }
public void readNBT(Capability<INetworkNode> capability, INetworkNode instance, EnumFacing side, NBTBase base) { public void readNBT(Capability<INetworkNode> capability, INetworkNode instance, EnumFacing side, NBTBase base) {
// no-op // NO OP
} }
}, () -> new INetworkNode() { }, () -> new INetworkNode() {
private INetworkMaster network; private INetworkMaster network;

View File

@@ -4,7 +4,6 @@ import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.RSBlocks; import com.raoulvdberge.refinedstorage.RSBlocks;
import com.raoulvdberge.refinedstorage.RSItems; import com.raoulvdberge.refinedstorage.RSItems;
import com.raoulvdberge.refinedstorage.RSUtils; import com.raoulvdberge.refinedstorage.RSUtils;
import com.raoulvdberge.refinedstorage.api.network.INetworkMaster;
import com.raoulvdberge.refinedstorage.apiimpl.API; import com.raoulvdberge.refinedstorage.apiimpl.API;
import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.craftingmonitor.*; import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.craftingmonitor.*;
import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.preview.CraftingPreviewElementFluidStack; import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.preview.CraftingPreviewElementFluidStack;
@@ -89,9 +88,6 @@ public class ProxyCommon {
API.instance().getReaderWriterHandlerRegistry().add(ReaderWriterHandlerRedstone.ID, tag -> new ReaderWriterHandlerRedstone()); API.instance().getReaderWriterHandlerRegistry().add(ReaderWriterHandlerRedstone.ID, tag -> new ReaderWriterHandlerRedstone());
API.instance().getReaderWriterHandlerRegistry().add(ReaderWriterHandlerForgeEnergy.ID, ReaderWriterHandlerForgeEnergy::new); API.instance().getReaderWriterHandlerRegistry().add(ReaderWriterHandlerForgeEnergy.ID, ReaderWriterHandlerForgeEnergy::new);
API.instance().getConnectableConditions().add((tile, side) -> tile != null &&
(tile instanceof INetworkMaster || tile.hasCapability(CapabilityNetworkNode.NETWORK_NODE_CAPABILITY, side)));
if (IntegrationTesla.isLoaded()) { if (IntegrationTesla.isLoaded()) {
API.instance().getReaderWriterHandlerRegistry().add(ReaderWriterHandlerTesla.ID, ReaderWriterHandlerTesla::new); API.instance().getReaderWriterHandlerRegistry().add(ReaderWriterHandlerTesla.ID, ReaderWriterHandlerTesla::new);
} }