1.21.6 - New EventBus 7

A full rewrite and redesign of the library that offers much better performance, new features and fixes all previous design flaws
See the migration guide here: https://gist.github.com/PaintNinja/ad82c224aecee25efac1ea3e2cf19b91

Co-authored-by: Paint_Ninja <[email protected]>
This commit is contained in:
LexManosandPaint_Ninja committed 2025-06-18 02:53:58 -07:00
1 parent a02f3f9841
commit 5e8942e0a7
642 files changed
+7258 -6187

No files matched your search

@@ -5,14 +5,23 @@
package net.minecraftforge.event;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
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.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;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.eventbus.api.GenericEvent;
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;
/**
* Fired whenever an object with Capabilities support {currently TileEntity/Item/Entity)
@@ -20,26 +29,69 @@ import net.minecraftforge.eventbus.api.GenericEvent;
*
* 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.
*
* @see Entities
* @see BlockEntities
* @see ItemStacks
* @see Levels
* @see LevelChunks
*/
public class AttachCapabilitiesEvent<T> extends GenericEvent<T>
{
@NullMarked
public abstract class AttachCapabilitiesEvent<T> extends MutableEvent implements InheritableEvent {
private final T obj;
private final Map<ResourceLocation, ICapabilityProvider> caps = Maps.newLinkedHashMap();
private final Map<ResourceLocation, ICapabilityProvider> caps = new LinkedHashMap<>();
private final Map<ResourceLocation, ICapabilityProvider> view = Collections.unmodifiableMap(caps);
private final List<Runnable> listeners = Lists.newArrayList();
private final List<Runnable> listeners = new ArrayList<>();
private final List<Runnable> listenersView = Collections.unmodifiableList(listeners);
public AttachCapabilitiesEvent(Class<T> type, T obj)
{
super(type);
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) {
this.obj = obj;
}
/**
* Retrieves the object that is being created, Not much state is set.
*/
public T getObject()
{
public T getObject() {
return this.obj;
}
@@ -51,8 +103,7 @@ public class AttachCapabilitiesEvent<T> extends GenericEvent<T>
* @param key The name of owner of this capability provider.
* @param cap The capability provider
*/
public void addCapability(ResourceLocation key, ICapabilityProvider cap)
{
public void addCapability(ResourceLocation key, ICapabilityProvider cap) {
if (caps.containsKey(key))
throw new IllegalStateException("Duplicate Capability Key: " + key + " " + cap);
this.caps.put(key, cap);
@@ -61,8 +112,7 @@ public class AttachCapabilitiesEvent<T> extends GenericEvent<T>
/**
* A unmodifiable view of the capabilities that will be attached to this object.
*/
public Map<ResourceLocation, ICapabilityProvider> getCapabilities()
{
public Map<ResourceLocation, ICapabilityProvider> getCapabilities() {
return view;
}
@@ -71,13 +121,11 @@ public class AttachCapabilitiesEvent<T> extends GenericEvent<T>
* 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)
{
public void addListener(Runnable listener) {
this.listeners.add(listener);
}
public List<Runnable> getListeners()
{
public List<Runnable> getListeners() {
return this.listenersView;
}
}