mirror of
https://github.com/FabricMC/fabric.git
synced 2024-11-14 19:25:23 -05:00
Fix FabricItemGroupBuilder not setting the Identifier
This commit is contained in:
parent
e1cd40e0d6
commit
8790b57d8c
4 changed files with 39 additions and 7 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue