mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-19 19:59:42 +00:00
New Capability system allowing for more manageable world object features.
When combined with @Optional this should address all issues of soft dependancy on mods/apis. This also addresses the issue of dynamic functionality in TileEntities/Entities. Current capability providers: TileEntity, Entity, ItemStack Also added INBTSerializeable, a generic interface for game objects that can be written to/from NBT tags. Vanilla capabilities will be coming soon, mostly on request and review. So start requesting capabiliteis on vanilla/Forge features.
This commit is contained in:
1 parent
4672f7bab0
commit
17db34ae31
16 files changed
+988
-44
No files matched your search
@@ -0,0 +1,117 @@
|
||||
package net.minecraftforge.event;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
|
||||
/**
|
||||
* Fired whenever an object with Capabilities support {currently TileEntity/Item/Entity)
|
||||
* is created. Allowing for the attachment of arbitrary capability providers.
|
||||
*
|
||||
* Please note that as this is fired for ALL object creations efficient code is recommended.
|
||||
* And if possible use one of the sub-classes to filter your intended objects.
|
||||
*/
|
||||
public class AttachCapabilitiesEvent extends Event
|
||||
{
|
||||
private final Object obj;
|
||||
private final Map<ResourceLocation, ICapabilityProvider> caps = Maps.newLinkedHashMap();
|
||||
private final Map<ResourceLocation, ICapabilityProvider> view = Collections.unmodifiableMap(caps);
|
||||
public AttachCapabilitiesEvent(Object obj)
|
||||
{
|
||||
this.obj = obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the object that is being created, Not much state is set.
|
||||
*/
|
||||
public Object getObject()
|
||||
{
|
||||
return this.obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a capability to be attached to this object.
|
||||
* Keys MUST be unique, it is suggested that you set the domain to your mod ID.
|
||||
* If the capability is an instance of INBTSerializeable, this key will be used when serializing this capability.
|
||||
*
|
||||
* @param key The name of owner of this capability provider.
|
||||
* @param cap The capability provider
|
||||
*/
|
||||
public void addCapability(ResourceLocation key, ICapabilityProvider cap)
|
||||
{
|
||||
if (caps.containsKey(key))
|
||||
throw new IllegalStateException("Duplicate Capability Key: " + key + " " + cap);
|
||||
this.caps.put(key, cap);
|
||||
}
|
||||
|
||||
/**
|
||||
* A unmodifiable view of the capabilities that will be attached to this object.
|
||||
*/
|
||||
public Map<ResourceLocation, ICapabilityProvider> getCapabilities()
|
||||
{
|
||||
return view;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A version of the parent event which is only fired for Tile Entities.
|
||||
*/
|
||||
public static class TileEntity extends AttachCapabilitiesEvent
|
||||
{
|
||||
private final net.minecraft.tileentity.TileEntity te;
|
||||
public TileEntity(net.minecraft.tileentity.TileEntity te)
|
||||
{
|
||||
super(te);
|
||||
this.te = te;
|
||||
}
|
||||
public net.minecraft.tileentity.TileEntity getTileEntity()
|
||||
{
|
||||
return this.te;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A version of the parent event which is only fired for Entities.
|
||||
*/
|
||||
public static class Entity extends AttachCapabilitiesEvent
|
||||
{
|
||||
private final net.minecraft.entity.Entity entity;
|
||||
public Entity(net.minecraft.entity.Entity entity)
|
||||
{
|
||||
super(entity);
|
||||
this.entity = entity;
|
||||
}
|
||||
public net.minecraft.entity.Entity getEntity()
|
||||
{
|
||||
return this.entity;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A version of the parent event which is only fired for ItemStacks.
|
||||
*/
|
||||
public static class Item extends AttachCapabilitiesEvent
|
||||
{
|
||||
private final net.minecraft.item.ItemStack stack;
|
||||
private final net.minecraft.item.Item item;
|
||||
public Item(net.minecraft.item.Item item, net.minecraft.item.ItemStack stack)
|
||||
{
|
||||
super(item);
|
||||
this.item = item;
|
||||
this.stack = stack;
|
||||
}
|
||||
public net.minecraft.item.Item getItem()
|
||||
{
|
||||
return this.item;
|
||||
}
|
||||
public net.minecraft.item.ItemStack getItemStack()
|
||||
{
|
||||
return this.stack;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user