Created
April 7, 2020 14:16
-
-
Save Lanse505/d449dcddd174b1db633138b08d7a5aa5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ModifiableTankProvider implements ICapabilityProvider, ICapabilitySerializable<CompoundNBT> { | |
public static final String MODIFIABLE_TANK = "Modifiable_Tank"; | |
private ItemStack stack; | |
private ModifiableTank tank; | |
private LazyOptional<ModifiableTank> optional = LazyOptional.of(() -> tank); | |
public ModifiableTankProvider(ItemStack stack) { | |
this.stack = stack; | |
tank = new ModifiableTank(16000, stack); | |
if (stack.getTag() != null) { | |
CompoundNBT stackNBT = stack.getTag(); | |
if (stackNBT.contains(MODIFIABLE_TANK)) { | |
stackNBT.put(MODIFIABLE_TANK, tank.writeToNBT(stackNBT.getCompound(MODIFIABLE_TANK))); | |
} else { | |
CompoundNBT modified = tank.writeToNBT(new CompoundNBT()); | |
stackNBT.put(MODIFIABLE_TANK, modified); | |
} | |
stack.setTag(stackNBT); | |
} else { | |
CompoundNBT nbt = new CompoundNBT(); | |
nbt.put(MODIFIABLE_TANK, tank.writeToNBT(new CompoundNBT())); | |
stack.setTag(nbt); | |
} | |
} | |
public ModifiableTankProvider(ItemStack stack, CompoundNBT nbt) { | |
this.stack = stack; | |
tank = new ModifiableTank(16000, stack); | |
CompoundNBT stackNBT = stack.getTag(); | |
if (stackNBT != null) { | |
if (stack.getTag().contains(MODIFIABLE_TANK)) { | |
CompoundNBT compound = tank.writeToNBT(stackNBT.getCompound(MODIFIABLE_TANK)); | |
stackNBT.put(MODIFIABLE_TANK, compound); | |
tank.writeToNBT(compound); | |
} else { | |
CompoundNBT compound = tank.writeToNBT(stackNBT.getCompound(MODIFIABLE_TANK)); | |
stackNBT.put(MODIFIABLE_TANK, compound); | |
tank.writeToNBT(compound); | |
} | |
} else { | |
CompoundNBT compound = new CompoundNBT(); | |
CompoundNBT modified = tank.writeToNBT(compound); | |
compound.put(MODIFIABLE_TANK, modified); | |
stack.setTag(compound); | |
} | |
} | |
@Nonnull | |
@Override | |
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { | |
return cap == CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY ? optional.cast() : LazyOptional.empty(); | |
} | |
@Override | |
public CompoundNBT serializeNBT() { | |
if (stack.getTag() != null && stack.getTag().contains(MODIFIABLE_TANK)) { | |
return tank.writeToNBT(stack.getTag().getCompound(MODIFIABLE_TANK)); | |
} | |
return tank.writeToNBT(new CompoundNBT()); | |
} | |
@Override | |
public void deserializeNBT(CompoundNBT nbt) { | |
tank.readFromNBT(nbt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment