NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityAutoCrafter.java

118 lines
4.9 KiB
Java
Raw Normal View History

2019-01-08 01:14:19 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.blocks.BlockAutoCrafter;
import de.ellpeck.naturesaura.blocks.multi.Multiblocks;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
import net.minecraft.entity.item.ItemEntity;
2021-12-04 15:40:09 +01:00
import net.minecraft.entity.player.Player;
2019-10-20 22:30:49 +02:00
import net.minecraft.inventory.CraftingInventory;
2020-01-21 21:04:44 +01:00
import net.minecraft.inventory.container.Container;
2019-01-08 01:14:19 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
2020-01-23 22:40:03 +01:00
import net.minecraft.item.crafting.IRecipeType;
2021-12-04 15:40:09 +01:00
import net.minecraft.tileentity.ITickableBlockEntity;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Direction;
2020-01-21 21:04:44 +01:00
import net.minecraft.util.EntityPredicates;
import net.minecraft.util.NonNullList;
2019-01-08 01:14:19 +01:00
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import java.util.List;
2021-12-04 15:40:09 +01:00
public class BlockEntityAutoCrafter extends BlockEntityImpl implements ITickableBlockEntity {
2020-01-21 21:04:44 +01:00
public final CraftingInventory crafting = new CraftingInventory(new Container(null, 0) {
2019-01-08 01:14:19 +01:00
@Override
2021-12-04 15:40:09 +01:00
public boolean canInteractWith(Player playerIn) {
2019-01-08 01:14:19 +01:00
return false;
}
}, 3, 3);
2021-12-04 15:40:09 +01:00
public BlockEntityAutoCrafter() {
2020-01-22 01:32:26 +01:00
super(ModTileEntities.AUTO_CRAFTER);
2020-01-21 21:04:44 +01:00
}
2019-01-08 01:14:19 +01:00
@Override
2020-01-21 21:04:44 +01:00
public void tick() {
2021-12-04 15:40:09 +01:00
if (!this.level.isClientSide) {
if (this.level.getGameTime() % 60 != 0)
2019-01-08 01:14:19 +01:00
return;
2021-12-04 15:40:09 +01:00
if (!Multiblocks.AUTO_CRAFTER.isComplete(this.level, this.worldPosition))
2019-01-08 01:14:19 +01:00
return;
this.crafting.clear();
2021-12-04 15:40:09 +01:00
BlockState state = this.level.getBlockState(this.worldPosition);
2020-01-21 21:04:44 +01:00
Direction facing = state.get(BlockAutoCrafter.FACING);
2021-12-04 15:40:09 +01:00
BlockPos middlePos = this.worldPosition.up();
2019-01-08 01:14:19 +01:00
BlockPos topPos = middlePos.offset(facing, 2);
BlockPos bottomPos = middlePos.offset(facing.getOpposite(), 2);
BlockPos[] poses = new BlockPos[]{
topPos.offset(facing.rotateYCCW(), 2),
topPos,
topPos.offset(facing.rotateY(), 2),
middlePos.offset(facing.rotateYCCW(), 2),
middlePos,
middlePos.offset(facing.rotateY(), 2),
bottomPos.offset(facing.rotateYCCW(), 2),
bottomPos,
bottomPos.offset(facing.rotateY(), 2)
};
2019-10-20 22:30:49 +02:00
ItemEntity[] items = new ItemEntity[9];
2019-01-08 01:14:19 +01:00
for (int i = 0; i < poses.length; i++) {
2021-12-04 15:40:09 +01:00
List<ItemEntity> entities = this.level.getEntitiesWithinAABB(
2019-10-20 22:30:49 +02:00
ItemEntity.class, new AxisAlignedBB(poses[i]).grow(0.25), EntityPredicates.IS_ALIVE);
2019-01-08 01:14:19 +01:00
if (entities.size() > 1)
return;
if (entities.isEmpty())
continue;
2019-10-20 22:30:49 +02:00
ItemEntity entity = entities.get(0);
2019-01-08 01:14:19 +01:00
if (entity.cannotPickup())
return;
ItemStack stack = entity.getItem();
if (stack.isEmpty())
return;
items[i] = entity;
this.crafting.setInventorySlotContents(i, stack.copy());
}
2021-12-04 15:40:09 +01:00
IRecipe recipe = this.level.getRecipeManager().getRecipe(IRecipeType.CRAFTING, this.crafting, this.level).orElse(null);
2019-01-08 01:14:19 +01:00
if (recipe == null)
return;
ItemStack result = recipe.getCraftingResult(this.crafting);
if (result.isEmpty())
return;
2021-12-04 15:40:09 +01:00
ItemEntity resultItem = new ItemEntity(this.level,
this.worldPosition.getX() + 0.5F, this.worldPosition.getY() - 0.35F, this.worldPosition.getZ() + 0.5F, result.copy());
2020-01-21 21:04:44 +01:00
resultItem.setMotion(0, 0, 0);
2021-12-04 15:40:09 +01:00
this.level.addEntity(resultItem);
2019-01-08 01:14:19 +01:00
NonNullList<ItemStack> remainingItems = recipe.getRemainingItems(this.crafting);
for (int i = 0; i < items.length; i++) {
2019-10-20 22:30:49 +02:00
ItemEntity item = items[i];
2019-01-08 01:14:19 +01:00
if (item == null)
continue;
ItemStack stack = item.getItem();
if (stack.getCount() <= 1)
2020-01-21 21:04:44 +01:00
item.remove();
2019-01-08 01:14:19 +01:00
else {
stack.shrink(1);
item.setItem(stack);
}
ItemStack remain = remainingItems.get(i);
if (!remain.isEmpty()) {
2021-12-04 15:40:09 +01:00
ItemEntity remItem = new ItemEntity(this.level, item.getPosX(), item.getPosY(), item.getPosZ(), remain.copy());
2020-01-21 21:04:44 +01:00
remItem.setMotion(0, 0, 0);
2021-12-04 15:40:09 +01:00
this.level.addEntity(remItem);
}
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32,
2020-01-28 18:08:56 +01:00
new PacketParticles((float) item.getPosX(), (float) item.getPosY(), (float) item.getPosZ(), PacketParticles.Type.ANIMAL_SPAWNER));
2019-01-08 01:14:19 +01:00
}
}
}
}