Add contains method to FabricComponentMapBuilder ()

* add contains method

* fix javadoc checkstyle i think
This commit is contained in:
TheDeathlyCow 2025-02-20 07:55:59 -10:00 committed by GitHub
parent 9448cd2df9
commit ee91fa1fd2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 0 deletions
fabric-item-api-v1/src
main/java/net/fabricmc/fabric
testmod/java/net/fabricmc/fabric/test/item/gametest

View file

@ -71,4 +71,14 @@ public interface FabricComponentMapBuilder {
default <T> List<T> getOrEmpty(ComponentType<List<T>> type) {
throw new AssertionError("Implemented in Mixin");
}
/**
* Checks if a component type has been registered to this builder.
*
* @param type The component type to check
* @return Returns true if the type has been registered to this builder, false otherwise
*/
default boolean contains(ComponentType<?> type) {
throw new AssertionError("Implemented in Mixin");
}
}

View file

@ -62,4 +62,9 @@ abstract class ComponentMapBuilderMixin implements FabricComponentMapBuilder {
this.add(type, existing);
return existing;
}
@Override
public boolean contains(ComponentType<?> type) {
return this.components.containsKey(type);
}
}

View file

@ -18,8 +18,10 @@ package net.fabricmc.fabric.test.item.gametest;
import java.util.function.Consumer;
import net.minecraft.component.ComponentMap;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.FireworksComponent;
import net.minecraft.component.type.UnbreakableComponent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
@ -90,4 +92,32 @@ public class DefaultItemComponentGameTest implements FabricGameTest {
context.assertTrue(expectedName.contains(itemName), errorMessage.formatted(itemName, expectedName));
context.complete();
}
@GameTest(templateName = EMPTY_STRUCTURE)
public void emptyComponentMapDoesNotContainUnbreakable(TestContext context) {
ComponentMap.Builder builder = ComponentMap.builder();
context.assertFalse(builder.contains(DataComponentTypes.UNBREAKABLE), "Empty component map contains unbreakable type");
context.complete();
}
@GameTest(templateName = EMPTY_STRUCTURE)
public void componentMapWithItemNameDoesNotContainUnbreakable(TestContext context) {
ComponentMap.Builder builder = ComponentMap.builder();
builder.add(DataComponentTypes.ITEM_NAME, Text.of("Weird Name"));
context.assertFalse(builder.contains(DataComponentTypes.UNBREAKABLE), "Component map should not contain unbreakable type");
context.complete();
}
@GameTest(templateName = EMPTY_STRUCTURE)
public void componentMapWithUnbreakableContainsUnbreakable(TestContext context) {
ComponentMap.Builder builder = ComponentMap.builder();
builder.add(DataComponentTypes.UNBREAKABLE, new UnbreakableComponent(true));
context.assertTrue(builder.contains(DataComponentTypes.UNBREAKABLE), "Component map does not contain unbreakable type");
context.complete();
}
}