Update To New Screen Handler API
All checks were successful
Twine/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-06-15 19:14:36 -04:00
parent e4b4ee0765
commit 4d14fe7487
4 changed files with 59 additions and 30 deletions

View File

@ -7,16 +7,17 @@ import com.thebrokenrail.twine.block.GlowingObsidianBlock;
import com.thebrokenrail.twine.item.BackpackItem; import com.thebrokenrail.twine.item.BackpackItem;
import com.thebrokenrail.twine.item.DivinerItem; import com.thebrokenrail.twine.item.DivinerItem;
import com.thebrokenrail.twine.mixin.CriteriaHook; import com.thebrokenrail.twine.mixin.CriteriaHook;
import com.thebrokenrail.twine.util.BackpackScreenHandler;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.fabricmc.fabric.api.container.ContainerProviderRegistry; import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.fabricmc.fabric.api.tag.TagRegistry; import net.fabricmc.fabric.api.tag.TagRegistry;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.item.*; import net.minecraft.item.*;
import net.minecraft.screen.GenericContainerScreenHandler; import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType; import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.tag.Tag; import net.minecraft.tag.Tag;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
@ -29,7 +30,7 @@ public class Twine implements ModInitializer {
public static final ItemGroup ITEM_GROUP = FabricItemGroupBuilder.build(new Identifier(NAMESPACE, "item_group"), () -> new ItemStack(GLOWING_OBSIDIAN)); public static final ItemGroup ITEM_GROUP = FabricItemGroupBuilder.build(new Identifier(NAMESPACE, "item_group"), () -> new ItemStack(GLOWING_OBSIDIAN));
public static final Identifier BACKPACK_SCREEN = new Identifier(NAMESPACE, "backpack"); private static final Identifier BACKPACK_SCREEN = new Identifier(NAMESPACE, "backpack");
public static final Item SMALL_BACKPACK = new BackpackItem(false); public static final Item SMALL_BACKPACK = new BackpackItem(false);
public static final Item LARGE_BACKPACK = new BackpackItem(true); public static final Item LARGE_BACKPACK = new BackpackItem(true);
@ -40,18 +41,15 @@ public class Twine implements ModInitializer {
public static final int STAGE_COUNT = 6; public static final int STAGE_COUNT = 6;
public static final int STAGE_TIME = 24000; public static final int STAGE_TIME = 24000;
public static ScreenHandlerType<? extends GenericContainerScreenHandler> BACKPACK_SCREEN_TYPE;
public static ChestBoatCriterion CHEST_BOAT_CRITERION = CriteriaHook.callRegister(new ChestBoatCriterion()); public static ChestBoatCriterion CHEST_BOAT_CRITERION = CriteriaHook.callRegister(new ChestBoatCriterion());
public static EnderChestBoatCriterion ENDER_CHEST_BOAT_CRITERION = CriteriaHook.callRegister(new EnderChestBoatCriterion()); public static EnderChestBoatCriterion ENDER_CHEST_BOAT_CRITERION = CriteriaHook.callRegister(new EnderChestBoatCriterion());
@Override @Override
public void onInitialize() { public void onInitialize() {
ContainerProviderRegistry.INSTANCE.registerFactory(BACKPACK_SCREEN, (i, identifier, playerEntity, buf) -> { BACKPACK_SCREEN_TYPE = ScreenHandlerRegistry.registerExtended(BACKPACK_SCREEN, BackpackScreenHandler::new);
boolean large = buf.readBoolean();
Hand hand = buf.readEnumConstant(Hand.class);
int rows = large ? 6 : 3;
ItemStack stack = playerEntity.getStackInHand(hand);
return new GenericContainerScreenHandler(ScreenHandlerType.STONECUTTER, i, playerEntity.inventory, new BackpackItem.ItemInventory(9 * rows, stack, hand), rows);
});
Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "small_backpack"), SMALL_BACKPACK); Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "small_backpack"), SMALL_BACKPACK);
Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "large_backpack"), LARGE_BACKPACK); Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "large_backpack"), LARGE_BACKPACK);

View File

