下面列出了怎么用net.minecraft.inventory.Inventory的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean matches(Inventory inv, World world_1) {
int invWidth = 3;
int invHeight = 3;
for (int x = 0; x <= invWidth - this.width; ++x) {
for (int y = 0; y <= invHeight - this.height; ++y) {
if (this.matchesSmall(inv, x, y, true)) {
return true;
}
if (this.matchesSmall(inv, x, y, false)) {
return true;
}
}
}
return false;
}
private boolean matchesSmall(Inventory inv, int int_1, int int_2, boolean boolean_1) {
int invWidth = 3;
int invHeight = 3;
for (int int_3 = 0; int_3 < invWidth; ++int_3) {
for (int int_4 = 0; int_4 < invHeight; ++int_4) {
int int_5 = int_3 - int_1;
int int_6 = int_4 - int_2;
Ingredient ingredient_1 = Ingredient.EMPTY;
if (int_5 >= 0 && int_6 >= 0 && int_5 < this.width && int_6 < this.height) {
if (boolean_1) {
ingredient_1 = this.ingredients.get(this.width - int_5 - 1 + int_6 * this.width);
} else {
ingredient_1 = this.ingredients.get(int_5 + int_6 * this.width);
}
}
if (!ingredient_1.test(inv.getStack(int_3 + int_4 * invWidth))) {
return false;
}
}
}
return true;
}
public OxygenCollectorScreenHandler(int syncId, PlayerEntity playerEntity, OxygenCollectorBlockEntity blockEntity) {
super(syncId, playerEntity, blockEntity, GalacticraftScreenHandlerTypes.OXYGEN_COLLECTOR_HANDLER);
Inventory inventory = blockEntity.getInventory().asInventory();
addProperty(status);
addProperty(oxygen);
addProperty(lastCollectAmount);
// Charging slot
this.addSlot(new ChargeSlot(inventory, 0, 20, 70));
// Player inventory slots
int playerInvYOffset = 99;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlot(new Slot(playerEntity.inventory, j + i * 9 + 9, 8 + j * 18, playerInvYOffset + i * 18));
}
}
// Hotbar slots
for (int i = 0; i < 9; ++i) {
this.addSlot(new Slot(playerEntity.inventory, i, 8 + i * 18, playerInvYOffset + 58));
}
}
public CoalGeneratorScreenHandler(int syncId, PlayerEntity playerEntity, CoalGeneratorBlockEntity generator) {
super(syncId, playerEntity, generator, GalacticraftScreenHandlerTypes.COAL_GENERATOR_HANDLER);
Inventory inventory = blockEntity.getInventory().asInventory();
addProperty(status);
// Coal Generator fuel slot
this.addSlot(new ItemSpecificSlot(inventory, 0, 8, 72, fuel));
this.addSlot(new ChargeSlot(inventory, 1, 8, 8));
// Player inventory slots
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlot(new Slot(playerEntity.inventory, j + i * 9 + 9, 8 + j * 18, 94 + i * 18));
}
}
// Hotbar slots
for (int i = 0; i < 9; ++i) {
this.addSlot(new Slot(playerEntity.inventory, i, 8 + i * 18, 152));
}
}
public BasicSolarPanelScreenHandler(int syncId, PlayerEntity playerEntity, BasicSolarPanelBlockEntity blockEntity) {
super(syncId, playerEntity, blockEntity, GalacticraftScreenHandlerTypes.BASIC_SOLAR_PANEL_HANDLER);
Inventory inventory = blockEntity.getInventory().asInventory();
addProperty(status);
this.addSlot(new ChargeSlot(inventory, 0, 8, 53));
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlot(new Slot(playerEntity.inventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}
for (int i = 0; i < 9; ++i) {
this.addSlot(new Slot(playerEntity.inventory, i, 8 + i * 18, 142));
}
}
public EnergyStorageModuleScreenHandler(int syncId, PlayerEntity playerEntity, EnergyStorageModuleBlockEntity blockEntity) {
super(syncId, playerEntity, blockEntity, GalacticraftScreenHandlerTypes.ENERGY_STORAGE_MODULE_HANDLER);
Inventory inventory = blockEntity.getInventory().asInventory();
// Battery slots
this.addSlot(new ChargeSlot(inventory, 0, 18 * 6 - 6, 18 + 6));
this.addSlot(new ChargeSlot(inventory, 1, 18 * 6 - 6, 18 * 2 + 12));
// Player inventory slots
int playerInvYOffset = 84;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlot(new Slot(playerEntity.inventory, j + i * 9 + 9, 8 + j * 18, playerInvYOffset + i * 18));
}
}
// Hotbar slots
for (int i = 0; i < 9; ++i) {
this.addSlot(new Slot(playerEntity.inventory, i, 8 + i * 18, playerInvYOffset + 58));
}
}
public <X> Mono<X> sbx$getComponent(WorldReader reader, Position position, BlockState state, Component<X> component, Mono<Direction> side) {
if (component == Components.INVENTORY_COMPONENT) {
if (this instanceof InventoryProvider) {
SidedInventory inventory = ((InventoryProvider) this).getInventory((net.minecraft.block.BlockState) state, (IWorld) reader, (BlockPos) position);
if (side.isPresent())
return Mono.of(new SidedRespective(inventory, side.get())).cast();
return Mono.of(new V2SInventory(inventory)).cast();
}
net.minecraft.block.entity.BlockEntity entity = ((BlockView) reader).getBlockEntity((BlockPos) position);
if (entity instanceof Inventory) {
if (side.isPresent() && entity instanceof SidedInventory)
return Mono.of(new SidedRespective((SidedInventory) entity, side.get())).cast();
return Mono.of(new V2SInventory((Inventory) entity)).cast();
}
}
if (component == Components.FLUID_COMPONENT) {
if (this instanceof Waterloggable) {
FluidLoggingContainer container = new FluidLoggingContainer((FluidLoggable) this, reader, position, state, side);
return Mono.of(container).cast();
}
}
return Mono.empty();
}
private static int setBlock(
ServerWorld world, BlockPos.Mutable mbpos, int x, int y, int z,
BlockStateArgument block, Predicate<CachedBlockPosition> replacement,
List<BlockPos> list
)
{
mbpos.set(x, y, z);
int success=0;
if (replacement == null || replacement.test(new CachedBlockPosition(world, mbpos, true)))
{
BlockEntity tileentity = world.getBlockEntity(mbpos);
if (tileentity instanceof Inventory)
{
((Inventory) tileentity).clear();
}
if (block.setBlockState(world, mbpos, 2))
{
list.add(mbpos.toImmutable());
++success;
}
}
return success;
}
public ItemStack getResultFromRecipeStack(Inventory inv) {
// Once this method has been called, we have verified that either a shapeless or shaped recipe is present with isValidRecipe. Ignore the warning on getShapedRecipe(inv).get().
Optional<ShapelessCompressingRecipe> shapelessRecipe = getShapelessRecipe(inv);
if (shapelessRecipe.isPresent()) {
return shapelessRecipe.get().craft(inv);
}
return getShapedRecipe(inv).orElseThrow(() -> new IllegalStateException("Neither a shapeless recipe or shaped recipe was present when getResultFromRecipeStack was called. This should never happen, as isValidRecipe should have been called first. That would have prevented this.")).craft(inv);
}
private ItemStack getResultFromRecipeStack(Inventory inv) {
// Once this method has been called, we have verified that either a shapeless or shaped recipe is present with isValidRecipe. Ignore the warning on getShapedRecipe(inv).get().
Optional<ShapelessCompressingRecipe> shapelessRecipe = getShapelessRecipe(inv);
if (shapelessRecipe.isPresent()) {
return shapelessRecipe.get().craft(inv);
}
return getShapedRecipe(inv).orElseThrow(() -> new IllegalStateException("Neither a shapeless recipe or shaped recipe was present when getResultFromRecipeStack was called. This should never happen, as isValidRecipe should have been called first. That would have prevented this.")).craft(inv);
}
@Override
public boolean matches(Inventory inv, World world_1) {
RecipeFinder recipeFinder_1 = new RecipeFinder();
int int_1 = 0;
for (int int_2 = 0; int_2 < inv.size(); ++int_2) {
ItemStack itemStack_1 = inv.getStack(int_2);
if (!itemStack_1.isEmpty()) {
++int_1;
recipeFinder_1.addItem(itemStack_1);
}
}
return int_1 == this.input.size() && recipeFinder_1.findRecipe(this, null);
}
public CircuitFabricatorScreenHandler(int syncId, PlayerEntity playerEntity, CircuitFabricatorBlockEntity blockEntity) {
super(syncId, playerEntity, blockEntity, GalacticraftScreenHandlerTypes.CIRCUIT_FABRICATOR_HANDLER);
addProperty(progress);
addProperty(status);
Inventory inventory = blockEntity.getInventory().asInventory();
// Energy slot
this.addSlot(new ChargeSlot(inventory, 0, 8, 79));
this.addSlot(new ItemSpecificSlot(inventory, 1, 8, 15, Items.DIAMOND));
this.addSlot(new ItemSpecificSlot(inventory, 2, 8 + (18 * 3), 79, GalacticraftItems.RAW_SILICON));
this.addSlot(new ItemSpecificSlot(inventory, 3, 8 + (18 * 3), 79 - 18, GalacticraftItems.RAW_SILICON));
this.addSlot(new ItemSpecificSlot(inventory, 4, 8 + (18 * 6), 79 - 18, Items.REDSTONE));
this.addSlot(new ItemSpecificSlot(inventory, 5, 8 + (18 * 7), 15, materials));
this.addSlot(new FurnaceOutputSlot(playerEntity, inventory, 6, 8 + (18 * 8), 79));
// Player inventory slots
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlot(new Slot(playerEntity.inventory, j + i * 9 + 9, 8 + j * 18, 110 + i * 18));
}
}
// Hotbar slots
for (int i = 0; i < 9; ++i) {
this.addSlot(new Slot(playerEntity.inventory, i, 8 + i * 18, 168));
}
}
private boolean isPlayerWearingCostume(PlayerEntity player) {
for (int i = 0; i < 4; i++) {
if (HallowedTags.Items.COSTUMES.contains(player.inventory.armor.get(i).getItem()))
return true;
}
Inventory inv = TrinketsApi.getTrinketsInventory(player);
for (int i = 0; i < inv.getInvSize(); i++) {
if (HallowedTags.Items.COSTUMES.contains(inv.getInvStack(i).getItem())) return true;
}
return false;
}
private Inventory getOutputInventory() {
Vec3d offsetToInventory = getBlockBelowCartOffset();
//The visual rotation point of the minecart is roughly 0.5 above its feet (determined visually ingame)
//Search 0.5 Blocks below the feet for an inventory
Inventory inv = HopperBlockEntity.getInventoryAt(this.world, this.getX() + offsetToInventory.x, this.getY() + 0.5 + offsetToInventory.y, this.getZ() + offsetToInventory.z);
//There is probably a way nicer way to determine the access side of the target inventory
if(inv instanceof BlockEntity){
BlockPos pos = ((BlockEntity) inv).getPos();
if(pos.getY() < MathHelper.floor(this.getY()))
outputDirection = Direction.DOWN;
else if(pos.getX() > MathHelper.floor(this.getX()))
outputDirection = Direction.EAST;
else if(pos.getX() < MathHelper.floor(this.getX()))
outputDirection = Direction.WEST;
else if(pos.getZ() > MathHelper.floor(this.getZ()))
outputDirection = Direction.SOUTH;
else if(pos.getZ() < MathHelper.floor(this.getZ()))
outputDirection = Direction.NORTH;
else outputDirection = Direction.DOWN;
}else
outputDirection = Direction.DOWN;
return inv;
}
private boolean insert(){
if(!this.isInvEmpty()){
Inventory inventory_1 = this.getOutputInventory();
if (inventory_1 == null) {
return false;
} else {
Direction direction_1 = getLastOutputDirection().getOpposite();
if (this.isInventoryFull(inventory_1, direction_1)) {
return false;
} else {
for(int int_1 = 0; int_1 < this.getInvSize(); ++int_1) {
if (!this.getInvStack(int_1).isEmpty()) {
ItemStack itemStack_1 = this.getInvStack(int_1).copy();
ItemStack itemStack_2 = transfer(this, inventory_1, this.takeInvStack(int_1, 1), direction_1);
if (itemStack_2.isEmpty()) {
inventory_1.markDirty();
return true;
}
this.setInvStack(int_1, itemStack_1);
}
}
return false;
}
}
}
return false;
}
public SyncedGuiDescription(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory, Inventory blockInventory, PropertyDelegate propertyDelegate) {
super(type, syncId);
this.blockInventory = blockInventory;
this.playerInventory = playerInventory;
this.world = playerInventory.player.world;
this.propertyDelegate = propertyDelegate;
if (propertyDelegate!=null && propertyDelegate.size()>0) this.addProperties(propertyDelegate);
}
public WItemSlot(Inventory inventory, int startIndex, int slotsWide, int slotsHigh, boolean big) {
this.inventory = inventory;
this.startIndex = startIndex;
this.slotsWide = slotsWide;
this.slotsHigh = slotsHigh;
this.big = big;
//this.ltr = ltr;
}
public static WItemSlot of(Inventory inventory, int index) {
WItemSlot w = new WItemSlot();
w.inventory = inventory;
w.startIndex = index;
return w;
}
public static WItemSlot of(Inventory inventory, int startIndex, int slotsWide, int slotsHigh) {
WItemSlot w = new WItemSlot();
w.inventory = inventory;
w.startIndex = startIndex;
w.slotsWide = slotsWide;
w.slotsHigh = slotsHigh;
return w;
}
public static WItemSlot outputOf(Inventory inventory, int index) {
WItemSlot w = new WItemSlot();
w.inventory = inventory;
w.startIndex = index;
w.big = true;
return w;
}
/**
* Creates a 9x3 slot widget from the "main" part of a player inventory.
*
* @param inventory the player inventory
* @return the created slot widget
* @see WPlayerInvPanel
*/
public static WItemSlot ofPlayerStorage(Inventory inventory) {
WItemSlot w = new WItemSlot();
w.inventory = inventory;
w.startIndex = 9;
w.slotsWide = 9;
w.slotsHigh = 3;
//w.ltr = false;
return w;
}
@Override
public void onCommand(String command, String[] args) throws Exception {
ItemStack item = mc.player.inventory.getCurrentItem();
if (!(item.getItem() instanceof BlockItem)) {
BleachLogger.errorMessage("Must be holding a containter to peek.");
return;
}
if (!(((BlockItem) item.getItem()).getBlock() instanceof ContainerBlock)) {
BleachLogger.errorMessage("Must be holding a containter to peek.");
return;
}
NonNullList<ItemStack> items = NonNullList.withSize(27, new ItemStack(Items.AIR));
CompoundNBT nbt = item.getTag();
if (nbt != null && nbt.contains("BlockEntityTag")) {
CompoundNBT itemnbt = nbt.getCompound("BlockEntityTag");
if (itemnbt.contains("Items")) ItemStackHelper.loadAllItems(itemnbt, items);
}
Inventory inv = new Inventory(items.toArray(new ItemStack[27]));
BleachQueue.queue.add(() -> {
mc.displayGuiScreen(new ShulkerBoxScreen(
new ShulkerBoxContainer(420, mc.player.inventory, inv),
mc.player.inventory,
item.getDisplayName()));
});
}
public static int validateSlot(int slot, Inventory inv)
{
int invSize = inv.getInvSize();
if (slot < 0)
slot = invSize + slot;
if (slot < 0 || slot >= invSize)
return inv.getInvSize(); // outside of inventory
return slot;
}
InventoryLocator(Object owner, BlockPos pos, Inventory i, int o, boolean isEnder)
{
this.owner = owner;
position = pos;
inventory = i;
offset = o;
this.isEnder = isEnder;
}
public void tick() {
if (disabled() || world.isClient) {
if (disabled()) {
idleEnergyDecrement(true);
}
return;
}
Inventory inv = new InventoryWrapper() {
@Override
public InventoryComponent getComponent() {
return getInventory();
}
@Override
public int size() {
return 9;
}
};
attemptChargeFromStack(FUEL_INPUT_SLOT);
if (getCapacitatorComponent().getCurrentEnergy() < 1) {
status = ElectricCompressorStatus.IDLE;
} else if (isValidRecipe(inv) && canPutStackInResultSlot(getResultFromRecipeStack(inv))) {
status = ElectricCompressorStatus.PROCESSING;
} else {
status = ElectricCompressorStatus.IDLE;
}
if (status == ElectricCompressorStatus.PROCESSING) {
ItemStack resultStack = getResultFromRecipeStack(inv);
this.getCapacitatorComponent().extractEnergy(GalacticraftEnergy.GALACTICRAFT_JOULES, getEnergyUsagePerTick(), ActionType.PERFORM);
this.progress++;
if (this.progress % 40 == 0 && this.progress > maxProgress / 2) {
this.world.playSound(null, this.getPos(), SoundEvents.BLOCK_ANVIL_LAND, SoundCategory.BLOCKS, 0.3F, this.world.random.nextFloat() * 0.1F + 0.9F);
}
if (this.progress == maxProgress) {
this.progress = 0;
craftItem(resultStack);
}
} else if (status == ElectricCompressorStatus.IDLE) {
if (progress > 0) {
progress--;
}
} else {
idleEnergyDecrement(false);
if (progress > 0) {
progress--;
}
}
trySpreadEnergy();
}
private Optional<ShapelessCompressingRecipe> getShapelessRecipe(Inventory input) {
return this.world.getRecipeManager().getFirstMatch(GalacticraftRecipes.SHAPELESS_COMPRESSING_TYPE, input, this.world);
}
private Optional<ShapedCompressingRecipe> getShapedRecipe(Inventory input) {
return this.world.getRecipeManager().getFirstMatch(GalacticraftRecipes.SHAPED_COMPRESSING_TYPE, input, this.world);
}
public boolean isValidRecipe(Inventory input) {
Optional<ShapelessCompressingRecipe> shapelessRecipe = getShapelessRecipe(input);
Optional<ShapedCompressingRecipe> shapedRecipe = getShapedRecipe(input);
return shapelessRecipe.isPresent() || shapedRecipe.isPresent();
}
private Optional<ShapelessCompressingRecipe> getShapelessRecipe(Inventory input) {
return this.world.getRecipeManager().getFirstMatch(GalacticraftRecipes.SHAPELESS_COMPRESSING_TYPE, input, this.world);
}
private Optional<ShapedCompressingRecipe> getShapedRecipe(Inventory input) {
return this.world.getRecipeManager().getFirstMatch(GalacticraftRecipes.SHAPED_COMPRESSING_TYPE, input, this.world);
}