package com.dairymoose.modernlife.tileentities; import com.dairymoose.modernlife.blocks.ExtractorBlock; import com.dairymoose.modernlife.core.CustomBlocks; import com.mojang.datafixers.types.Type; import java.util.Iterator; import java.util.List; import java.util.Optional; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.IntStream; import javax.annotation.Nullable; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.world.Container; import net.minecraft.world.ContainerHelper; import net.minecraft.world.WorldlyContainer; import net.minecraft.world.WorldlyContainerHolder; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntitySelector; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.HopperMenu; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.ChestBlock; import net.minecraft.world.level.block.HopperBlock; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.entity.ChestBlockEntity; import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.shapes.BooleanOp; import net.minecraft.world.phys.shapes.Shapes; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.VanillaInventoryCodeHooks; import org.apache.commons.lang3.tuple.Pair; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/ExtractorBlockEntity.class */ public class ExtractorBlockEntity extends RandomizableContainerBlockEntity implements IHorizontalHopper { public static final BlockEntityType EXTRACTOR = BlockEntityType.Builder.of(ExtractorBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_EXTRACTOR.get()}).build((Type) null); private static final Logger LOGGER = LogManager.getLogger(); private NonNullList items; private int cooldownTime; private long tickedGameTime; public ExtractorBlockEntity(BlockPos pos, BlockState state) { super(EXTRACTOR, pos, state); this.items = NonNullList.withSize(5, ItemStack.EMPTY); this.cooldownTime = -1; } public void load(CompoundTag p_230337_2_) { super.load(p_230337_2_); this.items = NonNullList.withSize(getContainerSize(), ItemStack.EMPTY); if (!tryLoadLootTable(p_230337_2_)) { ContainerHelper.loadAllItems(p_230337_2_, this.items); } this.cooldownTime = p_230337_2_.getInt("TransferCooldown"); } protected void saveAdditional(CompoundTag p_189515_1_) { if (!trySaveLootTable(p_189515_1_)) { ContainerHelper.saveAllItems(p_189515_1_, this.items); } p_189515_1_.putInt("TransferCooldown", this.cooldownTime); } public int getContainerSize() { return this.items.size(); } public ItemStack removeItem(int p_70298_1_, int p_70298_2_) { unpackLootTable((Player) null); return ContainerHelper.removeItem(getItems(), p_70298_1_, p_70298_2_); } public void setItem(int p_70299_1_, ItemStack p_70299_2_) { unpackLootTable((Player) null); getItems().set(p_70299_1_, p_70299_2_); if (p_70299_2_.getCount() > getMaxStackSize()) { p_70299_2_.setCount(getMaxStackSize()); } } protected Component m_6820_() { return new TranslatableComponent("container.extractor"); } public static void tick(Level level, BlockPos pos, BlockState state, ExtractorBlockEntity blockEntity) { blockEntity.tick(); } public void tick() { if (this.level != null && !this.level.isClientSide) { this.cooldownTime--; this.tickedGameTime = this.level.getGameTime(); if (!isOnCooldown()) { setCooldown(0); tryMoveItems(() -> { return Boolean.valueOf(suckInItems(this)); }); } } } private boolean tryMoveItems(Supplier p_200109_1_) { if (this.level != null && !this.level.isClientSide && !isOnCooldown() && ((Boolean) getBlockState().getValue(HopperBlock.ENABLED)).booleanValue()) { boolean flag = false; if (!isEmpty()) { flag = ejectItems(); } if (!inventoryFull()) { flag |= p_200109_1_.get().booleanValue(); } if (flag) { setCooldown(8); setChanged(); return true; } return false; } return false; } private boolean inventoryFull() { Iterator it = this.items.iterator(); while (it.hasNext()) { ItemStack itemstack = (ItemStack) it.next(); if (itemstack.isEmpty() || itemstack.getCount() != itemstack.getMaxStackSize()) { return false; } } return true; } @Override // com.dairymoose.modernlife.tileentities.IHorizontalHopper public Level getHopperLevel() { return this.level; } private static Optional> getItemHandler(IHorizontalHopper hopper, Direction hopperFacing) { double x = hopper.getLevelX() + hopperFacing.getStepX(); double y = hopper.getLevelY() + hopperFacing.getStepY(); double z = hopper.getLevelZ() + hopperFacing.getStepZ(); return VanillaInventoryCodeHooks.getItemHandler(hopper.getHopperLevel(), x, y, z, hopperFacing.getOpposite()); } private static boolean isFull(IItemHandler itemHandler) { for (int slot = 0; slot < itemHandler.getSlots(); slot++) { ItemStack stackInSlot = itemHandler.getStackInSlot(slot); if (stackInSlot.isEmpty() || stackInSlot.getCount() < itemHandler.getSlotLimit(slot)) { return false; } } return true; } private static boolean isEmpty(IItemHandler itemHandler) { for (int slot = 0; slot < itemHandler.getSlots(); slot++) { ItemStack stackInSlot = itemHandler.getStackInSlot(slot); if (stackInSlot.getCount() > 0) { return false; } } return true; } public static boolean insertHook(ExtractorBlockEntity hopper) { Direction hopperFacing = hopper.getBlockState().getValue(HopperBlock.FACING).getOpposite(); return ((Boolean) getItemHandler(hopper, hopperFacing).map(destinationResult -> { IItemHandler itemHandler = (IItemHandler) destinationResult.getKey(); Object destination = destinationResult.getValue(); if (isFull(itemHandler)) { return false; } for (int i = 0; i < hopper.getContainerSize(); i++) { if (!hopper.getItem(i).isEmpty()) { ItemStack originalSlotContents = hopper.getItem(i).copy(); ItemStack insertStack = hopper.removeItem(i, 1); ItemStack remainder = putStackInInventoryAllSlots(hopper, destination, itemHandler, insertStack); if (remainder.isEmpty()) { return true; } hopper.setItem(i, originalSlotContents); } } return false; }).orElse(false)).booleanValue(); } private static ItemStack putStackInInventoryAllSlots(BlockEntity source, Object destination, IItemHandler destInventory, ItemStack stack) { for (int slot = 0; slot < destInventory.getSlots() && !stack.isEmpty(); slot++) { stack = insertStack(source, destination, destInventory, stack, slot); } return stack; } private static ItemStack insertStack(BlockEntity source, Object destination, IItemHandler destInventory, ItemStack stack, int slot) { ItemStack itemstack = destInventory.getStackInSlot(slot); if (destInventory.insertItem(slot, stack, true).isEmpty()) { boolean insertedItem = false; boolean inventoryWasEmpty = isEmpty(destInventory); if (itemstack.isEmpty()) { destInventory.insertItem(slot, stack, false); stack = ItemStack.EMPTY; insertedItem = true; } else if (ItemHandlerHelper.canItemStacksStack(itemstack, stack)) { int originalSize = stack.getCount(); stack = destInventory.insertItem(slot, stack, false); insertedItem = originalSize < stack.getCount(); } if (insertedItem && inventoryWasEmpty && (destination instanceof ExtractorBlockEntity)) { ExtractorBlockEntity destinationHopper = (ExtractorBlockEntity) destination; if (!destinationHopper.isOnCustomCooldown()) { int k = 0; if ((source instanceof ExtractorBlockEntity) && destinationHopper.getLastUpdateTime() >= ((ExtractorBlockEntity) source).getLastUpdateTime()) { k = 1; } destinationHopper.setCooldown(8 - k); } } } return stack; } private boolean ejectItems() { if (insertHook(this)) { return true; } Container iinventory = getAttachedContainer(); if (iinventory == null) { return false; } Direction direction = (Direction) getBlockState().getValue(HopperBlock.FACING); if (isFullContainer(iinventory, direction)) { return false; } for (int i = 0; i < getContainerSize(); i++) { if (!getItem(i).isEmpty()) { ItemStack itemstack = getItem(i).copy(); ItemStack itemstack1 = addItem(this, iinventory, removeItem(i, 1), direction); if (itemstack1.isEmpty()) { iinventory.setChanged(); return true; } setItem(i, itemstack); } } return false; } private static IntStream getSlots(Container p_213972_0_, Direction p_213972_1_) { return p_213972_0_ instanceof WorldlyContainer ? IntStream.of(((WorldlyContainer) p_213972_0_).getSlotsForFace(p_213972_1_)) : IntStream.range(0, p_213972_0_.getContainerSize()); } private boolean isFullContainer(Container p_174919_1_, Direction p_174919_2_) { return getSlots(p_174919_1_, p_174919_2_).allMatch(p_213970_1_ -> { ItemStack itemstack = p_174919_1_.getItem(p_213970_1_); return itemstack.getCount() >= itemstack.getMaxStackSize(); }); } private static boolean isEmptyContainer(Container p_174917_0_, Direction p_174917_1_) { return getSlots(p_174917_0_, p_174917_1_).allMatch(p_213973_1_ -> { return p_174917_0_.getItem(p_213973_1_).isEmpty(); }); } @Nullable public static Boolean extractHook(ExtractorBlockEntity dest) { return (Boolean) getItemHandler(dest, dest.getBlockState().getValue(ExtractorBlock.FACING)).map(itemHandlerResult -> { IItemHandler handler = (IItemHandler) itemHandlerResult.getKey(); for (int i = 0; i < handler.getSlots(); i++) { ItemStack extractItem = handler.extractItem(i, 1, true); if (!extractItem.isEmpty()) { for (int j = 0; j < dest.getContainerSize(); j++) { ItemStack destStack = dest.getItem(j); if (dest.canPlaceItem(j, extractItem) && (destStack.isEmpty() || (destStack.getCount() < destStack.getMaxStackSize() && destStack.getCount() < dest.getMaxStackSize() && ItemHandlerHelper.canItemStacksStack(extractItem, destStack)))) { ItemStack extractItem2 = handler.extractItem(i, 1, false); if (destStack.isEmpty()) { dest.setItem(j, extractItem2); } else { destStack.grow(1); dest.setItem(j, destStack); } dest.setChanged(); return true; } } } } return false; }).orElse(null); } public static boolean suckInItems(ExtractorBlockEntity p_145891_0_) { Boolean ret = extractHook(p_145891_0_); if (ret != null) { return ret.booleanValue(); } Container iinventory = getSourceContainer(p_145891_0_, p_145891_0_.getBlockState()); if (iinventory != null) { Direction direction = Direction.DOWN; switch (C01571.$SwitchMap$net$minecraft$core$Direction[p_145891_0_.getBlockState().getValue(HopperBlock.FACING).ordinal()]) { case 1: direction = Direction.WEST; break; case 2: direction = Direction.SOUTH; break; case 3: direction = Direction.NORTH; break; case 4: direction = Direction.EAST; break; } Direction finalDirection = direction; if (isEmptyContainer(iinventory, finalDirection)) { return false; } return getSlots(iinventory, finalDirection).anyMatch(p_213971_3_ -> { return tryTakeInItemFromSlot(p_145891_0_, iinventory, p_213971_3_, finalDirection); }); } for (ItemEntity itementity : getFrontalItems(p_145891_0_, p_145891_0_.getBlockState())) { if (addItem(p_145891_0_, itementity)) { return true; } } return false; } /* renamed from: com.dairymoose.modernlife.tileentities.ExtractorBlockEntity$1 */ /* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/ExtractorBlockEntity$1.class */ public static /* synthetic */ class C01571 { static final /* synthetic */ int[] $SwitchMap$net$minecraft$core$Direction = new int[Direction.values().length]; static { try { $SwitchMap$net$minecraft$core$Direction[Direction.EAST.ordinal()] = 1; } catch (NoSuchFieldError e) { } try { $SwitchMap$net$minecraft$core$Direction[Direction.NORTH.ordinal()] = 2; } catch (NoSuchFieldError e2) { } try { $SwitchMap$net$minecraft$core$Direction[Direction.SOUTH.ordinal()] = 3; } catch (NoSuchFieldError e3) { } try { $SwitchMap$net$minecraft$core$Direction[Direction.WEST.ordinal()] = 4; } catch (NoSuchFieldError e4) { } } } public static boolean tryTakeInItemFromSlot(IHorizontalHopper p_174915_0_, Container p_174915_1_, int p_174915_2_, Direction p_174915_3_) { ItemStack itemstack = p_174915_1_.getItem(p_174915_2_); if (!itemstack.isEmpty() && canTakeItemFromContainer(p_174915_1_, itemstack, p_174915_2_, p_174915_3_)) { ItemStack itemstack1 = itemstack.copy(); ItemStack itemstack2 = addItem(p_174915_1_, p_174915_0_, p_174915_1_.removeItem(p_174915_2_, 1), (Direction) null); if (itemstack2.isEmpty()) { p_174915_1_.setChanged(); return true; } p_174915_1_.setItem(p_174915_2_, itemstack1); return false; } return false; } public static boolean addItem(Container p_200114_0_, ItemEntity p_200114_1_) { boolean flag = false; ItemStack itemstack = p_200114_1_.getItem().copy(); ItemStack itemstack1 = addItem((Container) null, p_200114_0_, itemstack, (Direction) null); if (itemstack1.isEmpty()) { flag = true; p_200114_1_.m_146870_(); } else { p_200114_1_.setItem(itemstack1); } return flag; } public static ItemStack addItem(@Nullable Container p_59327_, Container p_59328_, ItemStack p_59329_, @Nullable Direction p_59330_) { if ((p_59328_ instanceof WorldlyContainer) && p_59330_ != null) { WorldlyContainer worldlycontainer = (WorldlyContainer) p_59328_; int[] aint = worldlycontainer.getSlotsForFace(p_59330_); for (int k = 0; k < aint.length && !p_59329_.isEmpty(); k++) { p_59329_ = tryMoveInItem(p_59327_, p_59328_, p_59329_, aint[k], p_59330_); } } else { int i = p_59328_.getContainerSize(); for (int j = 0; j < i && !p_59329_.isEmpty(); j++) { p_59329_ = tryMoveInItem(p_59327_, p_59328_, p_59329_, j, p_59330_); } } return p_59329_; } private static boolean canPlaceItemInContainer(Container p_174920_0_, ItemStack p_174920_1_, int p_174920_2_, @Nullable Direction p_174920_3_) { if (p_174920_0_.canPlaceItem(p_174920_2_, p_174920_1_)) { return !(p_174920_0_ instanceof WorldlyContainer) || ((WorldlyContainer) p_174920_0_).canPlaceItemThroughFace(p_174920_2_, p_174920_1_, p_174920_3_); } return false; } private static boolean canTakeItemFromContainer(Container p_174921_0_, ItemStack p_174921_1_, int p_174921_2_, Direction p_174921_3_) { return !(p_174921_0_ instanceof WorldlyContainer) || ((WorldlyContainer) p_174921_0_).canTakeItemThroughFace(p_174921_2_, p_174921_1_, p_174921_3_); } private static ItemStack tryMoveInItem(@Nullable Container p_174916_0_, Container p_174916_1_, ItemStack p_174916_2_, int p_174916_3_, @Nullable Direction p_174916_4_) { ItemStack itemstack = p_174916_1_.getItem(p_174916_3_); if (canPlaceItemInContainer(p_174916_1_, p_174916_2_, p_174916_3_, p_174916_4_)) { boolean flag = false; boolean flag1 = p_174916_1_.isEmpty(); if (itemstack.isEmpty()) { p_174916_1_.setItem(p_174916_3_, p_174916_2_); p_174916_2_ = ItemStack.EMPTY; flag = true; } else if (canMergeItems(itemstack, p_174916_2_)) { int i = p_174916_2_.getMaxStackSize() - itemstack.getCount(); int j = Math.min(p_174916_2_.getCount(), i); p_174916_2_.shrink(j); itemstack.grow(j); flag = j > 0; } if (flag) { if (flag1 && (p_174916_1_ instanceof ExtractorBlockEntity)) { ExtractorBlockEntity ExtractorBlockEntity1 = (ExtractorBlockEntity) p_174916_1_; if (!ExtractorBlockEntity1.isOnCustomCooldown()) { int k = 0; if (p_174916_0_ instanceof ExtractorBlockEntity) { ExtractorBlockEntity ExtractorBlockEntity = (ExtractorBlockEntity) p_174916_0_; if (ExtractorBlockEntity1.tickedGameTime >= ExtractorBlockEntity.tickedGameTime) { k = 1; } } ExtractorBlockEntity1.setCooldown(8 - k); } } p_174916_1_.setChanged(); } } return p_174916_2_; } @Nullable private Container getAttachedContainer() { Direction direction = getBlockState().getValue(HopperBlock.FACING).getOpposite(); return getContainerAt(getHopperLevel(), this.worldPosition.relative(direction)); } @Nullable public static Container getSourceContainer(IHorizontalHopper p_145884_0_, BlockState state) { switch (C01571.$SwitchMap$net$minecraft$core$Direction[state.getValue(HopperBlock.FACING).ordinal()]) { case 1: return getContainerAt(p_145884_0_.getHopperLevel(), p_145884_0_.getLevelX() + 1.0d, p_145884_0_.getLevelY(), p_145884_0_.getLevelZ()); case 2: return getContainerAt(p_145884_0_.getHopperLevel(), p_145884_0_.getLevelX(), p_145884_0_.getLevelY(), p_145884_0_.getLevelZ() - 1.0d); case 3: return getContainerAt(p_145884_0_.getHopperLevel(), p_145884_0_.getLevelX(), p_145884_0_.getLevelY(), p_145884_0_.getLevelZ() + 1.0d); case 4: return getContainerAt(p_145884_0_.getHopperLevel(), p_145884_0_.getLevelX() - 1.0d, p_145884_0_.getLevelY(), p_145884_0_.getLevelZ()); default: return getContainerAt(p_145884_0_.getHopperLevel(), p_145884_0_.getLevelX(), p_145884_0_.getLevelY() + 1.0d, p_145884_0_.getLevelZ()); } } public static List getFrontalItems(IHorizontalHopper p_200115_0_, BlockState state) { return (List) p_200115_0_.getSuckShape(state).toAabbs().stream().flatMap(p_200110_1_ -> { return p_200115_0_.getHopperLevel().m_6443_(ItemEntity.class, p_200110_1_.move(p_200115_0_.getLevelX() - 0.5d, p_200115_0_.getLevelY() - 0.5d, p_200115_0_.getLevelZ() - 0.5d), EntitySelector.ENTITY_STILL_ALIVE).stream(); }).collect(Collectors.toList()); } @Nullable public static Container getContainerAt(Level p_195484_0_, BlockPos p_195484_1_) { return getContainerAt(p_195484_0_, p_195484_1_.getX() + 0.5d, p_195484_1_.getY() + 0.5d, p_195484_1_.getZ() + 0.5d); } @Nullable public static Container getContainerAt(Level p_145893_0_, double p_145893_1_, double p_145893_3_, double p_145893_5_) { WorldlyContainer worldlyContainer = null; BlockPos blockpos = new BlockPos(p_145893_1_, p_145893_3_, p_145893_5_); BlockState blockstate = p_145893_0_.getBlockState(blockpos); WorldlyContainerHolder getBlock = blockstate.getBlock(); if (getBlock instanceof WorldlyContainerHolder) { worldlyContainer = getBlock.getContainer(blockstate, p_145893_0_, blockpos); } else if (blockstate.m_155947_()) { WorldlyContainer blockEntity = p_145893_0_.getBlockEntity(blockpos); if (blockEntity instanceof Container) { worldlyContainer = (Container) blockEntity; if ((worldlyContainer instanceof ChestBlockEntity) && (getBlock instanceof ChestBlock)) { worldlyContainer = ChestBlock.getContainer((ChestBlock) getBlock, blockstate, p_145893_0_, blockpos, true); } } } if (worldlyContainer == null) { List list = p_145893_0_.getEntities((Entity) null, new AABB(p_145893_1_ - 0.5d, p_145893_3_ - 0.5d, p_145893_5_ - 0.5d, p_145893_1_ + 0.5d, p_145893_3_ + 0.5d, p_145893_5_ + 0.5d), EntitySelector.CONTAINER_ENTITY_SELECTOR); if (!list.isEmpty()) { worldlyContainer = (Container) list.get(p_145893_0_.random.nextInt(list.size())); } } return worldlyContainer; } private static boolean canMergeItems(ItemStack p_145894_0_, ItemStack p_145894_1_) { if (p_145894_0_.getItem() != p_145894_1_.getItem() || p_145894_0_.getDamageValue() != p_145894_1_.getDamageValue() || p_145894_0_.getCount() > p_145894_0_.getMaxStackSize()) { return false; } return ItemStack.tagMatches(p_145894_0_, p_145894_1_); } @Override // com.dairymoose.modernlife.tileentities.IHorizontalHopper public double getLevelX() { return this.worldPosition.getX() + 0.5d; } @Override // com.dairymoose.modernlife.tileentities.IHorizontalHopper public double getLevelY() { return this.worldPosition.getY() + 0.5d; } @Override // com.dairymoose.modernlife.tileentities.IHorizontalHopper public double getLevelZ() { return this.worldPosition.getZ() + 0.5d; } public void setCooldown(int p_145896_1_) { this.cooldownTime = p_145896_1_; } private boolean isOnCooldown() { return this.cooldownTime > 0; } public boolean isOnCustomCooldown() { return this.cooldownTime > 8; } protected NonNullList getItems() { return this.items; } protected void setItems(NonNullList p_199721_1_) { this.items = p_199721_1_; } public void entityInside(Entity p_200113_1_) { if (p_200113_1_ instanceof ItemEntity) { BlockPos blockpos = getBlockPos(); if (Shapes.joinIsNotEmpty(Shapes.create(p_200113_1_.getBoundingBox().move(-blockpos.getX(), -blockpos.getY(), -blockpos.getZ())), getSuckShape(getBlockState()), BooleanOp.AND)) { tryMoveItems(() -> { return Boolean.valueOf(addItem(this, (ItemEntity) p_200113_1_)); }); } } } protected AbstractContainerMenu m_6555_(int p_213906_1_, Inventory p_213906_2_) { return new HopperMenu(p_213906_1_, p_213906_2_, this); } protected IItemHandler createUnSidedHandler() { return new ExtractorItemHandler(this); } public long getLastUpdateTime() { return this.tickedGameTime; } }