1.5 -> 1.6 migration: item and fluid storage blocks. #1816

This commit is contained in:
raoulvdberge
2018-06-15 01:14:02 +02:00
parent 210112c3bc
commit 50e94ead18
9 changed files with 130 additions and 63 deletions

View File

@@ -77,9 +77,7 @@ public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage,
@Override
public void addFluidStorages(List<IStorage<FluidStack>> storages) {
if (storage != null) {
storages.add(storage);
}
storages.add(storage);
}
@Override
@@ -91,9 +89,7 @@ public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage,
public NBTTagCompound write(NBTTagCompound tag) {
super.write(tag);
if (storageId != null) {
tag.setUniqueId(NBT_ID, storageId);
}
tag.setUniqueId(NBT_ID, storageId);
return tag;
}
@@ -107,6 +103,8 @@ public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage,
loadStorage();
}
OneSixMigrationHelper.migrateFluidStorageBlock(this, tag);
}
public void loadStorage() {
@@ -173,7 +171,7 @@ public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage,
}
accessType = AccessTypeUtils.readAccessType(tag);
OneSixMigrationHelper.migrateEmptyWhitelistToEmptyBlacklist(version, this, null, filters);
}

View File

@@ -72,9 +72,7 @@ public class NetworkNodeStorage extends NetworkNode implements IGuiStorage, ISto
@Override
public void addItemStorages(List<IStorage<ItemStack>> storages) {
if (storage != null) {
storages.add(storage);
}
storages.add(storage);
}
@Override
@@ -91,9 +89,7 @@ public class NetworkNodeStorage extends NetworkNode implements IGuiStorage, ISto
public NBTTagCompound write(NBTTagCompound tag) {
super.write(tag);
if (storageId != null) {
tag.setUniqueId(NBT_ID, storageId);
}
tag.setUniqueId(NBT_ID, storageId);
return tag;
}
@@ -107,6 +103,8 @@ public class NetworkNodeStorage extends NetworkNode implements IGuiStorage, ISto
loadStorage();
}
OneSixMigrationHelper.migrateItemStorageBlock(this, tag);
}
public void loadStorage() {

View File

@@ -5,6 +5,10 @@ import com.raoulvdberge.refinedstorage.api.storage.disk.IStorageDiskProvider;
import com.raoulvdberge.refinedstorage.api.util.IFilter;
import com.raoulvdberge.refinedstorage.api.util.IOneSixMigrationHelper;
import com.raoulvdberge.refinedstorage.apiimpl.API;
import com.raoulvdberge.refinedstorage.apiimpl.network.node.storage.NetworkNodeFluidStorage;
import com.raoulvdberge.refinedstorage.apiimpl.network.node.storage.NetworkNodeStorage;
import com.raoulvdberge.refinedstorage.block.FluidStorageType;
import com.raoulvdberge.refinedstorage.block.ItemStorageType;
import com.raoulvdberge.refinedstorage.item.ItemPattern;
import com.raoulvdberge.refinedstorage.tile.config.IFilterable;
import net.minecraft.item.Item;
@@ -31,6 +35,57 @@ public class OneSixMigrationHelper implements IOneSixMigrationHelper {
private static final String NBT_FLUIDS = "Fluids";
private static UUID createItemDisk(World world, int capacity, NBTTagCompound legacyTag) {
UUID id = UUID.randomUUID();
IStorageDisk<ItemStack> newDisk = API.instance().createDefaultItemDisk(world, capacity);
NBTTagList list = (NBTTagList) legacyTag.getTag(NBT_ITEMS);
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound tag = list.getCompoundTagAt(i);
ItemStack stack = new ItemStack(
Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)),
tag.getInteger(NBT_ITEM_QUANTITY),
tag.getInteger(NBT_ITEM_DAMAGE),
tag.hasKey(NBT_ITEM_CAPS) ? tag.getCompoundTag(NBT_ITEM_CAPS) : null
);
stack.setTagCompound(tag.hasKey(NBT_ITEM_NBT) ? tag.getCompoundTag(NBT_ITEM_NBT) : null);
if (!stack.isEmpty()) {
newDisk.insert(stack, stack.getCount(), false);
}
}
API.instance().getStorageDiskManager(world).set(id, newDisk);
API.instance().getStorageDiskManager(world).markForSaving();
return id;
}
private static UUID createFluidDisk(World world, int capacity, NBTTagCompound legacyTag) {
UUID id = UUID.randomUUID();
IStorageDisk<FluidStack> newDisk = API.instance().createDefaultFluidDisk(world, capacity);
NBTTagList list = (NBTTagList) legacyTag.getTag(NBT_FLUIDS);
for (int i = 0; i < list.tagCount(); ++i) {
FluidStack stack = FluidStack.loadFluidStackFromNBT(list.getCompoundTagAt(i));
if (stack != null) {
newDisk.insert(stack, stack.amount, false);
}
}
API.instance().getStorageDiskManager(world).set(id, newDisk);
API.instance().getStorageDiskManager(world).markForSaving();
return id;
}
@Override
public boolean migrateDisk(World world, ItemStack disk) {
IStorageDiskProvider provider = (IStorageDiskProvider) disk.getItem();
@@ -38,33 +93,7 @@ public class OneSixMigrationHelper implements IOneSixMigrationHelper {
switch (provider.getType()) {
case ITEM:
if (disk.hasTagCompound() && disk.getTagCompound().hasKey(NBT_ITEMS)) {
UUID id = UUID.randomUUID();
IStorageDisk<ItemStack> newDisk = API.instance().createDefaultItemDisk(world, provider.getCapacity(disk));
NBTTagList list = (NBTTagList) disk.getTagCompound().getTag(NBT_ITEMS);
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound tag = list.getCompoundTagAt(i);
ItemStack stack = new ItemStack(
Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)),
tag.getInteger(NBT_ITEM_QUANTITY),
tag.getInteger(NBT_ITEM_DAMAGE),
tag.hasKey(NBT_ITEM_CAPS) ? tag.getCompoundTag(NBT_ITEM_CAPS) : null
);
stack.setTagCompound(tag.hasKey(NBT_ITEM_NBT) ? tag.getCompoundTag(NBT_ITEM_NBT) : null);
if (!stack.isEmpty()) {
newDisk.insert(stack, stack.getCount(), false);
}
}
API.instance().getStorageDiskManager(world).set(id, newDisk);
API.instance().getStorageDiskManager(world).markForSaving();
provider.setId(disk, id);
provider.setId(disk, createItemDisk(world, provider.getCapacity(disk), disk.getTagCompound()));
return true;
}
@@ -72,24 +101,7 @@ public class OneSixMigrationHelper implements IOneSixMigrationHelper {
break;
case FLUID:
if (disk.hasTagCompound() && disk.getTagCompound().hasKey(NBT_FLUIDS)) {
UUID id = UUID.randomUUID();
IStorageDisk<FluidStack> newDisk = API.instance().createDefaultFluidDisk(world, provider.getCapacity(disk));
NBTTagList list = (NBTTagList) disk.getTagCompound().getTag(NBT_FLUIDS);
for (int i = 0; i < list.tagCount(); ++i) {
FluidStack stack = FluidStack.loadFluidStackFromNBT(list.getCompoundTagAt(i));
if (stack != null) {
newDisk.insert(stack, stack.amount, false);
}
}
API.instance().getStorageDiskManager(world).set(id, newDisk);
API.instance().getStorageDiskManager(world).markForSaving();
provider.setId(disk, id);
provider.setId(disk, createFluidDisk(world, provider.getCapacity(disk), disk.getTagCompound()));
return true;
}
@@ -197,4 +209,42 @@ public class OneSixMigrationHelper implements IOneSixMigrationHelper {
filterable.setMode(IFilter.MODE_BLACKLIST);
}
}
private static final String NBT_STORAGE = "Storage";
public static void migrateItemStorageBlock(NetworkNodeStorage storage, NBTTagCompound tag) {
if (tag.hasKey(NBT_STORAGE)) {
NBTTagCompound storageTag = tag.getCompoundTag(NBT_STORAGE);
storage.setStorageId(createItemDisk(storage.getWorld(), storage.getType().getCapacity(), storageTag));
storage.loadStorage();
}
}
public static void migrateItemStorageBlockItem(World world, ItemStack stack) {
if (stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_STORAGE)) {
NBTTagCompound storageTag = stack.getTagCompound().getCompoundTag(NBT_STORAGE);
stack.setTagCompound(new NBTTagCompound());
stack.getTagCompound().setUniqueId(NetworkNodeStorage.NBT_ID, createItemDisk(world, ItemStorageType.getById(stack.getItemDamage()).getCapacity(), storageTag));
}
}
public static void migrateFluidStorageBlock(NetworkNodeFluidStorage storage, NBTTagCompound tag) {
if (tag.hasKey(NBT_STORAGE)) {
NBTTagCompound storageTag = tag.getCompoundTag(NBT_STORAGE);
storage.setStorageId(createFluidDisk(storage.getWorld(), storage.getType().getCapacity(), storageTag));
storage.loadStorage();
}
}
public static void migrateFluidStorageBlockItem(World world, ItemStack stack) {
if (stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_STORAGE)) {
NBTTagCompound storageTag = stack.getTagCompound().getCompoundTag(NBT_STORAGE);
stack.setTagCompound(new NBTTagCompound());
stack.getTagCompound().setUniqueId(NetworkNodeStorage.NBT_ID, createFluidDisk(world, FluidStorageType.getById(stack.getItemDamage()).getCapacity(), storageTag));
}
}
}