@ -1,27 +1,19 @@
package com.thebrokenrail.twine.client; package com.thebrokenrail.twine.client;
import com.thebrokenrail.twine.Twine; import com.thebrokenrail.twine.Twine;
import com.thebrokenrail.twine.item.BackpackItem;
import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry; import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
import net.fabricmc.fabric.api.client.screen.ScreenProviderRegistry; import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen; import net.minecraft.client.gui.screen.ingame.GenericContainerScreen;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.DyeableItem; import net.minecraft.item.DyeableItem;
import net.minecraft.screen.GenericContainerScreenHandler;
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public class TwineClient implements ClientModInitializer { public class TwineClient implements ClientModInitializer {
@Override @Override
public void onInitializeClient() { public void onInitializeClient() {
ScreenProviderRegistry.INSTANCE.registerFactory(Twine.BACKPACK_SCREEN, screenHandler -> { ScreenRegistry.register(Twine.BACKPACK_SCREEN_TYPE, GenericContainerScreen::new);
PlayerEntity player = MinecraftClient.getInstance().player;
assert player != null;
return new GenericContainerScreen((GenericContainerScreenHandler) screenHandler, player.inventory, ((BackpackItem.ItemInventory) ((GenericContainerScreenHandler) screenHandler).getInventory()).getHolder().getName());
});
ColorProviderRegistry.ITEM.register((stack, tintIndex) -> tintIndex > 0 ? -1 : ((DyeableItem) stack.getItem()).getColor(stack), Twine.SMALL_BACKPACK, Twine.LARGE_BACKPACK); ColorProviderRegistry.ITEM.register((stack, tintIndex) -> tintIndex > 0 ? -1 : ((DyeableItem) stack.getItem()).getColor(stack), Twine.SMALL_BACKPACK, Twine.LARGE_BACKPACK);
} }
} }

View File

@ -1,13 +1,19 @@
package com.thebrokenrail.twine.item; package com.thebrokenrail.twine.item;
import com.thebrokenrail.twine.Twine; import com.thebrokenrail.twine.Twine;
import net.fabricmc.fabric.api.container.ContainerProviderRegistry; import com.thebrokenrail.twine.util.BackpackScreenHandler;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventories; import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.Inventory; import net.minecraft.inventory.Inventory;
import net.minecraft.item.DyeableItem; import net.minecraft.item.DyeableItem;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult; import net.minecraft.util.TypedActionResult;
@ -21,17 +27,13 @@ public class BackpackItem extends Item implements DyeableItem {
private final Hand hand; private final Hand hand;
private final DefaultedList<ItemStack> inv; private final DefaultedList<ItemStack> inv;
public ItemInventory(int size, ItemStack stack, Hand hand) { public ItemInventory(int size, PlayerEntity player, Hand hand) {
this.size = size; this.size = size;
this.stack = stack; this.stack = player.getStackInHand(hand);
this.hand = hand; this.hand = hand;
inv = getInv(); inv = getInv();
} }
public ItemStack getHolder() {
return stack;
}
private DefaultedList<ItemStack> getInv() { private DefaultedList<ItemStack> getInv() {
DefaultedList<ItemStack> list = DefaultedList.ofSize(size, ItemStack.EMPTY); DefaultedList<ItemStack> list = DefaultedList.ofSize(size, ItemStack.EMPTY);
Inventories.fromTag(stack.getOrCreateTag(), list); Inventories.fromTag(stack.getOrCreateTag(), list);
@ -107,9 +109,22 @@ public class BackpackItem extends Item implements DyeableItem {
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) { public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack stack = user.getStackInHand(hand); ItemStack stack = user.getStackInHand(hand);
if (!world.isClient()) { if (!world.isClient()) {
ContainerProviderRegistry.INSTANCE.openContainer(Twine.BACKPACK_SCREEN, user, buf -> { user.openHandledScreen(new ExtendedScreenHandlerFactory() {
@Override
public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf buf) {
buf.writeBoolean(large); buf.writeBoolean(large);
buf.writeEnumConstant(hand); buf.writeEnumConstant(hand);
}
@Override
public Text getDisplayName() {
return stack.getName();
}
@Override
public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) {
return new BackpackScreenHandler(syncId, inv, large, hand);
}
}); });
} }
return new TypedActionResult<>(ActionResult.CONSUME, stack); return new TypedActionResult<>(ActionResult.CONSUME, stack);

View File

@ -0,0 +1,24 @@
package com.thebrokenrail.twine.util;
import com.thebrokenrail.twine.Twine;
import com.thebrokenrail.twine.item.BackpackItem;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.util.Hand;
public class BackpackScreenHandler extends GenericContainerScreenHandler {
public BackpackScreenHandler(int i, PlayerInventory playerInventory, PacketByteBuf buf) {
this(i, playerInventory, buf.readBoolean(), buf.readEnumConstant(Hand.class));
}
public BackpackScreenHandler(int i, PlayerInventory playerInventory, boolean large, Hand hand) {
this(Twine.BACKPACK_SCREEN_TYPE, i, playerInventory, new BackpackItem.ItemInventory(9 * (large ? 6 : 3), playerInventory.player, hand));
}
public BackpackScreenHandler(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory, Inventory inventory) {
super(type, syncId, playerInventory, inventory, inventory.size() / 9);
}
}