Search in sources :

Example 1 with Container

use of net.minecraft.container.Container in project tweakermore by Fallen-Breath.

the class ContainerProcessor method process.

public static void process(Container container) {
    if (hasTweakEnabled()) {
        Screen screen = MinecraftClient.getInstance().currentScreen;
        ClientPlayerEntity player = MinecraftClient.getInstance().player;
        // not inventory and not crafting table
        if (player != null && screen instanceof ContainerScreen<?> && !(screen instanceof AbstractInventoryScreen) && !(screen instanceof CraftingTableScreen)) {
            ContainerScreen<?> containerScreen = (ContainerScreen<?>) screen;
            if (containerScreen.getContainer() != container || !((AutoProcessableScreen) screen).shouldProcess()) {
                return;
            }
            ((AutoProcessableScreen) screen).setShouldProcess(false);
            List<Slot> allSlots = container.slots;
            List<Slot> playerInvSlots = allSlots.stream().filter(slot -> slot.inventory instanceof PlayerInventory).collect(Collectors.toList());
            if (allSlots.isEmpty() || playerInvSlots.isEmpty()) {
                return;
            }
            List<Slot> containerInvSlots = allSlots.stream().filter(slot -> ItemScrollerInventoryUtilsAccessor.areSlotsInSameInventory(slot, allSlots.get(0))).collect(Collectors.toList());
            if (containerInvSlots.isEmpty()) {
                return;
            }
            for (Processor processor : CONTAINER_PROCESSORS) {
                if (processor.isEnabled()) {
                    boolean success = processor.process(player, containerScreen, allSlots, playerInvSlots, containerInvSlots);
                    if (success) {
                        break;
                    }
                }
            }
            player.closeContainer();
        }
    }
}
Also used : PlayerInventory(net.minecraft.entity.player.PlayerInventory) AbstractInventoryScreen(net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen) ContainerScreen(net.minecraft.client.gui.screen.ingame.ContainerScreen) Slot(net.minecraft.container.Slot) Collectors(java.util.stream.Collectors) List(java.util.List) Screen(net.minecraft.client.gui.screen.Screen) ImmutableList(com.google.common.collect.ImmutableList) CraftingTableScreen(net.minecraft.client.gui.screen.ingame.CraftingTableScreen) MinecraftClient(net.minecraft.client.MinecraftClient) ClientPlayerEntity(net.minecraft.client.network.ClientPlayerEntity) ItemScrollerInventoryUtilsAccessor(me.fallenbreath.tweakermore.mixins.tweaks.tweakmAutoContainerProcess.ItemScrollerInventoryUtilsAccessor) Container(net.minecraft.container.Container) AbstractInventoryScreen(net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen) ContainerScreen(net.minecraft.client.gui.screen.ingame.ContainerScreen) Screen(net.minecraft.client.gui.screen.Screen) CraftingTableScreen(net.minecraft.client.gui.screen.ingame.CraftingTableScreen) PlayerInventory(net.minecraft.entity.player.PlayerInventory) ContainerScreen(net.minecraft.client.gui.screen.ingame.ContainerScreen) Slot(net.minecraft.container.Slot) CraftingTableScreen(net.minecraft.client.gui.screen.ingame.CraftingTableScreen) ClientPlayerEntity(net.minecraft.client.network.ClientPlayerEntity) AbstractInventoryScreen(net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen)

Example 2 with Container

use of net.minecraft.container.Container in project fabric by FabricMC.

the class ContainerProviderImpl method openContainer.

@Override
public void openContainer(Identifier identifier, ServerPlayerEntity player, Consumer<PacketByteBuf> writer) {
    int syncId;
    if (player instanceof ServerPlayerEntitySyncHook) {
        ServerPlayerEntitySyncHook serverPlayerEntitySyncHook = (ServerPlayerEntitySyncHook) player;
        syncId = serverPlayerEntitySyncHook.fabric_incrementSyncId();
    } else if (player instanceof ServerPlayerEntityAccessor) {
        if (!emittedNoSyncHookWarning) {
            LOGGER.warn("ServerPlayerEntitySyncHook could not be applied - fabric-containers is using a hack!");
            emittedNoSyncHookWarning = true;
        }
        syncId = (((ServerPlayerEntityAccessor) player).getContainerSyncId() + 1) % 100;
        ((ServerPlayerEntityAccessor) player).setContainerSyncId(syncId);
    } else {
        throw new RuntimeException("Neither ServerPlayerEntitySyncHook nor Accessor present! This should not happen!");
    }
    PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
    buf.writeIdentifier(identifier);
    buf.writeByte(syncId);
    writer.accept(buf);
    player.networkHandler.sendPacket(new CustomPayloadS2CPacket(PacketTypes.OPEN_CONTAINER, buf));
    PacketByteBuf clonedBuf = new PacketByteBuf(buf.duplicate());
    clonedBuf.readIdentifier();
    clonedBuf.readUnsignedByte();
    Container container = createContainer(syncId, identifier, player, clonedBuf);
    if (container == null) {
        return;
    }
    player.container = container;
    player.container.addListener(player);
}
Also used : Container(net.minecraft.container.Container) CustomPayloadS2CPacket(net.minecraft.client.network.packet.CustomPayloadS2CPacket) PacketByteBuf(net.minecraft.util.PacketByteBuf) ServerPlayerEntityAccessor(net.fabricmc.fabric.mixin.container.ServerPlayerEntityAccessor)

Aggregations

Container (net.minecraft.container.Container)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ItemScrollerInventoryUtilsAccessor (me.fallenbreath.tweakermore.mixins.tweaks.tweakmAutoContainerProcess.ItemScrollerInventoryUtilsAccessor)1 ServerPlayerEntityAccessor (net.fabricmc.fabric.mixin.container.ServerPlayerEntityAccessor)1 MinecraftClient (net.minecraft.client.MinecraftClient)1 Screen (net.minecraft.client.gui.screen.Screen)1 AbstractInventoryScreen (net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen)1 ContainerScreen (net.minecraft.client.gui.screen.ingame.ContainerScreen)1 CraftingTableScreen (net.minecraft.client.gui.screen.ingame.CraftingTableScreen)1 ClientPlayerEntity (net.minecraft.client.network.ClientPlayerEntity)1 CustomPayloadS2CPacket (net.minecraft.client.network.packet.CustomPayloadS2CPacket)1 Slot (net.minecraft.container.Slot)1 PlayerInventory (net.minecraft.entity.player.PlayerInventory)1 PacketByteBuf (net.minecraft.util.PacketByteBuf)1