/** * A simple {@code SidedInventory} implementation with only default methods + an item list getter. * * <h2>Reading and writing to tags</h2> * Use {@link Inventories#writeNbt(NbtCompound, DefaultedList, RegistryWrapper.WrapperLookup)} and {@link Inventories#readNbt(NbtCompound, DefaultedList, RegistryWrapper.WrapperLookup)} * on {@linkplain #getItems() the item list}. * * License: <a href="https://creativecommons.org/publicdomain/zero/1.0/">CC0</a> * @author Juuz */ @FunctionalInterface publicinterfaceImplementedInventoryextendsSidedInventory { // 这个接口的作用是实现一个简单的SidedInventory,只有默认方法和一个获取物品列表的方法,便于我们在BlockEntity中使用 /** * Gets the item list of this inventory. * Must return the same instance every time it's called. * * @return the item list */ DefaultedList<ItemStack> getItems();
/** * Creates an inventory from the item list. * * @param items the item list * @return a new inventory */ static ImplementedInventory of(DefaultedList<ItemStack> items) { return () -> items; }
/** * Creates a new inventory with the size. * * @param size the inventory size * @return a new inventory */ static ImplementedInventory ofSize(int size) { return of(DefaultedList.ofSize(size, ItemStack.EMPTY)); }
// SidedInventory
/** * Gets the available slots to automation on the side. * * <p>The default implementation returns an array of all slots. * * @param side the side * @return the available slots */ @Override defaultint[] getAvailableSlots(Direction side) { int[] result = newint[getItems().size()]; for (inti=0; i < result.length; i++) { result[i] = i; }
return result; }
/** * Returns true if the stack can be inserted in the slot at the side. * * <p>The default implementation returns true. * * @param slot the slot * @param stack the stack * @param side the side * @return true if the stack can be inserted */ @Override defaultbooleancanInsert(int slot, ItemStack stack, @Nullable Direction side) { returntrue; }
/** * Returns true if the stack can be extracted from the slot at the side. * * <p>The default implementation returns true. * * @param slot the slot * @param stack the stack * @param side the side * @return true if the stack can be extracted */ @Override defaultbooleancanExtract(int slot, ItemStack stack, Direction side) { returntrue; }
// Inventory
/** * Returns the inventory size. * * <p>The default implementation returns the size of {@link #getItems()}. * * @return the inventory size */ @Override defaultintsize() { return getItems().size(); }
/** * @return true if this inventory has only empty stacks, false otherwise */ @Override defaultbooleanisEmpty() { for (inti=0; i < size(); i++) { ItemStackstack= getStack(i); if (!stack.isEmpty()) { returnfalse; } }
returntrue; }
/** * Gets the item in the slot. * * @param slot the slot * @return the item in the slot */ @Override default ItemStack getStack(int slot) { return getItems().get(slot); }
/** * Takes a stack of the size from the slot. * * <p>(default implementation) If there are less items in the slot than what are requested, * takes all items in that slot. * * @param slot the slot * @param count the item count * @return a stack */ @Override default ItemStack removeStack(int slot, int count) { ItemStackresult= Inventories.splitStack(getItems(), slot, count); if (!result.isEmpty()) { markDirty(); }
return result; }
/** * Removes the current stack in the {@code slot} and returns it. * * <p>The default implementation uses {@link Inventories#removeStack(List, int)} * * @param slot the slot * @return the removed stack */ @Override default ItemStack removeStack(int slot) { return Inventories.removeStack(getItems(), slot); }
/** * Replaces the current stack in the {@code slot} with the provided stack. * * <p>If the stack is too big for this inventory ({@link Inventory#getMaxCountPerStack()}), * it gets resized to this inventory's maximum amount. * * @param slot the slot * @param stack the stack */ @Override defaultvoidsetStack(int slot, ItemStack stack) { getItems().set(slot, stack); if (stack.getCount() > getMaxCountPerStack()) { stack.setCount(getMaxCountPerStack()); } markDirty(); }
/* * This file is part of RebornCore, licensed under the MIT License (MIT). * * Copyright (c) 2024 TeamReborn * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */
if (currentScreenHandler == null) { returnfalse; } if (!screenHandlerPredicate.test(currentScreenHandler)) { returnfalse; } return currentScreenHandler.canUse(player); }
default <T extendsBlockEntity> T getBlockEntity(BlockEntityType<T> type, PlayerEntity player) { if (!isWithinDistance(player, 64)) { thrownewIllegalStateException("Player cannot use this block entity as its too far away"); } BlockEntityblockEntity= getBlockEntity(player); if (type != blockEntity.getType()) { thrownewIllegalStateException("Block entity is not of the correct type. Expected: " + Registries.BLOCK_ENTITY_TYPE.getId(type) + " but got: " + Registries.BLOCK_ENTITY_TYPE.getId(blockEntity.getType())); } return (T) blockEntity; }
default <T extendsBlockEntity> T getBlockEntity(Class<T> baseClass, PlayerEntity player) { if (!isWithinDistance(player, 64)) { thrownewIllegalStateException("Player cannot use this block entity as its too far away"); }
BlockEntityblockEntity= getBlockEntity(player);
if (!baseClass.isInstance(blockEntity)) { thrownewIllegalStateException("Block entity is not of the correct class"); }
default BlockEntity getBlockEntity(PlayerEntity player) { if (!isWithinDistance(player, 64)) { thrownewIllegalStateException("Player cannot use this block entity as its too far away"); }
privatevoidrenderProgressArrow(DrawContext context, int x, int y) { if (handler.isCrafting() && handler.isRaining()) { context.drawTexture(TEXTURE, x + 85, y + 30, 176, 0, 8, handler.getScaledProgress()); } }
privatevoidaddPlayerHotbar(PlayerInventory playerInventory) { for (inti=0; i < 9; ++i) { this.addSlot(newSlot(playerInventory, i, 8 + i * 18, 142)); } }
privatevoidaddPlayerInventory(PlayerInventory playerInventory) { for (inti=0; i < 3; ++i) { for (intj=0; j < 9; ++j) { this.addSlot(newSlot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } }