2016-06-22 23:49:47 -04:00
|
|
|
/*
|
2022-02-28 13:41:23 -08:00
|
|
|
* Minecraft Forge - Forge Development LLC
|
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-only
|
2016-06-22 23:49:47 -04:00
|
|
|
*/
|
|
|
|
|
|
2016-01-11 16:04:22 -08:00
|
|
|
package net.minecraftforge.event;
|
|
|
|
|
|
|
|
|
|
import java.util.Collections;
|
2019-02-06 22:55:17 -08:00
|
|
|
import java.util.List;
|
2016-01-11 16:04:22 -08:00
|
|
|
import java.util.Map;
|
2019-05-23 16:02:15 -07:00
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
import com.google.common.collect.Maps;
|
2021-07-17 00:08:49 -05:00
|
|
|
import net.minecraft.resources.ResourceLocation;
|
2019-05-23 16:02:15 -07:00
|
|
|
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
|
|
|
|
import net.minecraftforge.eventbus.api.GenericEvent;
|
|
|
|
|
|
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-06-15 19:26:14 +01:00
|
|
|
private final Map<ResourceLocation, ICapabilityProvider> caps = Maps.newLinkedHashMap();
|
|
|
|
|
private final Map<ResourceLocation, ICapabilityProvider> view = Collections.unmodifiableMap(caps);
|
2019-02-06 22:55:17 -08:00
|
|
|
private final List<Runnable> listeners = Lists.newArrayList();
|
|
|
|
|
private final List<Runnable> listenersView = Collections.unmodifiableList(listeners);
|
2016-09-17 15:08:23 -07:00
|
|
|
|
|
|
|
|
public AttachCapabilitiesEvent(Class<T> type, T obj)
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
2019-02-06 22:55:17 -08:00
|
|
|
|
|
|
|
|
/**
|
2019-03-19 21:21:37 -07:00
|
|
|
* Adds a callback that is fired when the attached object is invalidated.
|
2019-02-06 22:55:17 -08:00
|
|
|
* Such as a Entity/TileEntity being removed from world.
|
|
|
|
|
* All attached providers should invalidate all of their held capability instances.
|
|
|
|
|
*/
|
|
|
|
|
public void addListener(Runnable listener)
|
|
|
|
|
{
|
|
|
|
|
this.listeners.add(listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Runnable> getListeners()
|
|
|
|
|
{
|
|
|
|
|
return this.listenersView;
|
|
|
|
|
}
|
2016-01-11 16:04:22 -08:00
|
|
|
}
|