|
| 1 | +package com.gregtechceu.gtceu.common.machine.multiblock.electric.gcym; |
| 2 | + |
| 3 | +import com.gregtechceu.gtceu.api.capability.recipe.IO; |
| 4 | +import com.gregtechceu.gtceu.api.capability.recipe.ItemRecipeCapability; |
| 5 | +import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity; |
| 6 | +import com.gregtechceu.gtceu.api.machine.TickableSubscription; |
| 7 | +import com.gregtechceu.gtceu.api.machine.multiblock.WorkableElectricMultiblockMachine; |
| 8 | +import com.gregtechceu.gtceu.api.pattern.util.RelativeDirection; |
| 9 | + |
| 10 | +import net.minecraft.core.BlockPos; |
| 11 | +import net.minecraft.world.entity.item.ItemEntity; |
| 12 | +import net.minecraft.world.phys.AABB; |
| 13 | +import net.minecraftforge.items.IItemHandler; |
| 14 | +import net.minecraftforge.items.ItemHandlerHelper; |
| 15 | + |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +public class LargeMacerationTowerMachine extends WorkableElectricMultiblockMachine { |
| 22 | + |
| 23 | + @NotNull |
| 24 | + private AABB grindBound = new AABB(BlockPos.ZERO); |
| 25 | + @NotNull |
| 26 | + private final List<IItemHandler> handlers = new ArrayList<>(); |
| 27 | + |
| 28 | + private TickableSubscription hurtSub; |
| 29 | + |
| 30 | + public LargeMacerationTowerMachine(IMachineBlockEntity holder) { |
| 31 | + super(holder); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public void onStructureFormed() { |
| 36 | + super.onStructureFormed(); |
| 37 | + updateBounds(); |
| 38 | + for (var holder : getCapabilitiesFlat(IO.IN, ItemRecipeCapability.CAP)) { |
| 39 | + if (holder instanceof IItemHandler ih) { |
| 40 | + handlers.add(ih); |
| 41 | + } |
| 42 | + } |
| 43 | + hurtSub = subscribeServerTick(this::spinWheels); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onStructureInvalid() { |
| 48 | + super.onStructureInvalid(); |
| 49 | + unsubscribe(hurtSub); |
| 50 | + hurtSub = null; |
| 51 | + handlers.clear(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onUnload() { |
| 56 | + super.onUnload(); |
| 57 | + unsubscribe(hurtSub); |
| 58 | + hurtSub = null; |
| 59 | + handlers.clear(); |
| 60 | + } |
| 61 | + |
| 62 | + private void updateBounds() { |
| 63 | + var fl = RelativeDirection.offsetPos(getPos(), getFrontFacing(), getUpwardsFacing(), isFlipped(), 1, 1, -1); |
| 64 | + var br = RelativeDirection.offsetPos(getPos(), getFrontFacing(), getUpwardsFacing(), isFlipped(), 2, -2, -4); |
| 65 | + grindBound = new AABB(fl, br); |
| 66 | + } |
| 67 | + |
| 68 | + private void spinWheels() { |
| 69 | + if (isRemote() || getLevel() == null) return; |
| 70 | + |
| 71 | + List<ItemEntity> itemEntities = new ArrayList<>(); |
| 72 | + for (var entity : getLevel().getEntities(null, grindBound)) { |
| 73 | + if (entity instanceof ItemEntity ie) { |
| 74 | + itemEntities.add(ie); |
| 75 | + } else { |
| 76 | + if (recipeLogic.isWorking()) { |
| 77 | + entity.hurt(entity.damageSources().cramming(), 2.0f); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (handlers.isEmpty()) return; |
| 83 | + |
| 84 | + for (ItemEntity item : itemEntities) { |
| 85 | + if (item.isRemoved()) continue; |
| 86 | + for (var holder : handlers) { |
| 87 | + item.setItem(ItemHandlerHelper.insertItem(holder, item.getItem(), false)); |
| 88 | + if (item.getItem().isEmpty()) { |
| 89 | + item.discard(); |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments