Skip to content

Instantly share code, notes, and snippets.

@gigaherz
Last active August 6, 2016 15:51
Show Gist options
  • Save gigaherz/6f80718f69e513b57e72 to your computer and use it in GitHub Desktop.
Save gigaherz/6f80718f69e513b57e72 to your computer and use it in GitHub Desktop.
package gigaherz.survivalist.itemcap;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;
import java.util.UUID;
import java.util.concurrent.Callable;
public class CapabilityTargettable
{
@CapabilityInject(ITargettable.class)
public static Capability<ITargettable> TARGETTABLE_CAPABILITY = null;
/**
* Call this from pre-init if you want to use this capability.
*/
public static void register()
{
CapabilityManager.INSTANCE.register(ITargettable.class, new Storage(), new Callable<ITargettable>()
{
@Override
public ITargettable call() throws Exception
{
return new TargettableImpl();
}
});
}
private static class Storage
implements Capability.IStorage<ITargettable>
{
@Override
public NBTBase writeNBT(Capability<ITargettable> capability, ITargettable instance, EnumFacing side)
{
NBTTagCompound data = new NBTTagCompound();
data.setString("Target", instance.getTarget().getPersistentID().toString());
return data;
}
@Override
public void readNBT(Capability<ITargettable> capability, ITargettable instance, EnumFacing side, NBTBase nbt)
{
NBTTagCompound data = (NBTTagCompound) nbt;
UUID target = UUID.fromString(data.getString("Target"));
// TODO: UUID to Entity -----------------------------------------------------------------------------------------
Entity targetEntity = null;
instance.setTarget(targetEntity);
}
}
}
package gigaherz.survivalist.itemcap;
import net.minecraft.entity.Entity;
public interface ITargettable
{
Entity getTarget();
void setTarget(Entity entity);
}
package gigaherz.survivalist.itemcap;
import net.minecraft.entity.Entity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
public class ItemTargettable extends Item
{
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt)
{
return new ICapabilitySerializable<NBTTagCompound>()
{
TargettableImpl instance = new TargettableImpl();
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
{
return capability == CapabilityTargettable.TARGETTABLE_CAPABILITY;
}
@SuppressWarnings("unchecked")
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
{
if (capability == CapabilityTargettable.TARGETTABLE_CAPABILITY)
return (T)instance;
return null;
}
@Override
public NBTTagCompound serializeNBT()
{
return (NBTTagCompound)CapabilityTargettable.TARGETTABLE_CAPABILITY.getStorage()
.writeNBT(CapabilityTargettable.TARGETTABLE_CAPABILITY, instance, null);
}
@Override
public void deserializeNBT(NBTTagCompound nbt)
{
CapabilityTargettable.TARGETTABLE_CAPABILITY.getStorage()
.readNBT(CapabilityTargettable.TARGETTABLE_CAPABILITY, instance, null, nbt);
}
};
}
public void setTarget(ItemStack stack, Entity target)
{
if(stack.hasCapability(CapabilityTargettable.TARGETTABLE_CAPABILITY, null))
stack.getCapability(CapabilityTargettable.TARGETTABLE_CAPABILITY, null).setTarget(target);
}
public Entity getTarget(ItemStack stack)
{
if(stack.hasCapability(CapabilityTargettable.TARGETTABLE_CAPABILITY, null))
return stack.getCapability(CapabilityTargettable.TARGETTABLE_CAPABILITY, null).getTarget();
return null;
}
}
package gigaherz.survivalist.itemcap;
import net.minecraft.entity.Entity;
public class TargettableImpl implements ITargettable
{
private Entity target;
public Entity getTarget()
{
return target;
}
public void setTarget(Entity target)
{
this.target = target;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment