Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
custom capability
package transfarmer.adventureitems.capabilities;
public interface ISoulWeapon {
void setCurrentType(SoulWeapon.Type type);
SoulWeapon.Type getCurrentType();
}
package transfarmer.adventureitems;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import transfarmer.adventureitems.capabilities.ISoulWeapon;
import transfarmer.adventureitems.capabilities.Provider;
import transfarmer.adventureitems.capabilities.SoulWeapon;
import transfarmer.adventureitems.capabilities.Storage;
@Mod(Main.MODID)
@Mod.EventBusSubscriber(modid = Main.MODID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class Main {
public static final String MODID = "adventureitems";
public Main() {
DistExecutor.runWhenOn(Dist.CLIENT, Keybindings.RegisterTask.get());
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::onCommonSetup);
}
@SubscribeEvent
public static void onAttachCapabilities(AttachCapabilitiesEvent<Entity> event) {
if (event.getObject() instanceof PlayerEntity) {
event.addCapability(new ResourceLocation(MODID, "type"), new Provider());
}
}
@SubscribeEvent
public void onCommonSetup(FMLCommonSetupEvent event) {
CapabilityManager.INSTANCE.register(ISoulWeapon.class, new Storage(), SoulWeapon::new);
}
}
package transfarmer.adventureitems.capabilities;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
public class Provider implements ICapabilitySerializable<INBT> {
@CapabilityInject(ISoulWeapon.class)
public static final Capability<ISoulWeapon> TYPE = null;
private LazyOptional<ISoulWeapon> instance = LazyOptional.of(TYPE::getDefaultInstance);
@Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction side) {
return capability == TYPE ? instance.cast() : LazyOptional.empty();
}
@Override
public INBT serializeNBT() {
return TYPE.getStorage().writeNBT(TYPE, instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional is empty")), null);
}
@Override
public void deserializeNBT(INBT nbt) {
TYPE.getStorage().readNBT(TYPE, instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional is empty")), null, nbt);
}
}
package transfarmer.adventureitems.capabilities;
public class SoulWeapon implements ISoulWeapon {
private Type current = null;
public enum Type {
BIGSWORD("BIGSWORD"),
SWORD("SWORD"),
DAGGER("DAGGER");
private String name;
Type(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public static Type get(String name) {
switch (name) {
case "BIGSWORD":
return BIGSWORD;
case "SWORD":
return SWORD;
case "DAGGER":
return DAGGER;
}
return null;
}
}
@Override
public Type getCurrentType() {
return current;
}
public void setCurrentType(Type type) {
current = type;
}
}
package transfarmer.adventureitems.capabilities;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.Capability.IStorage;
public class Storage implements IStorage<ISoulWeapon> {
@Override
public INBT writeNBT(Capability<ISoulWeapon> capability, ISoulWeapon instance, Direction side) {
CompoundNBT tag = new CompoundNBT();
tag.putString("adventureitems.type", instance.getCurrentType().getName());
return tag;
}
@Override
public void readNBT(Capability<ISoulWeapon> capability, ISoulWeapon instance, Direction side, INBT nbt) {
CompoundNBT tag = (CompoundNBT) nbt;
instance.setCurrentType(SoulWeapon.Type.get(tag.getString("type")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.