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

118 lines
4.7 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;
import net.minecraft.entity.player.PlayerEntity;
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;
2020-01-21 21:04:44 +01:00
import net.minecraft.tileentity.ITickableTileEntity;
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;
2020-01-21 21:04:44 +01:00
public class TileEntityAutoCrafter extends TileEntityImpl implements ITickableTileEntity {
public final CraftingInventory crafting = new CraftingInventory(new Container(null, 0) {
2019-01-08 01:14:19 +01:00
@Override
2019-10-20 22:30:49 +02:00
public boolean canInteractWith(PlayerEntity playerIn) {
2019-01-08 01:14:19 +01:00
return false;
}
}, 3, 3);
2020-01-22 01:32:26 +01:00
public TileEntityAutoCrafter() {
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() {
2019-01-08 01:14:19 +01:00
if (!this.world.isRemote) {
2020-01-21 21:04:44 +01:00
if (this.world.getGameTime() % 60 != 0)
2019-01-08 01:14:19 +01:00
return;
if (!Multiblocks.AUTO_CRAFTER.isComplete(this.world, this.pos))
return;
this.crafting.clear();
2019-10-20 22:30:49 +02:00
BlockState state = this.world.getBlockState(this.pos);
2020-01-21 21:04:44 +01:00
Direction facing = state.get(BlockAutoCrafter.FACING);
2019-01-08 01:14:19 +01:00
BlockPos middlePos = this.pos.up();
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++) {
2019-10-20 22:30:49 +02:00
List<ItemEntity> entities = this.world.getEntitiesWithinAABB(
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());
}
2020-01-23 22:40:03 +01:00
IRecipe recipe = this.world.getRecipeManager().getRecipe(IRecipeType.CRAFTING, this.crafting, this.world).orElse(null);
2019-01-08 01:14:19 +01:00
if (recipe == null)
return;
ItemStack result = recipe.getCraftingResult(this.crafting);
if (result.isEmpty())
return;
2019-10-20 22:30:49 +02:00
ItemEntity resultItem = new ItemEntity(this.world,
2019-01-08 01:14:19 +01:00
this.pos.getX() + 0.5F, this.pos.getY() - 0.35F, this.pos.getZ() + 0.5F, result.copy());
2020-01-21 21:04:44 +01:00
resultItem.setMotion(0, 0, 0);
this.world.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()) {
2019-10-20 22:30:49 +02:00
ItemEntity remItem = new ItemEntity(this.world, item.posX, item.posY, item.posZ, remain.copy());
2020-01-21 21:04:44 +01:00
remItem.setMotion(0, 0, 0);
this.world.addEntity(remItem);
}
2020-01-23 22:40:03 +01:00
PacketHandler.sendToAllAround(this.world, this.pos, 32,
2020-01-22 23:21:52 +01:00
new PacketParticles((float) item.posX, (float) item.posY, (float) item.posZ, 19));
2019-01-08 01:14:19 +01:00
}
}
}
}