Search in sources :

Example 1 with QIOItemViewerContainer

use of mekanism.common.inventory.container.QIOItemViewerContainer in project Mekanism by mekanism.

the class PacketQIOFillCraftingWindow method handle.

@Override
public void handle(NetworkEvent.Context context) {
    ServerPlayerEntity player = context.getSender();
    if (player != null && player.containerMenu instanceof QIOItemViewerContainer) {
        QIOItemViewerContainer container = (QIOItemViewerContainer) player.containerMenu;
        byte selectedCraftingGrid = container.getSelectedCraftingGrid(player.getUUID());
        if (selectedCraftingGrid == -1) {
            Mekanism.logger.warn("Received transfer request from: {}, but they do not currently have a crafting window open.", player);
        } else {
            Optional<? extends IRecipe<?>> optionalRecipe = player.level.getRecipeManager().byKey(recipeID);
            if (optionalRecipe.isPresent()) {
                IRecipe<?> recipe = optionalRecipe.get();
                if (recipe instanceof ICraftingRecipe) {
                    QIOServerCraftingTransferHandler.tryTransfer(container, selectedCraftingGrid, player, recipeID, (ICraftingRecipe) recipe, sources);
                } else {
                    Mekanism.logger.warn("Received transfer request from: {}, but the type ({}) of the specified recipe was not a crafting recipe.", player, recipe.getClass());
                }
            } else {
                Mekanism.logger.warn("Received transfer request from: {}, but could not find specified recipe.", player);
            }
        }
    }
}
Also used : QIOItemViewerContainer(mekanism.common.inventory.container.QIOItemViewerContainer) ICraftingRecipe(net.minecraft.item.crafting.ICraftingRecipe) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity)

Example 2 with QIOItemViewerContainer

use of mekanism.common.inventory.container.QIOItemViewerContainer in project Mekanism by mekanism.

the class PacketGuiItemDataRequest method handle.

@Override
public void handle(NetworkEvent.Context context) {
    ServerPlayerEntity player = context.getSender();
    if (player != null) {
        if (type == Type.QIO_ITEM_VIEWER) {
            if (player.containerMenu instanceof QIOItemViewerContainer) {
                QIOItemViewerContainer container = (QIOItemViewerContainer) player.containerMenu;
                QIOFrequency freq = container.getFrequency();
                if (!player.level.isClientSide() && freq != null) {
                    freq.openItemViewer(player);
                }
            }
        }
    }
}
Also used : QIOItemViewerContainer(mekanism.common.inventory.container.QIOItemViewerContainer) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) QIOFrequency(mekanism.common.content.qio.QIOFrequency)

Example 3 with QIOItemViewerContainer

use of mekanism.common.inventory.container.QIOItemViewerContainer in project Mekanism by mekanism.

the class QIOFrequency method tick.

@Override
public void tick() {
    super.tick();
    if (!uuidsToInvalidate.isEmpty()) {
        // If we have uuids we need to invalidate the Item UUID pairing of them
        for (UUID uuidToInvalidate : uuidsToInvalidate) {
            itemTypeLookup.inverse().remove(uuidToInvalidate);
        }
        uuidsToInvalidate.clear();
    }
    if (!updatedItems.isEmpty() || needsUpdate) {
        Object2LongMap<UUIDAwareHashedItem> map = new Object2LongOpenHashMap<>();
        updatedItems.forEach(type -> {
            QIOItemTypeData data = itemDataMap.get(type);
            map.put(type, data == null ? 0 : data.count);
        });
        // flush players that somehow didn't send a container close packet
        playersViewingItems.removeIf(player -> !(player.containerMenu instanceof QIOItemViewerContainer));
        playersViewingItems.forEach(player -> Mekanism.packetHandler.sendTo(PacketQIOItemViewerGuiSync.update(map, totalCountCapacity, totalTypeCapacity), player));
        updatedItems.clear();
        needsUpdate = false;
    }
    // is forcibly shut down.
    if (isDirty && rand.nextInt(100) == 0) {
        saveAll();
        isDirty = false;
    }
    if (CommonWorldTickHandler.flushTagAndRecipeCaches) {
        // Note: We only need to clear tags here as the modids cannot change just because a reload happened
        tagLookupMap.clear();
        tagWildcardCache.clear();
        itemDataMap.values().forEach(item -> tagLookupMap.putAll(TagCache.getItemTags(item.itemType.getStack()), item.itemType));
    }
}
Also used : Object2LongOpenHashMap(it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap) QIOItemViewerContainer(mekanism.common.inventory.container.QIOItemViewerContainer) UUID(java.util.UUID) UUIDAwareHashedItem(mekanism.common.lib.inventory.HashedItem.UUIDAwareHashedItem)

