Skip to content

Commit

Permalink
Add TransferVariant.getComponentMap() (#4074)
Browse files Browse the repository at this point in the history
* Add TransferVariant.getComponentMap()

* used the cached stack

* Even better

(cherry picked from commit 0771530)
  • Loading branch information
modmuss50 committed Sep 10, 2024
1 parent 4053855 commit d38f898
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Objects;

import net.minecraft.component.ComponentChanges;
import net.minecraft.component.ComponentMap;

/**
* An immutable association of an immutable object instance (for example {@code Item} or {@code Fluid}) and data components.
Expand Down Expand Up @@ -47,6 +48,11 @@ public interface TransferVariant<O> {
*/
ComponentChanges getComponents();

/**
* @return The {@link ComponentMap} of this variant.
*/
ComponentMap getComponentMap();

/**
* Return true if this variant has a component changes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.jetbrains.annotations.Nullable;

import net.minecraft.component.ComponentChanges;
import net.minecraft.component.ComponentMap;
import net.minecraft.component.ComponentMapImpl;
import net.minecraft.fluid.FlowableFluid;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
Expand Down Expand Up @@ -63,11 +65,13 @@ public static FluidVariant of(RegistryEntry<Fluid> fluid, ComponentChanges compo

private final Fluid fluid;
private final ComponentChanges components;
private final ComponentMap componentMap;
private final int hashCode;

public FluidVariantImpl(Fluid fluid, ComponentChanges components) {
this.fluid = fluid;
this.components = components;
this.componentMap = components == ComponentChanges.EMPTY ? ComponentMap.EMPTY : ComponentMapImpl.create(ComponentMap.EMPTY, components);
this.hashCode = Objects.hash(fluid, components);
}

Expand All @@ -86,6 +90,11 @@ public Fluid getObject() {
return components;
}

@Override
public ComponentMap getComponentMap() {
return componentMap;
}

@Override
public String toString() {
return "FluidVariant{fluid=" + fluid + ", components=" + components + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jetbrains.annotations.Nullable;

import net.minecraft.component.ComponentChanges;
import net.minecraft.component.ComponentMap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
Expand Down Expand Up @@ -70,6 +71,11 @@ public ComponentChanges getComponents() {
return components;
}

@Override
public ComponentMap getComponentMap() {
return getCachedStack().getComponents();
}

@Override
public boolean isBlank() {
return item == Items.AIR;
Expand Down

0 comments on commit d38f898

Please sign in to comment.