Item Group API: fix bugs in 22w45a port (#2653)

* Fix item group being missing when Operator Tab is hidden

* Do not modify special item groups

* Mark impl as internal

* Select the modded tab after switching tabs

* Allow adding items to Operator Blocks
This commit is contained in:
apple502j 2022-11-13 23:45:19 +09:00 committed by GitHub
parent f84bf2d968
commit cfcffd6cc7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 5 deletions

View file

@ -16,6 +16,9 @@
package net.fabricmc.fabric.impl.itemgroup;
import org.jetbrains.annotations.ApiStatus;
@ApiStatus.Internal
public interface CreativeGuiExtensions {
void fabric_nextPage();

View file

@ -20,6 +20,7 @@ import java.util.Set;
import java.util.function.Consumer;
import com.mojang.blaze3d.systems.RenderSystem;
import org.jetbrains.annotations.ApiStatus;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
@ -29,6 +30,7 @@ import net.minecraft.item.ItemGroups;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
@ApiStatus.Internal
public class FabricCreativeGuiComponents {
private static final Identifier BUTTON_TEX = new Identifier("fabric", "textures/gui/creative_buttons.png");
public static final Set<ItemGroup> COMMON_GROUPS = Set.of(ItemGroups.SEARCH, ItemGroups.INVENTORY, ItemGroups.HOTBAR);

View file

@ -35,6 +35,7 @@ import net.minecraft.text.Text;
import net.fabricmc.fabric.impl.itemgroup.CreativeGuiExtensions;
import net.fabricmc.fabric.impl.itemgroup.FabricCreativeGuiComponents;
import net.fabricmc.fabric.impl.itemgroup.FabricItemGroup;
import net.fabricmc.fabric.impl.itemgroup.ItemGroupHelper;
@Mixin(CreativeInventoryScreen.class)
public abstract class CreativeInventoryScreenMixin<T extends ScreenHandler> extends AbstractInventoryScreen<T> implements CreativeGuiExtensions {
@ -70,7 +71,7 @@ public abstract class CreativeInventoryScreenMixin<T extends ScreenHandler> exte
@Override
public boolean fabric_isButtonVisible(FabricCreativeGuiComponents.Type type) {
return ItemGroups.getGroupsToDisplay().size() > 14;
return ItemGroups.getGroupsToDisplay().size() > (ItemGroups.operatorEnabled ? 14 : 13);
}
@Override
@ -87,7 +88,7 @@ public abstract class CreativeInventoryScreenMixin<T extends ScreenHandler> exte
}
private void fabric_updateSelection() {
ItemGroups.getGroupsToDisplay().stream()
ItemGroupHelper.sortedGroups.stream()
.filter(this::fabric_isGroupVisible)
.findFirst()
.ifPresent(this::setSelectedTab);

View file

@ -17,6 +17,7 @@
package net.fabricmc.fabric.impl.itemgroup;
import java.util.ArrayList;
import java.util.List;
import org.jetbrains.annotations.ApiStatus;
@ -30,6 +31,11 @@ public final class ItemGroupHelper {
private ItemGroupHelper() {
}
/**
* A list of item groups, but with special groups grouped at the end.
*/
public static List<ItemGroup> sortedGroups = ItemGroups.getGroups();
public static void appendItemGroup(ItemGroup itemGroup) {
for (ItemGroup existingGroup : ItemGroups.getGroups()) {
if (existingGroup.getId().equals(itemGroup.getId())) {
@ -40,6 +46,12 @@ public final class ItemGroupHelper {
var itemGroups = new ArrayList<>(ItemGroups.getGroups());
itemGroups.add(itemGroup);
ItemGroupsAccessor.setGroups(ItemGroupsAccessor.invokeCollect(itemGroups.toArray(ItemGroup[]::new)));
List<ItemGroup> validated = ItemGroupsAccessor.invokeCollect(itemGroups.toArray(ItemGroup[]::new));
ItemGroupsAccessor.setGroups(validated);
sortedGroups = validated.stream().sorted((a, b) -> {
if (a.isSpecial() && !b.isSpecial()) return 1;
if (!a.isSpecial() && b.isSpecial()) return -1;
return 0;
}).toList();
}
}

View file

@ -60,6 +60,13 @@ abstract class ItemGroupMixin implements IdentifiableItemGroup, FabricItemGroup
@SuppressWarnings("ConstantConditions")
@Inject(method = "updateEntries", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemGroup;reloadSearchProvider()V"))
public void getStacks(FeatureSet enabledFeatures, boolean operatorEnabled, CallbackInfo ci) {
ItemGroup self = (ItemGroup) (Object) this;
// Do not modify special item groups (except Operator Blocks) at all.
// Special item groups include Saved Hotbars, Search, and Survival Inventory.
// Note, search gets modified as part of the parent item group.
if (self.isSpecial() && self != ItemGroups.OPERATOR) return;
// Sanity check for the injection point. It should be after these fields are set.
Objects.requireNonNull(displayStacks, "displayStacks");
Objects.requireNonNull(searchTabStacks, "searchTabStacks");
@ -76,8 +83,6 @@ abstract class ItemGroupMixin implements IdentifiableItemGroup, FabricItemGroup
}
// Now trigger the global event
ItemGroup self = (ItemGroup) (Object) this;
if (self != ItemGroups.OPERATOR || ItemGroups.operatorEnabled) {
ItemGroupEvents.MODIFY_ENTRIES_ALL.invoker().modifyEntries(self, entries);
}