Start firing AttachCapabilitiesEvent<ItemStack> (#3645)
This commit is contained in:
parent
5cce054548
commit
bf154730bd
|
@ -40,8 +40,8 @@ import javax.annotation.Nonnull;
|
||||||
public class AttachCapabilitiesEvent<T> extends GenericEvent<T>
|
public class AttachCapabilitiesEvent<T> extends GenericEvent<T>
|
||||||
{
|
{
|
||||||
private final T obj;
|
private final T obj;
|
||||||
private final Map<ResourceLocation, ICapabilityProvider> caps = Maps.newLinkedHashMap();
|
final Map<ResourceLocation, ICapabilityProvider> caps;// package-private for ForgeEventFactory
|
||||||
private final Map<ResourceLocation, ICapabilityProvider> view = Collections.unmodifiableMap(caps);
|
private final Map<ResourceLocation, ICapabilityProvider> view;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Deprecated
|
@Deprecated
|
||||||
|
@ -50,9 +50,16 @@ public class AttachCapabilitiesEvent<T> extends GenericEvent<T>
|
||||||
this((Class<T>)Object.class, obj);
|
this((Class<T>)Object.class, obj);
|
||||||
}
|
}
|
||||||
public AttachCapabilitiesEvent(Class<T> type, T obj)
|
public AttachCapabilitiesEvent(Class<T> type, T obj)
|
||||||
|
{
|
||||||
|
this(type, obj, Maps.<ResourceLocation, ICapabilityProvider>newLinkedHashMap());
|
||||||
|
}
|
||||||
|
// package-private for ForgeEventFactory
|
||||||
|
AttachCapabilitiesEvent(Class<T> type, T obj, Map<ResourceLocation, ICapabilityProvider> caps)
|
||||||
{
|
{
|
||||||
super(type);
|
super(type);
|
||||||
this.obj = obj;
|
this.obj = obj;
|
||||||
|
this.caps = caps;
|
||||||
|
this.view = Collections.unmodifiableMap(caps);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -126,10 +133,10 @@ public class AttachCapabilitiesEvent<T> extends GenericEvent<T>
|
||||||
/**
|
/**
|
||||||
* A version of the parent event which is only fired for ItemStacks.
|
* A version of the parent event which is only fired for ItemStacks.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public static class Item extends AttachCapabilitiesEvent<net.minecraft.item.Item>
|
public static class Item extends AttachCapabilitiesEvent<net.minecraft.item.Item>
|
||||||
{
|
{
|
||||||
private final net.minecraft.item.ItemStack stack;
|
private final net.minecraft.item.ItemStack stack;
|
||||||
@Deprecated
|
|
||||||
private final net.minecraft.item.Item item;
|
private final net.minecraft.item.Item item;
|
||||||
public Item(net.minecraft.item.Item item, @Nonnull net.minecraft.item.ItemStack stack)
|
public Item(net.minecraft.item.Item item, @Nonnull net.minecraft.item.ItemStack stack)
|
||||||
{
|
{
|
||||||
|
@ -137,7 +144,6 @@ public class AttachCapabilitiesEvent<T> extends GenericEvent<T>
|
||||||
this.item = item;
|
this.item = item;
|
||||||
this.stack = stack;
|
this.stack = stack;
|
||||||
}
|
}
|
||||||
@Deprecated
|
|
||||||
public net.minecraft.item.Item getItem()
|
public net.minecraft.item.Item getItem()
|
||||||
{
|
{
|
||||||
return this.item;
|
return this.item;
|
||||||
|
|
|
@ -561,7 +561,13 @@ public class ForgeEventFactory
|
||||||
@Nullable
|
@Nullable
|
||||||
public static CapabilityDispatcher gatherCapabilities(Item item, ItemStack stack, ICapabilityProvider parent)
|
public static CapabilityDispatcher gatherCapabilities(Item item, ItemStack stack, ICapabilityProvider parent)
|
||||||
{
|
{
|
||||||
return gatherCapabilities(new AttachCapabilitiesEvent.Item(item, stack), parent);
|
// first fire the legacy event
|
||||||
|
AttachCapabilitiesEvent.Item legacyEvent = new AttachCapabilitiesEvent.Item(item, stack);
|
||||||
|
MinecraftForge.EVENT_BUS.post(legacyEvent);
|
||||||
|
|
||||||
|
// fire new event with the caps that were already registered on the legacy event
|
||||||
|
AttachCapabilitiesEvent<ItemStack> event = new AttachCapabilitiesEvent<ItemStack>(ItemStack.class, stack, legacyEvent.caps);
|
||||||
|
return gatherCapabilities(event, parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
|
@ -2,6 +2,8 @@ package net.minecraftforge.test;
|
||||||
|
|
||||||
import net.minecraft.init.Blocks;
|
import net.minecraft.init.Blocks;
|
||||||
import net.minecraft.init.Items;
|
import net.minecraft.init.Items;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.nbt.NBTBase;
|
import net.minecraft.nbt.NBTBase;
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
|
@ -70,7 +72,6 @@ public class TestCapabilityMod
|
||||||
public void onInteractItem(PlayerInteractEvent.RightClickItem event)
|
public void onInteractItem(PlayerInteractEvent.RightClickItem event)
|
||||||
{
|
{
|
||||||
if (!event.getEntityPlayer().isSneaking()) return;
|
if (!event.getEntityPlayer().isSneaking()) return;
|
||||||
if (event.getItemStack().getItem() != Items.STICK) return;
|
|
||||||
|
|
||||||
if (event.getItemStack().hasCapability(TEST_CAP, null))
|
if (event.getItemStack().hasCapability(TEST_CAP, null))
|
||||||
{
|
{
|
||||||
|
@ -143,19 +144,35 @@ public class TestCapabilityMod
|
||||||
// version of the has/getCapability functions yourself. So you have control
|
// version of the has/getCapability functions yourself. So you have control
|
||||||
// over everything yours being called first.
|
// over everything yours being called first.
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public void onTELoad(AttachCapabilitiesEvent.TileEntity event)
|
public void onTELoad(AttachCapabilitiesEvent<TileEntity> event)
|
||||||
{
|
{
|
||||||
//Attach it! The resource location MUST be unique it's recommended that you tag it with your modid and what the cap is.
|
//Attach it! The resource location MUST be unique it's recommended that you tag it with your modid and what the cap is.
|
||||||
event.addCapability(new ResourceLocation("forge.testcapmod:dummy_cap"), new Provider(event.getTileEntity()));
|
event.addCapability(new ResourceLocation("forge.testcapmod:dummy_cap"), new Provider<TileEntity>(event.getObject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public void onItemLoad(AttachCapabilitiesEvent.Item event)
|
public void onItemLoad(AttachCapabilitiesEvent<ItemStack> event)
|
||||||
{
|
{
|
||||||
if (event.getItemStack().getItem() == Items.STICK)
|
if (event.getObject().getItem() == Items.STICK)
|
||||||
event.addCapability(new ResourceLocation("forge.testcapmod:dummy_cap"), new Provider(event.getItemStack()));
|
event.addCapability(new ResourceLocation("forge.testcapmod:dummy_cap"), new Provider<ItemStack>(event.getObject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// these can be removed if we remove these legacy events
|
||||||
|
@SubscribeEvent
|
||||||
|
public void onItemLoadLegacy(AttachCapabilitiesEvent<Item> event)
|
||||||
|
{
|
||||||
|
if (event.getObject() == Items.APPLE)
|
||||||
|
event.addCapability(new ResourceLocation("forge.testcapmod:dummy_cap"), new Provider<Item>(event.getObject()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public void onItemLoadLegacy(AttachCapabilitiesEvent.Item event)
|
||||||
|
{
|
||||||
|
if (event.getObject() == Items.ARROW)
|
||||||
|
event.addCapability(new ResourceLocation("forge.testcapmod:dummy_cap"), new Provider<Item>(event.getObject()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public void attachEvent(AttachCapabilitiesEvent event) //Test Raw type gets everything still.
|
public void attachEvent(AttachCapabilitiesEvent event) //Test Raw type gets everything still.
|
||||||
|
|
Loading…
Reference in New Issue