View File

@@ -99,6 +99,7 @@ public class BlockCable extends BlockNode {
return getActualState(stateForRendering, world, pos);
}
// TODO: investigate connection issue
public static boolean hasConnectionWith(IBlockAccess world, BlockPos pos, BlockBase block, TileEntity tile, EnumFacing direction) {
if (!(tile instanceof TileNode)) {
return false;

View File

@@ -6,8 +6,10 @@ 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 com.raoulvdberge.refinedstorage.apiimpl.util.OneSixMigrationHelper;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
@@ -100,4 +102,13 @@ public class ItemBlockFluidStorage extends ItemBlockBase {
private boolean isValid(ItemStack disk) {
return disk.hasTagCompound() && disk.getTagCompound().hasUniqueId(NetworkNodeFluidStorage.NBT_ID);
}
@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) {
super.onUpdate(stack, world, entity, itemSlot, isSelected);
if (!world.isRemote) {
OneSixMigrationHelper.migrateFluidStorageBlockItem(world, stack);
}
}
}

View File

@@ -6,8 +6,10 @@ 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.NetworkNodeStorage;
import com.raoulvdberge.refinedstorage.apiimpl.util.OneSixMigrationHelper;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
@@ -100,4 +102,13 @@ public class ItemBlockStorage extends ItemBlockBase {
private boolean isValid(ItemStack disk) {
return disk.hasTagCompound() && disk.getTagCompound().hasUniqueId(NetworkNodeStorage.NBT_ID);
}
@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) {
super.onUpdate(stack, world, entity, itemSlot, isSelected);
if (!world.isRemote) {
OneSixMigrationHelper.migrateItemStorageBlockItem(world, stack);
}
}
}

