-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0a82d7
commit 5da16e6
Showing
6 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/net/minecraftforge/event/GatherComponentsEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package net.minecraftforge.event; | ||
|
||
import net.minecraft.core.component.DataComponentMap; | ||
import net.minecraft.core.component.DataComponentType; | ||
import net.minecraft.core.component.DataComponents; | ||
import net.minecraft.world.item.Item; | ||
import net.minecraftforge.eventbus.api.Event; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** | ||
Where all the events fore Gathering Components will exist. | ||
*/ | ||
|
||
public abstract class GatherComponentsEvent extends Event { | ||
private final DataComponentMap.Builder components = DataComponentMap.builder(); | ||
private final DataComponentMap originalComponents; | ||
private final Object owner; | ||
|
||
protected GatherComponentsEvent(Object owner, DataComponentMap originalComponents) { | ||
this.originalComponents = originalComponents; | ||
this.owner = owner; | ||
} | ||
|
||
public <T> void register(DataComponentType<T> componentType, @Nullable T value) { | ||
components.set(componentType, value); | ||
} | ||
|
||
@ApiStatus.Internal | ||
public DataComponentMap getDataComponentMap() { | ||
return components.build(); | ||
} | ||
|
||
public DataComponentMap getOriginalComponentMap() { | ||
return originalComponents; | ||
} | ||
|
||
public Object getOwner() { | ||
return owner; | ||
} | ||
|
||
/** | ||
* Used to get additional Components for any {@link net.minecraft.world.item.Item} | ||
* | ||
* Fired once for every {@link net.minecraft.world.item.Item} instance, only once, Lazily. | ||
* | ||
* Recursion is not supported. Cant call {@link net.minecraft.world.item.Item#components()} as that would cause a loop. | ||
* | ||
* References in {@link net.minecraft.world.item.Items} may not be valid at the current time. | ||
*/ | ||
public static class Item extends GatherComponentsEvent { | ||
public Item(net.minecraft.world.item.Item item, DataComponentMap dataComponents) { | ||
super(item, dataComponents); | ||
} | ||
|
||
@Override | ||
public net.minecraft.world.item.Item getOwner() { | ||
return (net.minecraft.world.item.Item) super.getOwner(); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/java/net/minecraftforge/debug/gameplay/item/GatherComponentsEventTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package net.minecraftforge.debug.gameplay.item; | ||
|
||
import net.minecraft.core.component.DataComponents; | ||
import net.minecraft.gametest.framework.GameTest; | ||
import net.minecraft.gametest.framework.GameTestHelper; | ||
import net.minecraft.world.food.FoodProperties; | ||
import net.minecraft.world.item.Items; | ||
import net.minecraftforge.common.MinecraftForge; | ||
import net.minecraftforge.event.GatherComponentsEvent; | ||
import net.minecraftforge.eventbus.api.IEventBus; | ||
import net.minecraftforge.fml.common.Mod; | ||
import net.minecraftforge.gametest.GameTestHolder; | ||
import net.minecraftforge.test.BaseTestMod; | ||
|
||
import java.util.List; | ||
|
||
@GameTestHolder("forge." + GatherComponentsEventTest.MOD_ID) | ||
@Mod(GatherComponentsEventTest.MOD_ID) | ||
public class GatherComponentsEventTest extends BaseTestMod { | ||
public static final String MOD_ID = "gather_components_test_event"; | ||
|
||
public GatherComponentsEventTest() { | ||
IEventBus bus = MinecraftForge.EVENT_BUS; | ||
bus.addListener(this::onItem); | ||
} | ||
|
||
public void onItem(GatherComponentsEvent.Item itemEvent) { | ||
if (!itemEvent.getOriginalComponentMap().has(DataComponents.FOOD) && itemEvent.getOwner() == Items.IRON_NUGGET) { | ||
itemEvent.register(DataComponents.FOOD, new FoodProperties( | ||
2, | ||
7, | ||
true, | ||
3, | ||
List.of() | ||
)); | ||
} | ||
} | ||
|
||
@GameTest(template = "forge:empty3x3x3") | ||
public static void onTestForFood(GameTestHelper helper) { | ||
helper.assertTrue(Items.IRON_NUGGET.components().has(DataComponents.FOOD), "Iron Nugget is not edible, failed to apply DataComponents.FOOD to it."); | ||
helper.assertFalse(Items.IRON_INGOT.components().has(DataComponents.FOOD), "Iron Ingot is edible, should not have DataComponents.FOOD"); | ||
helper.succeed(); | ||
} | ||
} |