Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.20.6] Add GatherItemComponentsEvent #9944

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7f1cb1b
Added GatherItemComponentsEvent.java
RealMangorage Apr 30, 2024
afe9dc3
Cleaned up the code for the patch file, moving it into ForgeHooks.java.
RealMangorage May 1, 2024
bfc5640
Cleaned up the Hooks, moved everything where it belongs.
RealMangorage May 1, 2024
8f6851a
Merge remote-tracking branch 'refs/remotes/origin/1.20.x' into 1.20.x…
RealMangorage May 1, 2024
8a5fe1f
Fixed ForgeEventFactory.java
RealMangorage May 1, 2024
cedf346
Cleaned up the Event, adding a DataComponentsMap getter for the event…
RealMangorage May 8, 2024
3ce878d
Cleaned up ForgeHooks.gatherItemComponents to use DataComponentsMap.c…
RealMangorage May 8, 2024
362f6a6
Added GatherItemComponentsEvent.java
RealMangorage Apr 30, 2024
055ae7b
Cleaned up the code for the patch file, moving it into ForgeHooks.java.
RealMangorage May 1, 2024
66079bd
Fixed ForgeEventFactory.java
RealMangorage May 1, 2024
6ff478c
Cleaned up the Event, adding a DataComponentsMap getter for the event…
RealMangorage May 8, 2024
d81945e
Updated
RealMangorage May 14, 2024
25496cb
Merge remote-tracking branch 'refs/remotes/origin/1.20.x' into 1.20.x…
RealMangorage May 14, 2024
08c634c
Updated
RealMangorage May 14, 2024
51393b5
Changed event from .ItemEvent to .Item
RealMangorage May 14, 2024
0394d12
Added JavaDoc
RealMangorage May 14, 2024
d899f72
Added JavaDocs & cleanup
RealMangorage May 14, 2024
933918f
Added JavaDocs & cleanup
RealMangorage May 14, 2024
5f52127
Added GameTest
RealMangorage May 14, 2024
b232bd1
Finished GameTests, Fixed build_forge.gradle, now test runs work.
RealMangorage May 15, 2024
5ca461f
Cleaned up GameTest for GatherComponentsEventTest.java
RealMangorage May 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion patches/minecraft/net/minecraft/world/item/Item.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,26 @@
}

@Deprecated
@@ -111,6 +_,7 @@
@@ -100,17 +_,25 @@
return this.builtInRegistryHolder;
}

+ @Nullable
+ private DataComponentMap builtComponents = null;
+
public DataComponentMap components() {
- return this.components;
+ if (builtComponents == null) {
+ builtComponents = net.minecraftforge.common.ForgeHooks.gatherItemComponents(this, components);
+ }
+ return builtComponents;
}

public int getDefaultMaxStackSize() {
+ if (builtComponents != null) return this.builtComponents.getOrDefault(DataComponents.MAX_STACK_SIZE, 1);
return this.components.getOrDefault(DataComponents.MAX_STACK_SIZE, 1);
}

public void onUseTick(Level p_41428_, LivingEntity p_41429_, ItemStack p_41430_, int p_41431_) {
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/net/minecraftforge/common/ForgeHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import net.minecraft.core.HolderLookup;
import net.minecraft.core.HolderSet;
import net.minecraft.core.NonNullList;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.Connection;
Expand Down Expand Up @@ -114,7 +115,6 @@
import net.minecraft.world.level.biome.MobSpawnSettings;
import net.minecraftforge.common.crafting.conditions.ConditionCodec;
import net.minecraftforge.common.crafting.conditions.ICondition;
import net.minecraftforge.common.crafting.conditions.ICondition.IContext;
import net.minecraftforge.common.crafting.ingredients.IIngredientSerializer;
import net.minecraftforge.common.util.BlockSnapshot;
import net.minecraftforge.common.util.BrainBuilder;
Expand Down Expand Up @@ -1261,4 +1261,8 @@ public static StreamCodec<RegistryFriendlyByteBuf, Ingredient> ingredientStreamC
}
);
}

public static DataComponentMap gatherItemComponents(Item item, DataComponentMap dataComponents) {
return DataComponentMap.composite(dataComponents, ForgeEventFactory.gatherItemComponentsEvent(item, dataComponents).getDataComponentMap());
}
}
6 changes: 6 additions & 0 deletions src/main/java/net/minecraftforge/event/ForgeEventFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;

import net.minecraft.core.component.DataComponentMap;
import net.minecraft.world.item.Item;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -1060,4 +1062,8 @@ public static AnvilRepairEvent onAnvilRepair(Player player, @NotNull ItemStack o
public static void onPlayerTradeWithVillager(Player player, MerchantOffer offer, AbstractVillager villager) {
post(new TradeWithVillagerEvent(player, offer, villager));
}

public static GatherComponentsEvent.GatherItemComponentsEvent gatherItemComponentsEvent(Item item, DataComponentMap dataComponents) {
return fire(new GatherComponentsEvent.GatherItemComponentsEvent(item, dataComponents));
}
}
45 changes: 45 additions & 0 deletions src/main/java/net/minecraftforge/event/GatherComponentsEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.minecraftforge.event;

import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentType;
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();

public <T> void register(DataComponentType<T> componentType, @Nullable T value) {
components.set(componentType, value);
}

@ApiStatus.Internal
public DataComponentMap getDataComponentMap() {
return components.build();
}


public static class GatherItemComponentsEvent extends GatherComponentsEvent {
RealMangorage marked this conversation as resolved.
Show resolved Hide resolved
private final Item item;
private final DataComponentMap dataComponents;

public GatherItemComponentsEvent(Item item, DataComponentMap dataComponents) {
this.item = item;
this.dataComponents = dataComponents;
}

public Item getItem() {
return item;
}

public DataComponentMap getDataComponentMap() {
return dataComponents;
}
}
}