mirror of
https://github.com/FabricMC/fabric.git
synced 2025-04-21 03:10:54 -04:00
Add contains method to FabricComponentMapBuilder (#4446)
* add contains method
* fix javadoc checkstyle i think
(cherry picked from commit ee91fa1fd2
)
This commit is contained in:
parent
68d5d6abf3
commit
b50102238a
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
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
@ -88,4 +90,32 @@ public class DefaultItemComponentGameTest {
|
|||
context.assertTrue(expectedName.contains(itemName), Text.literal(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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue