Make fluid storage blocks work again.
This commit is contained in:
@@ -6,7 +6,10 @@ import com.raoulvdberge.refinedstorage.api.network.INetwork;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.AccessType;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.IStorage;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.IStorageProvider;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.disk.IStorageDisk;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.disk.IStorageDiskContainerContext;
|
||||
import com.raoulvdberge.refinedstorage.api.util.IComparer;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.API;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.network.node.IGuiStorage;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.network.node.NetworkNode;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.storage.StorageCacheFluid;
|
||||
@@ -26,14 +29,16 @@ import net.minecraft.world.World;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage, IStorageProvider, IComparable, IFilterable, IPrioritizable, IExcessVoidable, IAccessType {
|
||||
public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage, IStorageProvider, IComparable, IFilterable, IPrioritizable, IExcessVoidable, IAccessType, IStorageDiskContainerContext {
|
||||
public static final String ID = "fluid_storage";
|
||||
|
||||
private static final String NBT_PRIORITY = "Priority";
|
||||
private static final String NBT_COMPARE = "Compare";
|
||||
private static final String NBT_MODE = "Mode";
|
||||
private static final String NBT_VOID_EXCESS = "VoidExcess";
|
||||
public static final String NBT_ID = "Id";
|
||||
|
||||
private ItemHandlerFluid filters = new ItemHandlerFluid(9, new ItemHandlerListenerNetworkNode(this));
|
||||
|
||||
@@ -45,6 +50,9 @@ public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage,
|
||||
private int mode = IFilterable.BLACKLIST;
|
||||
private boolean voidExcess = false;
|
||||
|
||||
private UUID storageId;
|
||||
private IStorageDisk<FluidStack> storage;
|
||||
|
||||
public NetworkNodeFluidStorage(World world, BlockPos pos) {
|
||||
super(world, pos);
|
||||
}
|
||||
@@ -68,7 +76,7 @@ public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage,
|
||||
|
||||
@Override
|
||||
public void addFluidStorages(List<IStorage<FluidStack>> storages) {
|
||||
// NO OP
|
||||
storages.add(storage);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -76,6 +84,51 @@ public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage,
|
||||
return ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound write(NBTTagCompound tag) {
|
||||
super.write(tag);
|
||||
|
||||
tag.setUniqueId(NBT_ID, storageId);
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(NBTTagCompound tag) {
|
||||
super.read(tag);
|
||||
|
||||
if (tag.hasUniqueId(NBT_ID)) {
|
||||
storageId = tag.getUniqueId(NBT_ID);
|
||||
|
||||
loadStorage();
|
||||
}
|
||||
}
|
||||
|
||||
public void loadStorage() {
|
||||
IStorageDisk disk = API.instance().getStorageDiskManager(world).get(storageId);
|
||||
|
||||
if (disk == null) {
|
||||
API.instance().getStorageDiskManager(world).set(storageId, disk = API.instance().createDefaultFluidDisk(world, getType().getCapacity()));
|
||||
API.instance().getStorageDiskManager(world).markForSaving();
|
||||
}
|
||||
|
||||
this.storage = new StorageDiskFluidStorageWrapper(this, disk);
|
||||
}
|
||||
|
||||
public void setStorageId(UUID id) {
|
||||
this.storageId = id;
|
||||
|
||||
markDirty();
|
||||
}
|
||||
|
||||
public UUID getStorageId() {
|
||||
return storageId;
|
||||
}
|
||||
|
||||
public IStorageDisk<FluidStack> getStorage() {
|
||||
return storage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound writeConfiguration(NBTTagCompound tag) {
|
||||
super.writeConfiguration(tag);
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.raoulvdberge.refinedstorage.apiimpl.network.node.storage;
|
||||
|
||||
import com.raoulvdberge.refinedstorage.api.storage.AccessType;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.disk.IStorageDisk;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.disk.IStorageDiskContainerContext;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.disk.IStorageDiskListener;
|
||||
import com.raoulvdberge.refinedstorage.tile.config.IFilterable;
|
||||
import com.raoulvdberge.refinedstorage.util.StackUtils;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
|
||||
public class StorageDiskFluidStorageWrapper implements IStorageDisk<FluidStack> {
|
||||
private NetworkNodeFluidStorage storage;
|
||||
private IStorageDisk<FluidStack> parent;
|
||||
|
||||
public StorageDiskFluidStorageWrapper(NetworkNodeFluidStorage storage, IStorageDisk<FluidStack> parent) {
|
||||
this.storage = storage;
|
||||
this.parent = parent;
|
||||
this.setSettings(null, storage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return storage.getPriority();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessType getAccessType() {
|
||||
return parent.getAccessType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<FluidStack> getStacks() {
|
||||
return parent.getStacks();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public FluidStack insert(@Nonnull FluidStack stack, int size, boolean simulate) {
|
||||
if (!IFilterable.canTakeFluids(storage.getFilters(), storage.getMode(), storage.getCompare(), stack)) {
|
||||
return StackUtils.copy(stack, size);
|
||||
}
|
||||
|
||||
return parent.insert(stack, size, simulate);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidStack extract(@Nonnull FluidStack stack, int size, int flags, boolean simulate) {
|
||||
return parent.extract(stack, size, flags, simulate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStored() {
|
||||
return parent.getStored();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCacheDelta(int storedPreInsertion, int size, @Nullable FluidStack remainder) {
|
||||
return parent.getCacheDelta(storedPreInsertion, size, remainder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCapacity() {
|
||||
return parent.getCapacity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSettings(@Nullable IStorageDiskListener listener, IStorageDiskContainerContext context) {
|
||||
parent.setSettings(listener, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound writeToNbt() {
|
||||
return parent.writeToNbt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return parent.getId();
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,29 @@
|
||||
package com.raoulvdberge.refinedstorage.block;
|
||||
|
||||
import com.raoulvdberge.refinedstorage.RSBlocks;
|
||||
import com.raoulvdberge.refinedstorage.RSGui;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.network.node.storage.NetworkNodeFluidStorage;
|
||||
import com.raoulvdberge.refinedstorage.item.ItemBlockFluidStorage;
|
||||
import com.raoulvdberge.refinedstorage.tile.TileFluidStorage;
|
||||
import net.minecraft.block.properties.PropertyEnum;
|
||||
import net.minecraft.block.state.BlockStateContainer;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.UUID;
|
||||
|
||||
public class BlockFluidStorage extends BlockNode {
|
||||
public static final PropertyEnum TYPE = PropertyEnum.create("type", FluidStorageType.class);
|
||||
@@ -76,4 +82,34 @@ public class BlockFluidStorage extends BlockNode {
|
||||
public Direction getDirection() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase player, ItemStack stack) {
|
||||
if (!world.isRemote) {
|
||||
NetworkNodeFluidStorage storage = ((TileFluidStorage) world.getTileEntity(pos)).getNode();
|
||||
|
||||
if (stack.hasTagCompound() && stack.getTagCompound().hasUniqueId(NetworkNodeFluidStorage.NBT_ID)) {
|
||||
storage.setStorageId(stack.getTagCompound().getUniqueId(NetworkNodeFluidStorage.NBT_ID));
|
||||
} else {
|
||||
storage.setStorageId(UUID.randomUUID());
|
||||
}
|
||||
|
||||
storage.loadStorage();
|
||||
}
|
||||
|
||||
// Call this after loading the storage, so the network discovery can use the loaded storage.
|
||||
super.onBlockPlacedBy(world, pos, state, player, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
|
||||
TileFluidStorage storage = (TileFluidStorage) world.getTileEntity(pos);
|
||||
|
||||
ItemStack stack = new ItemStack(RSBlocks.FLUID_STORAGE, 1, getMetaFromState(state));
|
||||
|
||||
stack.setTagCompound(new NBTTagCompound());
|
||||
stack.getTagCompound().setUniqueId(NetworkNodeFluidStorage.NBT_ID, storage.getNode().getStorageId());
|
||||
|
||||
drops.add(stack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,103 @@
|
||||
package com.raoulvdberge.refinedstorage.item;
|
||||
|
||||
import com.raoulvdberge.refinedstorage.RSBlocks;
|
||||
import com.raoulvdberge.refinedstorage.RSItems;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.disk.IStorageDisk;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.disk.IStorageDiskSyncData;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.API;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.network.node.storage.NetworkNodeFluidStorage;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.client.util.ITooltipFlag;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.InventoryHelper;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.EnumActionResult;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ItemBlockFluidStorage extends ItemBlockBase {
|
||||
public ItemBlockFluidStorage() {
|
||||
super(RSBlocks.FLUID_STORAGE, RSBlocks.FLUID_STORAGE.getDirection(), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInformation(ItemStack stack, @Nullable World world, List<String> tooltip, ITooltipFlag flag) {
|
||||
super.addInformation(stack, world, tooltip, flag);
|
||||
|
||||
if (isValid(stack)) {
|
||||
UUID id = getId(stack);
|
||||
|
||||
API.instance().getStorageDiskSync().sendRequest(id);
|
||||
|
||||
IStorageDiskSyncData data = API.instance().getStorageDiskSync().getData(id);
|
||||
if (data != null) {
|
||||
if (data.getCapacity() == -1) {
|
||||
tooltip.add(I18n.format("misc.refinedstorage:storage.stored", API.instance().getQuantityFormatter().format(data.getStored())));
|
||||
} else {
|
||||
tooltip.add(I18n.format("misc.refinedstorage:storage.stored_capacity", API.instance().getQuantityFormatter().format(data.getStored()), API.instance().getQuantityFormatter().format(data.getCapacity())));
|
||||
}
|
||||
}
|
||||
|
||||
if (flag.isAdvanced()) {
|
||||
tooltip.add(id.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
|
||||
ItemStack storageStack = player.getHeldItem(hand);
|
||||
|
||||
if (!world.isRemote && player.isSneaking() && storageStack.getMetadata() != ItemFluidStorageDisk.TYPE_CREATIVE) {
|
||||
UUID diskId = null;
|
||||
IStorageDisk disk = null;
|
||||
|
||||
if (isValid(storageStack)) {
|
||||
diskId = getId(storageStack);
|
||||
disk = API.instance().getStorageDiskManager(world).get(diskId);
|
||||
}
|
||||
|
||||
// Newly created storages won't have a tag yet, so allow invalid disks as well.
|
||||
if (disk == null || disk.getStored() == 0) {
|
||||
ItemStack storagePart = new ItemStack(RSItems.FLUID_STORAGE_PART, storageStack.getCount(), storageStack.getMetadata());
|
||||
|
||||
if (!player.inventory.addItemStackToInventory(storagePart.copy())) {
|
||||
InventoryHelper.spawnItemStack(world, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), storagePart);
|
||||
}
|
||||
|
||||
ItemStack processor = new ItemStack(RSItems.PROCESSOR, storageStack.getCount(), ItemProcessor.TYPE_BASIC);
|
||||
|
||||
if (!player.inventory.addItemStackToInventory(processor.copy())) {
|
||||
InventoryHelper.spawnItemStack(world, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), processor);
|
||||
}
|
||||
|
||||
if (disk != null) {
|
||||
API.instance().getStorageDiskManager(world).remove(diskId);
|
||||
API.instance().getStorageDiskManager(world).markForSaving();
|
||||
}
|
||||
|
||||
return new ActionResult<>(EnumActionResult.SUCCESS, new ItemStack(RSBlocks.MACHINE_CASING));
|
||||
}
|
||||
}
|
||||
|
||||
return new ActionResult<>(EnumActionResult.PASS, storageStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEntityLifespan(ItemStack stack, World world) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
private UUID getId(ItemStack disk) {
|
||||
return disk.getTagCompound().getUniqueId(NetworkNodeFluidStorage.NBT_ID);
|
||||
}
|
||||
|
||||
private boolean isValid(ItemStack disk) {
|
||||
return disk.hasTagCompound() && disk.getTagCompound().hasUniqueId(NetworkNodeFluidStorage.NBT_ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,13 +64,13 @@ public class ItemBlockStorage extends ItemBlockBase {
|
||||
|
||||
// Newly created storages won't have a tag yet, so allow invalid disks as well.
|
||||
if (disk == null || disk.getStored() == 0) {
|
||||
ItemStack storagePart = new ItemStack(RSItems.STORAGE_PART, 1, storageStack.getMetadata());
|
||||
ItemStack storagePart = new ItemStack(RSItems.STORAGE_PART, storageStack.getCount(), storageStack.getMetadata());
|
||||
|
||||
if (!player.inventory.addItemStackToInventory(storagePart.copy())) {
|
||||
InventoryHelper.spawnItemStack(world, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), storagePart);
|
||||
}
|
||||
|
||||
ItemStack processor = new ItemStack(RSItems.PROCESSOR, 1, ItemProcessor.TYPE_BASIC);
|
||||
ItemStack processor = new ItemStack(RSItems.PROCESSOR, storageStack.getCount(), ItemProcessor.TYPE_BASIC);
|
||||
|
||||
if (!player.inventory.addItemStackToInventory(processor.copy())) {
|
||||
InventoryHelper.spawnItemStack(world, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), processor);
|
||||
|
||||
@@ -100,7 +100,7 @@ public class ItemFluidStorageDisk extends ItemBase implements IStorageDiskProvid
|
||||
IStorageDisk disk = API.instance().getStorageDiskManager(world).getByStack(diskStack);
|
||||
|
||||
if (disk != null && disk.getStored() == 0) {
|
||||
ItemStack storagePart = new ItemStack(RSItems.FLUID_STORAGE_PART, 1, diskStack.getMetadata());
|
||||
ItemStack storagePart = new ItemStack(RSItems.FLUID_STORAGE_PART, diskStack.getCount(), diskStack.getMetadata());
|
||||
|
||||
if (!player.inventory.addItemStackToInventory(storagePart.copy())) {
|
||||
InventoryHelper.spawnItemStack(world, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), storagePart);
|
||||
|
||||
@@ -100,7 +100,7 @@ public class ItemStorageDisk extends ItemBase implements IStorageDiskProvider {
|
||||
IStorageDisk disk = API.instance().getStorageDiskManager(world).getByStack(diskStack);
|
||||
|
||||
if (disk != null && disk.getStored() == 0) {
|
||||
ItemStack storagePart = new ItemStack(RSItems.STORAGE_PART, 1, diskStack.getMetadata());
|
||||
ItemStack storagePart = new ItemStack(RSItems.STORAGE_PART, diskStack.getCount(), diskStack.getMetadata());
|
||||
|
||||
if (!player.inventory.addItemStackToInventory(storagePart.copy())) {
|
||||
InventoryHelper.spawnItemStack(world, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), storagePart);
|
||||
|
||||
@@ -16,7 +16,7 @@ public class TileFluidStorage extends TileNode<NetworkNodeFluidStorage> {
|
||||
public static final TileDataParameter<Boolean, TileFluidStorage> VOID_EXCESS = IExcessVoidable.createParameter();
|
||||
public static final TileDataParameter<Integer, TileFluidStorage> MODE = IFilterable.createParameter();
|
||||
public static final TileDataParameter<AccessType, TileFluidStorage> ACCESS_TYPE = IAccessType.createParameter();
|
||||
public static final TileDataParameter<Integer, TileFluidStorage> STORED = new TileDataParameter<>(DataSerializers.VARINT, 0, t -> 0); // TODO
|
||||
public static final TileDataParameter<Integer, TileFluidStorage> STORED = new TileDataParameter<>(DataSerializers.VARINT, 0, t -> t.getNode().getStorage().getStored());
|
||||
|
||||
public TileFluidStorage() {
|
||||
dataManager.addWatchedParameter(PRIORITY);
|
||||
|
||||
Reference in New Issue
Block a user