From 1345c08cb84b481ee04ac385637e8bb8a251a3c5 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Sat, 29 Dec 2018 00:21:13 +0000 Subject: [PATCH] Fix ItemGroup API on servers --- .../itemgroup/FabricItemGroupBuilder.java | 3 +- .../client/itemgroup/MixinItemGroup.java | 17 +------- .../mixin/itemgroup/MixinItemGroup.java | 43 +++++++++++++++++++ .../net.fabricmc.fabric.mixins.common.json | 1 + .../fabric/itemgroup/ItemGroupMod.java | 6 +-- 5 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 src/main/java/net/fabricmc/fabric/mixin/itemgroup/MixinItemGroup.java diff --git a/src/main/java/net/fabricmc/fabric/api/client/itemgroup/FabricItemGroupBuilder.java b/src/main/java/net/fabricmc/fabric/api/client/itemgroup/FabricItemGroupBuilder.java index 5f8a9757a..7504649ec 100644 --- a/src/main/java/net/fabricmc/fabric/api/client/itemgroup/FabricItemGroupBuilder.java +++ b/src/main/java/net/fabricmc/fabric/api/client/itemgroup/FabricItemGroupBuilder.java @@ -92,12 +92,11 @@ public final class FabricItemGroupBuilder { public ItemGroup build() { ((ItemGroupExtensions) ItemGroup.BUILDING_BLOCKS).fabric_expandArray(); return new ItemGroup(ItemGroup.GROUPS.length - 1, String.format("%s.%s", identifier.getNamespace(), identifier.getPath())) { - @Override + public ItemStack getIconItem() { return stackSupplier.get(); } - @Override public void getStacksForDisplay(DefaultedList stacks) { if(stacksForDisplay != null){ stacksForDisplay.accept(stacks); diff --git a/src/main/java/net/fabricmc/fabric/mixin/client/itemgroup/MixinItemGroup.java b/src/main/java/net/fabricmc/fabric/mixin/client/itemgroup/MixinItemGroup.java index 630ca7fa5..b874599f6 100644 --- a/src/main/java/net/fabricmc/fabric/mixin/client/itemgroup/MixinItemGroup.java +++ b/src/main/java/net/fabricmc/fabric/mixin/client/itemgroup/MixinItemGroup.java @@ -17,23 +17,16 @@ package net.fabricmc.fabric.mixin.client.itemgroup; import net.fabricmc.fabric.client.itemgroup.FabricCreativeGuiComponents; -import net.fabricmc.fabric.client.itemgroup.ItemGroupExtensions; import net.minecraft.item.ItemGroup; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Mutable; 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.CallbackInfoReturnable; @Mixin(ItemGroup.class) -public abstract class MixinItemGroup implements ItemGroupExtensions { - - @Shadow - @Final - @Mutable - public static ItemGroup[] GROUPS; +public abstract class MixinItemGroup { @Shadow public abstract int getId(); @@ -45,14 +38,6 @@ public abstract class MixinItemGroup implements ItemGroupExtensions { @Final private int id; - @Override - public void fabric_expandArray() { - ItemGroup[] tempGroups = GROUPS; - GROUPS = new ItemGroup[GROUPS.length + 1]; - for (ItemGroup group : tempGroups) { - GROUPS[group.getId()] = group; - } - } @Inject(method = "isTopRow", cancellable = true, at = @At("HEAD")) private void isTopRow(CallbackInfoReturnable info) { diff --git a/src/main/java/net/fabricmc/fabric/mixin/itemgroup/MixinItemGroup.java b/src/main/java/net/fabricmc/fabric/mixin/itemgroup/MixinItemGroup.java new file mode 100644 index 000000000..23e5a2164 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/mixin/itemgroup/MixinItemGroup.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2016, 2017, 2018 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 net.fabricmc.fabric.client.itemgroup.ItemGroupExtensions; +import net.minecraft.item.ItemGroup; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(ItemGroup.class) +public abstract class MixinItemGroup implements ItemGroupExtensions { + + @Shadow + @Final + @Mutable + public static ItemGroup[] GROUPS; + + @Override + public void fabric_expandArray() { + ItemGroup[] tempGroups = GROUPS; + GROUPS = new ItemGroup[GROUPS.length + 1]; + for (int i = 0; i < tempGroups.length; i++) { + GROUPS[i] = tempGroups[i]; + } + } + +} diff --git a/src/main/resources/net.fabricmc.fabric.mixins.common.json b/src/main/resources/net.fabricmc.fabric.mixins.common.json index 774bededf..e503d7084 100644 --- a/src/main/resources/net.fabricmc.fabric.mixins.common.json +++ b/src/main/resources/net.fabricmc.fabric.mixins.common.json @@ -17,6 +17,7 @@ "events.server.MixinMinecraftServer", "events.tick.MixinMinecraftServer", "events.tick.MixinWorld", + "itemgroup.MixinItemGroup", "misc.MixinCrashReport", "networking.MixinServerPlayNetworkHandler", "networking.MixinSPacketCustomPayload", diff --git a/src/test/java/net/fabricmc/fabric/itemgroup/ItemGroupMod.java b/src/test/java/net/fabricmc/fabric/itemgroup/ItemGroupMod.java index a50b53dcd..fbc1ee232 100644 --- a/src/test/java/net/fabricmc/fabric/itemgroup/ItemGroupMod.java +++ b/src/test/java/net/fabricmc/fabric/itemgroup/ItemGroupMod.java @@ -16,7 +16,7 @@ package net.fabricmc.fabric.itemgroup; -import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; import net.minecraft.block.Blocks; import net.minecraft.item.Item; @@ -27,9 +27,9 @@ import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; -public class ItemGroupMod implements ClientModInitializer { +public class ItemGroupMod implements ModInitializer { @Override - public void onInitializeClient() { + public void onInitialize() { //This creates your standard Item Group ItemGroup group = FabricItemGroupBuilder.build(new Identifier("fabric", "fabric_test_tab"), () -> new ItemStack(Items.IRON_CHESTPLATE)); Item testItem = new Item(new Item.Settings().itemGroup(group));