Example 4 with QIOItemViewerContainer

use of mekanism.common.inventory.container.QIOItemViewerContainer in project Mekanism by mekanism.

the class PacketQIOItemViewerSlotInteract method handle.

@Override
public void handle(NetworkEvent.Context context) {
    ServerPlayerEntity player = context.getSender();
    if (player != null && player.containerMenu instanceof QIOItemViewerContainer) {
        QIOItemViewerContainer container = (QIOItemViewerContainer) player.containerMenu;
        QIOFrequency freq = container.getFrequency();
        ItemStack curStack = player.inventory.getCarried();
        if (freq != null) {
            if (type == Type.TAKE) {
                ItemStack ret = freq.removeByType(freq.getTypeByUUID(typeUUID), count);
                if (curStack.isEmpty()) {
                    player.inventory.setCarried(ret);
                } else if (InventoryUtils.areItemsStackable(ret, curStack)) {
                    curStack.grow(ret.getCount());
                }
                player.connection.send(new SSetSlotPacket(-1, -1, player.inventory.getCarried()));
            } else if (type == Type.SHIFT_TAKE) {
                HashedItem itemType = freq.getTypeByUUID(typeUUID);
                if (itemType != null) {
                    ItemStack ret = freq.removeByType(itemType, itemType.getStack().getMaxStackSize());
                    ItemStack remainder = container.insertIntoPlayerInventory(player.getUUID(), ret);
                    if (!remainder.isEmpty()) {
                        remainder = freq.addItem(remainder);
                        if (!remainder.isEmpty()) {
                            Mekanism.logger.error("QIO shift-click transfer resulted in lost items ({}). This shouldn't happen!", remainder);
                        }
                    }
                }
            } else if (type == Type.PUT) {
                if (!curStack.isEmpty()) {
                    ItemStack rejects = freq.addItem(StackUtils.size(curStack, Math.min(count, curStack.getCount())));
                    ItemStack newStack = StackUtils.size(curStack, curStack.getCount() - (count - rejects.getCount()));
                    player.inventory.setCarried(newStack);
                }
                player.connection.send(new SSetSlotPacket(-1, -1, player.inventory.getCarried()));
            }
        }
    }
}
Also used : QIOItemViewerContainer(mekanism.common.inventory.container.QIOItemViewerContainer) SSetSlotPacket(net.minecraft.network.play.server.SSetSlotPacket) HashedItem(mekanism.common.lib.inventory.HashedItem) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ItemStack(net.minecraft.item.ItemStack) QIOFrequency(mekanism.common.content.qio.QIOFrequency)

Aggregations

QIOItemViewerContainer (mekanism.common.inventory.container.QIOItemViewerContainer)4 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)3 QIOFrequency (mekanism.common.content.qio.QIOFrequency)2 Object2LongOpenHashMap (it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap)1 UUID (java.util.UUID)1 HashedItem (mekanism.common.lib.inventory.HashedItem)1 UUIDAwareHashedItem (mekanism.common.lib.inventory.HashedItem.UUIDAwareHashedItem)1 ItemStack (net.minecraft.item.ItemStack)1 ICraftingRecipe (net.minecraft.item.crafting.ICraftingRecipe)1 SSetSlotPacket (net.minecraft.network.play.server.SSetSlotPacket)1