2016-06-22 23:49:47 -04:00
|
|
|
/*
|
|
|
|
|
* Minecraft Forge
|
|
|
|
|
* Copyright (c) 2016.
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation version 2.1
|
|
|
|
|
* of the License.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
2016-01-11 16:04:22 -08:00
|
|
|
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;
|
2016-09-17 15:08:23 -07:00
|
|
|
import net.minecraftforge.fml.common.eventhandler.GenericEvent;
|
2016-01-11 16:04:22 -08:00
|
|
|
|
2016-11-18 16:24:03 -05:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
|
2016-01-11 16:04:22 -08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2016-09-17 15:08:23 -07:00
|
|
|
public class AttachCapabilitiesEvent<T> extends GenericEvent<T>
|
2016-01-11 16:04:22 -08:00
|
|
|
{
|
2016-09-17 15:08:23 -07:00
|
|
|
private final T obj;
|
2017-02-24 02:09:02 +01:00
|
|
|
final Map<ResourceLocation, ICapabilityProvider> caps;// package-private for ForgeEventFactory
|
|
|
|
|
private final Map<ResourceLocation, ICapabilityProvider> view;
|
2016-09-17 15:08:23 -07:00
|
|
|
|
|
|
|
|
public AttachCapabilitiesEvent(Class<T> type, T obj)
|
2017-02-24 02:09:02 +01:00
|
|
|
{
|
|
|
|
|
this(type, obj, Maps.<ResourceLocation, ICapabilityProvider>newLinkedHashMap());
|
|
|
|
|
}
|
|
|
|
|
// package-private for ForgeEventFactory
|
|
|
|
|
AttachCapabilitiesEvent(Class<T> type, T obj, Map<ResourceLocation, ICapabilityProvider> caps)
|
2016-01-11 16:04:22 -08:00
|
|
|
{
|
2016-09-17 15:08:23 -07:00
|
|
|
super(type);
|
2016-01-11 16:04:22 -08:00
|
|
|
this.obj = obj;
|
2017-02-24 02:09:02 +01:00
|
|
|
this.caps = caps;
|
|
|
|
|
this.view = Collections.unmodifiableMap(caps);
|
2016-01-11 16:04:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves the object that is being created, Not much state is set.
|
|
|
|
|
*/
|
2016-09-17 15:08:23 -07:00
|
|
|
public T getObject()
|
2016-01-11 16:04:22 -08:00
|
|
|
{
|
|
|
|
|
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.
|
2016-03-23 17:34:48 +03:00
|
|
|
* If the capability is an instance of INBTSerializable, this key will be used when serializing this capability.
|
2016-01-11 16:04:22 -08:00
|
|
|
*
|
|
|
|
|
* @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 ItemStacks.
|
|
|
|
|
*/
|
2016-09-17 15:08:23 -07:00
|
|
|
public static class Item extends AttachCapabilitiesEvent<net.minecraft.item.Item>
|
2016-01-11 16:04:22 -08:00
|
|
|
{
|
|
|
|
|
private final net.minecraft.item.ItemStack stack;
|
2016-11-18 16:24:03 -05:00
|
|
|
public Item(net.minecraft.item.Item item, @Nonnull net.minecraft.item.ItemStack stack)
|
2016-01-11 16:04:22 -08:00
|
|
|
{
|
2016-09-17 15:08:23 -07:00
|
|
|
super(net.minecraft.item.Item.class, item);
|
2016-01-11 16:04:22 -08:00
|
|
|
this.stack = stack;
|
|
|
|
|
}
|
2016-11-18 16:24:03 -05:00
|
|
|
@Nonnull
|
2016-01-11 16:04:22 -08:00
|
|
|
public net.minecraft.item.ItemStack getItemStack()
|
|
|
|
|
{
|
|
|
|
|
return this.stack;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|