package techreborn.items;

import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import techreborn.TechReborn;
import techreborn.init.TRContent.StorageUnit;
import techreborn.init.TRContent.TankUnit;

public class UpgraderItem extends Item {

	public UpgraderItem() {
		super(new Item.Settings().group(TechReborn.ITEMGROUP));
	}

	@Override
	public ActionResult useOnBlock(ItemUsageContext context) {
		World world = context.getWorld();
		BlockPos blockPos = context.getBlockPos();
		BlockEntity oldBlockEntity = world.getBlockEntity(blockPos);
		BlockState oldBlockState = world.getBlockState(blockPos);
		BlockState newBlockState = null;
		// if no storage upgrader, the isOf compares with null and returns false
		if (oldBlockState.isOf(StorageUnit.getUpgradableFor(this).map(StorageUnit::asBlock).orElse(null))) {
			// upgradable is now guaranteed to be present, or something is seriously wrong
			// we want to get the next unit in the enum, hence ordinal()+1
			newBlockState = StorageUnit.values()[StorageUnit.getUpgradableFor(this).orElseThrow().ordinal()+1].asBlock().getStateWithProperties(oldBlockState);
		}
		// same for the tank
		else if (oldBlockState.isOf(TankUnit.getUpgradableFor(this).map(TankUnit::asBlock).orElse(null))) {
			newBlockState = TankUnit.values()[TankUnit.getUpgradableFor(this).orElseThrow().ordinal()+1].asBlock().getDefaultState();
		}
		if (newBlockState == null)
			return ActionResult.PASS;
		NbtCompound data = oldBlockEntity.createNbt();
		world.getBlockEntity(blockPos).readNbt(new NbtCompound());
		world.setBlockState(blockPos, newBlockState);
		world.getBlockEntity(blockPos).readNbt(data);
		world.getBlockEntity(blockPos).readNbt(data);
		return ActionResult.SUCCESS;
	}
}

@@ -326,94 +326,250 @@
 aft.
-util.registry.Registry;%0Aimport net.minecraft.world.World;%0Aimport techreborn.TechReborn
+world.World;%0Aimport reborncore.common.blockentity.MachineBaseBlockEntity;%0Aimport techreborn.TechReborn;%0Aimport techreborn.blockentity.storage.fluid.TankUnitBaseBlockEntity;%0Aimport techreborn.blockentity.storage.item.StorageUnitBaseBlockEntity
 ;%0Aim
@@ -992,32 +992,249 @@
 tity(blockPos);%0A
+%09%09if (oldBlockEntity == null)%7B%0A%09%09%09return ActionResult.PASS;%0A%09%09%7D%0A%09%09if (!(oldBlockEntity instanceof StorageUnitBaseBlockEntity) && !(oldBlockEntity instanceof TankUnitBaseBlockEntity))%7B%0A%09%09%09return ActionResult.PASS;%0A%09%09%7D%0A
 %09%09BlockState old
@@ -1312,16 +1312,39 @@
 = null;%0A
+%09%09String newType = %22%22;%0A
 %09%09// if 
@@ -1812,16 +1812,120 @@
 State);%0A
+%09%09%09newType = StorageUnit.values()%5BStorageUnit.getUpgradableFor(this).orElseThrow().ordinal()+1%5D.name();%0A
 %09%09%7D%0A%09%09//
@@ -2166,24 +2166,122 @@
 ultState();%0A
+%09%09%09newType = TankUnit.values()%5BTankUnit.getUpgradableFor(this).orElseThrow().ordinal()+1%5D.name();%0A
 %09%09%7D%0A%09%09if (ne
@@ -2300,16 +2300,18 @@
 == null)
+ %7B
 %0A%09%09%09retu
@@ -2332,16 +2332,21 @@
 t.PASS;%0A
+%09%09%7D%0A%0A
 %09%09NbtCom
@@ -2384,33 +2384,108 @@
 ateNbt();%0A%09%09
-world.get
+data.putString(%22unitType%22, newType);%0A%0A%09%09// empty storage to prevent item spill%0A%09%09old
 BlockEntity(
@@ -2479,34 +2479,24 @@
 dBlockEntity
-(blockPos)
 .readNbt(new
@@ -2512,16 +2512,17 @@
 und());%0A
+%0A
 %09%09world.
@@ -2565,103 +2565,248 @@
 e);%0A
+%0A
 %09%09
-world.getBlockEntity(blockPos).readNbt(data);%0A%09%09world.getBlockEntity(blockPos).readNbt(data);
+// restore content and set a new storage type%0A%09%09BlockEntity newBlockEntity = world.getBlockEntity(blockPos);%0A%09%09if (newBlockEntity != null)%7B%0A%09%09%09newBlockEntity.readNbt(data);%0A%09%09%09((MachineBaseBlockEntity) newBlockEntity).syncWithAll();%0A%09%09%7D%0A
 %0A%09%09r

Fix storage upgrade. Closes #2999