Fix FabricItemGroupBuilder not setting the Identifier

This commit is contained in:
modmuss50 2022-11-22 16:48:04 +00:00
parent e1cd40e0d6
commit 8790b57d8c
4 changed files with 39 additions and 7 deletions

View file

@ -16,8 +16,12 @@
package net.fabricmc.fabric.impl.itemgroup;
import net.minecraft.util.Identifier;
public interface FabricItemGroup {
int getPage();
void setPage(int page);
void setId(Identifier identifier);
}

View file

@ -16,6 +16,8 @@
package net.fabricmc.fabric.impl.itemgroup;
import java.util.Objects;
import org.jetbrains.annotations.ApiStatus;
import net.minecraft.item.ItemGroup;
@ -23,14 +25,19 @@ import net.minecraft.util.Identifier;
@ApiStatus.Internal
public final class FabricItemGroupBuilderImpl extends ItemGroup.Builder {
private final Identifier identifier;
public FabricItemGroupBuilderImpl(Identifier identifier) {
// Set when building.
super(null, -1);
this.identifier = Objects.requireNonNull(identifier);
}
@Override
public ItemGroup build() {
final ItemGroup itemGroup = super.build();
final FabricItemGroup fabricItemGroup = (FabricItemGroup) itemGroup;
fabricItemGroup.setId(identifier);
ItemGroupHelper.appendItemGroup(itemGroup);
return itemGroup;
}

View file

@ -55,6 +55,9 @@ abstract class ItemGroupMixin implements IdentifiableItemGroup, FabricItemGroup
@Unique
private int fabric_page = -1;
@Unique
private Identifier identifier;
@Unique
@Nullable
private UUID fabric_fallbackUUID;
@ -99,15 +102,19 @@ abstract class ItemGroupMixin implements IdentifiableItemGroup, FabricItemGroup
@Override
public Identifier getId() {
final Identifier identifier = MinecraftItemGroups.GROUP_ID_MAP.get((ItemGroup) (Object) this);
if (this.identifier != null) {
return identifier;
}
final Identifier vanillaId = MinecraftItemGroups.GROUP_ID_MAP.get((ItemGroup) (Object) this);
if (vanillaId != null) {
return vanillaId;
}
// No id known, generate a random one
if (identifier == null) {
if (fabric_fallbackUUID == null) {
fabric_fallbackUUID = UUID.randomUUID();
}
// Fallback when no ID is found for this ItemGroup.
return new Identifier("minecraft", "unidentified_" + fabric_fallbackUUID);
setId(new Identifier("minecraft", "unidentified_" + UUID.randomUUID()));
}
return identifier;
@ -126,4 +133,13 @@ abstract class ItemGroupMixin implements IdentifiableItemGroup, FabricItemGroup
public void setPage(int page) {
this.fabric_page = page;
}
@Override
public void setId(Identifier identifier) {
if (this.identifier != null) {
throw new IllegalStateException("Cannot set id to (%s) as item group already has id (%s)".formatted(identifier, this.identifier));
}
this.identifier = identifier;
}
}

View file

@ -16,6 +16,8 @@
package net.fabricmc.fabric.test.item.group;
import java.util.Objects;
import com.google.common.base.Supplier;
import net.minecraft.block.Blocks;
@ -89,5 +91,8 @@ public class ItemGroupTest implements ModInitializer {
})
.build();
}
assert Objects.equals(ItemGroups.HOTBAR.getId().toString(), "minecraft:hotbar");
assert Objects.equals(ITEM_GROUP.getId().toString(), "fabric-item-group-api-v1-testmod:test_group");
}
}