mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 16:27:47 +00:00
Update to Official classnames Patchwork Make the game compile and run Merge in the new HUD overlay API Merge in the new render properties API PEEL Use Gradle 7.2 snapshot to fix eclipse integration bug in 7.1, Would use 7.0 but FG needs 7.1 for some reason. New FML installer Update to 1.17.1 Co-authored-by: LexManos <lexmanos@gmail.com> Co-authored-by: SizableShrimp <sizableshrimp@sizableshrimp.me> Co-authored-by: cpw <cpw+github@weeksfamily.ca> Co-authored-by: Curle <curle@gemwire.uk> Co-authored-by: Unnoen <Unnoen@users.noreply.github.com> Co-authored-by: covers1624 <laughlan.cov@internode.on.net> Co-authored-by: DaemonUmbra <daemon.umbra@gmail.com> Co-authored-by: gigaherz <gigaherz@gmail.com> Signed-off-by: SizableShrimp <sizableshrimp@sizableshrimp.me>
98 lines
3.4 KiB
Java
98 lines
3.4 KiB
Java
/*
|
|
* Minecraft Forge
|
|
* Copyright (c) 2016-2021.
|
|
*
|
|
* 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
|
|
*/
|
|
|
|
package net.minecraftforge.event;
|
|
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Maps;
|
|
import net.minecraft.resources.ResourceLocation;
|
|
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
|
import net.minecraftforge.eventbus.api.GenericEvent;
|
|
|
|
/**
|
|
* 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<T> extends GenericEvent<T>
|
|
{
|
|
private final T obj;
|
|
private final Map<ResourceLocation, ICapabilityProvider> caps = Maps.newLinkedHashMap();
|
|
private final Map<ResourceLocation, ICapabilityProvider> view = Collections.unmodifiableMap(caps);
|
|
private final List<Runnable> listeners = Lists.newArrayList();
|
|
private final List<Runnable> listenersView = Collections.unmodifiableList(listeners);
|
|
|
|
public AttachCapabilitiesEvent(Class<T> type, T obj)
|
|
{
|
|
super(type);
|
|
this.obj = obj;
|
|
}
|
|
|
|
/**
|
|
* Retrieves the object that is being created, Not much state is set.
|
|
*/
|
|
public T 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 INBTSerializable, 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;
|
|
}
|
|
|
|
/**
|
|
* Adds a callback that is fired when the attached object is invalidated.
|
|
* 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;
|
|
}
|
|
}
|