2016-06-22 23:49:47 -04:00
|
|
|
/*
|
2022-04-14 03:42:00 +00:00
|
|
|
* Copyright (c) Forge Development LLC and contributors
|
2022-02-28 13:41:23 -08:00
|
|
|
* 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;
|
|
|
|
|
|
2025-06-18 02:34:13 -07:00
|
|
|
import java.util.ArrayList;
|
2016-01-11 16:04:22 -08:00
|
|
|
import java.util.Collections;
|
2025-06-18 02:34:13 -07:00
|
|
|
import java.util.LinkedHashMap;
|
2019-02-06 22:55:17 -08:00
|
|
|
import java.util.List;
|
2016-01-11 16:04:22 -08:00
|
|
|
import java.util.Map;
|
2025-06-18 02:34:13 -07:00
|
|
|
|
2021-07-17 00:08:49 -05:00
|
|
|
import net.minecraft.resources.ResourceLocation;
|
2025-06-18 02:34:13 -07:00
|
|
|
import net.minecraft.world.entity.Entity;
|
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
|
import net.minecraft.world.level.chunk.LevelChunk;
|
2019-05-23 16:02:15 -07:00
|
|
|
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
2025-06-18 02:34:13 -07:00
|
|
|
import net.minecraftforge.eventbus.api.bus.EventBus;
|
|
|
|
|
import net.minecraftforge.eventbus.api.event.InheritableEvent;
|
|
|
|
|
import net.minecraftforge.eventbus.api.event.MutableEvent;
|
|
|
|
|
import org.jspecify.annotations.NullMarked;
|
2019-05-23 16:02:15 -07:00
|
|
|
|
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.
|
2025-06-18 02:34:13 -07:00
|
|
|
*
|
|
|
|
|
* @see Entities
|
|
|
|
|
* @see BlockEntities
|
|
|
|
|
* @see ItemStacks
|
|
|
|
|
* @see Levels
|
|
|
|
|
* @see LevelChunks
|
2016-01-11 16:04:22 -08:00
|
|
|
*/
|
2025-06-18 02:34:13 -07:00
|
|
|
@NullMarked
|
|
|
|
|
public abstract class AttachCapabilitiesEvent<T> extends MutableEvent implements InheritableEvent {
|
2016-09-17 15:08:23 -07:00
|
|
|
private final T obj;
|
2025-06-18 02:34:13 -07:00
|
|
|
private final Map<ResourceLocation, ICapabilityProvider> caps = new LinkedHashMap<>();
|
2017-06-15 19:26:14 +01:00
|
|
|
private final Map<ResourceLocation, ICapabilityProvider> view = Collections.unmodifiableMap(caps);
|
2025-06-18 02:34:13 -07:00
|
|
|
private final List<Runnable> listeners = new ArrayList<>();
|
2019-02-06 22:55:17 -08:00
|
|
|
private final List<Runnable> listenersView = Collections.unmodifiableList(listeners);
|
2016-09-17 15:08:23 -07:00
|
|
|
|
2025-06-18 02:34:13 -07:00
|
|
|
public static class Entities extends AttachCapabilitiesEvent<Entity> {
|
|
|
|
|
public static final EventBus<Entities> BUS = EventBus.create(Entities.class);
|
|
|
|
|
|
|
|
|
|
public Entities(Entity obj) {
|
|
|
|
|
super(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class BlockEntities extends AttachCapabilitiesEvent<BlockEntity> {
|
|
|
|
|
public static final EventBus<BlockEntities> BUS = EventBus.create(BlockEntities.class);
|
|
|
|
|
|
|
|
|
|
public BlockEntities(BlockEntity obj) {
|
|
|
|
|
super(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class ItemStacks extends AttachCapabilitiesEvent<ItemStack> {
|
|
|
|
|
public static final EventBus<ItemStacks> BUS = EventBus.create(ItemStacks.class);
|
|
|
|
|
|
|
|
|
|
public ItemStacks(ItemStack obj) {
|
|
|
|
|
super(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class Levels extends AttachCapabilitiesEvent<Level> {
|
|
|
|
|
public static final EventBus<Levels> BUS = EventBus.create(Levels.class);
|
|
|
|
|
|
|
|
|
|
public Levels(Level obj) {
|
|
|
|
|
super(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class LevelChunks extends AttachCapabilitiesEvent<LevelChunk> {
|
|
|
|
|
public static final EventBus<LevelChunks> BUS = EventBus.create(LevelChunks.class);
|
|
|
|
|
|
|
|
|
|
public LevelChunks(LevelChunk obj) {
|
|
|
|
|
super(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected AttachCapabilitiesEvent(T obj) {
|
2016-01-11 16:04:22 -08:00
|
|
|
this.obj = obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves the object that is being created, Not much state is set.
|
|
|
|
|
*/
|
2025-06-18 02:34:13 -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
|
|
|
|
|
*/
|
2025-06-18 02:34:13 -07:00
|
|
|
public void addCapability(ResourceLocation key, ICapabilityProvider cap) {
|
2016-01-11 16:04:22 -08:00
|
|
|
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.
|
|
|
|
|
*/
|
2025-06-18 02:34:13 -07:00
|
|
|
public Map<ResourceLocation, ICapabilityProvider> getCapabilities() {
|
2016-01-11 16:04:22 -08:00
|
|
|
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.
|
|
|
|
|
*/
|
2025-06-18 02:34:13 -07:00
|
|
|
public void addListener(Runnable listener) {
|
2019-02-06 22:55:17 -08:00
|
|
|
this.listeners.add(listener);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 02:34:13 -07:00
|
|
|
public List<Runnable> getListeners() {
|
2019-02-06 22:55:17 -08:00
|
|
|
return this.listenersView;
|
|
|
|
|
}
|
2016-01-11 16:04:22 -08:00
|
|
|
}
|