Skip to content

Commit

Permalink
Rework the DataComponentHolder patches (Updates for #817) (#821)
Browse files Browse the repository at this point in the history
  • Loading branch information
ApexModder authored and Technici4n committed Apr 23, 2024
1 parent ddfb94c commit 5c37aba
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 31 deletions.
11 changes: 11 additions & 0 deletions patches/net/minecraft/core/component/DataComponentMap.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- a/net/minecraft/core/component/DataComponentMap.java
+++ b/net/minecraft/core/component/DataComponentMap.java
@@ -126,7 +_,7 @@
};
}

- public static class Builder {
+ public static class Builder implements net.neoforged.neoforge.common.extensions.IDataComponentMapBuilderExtensions {
private final Reference2ObjectMap<DataComponentType<?>, Object> map = new Reference2ObjectArrayMap<>();

Builder() {
7 changes: 4 additions & 3 deletions patches/net/minecraft/world/item/Item.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@
}

public ItemStack getDefaultInstance() {
@@ -335,6 +_,32 @@
@@ -335,13 +_,40 @@
return this.requiredFeatures;
}

- public static class Properties {
+ // NEO START
+ private Object renderProperties;
+
Expand All @@ -155,10 +156,10 @@
+ }
+ // END NEO
+
public static class Properties {
+ public static class Properties implements net.neoforged.neoforge.common.extensions.IItemPropertiesExtensions {
private static final Interner<DataComponentMap> COMPONENT_INTERNER = Interners.newStrongInterner();
@Nullable
@@ -342,6 +_,7 @@
private DataComponentMap.Builder components;
@Nullable
Item craftingRemainingItem;
FeatureFlagSet requiredFeatures = FeatureFlags.VANILLA_SET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@
}

public boolean triggerEvent(int p_58889_, int p_58890_) {
@@ -228,6 +_,27 @@

public BlockEntityType<?> getType() {
@@ -230,6 +_,27 @@
return this.type;
+ }
+
}
+ @Override
+ public CompoundTag getPersistentData() {
+ if (this.customPersistentData == null)
Expand All @@ -77,6 +75,24 @@
+ public final <T> T removeData(net.neoforged.neoforge.attachment.AttachmentType<T> type) {
+ setChanged();
+ return super.removeData(type);
}
+ }
+
@Deprecated
public void setBlockState(BlockState p_155251_) {
this.blockState = p_155251_;
@@ -298,5 +_,15 @@
<T> T get(DataComponentType<T> p_338658_);

<T> T getOrDefault(DataComponentType<? extends T> p_338573_, T p_338734_);
+
+ // Neo: Utility for modded component types, to remove the need to invoke '.value()'
+ @Nullable
+ default <T> T get(java.util.function.Supplier<? extends DataComponentType<T>> componentType) {
+ return get(componentType.get());
+ }
+
+ default <T> T getOrDefault(java.util.function.Supplier<? extends DataComponentType<T>> componentType, T value) {
+ return getOrDefault(componentType.get(), value);
+ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import net.minecraft.core.component.DataComponentHolder;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponentType;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -74,16 +76,38 @@ default <T> T remove(Supplier<? extends DataComponentType<? extends T>> componen
}

/**
* Copes a data component from {@code src} into {@code target}
* Copies all data components from {@code src}
*
* @implNote This will clear any components if the requested {@code src} holder does not contain a matching value.
*/
static <T> void copyFrom(DataComponentHolder src, MutableDataComponentHolder target, DataComponentType<T> componentType) {
target.set(componentType, src.get(componentType));
default void copyFrom(DataComponentHolder src, DataComponentType<?>... componentTypes) {
for (var componentType : componentTypes) {
copyFrom(componentType, src);
}
}

/**
* Copes a data component from {@code src} into {@code target}
* Copies all data components from {@code src}
*
* @implNote This will clear any components if the requested {@code src} holder does not contain a matching value.
*/
static <T> void copyFrom(DataComponentHolder src, MutableDataComponentHolder target, Supplier<? extends DataComponentType<T>> componentType) {
copyFrom(src, target, componentType.get());
default void copyFrom(DataComponentHolder src, Supplier<? extends DataComponentType<?>>... componentTypes) {
for (var componentType : componentTypes) {
copyFrom(componentType.get(), src);
}
}

/**
* Applies a set of component changes to this stack.
*/
void applyComponents(DataComponentPatch patch);

/**
* Applies a set of component changes to this stack.
*/
void applyComponents(DataComponentMap components);

private <T> void copyFrom(DataComponentType<T> componentType, DataComponentHolder src) {
set(componentType, src.get(componentType));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.Arrays;
import java.util.function.Supplier;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.component.DataComponentMap;
Expand Down Expand Up @@ -110,6 +111,13 @@ public static <T> DataComponentIngredient of(boolean strict, DataComponentType<?
return of(strict, DataComponentPredicate.builder().expect(type, value).build(), items);
}

/**
* Creates a new ingredient matching any item from the list, containing the given components
*/
public static <T> DataComponentIngredient of(boolean strict, Supplier<? extends DataComponentType<? super T>> type, T value, ItemLike... items) {
return of(strict, type.get(), value, items);
}

/**
* Creates a new ingredient matching any item from the list, containing the given components
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.function.Supplier;
import net.minecraft.core.component.DataComponentHolder;
import net.minecraft.core.component.DataComponentType;
import net.neoforged.neoforge.common.MutableDataComponentHolder;
import org.jetbrains.annotations.Nullable;

public interface IDataComponentHolderExtension {
Expand All @@ -29,18 +28,4 @@ default <T> T getOrDefault(Supplier<? extends DataComponentType<? extends T>> ty
default boolean has(Supplier<? extends DataComponentType<?>> type) {
return self().has(type.get());
}

/**
* Copies a data component from {@code this} component holder into the {@code target} component holder.
*/
default <T> void copyFrom(MutableDataComponentHolder target, DataComponentType<T> componentType) {
MutableDataComponentHolder.copyFrom(self(), target, componentType);
}

/**
* Copies a data component from {@code this} component holder into the {@code target} component holder.
*/
default <T> void copyFrom(MutableDataComponentHolder target, Supplier<? extends DataComponentType<T>> componentType) {
MutableDataComponentHolder.copyFrom(self(), target, componentType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.common.extensions;

import java.util.function.Supplier;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentType;
import org.jetbrains.annotations.Nullable;

public interface IDataComponentMapBuilderExtensions {
private DataComponentMap.Builder self() {
return (DataComponentMap.Builder) this;
}

default <T> DataComponentMap.Builder set(Supplier<? extends DataComponentType<T>> componentType, @Nullable T value) {
return self().set(componentType.get(), value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.common.extensions;

import java.util.function.Supplier;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.world.item.Item;

public interface IItemPropertiesExtensions {
private Item.Properties self() {
return (Item.Properties) this;
}

default <T> Item.Properties component(Supplier<? extends DataComponentType<T>> componentType, T value) {
return self().component(componentType.get(), value);
}
}
4 changes: 4 additions & 0 deletions src/main/java/net/neoforged/neoforge/fluids/FluidStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ public String toString() {
* Sets a data component.
*/
@Nullable
@Override
public <T> T set(DataComponentType<? super T> type, @Nullable T component) {
return this.components.set(type, component);
}
Expand All @@ -394,20 +395,23 @@ public <T> T set(DataComponentType<? super T> type, @Nullable T component) {
* Removes a data component.
*/
@Nullable
@Override
public <T> T remove(DataComponentType<? extends T> type) {
return this.components.remove(type);
}

/**
* Applies a set of component changes to this stack.
*/
@Override
public void applyComponents(DataComponentPatch patch) {
this.components.applyPatch(patch);
}

/**
* Applies a set of component changes to this stack.
*/
@Override
public void applyComponents(DataComponentMap components) {
this.components.setAll(components);
}
Expand Down

0 comments on commit 5c37aba

Please sign in to comment.