Fix storage upgrade. Closes #2999
This commit is contained in:
parent
48a35be5d0
commit
735da1338e
1 changed files with 28 additions and 5 deletions
|
@ -7,9 +7,11 @@ import net.minecraft.item.ItemUsageContext;
|
||||||
import net.minecraft.nbt.NbtCompound;
|
import net.minecraft.nbt.NbtCompound;
|
||||||
import net.minecraft.util.ActionResult;
|
import net.minecraft.util.ActionResult;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.util.registry.Registry;
|
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
import reborncore.common.blockentity.MachineBaseBlockEntity;
|
||||||
import techreborn.TechReborn;
|
import techreborn.TechReborn;
|
||||||
|
import techreborn.blockentity.storage.fluid.TankUnitBaseBlockEntity;
|
||||||
|
import techreborn.blockentity.storage.item.StorageUnitBaseBlockEntity;
|
||||||
import techreborn.init.TRContent.StorageUnit;
|
import techreborn.init.TRContent.StorageUnit;
|
||||||
import techreborn.init.TRContent.TankUnit;
|
import techreborn.init.TRContent.TankUnit;
|
||||||
|
|
||||||
|
@ -24,25 +26,46 @@ public class UpgraderItem extends Item {
|
||||||
World world = context.getWorld();
|
World world = context.getWorld();
|
||||||
BlockPos blockPos = context.getBlockPos();
|
BlockPos blockPos = context.getBlockPos();
|
||||||
BlockEntity oldBlockEntity = world.getBlockEntity(blockPos);
|
BlockEntity oldBlockEntity = world.getBlockEntity(blockPos);
|
||||||
|
if (oldBlockEntity == null){
|
||||||
|
return ActionResult.PASS;
|
||||||
|
}
|
||||||
|
if (!(oldBlockEntity instanceof StorageUnitBaseBlockEntity) && !(oldBlockEntity instanceof TankUnitBaseBlockEntity)){
|
||||||
|
return ActionResult.PASS;
|
||||||
|
}
|
||||||
BlockState oldBlockState = world.getBlockState(blockPos);
|
BlockState oldBlockState = world.getBlockState(blockPos);
|
||||||
BlockState newBlockState = null;
|
BlockState newBlockState = null;
|
||||||
|
String newType = "";
|
||||||
// if no storage upgrader, the isOf compares with null and returns false
|
// if no storage upgrader, the isOf compares with null and returns false
|
||||||
if (oldBlockState.isOf(StorageUnit.getUpgradableFor(this).map(StorageUnit::asBlock).orElse(null))) {
|
if (oldBlockState.isOf(StorageUnit.getUpgradableFor(this).map(StorageUnit::asBlock).orElse(null))) {
|
||||||
// upgradable is now guaranteed to be present, or something is seriously wrong
|
// 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
|
// 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);
|
newBlockState = StorageUnit.values()[StorageUnit.getUpgradableFor(this).orElseThrow().ordinal()+1].asBlock().getStateWithProperties(oldBlockState);
|
||||||
|
newType = StorageUnit.values()[StorageUnit.getUpgradableFor(this).orElseThrow().ordinal()+1].name();
|
||||||
}
|
}
|
||||||
// same for the tank
|
// same for the tank
|
||||||
else if (oldBlockState.isOf(TankUnit.getUpgradableFor(this).map(TankUnit::asBlock).orElse(null))) {
|
else if (oldBlockState.isOf(TankUnit.getUpgradableFor(this).map(TankUnit::asBlock).orElse(null))) {
|
||||||
newBlockState = TankUnit.values()[TankUnit.getUpgradableFor(this).orElseThrow().ordinal()+1].asBlock().getDefaultState();
|
newBlockState = TankUnit.values()[TankUnit.getUpgradableFor(this).orElseThrow().ordinal()+1].asBlock().getDefaultState();
|
||||||
|
newType = TankUnit.values()[TankUnit.getUpgradableFor(this).orElseThrow().ordinal()+1].name();
|
||||||
}
|
}
|
||||||
if (newBlockState == null)
|
if (newBlockState == null) {
|
||||||
return ActionResult.PASS;
|
return ActionResult.PASS;
|
||||||
|
}
|
||||||
|
|
||||||
NbtCompound data = oldBlockEntity.createNbt();
|
NbtCompound data = oldBlockEntity.createNbt();
|
||||||
world.getBlockEntity(blockPos).readNbt(new NbtCompound());
|
data.putString("unitType", newType);
|
||||||
|
|
||||||
|
// empty storage to prevent item spill
|
||||||
|
oldBlockEntity.readNbt(new NbtCompound());
|
||||||
|
|
||||||
world.setBlockState(blockPos, newBlockState);
|
world.setBlockState(blockPos, newBlockState);
|
||||||
world.getBlockEntity(blockPos).readNbt(data);
|
|
||||||
world.getBlockEntity(blockPos).readNbt(data);
|
// restore content and set a new storage type
|
||||||
|
BlockEntity newBlockEntity = world.getBlockEntity(blockPos);
|
||||||
|
if (newBlockEntity != null){
|
||||||
|
newBlockEntity.readNbt(data);
|
||||||
|
((MachineBaseBlockEntity) newBlockEntity).syncWithAll();
|
||||||
|
}
|
||||||
|
|
||||||
return ActionResult.SUCCESS;
|
return ActionResult.SUCCESS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue