Skip to content

Commit

Permalink
Add internal FabricItemGroup class
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Feb 22, 2024
1 parent cefbb70 commit c9161c2
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import net.fabricmc.fabric.impl.client.itemgroup.FabricCreativeGuiComponents;

import net.fabricmc.fabric.impl.itemgroup.FabricItemGroup;

import net.minecraft.item.ItemGroups;

import net.minecraftforge.client.gui.CreativeTabsScreenPage;
Expand All @@ -31,7 +33,12 @@
import net.minecraft.item.ItemGroup;

import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -59,6 +66,11 @@ private static int fabric_getPage() {
return fabric_currentPage;
}

@Inject(method = "init", at = @At(value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z", ordinal = 0), locals = LocalCapture.CAPTURE_FAILHARD)
private void initPages(CallbackInfo ci, int tabIndex, List<ItemGroup> currentPage, Iterator<?> it, ItemGroup sortedCreativeModeTab) {
((FabricItemGroup) sortedCreativeModeTab).setPage(this.pages.size());
}

@Override
public void fabric_previousPage() {
this.setCurrentPage(this.pages.get(Math.max(this.pages.indexOf(this.currentPage) - 1, 0)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.impl.itemgroup;

public interface FabricItemGroup {
int getPage();

void setPage(int page);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class FabricItemGroupBuilderImpl extends ItemGroup.Builder {

public FabricItemGroupBuilderImpl() {
// Set when building.
super(null, -1);
super(ItemGroup.Row.TOP, -1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.itemgroup;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

import net.minecraft.item.ItemGroup;

import net.fabricmc.fabric.impl.itemgroup.FabricItemGroup;

@Mixin(ItemGroup.class)
abstract class ItemGroupMixin implements FabricItemGroup {
@Unique
private int fabric_page = -1;

@Override
public int getPage() {
if (fabric_page < 0) {
throw new IllegalStateException("Item group has no page");
}

return fabric_page;
}

@Override
public void setPage(int page) {
this.fabric_page = page;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"required": true,
"package": "net.fabricmc.fabric.mixin.itemgroup",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ItemGroupMixin"
],
"injectors": {
"defaultRequire": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"description": "An API for adding custom item groups.",
"mixins": [
"fabric-item-group-api-v1.mixins.json",
{
"config": "fabric-item-group-api-v1.client.mixins.json",
"environment": "client"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ fabric-client-tags-api-v1-version=1.1.2
loom.platform=forge
forge_version=1.20.1-47.2.6
pack_format=15
forgified_version=1.11.2
forgified_version=1.11.3
forge_fabric_loader_version=2.6.0+0.15.0+1.20.1

0 comments on commit c9161c2

Please sign in to comment.