View File

@@ -279,7 +279,7 @@ public class ProxyClient extends ProxyCommon {
ModelLoader.setCustomStateMapper(RSBlocks.PORTABLE_GRID, new StateMap.Builder().ignore(BlockPortableGrid.TYPE).build());
ModelLoader.setCustomMeshDefinition(Item.getItemFromBlock(RSBlocks.PORTABLE_GRID), stack -> {
PortableGrid portableGrid = new PortableGrid(null, stack);
PortableGrid portableGrid = new PortableGrid(null, stack); // TODO: pass fullness?
return new ModelResourceLocation("refinedstorage:portable_grid", "connected=" + Boolean.toString(portableGrid.getEnergy() != 0 && !portableGrid.getDisk().getStackInSlot(0).isEmpty()) + ",direction=north,disk_state=" + TilePortableGrid.getDiskState(portableGrid));
});

View File

@@ -8,13 +8,11 @@ import com.raoulvdberge.refinedstorage.apiimpl.API;
import io.netty.buffer.ByteBuf;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Items;
import net.minecraft.init.PotionTypes;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.potion.PotionUtils;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
@@ -38,8 +36,6 @@ import java.util.function.Function;
public final class StackUtils {
public static final ItemStack EMPTY_BUCKET = new ItemStack(Items.BUCKET);
public static final ItemStack EMPTY_BOTTLE = new ItemStack(Items.GLASS_BOTTLE);
public static final ItemStack WATER_BOTTLE = PotionUtils.addPotionToItemStack(new ItemStack(Items.POTIONITEM), PotionTypes.WATER);
private static final String NBT_INVENTORY = "Inventory_%d";
private static final String NBT_SLOT = "Slot";