-
-
Save loordgek/e3986c6684e536940bba5494a51c13f2 to your computer and use it in GitHub Desktop.
This file contains 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
package com.example.examplemod; | |
import net.minecraft.nbt.IntNBT; | |
import net.minecraft.util.Direction; | |
import net.minecraftforge.common.capabilities.Capability; | |
import net.minecraftforge.common.capabilities.ICapabilitySerializable; | |
import net.minecraftforge.common.util.LazyOptional; | |
import net.minecraftforge.energy.CapabilityEnergy; | |
import net.minecraftforge.energy.IEnergyStorage; | |
import javax.annotation.Nonnull; | |
import javax.annotation.Nullable; | |
public class BatteryCap implements ICapabilitySerializable<IntNBT> { | |
SettableEnergyStorage storage = new SettableEnergyStorage(1000); | |
LazyOptional<IEnergyStorage> energyCap = LazyOptional.of(()-> storage); | |
@Nonnull | |
@Override | |
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { | |
return CapabilityEnergy.ENERGY.orEmpty(cap, energyCap); | |
} | |
@Override | |
public IntNBT serializeNBT() { | |
return IntNBT.valueOf(storage.getEnergyStored()); | |
} | |
@Override | |
public void deserializeNBT(IntNBT nbt) { | |
storage.setEnergy(nbt.getInt()); | |
} | |
} |
This file contains 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
package com.example.examplemod; | |
import net.minecraft.item.Item; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.nbt.CompoundNBT; | |
import net.minecraftforge.common.capabilities.ICapabilityProvider; | |
import javax.annotation.Nullable; | |
public class BatteryItem extends Item { | |
public BatteryItem(Properties properties) { | |
super(properties); | |
} | |
@Nullable | |
@Override | |
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) { | |
return new BatteryCap(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment