Skip to content

Instantly share code, notes, and snippets.

@Daomephsta
Created August 18, 2016 05:26
Show Gist options
  • Save Daomephsta/9f77be11fdd49ef57bc0018d384e0ea1 to your computer and use it in GitHub Desktop.
Save Daomephsta/9f77be11fdd49ef57bc0018d384e0ea1 to your computer and use it in GitHub Desktop.
Capability
package com.leviathan143.dogwhistle.api.whistle;
import java.util.concurrent.Callable;
import net.minecraft.nbt.NBTBase;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;
public class CapabilityWhistleableEntity
{
@CapabilityInject(IWhistleableEntity.class)
public static Capability<IWhistleableEntity> CAP_WHISTLEABLE_ENTITY = null;
public static void register()
{
CapabilityManager.INSTANCE.register(IWhistleableEntity.class, new Capability.IStorage<IWhistleableEntity>()
{
@Override
public NBTBase writeNBT(Capability<IWhistleableEntity> capability,
IWhistleableEntity instance, EnumFacing side) {return null;}
@Override
public void readNBT(Capability<IWhistleableEntity> capability, IWhistleableEntity instance, EnumFacing side, NBTBase nbt) {}
},
new Callable<IWhistleableEntity>()
{
@Override
public IWhistleableEntity call() throws Exception {return null;}
});
}
}
package com.leviathan143.dogwhistle.api.whistle;
import net.minecraft.entity.Entity;
public interface IWhistleableEntity
{
public void sit(Entity entity);
}
package com.leviathan143.dogwhistle.common.capability;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class VanillaEntityCapAttacher
{
@SubscribeEvent
public static void attachCaps(AttachCapabilitiesEvent.Entity event)
{
if(event.getEntity() instanceof EntityWolf)
{
event.addCapability(WolfWhistleHandler.CAP_KEY, WolfWhistleHandler.INSTANCE);
}
}
}
package com.leviathan143.dogwhistle.common.capability;
import net.minecraft.entity.Entity;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import com.leviathan143.dogwhistle.api.whistle.CapabilityWhistleableEntity;
import com.leviathan143.dogwhistle.api.whistle.IWhistleableEntity;
import com.leviathan143.dogwhistle.api.whistle.Whistle;
import com.leviathan143.dogwhistle.common.DogWhistleMain.Constants;
public class WolfWhistleHandler implements ICapabilityProvider
{
public static final ResourceLocation CAP_KEY = new ResourceLocation(Constants.MODID, "wolfWhistle");
public static final WolfWhistleHandler INSTANCE = new WolfWhistleHandler();
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
{
if(capability == CapabilityWhistleableEntity.CAP_WHISTLEABLE_ENTITY)
{
return true;
}
return false;
}
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
{
if(capability == CapabilityWhistleableEntity.CAP_WHISTLEABLE_ENTITY) return (T) WolfWhistleCap.INSTANCE;
return null;
}
public static class WolfWhistleCap implements IWhistleableEntity
{
public static final WolfWhistleCap INSTANCE = new WolfWhistleCap();
@Override
public void sit(Entity entity)
{
((EntityWolf)entity).getAISit().setSitting(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment