Skip to content

Commit

Permalink
Add ItemVariant#withComponentChanges and FluidVariant#withComponentCh…
Browse files Browse the repository at this point in the history
…anges (#4082)

* add ItemVariant#withChanges and FluidVariant#withChanges

* withChanges -> withComponentChanges

* add TransferVariant#withComponentChanges

* make TransferVariant#withComponentChanges throw

(cherry picked from commit 1d5c243)
  • Loading branch information
BasiqueEvangelist authored and modmuss50 committed Sep 23, 2024
1 parent c7469b2 commit 5253a6d
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ default Fluid getFluid() {
default RegistryEntry<Fluid> getRegistryEntry() {
return getFluid().getRegistryEntry();
}

/**
* Creates a copy of this FluidVariant with the provided component changes applied.
* @param changes the changes to apply
* @return the new variant with the changes applied
*/
@Override
FluidVariant withComponentChanges(ComponentChanges changes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,14 @@ default ItemStack toStack(int count) {
if (isBlank()) return ItemStack.EMPTY;
return new ItemStack(getRegistryEntry(), count, getComponents());
}

/**
* Creates a copy of this ItemVariant with the provided component changes applied.
* @param changes the changes to apply
* @return the new variant with the changes applied
*
* @see ItemStack#applyUnvalidatedChanges(ComponentChanges)
*/
@Override
ItemVariant withComponentChanges(ComponentChanges changes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ default boolean componentsMatch(ComponentChanges other) {
default boolean isOf(O object) {
return getObject() == object;
}

/**
* Creates a copy of this TransferVariant with the provided component changes applied.
* @param changes the changes to apply
* @return the new variant with the changes applied
*/
default TransferVariant<O> withComponentChanges(ComponentChanges changes) {
throw new UnsupportedOperationException("withComponentChanges is not supported by this TransferVariant");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.minecraft.component.ComponentChanges;
import net.minecraft.component.ComponentType;

import net.fabricmc.fabric.api.transfer.v1.storage.SlottedStorage;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageView;
Expand Down Expand Up @@ -107,4 +112,24 @@ public int size() {
}
};
}

public static ComponentChanges mergeChanges(ComponentChanges base, ComponentChanges applied) {
ComponentChanges.Builder builder = ComponentChanges.builder();

writeChangesTo(base, builder);
writeChangesTo(applied, builder);

return builder.build();
}

@SuppressWarnings("unchecked")
private static void writeChangesTo(ComponentChanges changes, ComponentChanges.Builder builder) {
for (Map.Entry<ComponentType<?>, Optional<?>> entry : changes.entrySet()) {
if (entry.getValue().isPresent()) {
builder.add((ComponentType<Object>) entry.getKey(), entry.getValue().get());
} else {
builder.remove(entry.getKey());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.fabricmc.fabric.impl.transfer.TransferApiImpl;

public class FluidVariantImpl implements FluidVariant {
public static FluidVariant of(Fluid fluid, ComponentChanges components) {
Expand Down Expand Up @@ -95,6 +96,11 @@ public ComponentMap getComponentMap() {
return componentMap;
}

@Override
public FluidVariant withComponentChanges(ComponentChanges changes) {
return of(fluid, TransferApiImpl.mergeChanges(getComponents(), changes));
}

@Override
public String toString() {
return "FluidVariant{fluid=" + fluid + ", components=" + components + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.minecraft.registry.entry.RegistryEntry;

import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.impl.transfer.TransferApiImpl;

public class ItemVariantImpl implements ItemVariant {
public static ItemVariant of(Item item, ComponentChanges components) {
Expand Down Expand Up @@ -76,6 +77,11 @@ public ComponentMap getComponentMap() {
return getCachedStack().getComponents();
}

@Override
public ItemVariant withComponentChanges(ComponentChanges changes) {
return of(item, TransferApiImpl.mergeChanges(getComponents(), changes));
}

@Override
public boolean isBlank() {
return item == Items.AIR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

import static net.fabricmc.fabric.test.transfer.TestUtil.assertEquals;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import net.minecraft.component.ComponentChanges;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.util.Unit;

import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;

Expand All @@ -40,6 +44,28 @@ public void testFlowing() {
assertEquals(FluidVariant.of(Fluids.LAVA), FluidVariant.of(Fluids.FLOWING_LAVA));
}

@Test
public void testWithComponentChanges() {
FluidVariant variant = FluidVariant.of(Fluids.WATER, ComponentChanges.builder()
.add(DataComponentTypes.HIDE_TOOLTIP, Unit.INSTANCE)
.build());

FluidVariant newVariant = variant.withComponentChanges(ComponentChanges.builder()
.remove(DataComponentTypes.HIDE_TOOLTIP)
.add(DataComponentTypes.GLIDER, Unit.INSTANCE)
.build());

Assertions.assertFalse(
newVariant.getComponentMap().contains(DataComponentTypes.HIDE_TOOLTIP),
"New variant's HIDE_TOOLTIP component was removed, but is still present"
);

Assertions.assertTrue(
newVariant.getComponentMap().contains(DataComponentTypes.GLIDER),
"New variant's GLIDER component was added, but is not present"
);
}

private static void assertFluidEquals(Fluid fluid, FluidVariant... variants) {
for (FluidVariant variant : variants) {
if (variant.getFluid() != fluid) {
Expand Down

0 comments on commit 5253a6d

Please sign in to comment.