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

144 lines
5.6 KiB
Java
Raw Normal View History

2018-11-19 20:18:22 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
2018-12-30 20:37:00 +01:00
import de.ellpeck.naturesaura.Helper;
2018-11-20 19:59:18 +01:00
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;
2021-01-14 23:15:02 +01:00
import de.ellpeck.naturesaura.recipes.ModRecipes;
import de.ellpeck.naturesaura.recipes.OfferingRecipe;
2021-12-08 00:31:29 +01:00
import net.minecraft.core.BlockPos;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-08 00:31:29 +01:00
import net.minecraft.nbt.ListTag;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
2018-11-19 20:18:22 +01:00
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;
2018-11-20 19:59:18 +01:00
import java.util.ArrayDeque;
import java.util.Queue;
2021-12-04 15:40:09 +01:00
public class BlockEntityOfferingTable extends BlockEntityImpl implements ITickableBlockEntity {
2021-12-08 00:31:29 +01:00
2018-11-21 01:05:50 +01:00
public final ItemStackHandler items = new ItemStackHandlerNA(1, this, true) {
@Override
public int getSlotLimit(int slot) {
return 16;
}
};
2018-11-20 19:59:18 +01:00
private final Queue<ItemStack> itemsToSpawn = new ArrayDeque<>();
2021-12-08 00:31:29 +01:00
public BlockEntityOfferingTable(BlockPos pos, BlockState state) {
2021-12-19 15:32:45 +01:00
super(ModBlockEntities.OFFERING_TABLE, pos, state);
2020-01-21 21:04:44 +01:00
}
2020-04-29 16:38:50 +02:00
private OfferingRecipe getRecipe(ItemStack input) {
2023-07-25 14:12:29 +02:00
for (var recipe : this.level.getRecipeManager().getRecipesFor(ModRecipes.OFFERING_TYPE, null, this.level))
2020-02-07 15:22:30 +01:00
if (recipe.input.test(input))
return recipe;
return null;
}
2018-11-20 19:59:18 +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() % 20 == 0) {
if (!Multiblocks.OFFERING_TABLE.isComplete(this.level, this.worldPosition))
2018-11-20 19:59:18 +01:00
return;
2021-12-15 16:30:22 +01:00
var stack = this.items.getStackInSlot(0);
2018-11-20 19:59:18 +01:00
if (stack.isEmpty())
return;
2021-12-15 16:30:22 +01:00
var items = this.level.getEntitiesOfClass(ItemEntity.class, new AABB(this.worldPosition).inflate(1));
2018-11-20 19:59:18 +01:00
if (items.isEmpty())
return;
2021-12-15 16:30:22 +01:00
var recipe = this.getRecipe(stack);
2018-11-20 19:59:18 +01:00
if (recipe == null)
return;
2021-12-15 16:30:22 +01:00
for (var item : items) {
2021-12-08 00:31:29 +01:00
if (!item.isAlive() || item.hasPickUpDelay())
2018-11-20 19:59:18 +01:00
continue;
2021-12-15 16:30:22 +01:00
var itemStack = item.getItem();
2018-11-20 19:59:18 +01:00
if (itemStack.isEmpty() || itemStack.getCount() != 1)
continue;
2020-01-21 21:04:44 +01:00
if (!recipe.startItem.test(itemStack))
2018-11-20 19:59:18 +01:00
continue;
2021-12-15 16:30:22 +01:00
var amount = Helper.getIngredientAmount(recipe.input);
var recipeCount = stack.getCount() / amount;
2018-12-30 20:37:00 +01:00
stack.shrink(recipeCount * amount);
2021-12-08 00:31:29 +01:00
item.kill();
2018-11-20 19:59:18 +01:00
this.sendToClients();
2021-12-15 16:30:22 +01:00
for (var i = 0; i < recipeCount; i++)
2018-11-20 19:59:18 +01:00
this.itemsToSpawn.add(recipe.output.copy());
2021-12-04 15:40:09 +01:00
if (Multiblocks.OFFERING_TABLE.forEach(this.worldPosition, 'R', (pos, m) -> this.level.getBlockState(pos).getBlock() == Blocks.WITHER_ROSE)) {
2021-12-15 16:30:22 +01:00
for (var i = this.level.random.nextInt(5) + 3; i >= 0; i--)
2021-01-14 23:15:02 +01:00
this.itemsToSpawn.add(new ItemStack(Items.BLACK_DYE));
}
2021-12-15 16:30:22 +01:00
var lightningboltentity = EntityType.LIGHTNING_BOLT.create(this.level);
2021-12-08 00:31:29 +01:00
lightningboltentity.setVisualOnly(true);
lightningboltentity.moveTo(Vec3.atCenterOf(this.worldPosition));
this.level.addFreshEntity(lightningboltentity);
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32, new PacketParticles(
2021-12-08 00:31:29 +01:00
(float) item.getX(), (float) item.getY(), (float) item.getZ(), PacketParticles.Type.OFFERING_TABLE,
2021-12-04 15:40:09 +01:00
this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ()));
2018-11-21 01:05:50 +01:00
2018-11-20 19:59:18 +01:00
break;
}
2021-12-04 15:40:09 +01:00
} else if (this.level.getGameTime() % 3 == 0) {
2018-11-20 19:59:18 +01:00
if (!this.itemsToSpawn.isEmpty())
2021-12-08 00:31:29 +01:00
this.level.addFreshEntity(new ItemEntity(this.level, this.worldPosition.getX() + 0.5F, 256, this.worldPosition.getZ() + 0.5F, this.itemsToSpawn.remove()));
2018-11-20 19:59:18 +01:00
}
}
}
2018-11-19 20:18:22 +01:00
@Override
2021-12-04 15:40:09 +01:00
public void writeNBT(CompoundTag compound, SaveType type) {
2018-11-19 20:18:22 +01:00
super.writeNBT(compound, type);
if (type != SaveType.BLOCK) {
2020-01-21 21:04:44 +01:00
compound.put("items", this.items.serializeNBT());
2018-11-20 19:59:18 +01:00
if (type != SaveType.SYNC) {
2021-12-15 16:30:22 +01:00
var list = new ListTag();
for (var stack : this.itemsToSpawn)
2020-01-21 21:04:44 +01:00
list.add(stack.serializeNBT());
compound.put("items_to_spawn", list);
2018-11-20 19:59:18 +01:00
}
2018-11-19 20:18:22 +01:00
}
}
@Override
2021-12-04 15:40:09 +01:00
public void readNBT(CompoundTag compound, SaveType type) {
2018-11-19 20:18:22 +01:00
super.readNBT(compound, type);
if (type != SaveType.BLOCK) {
2020-01-21 21:04:44 +01:00
this.items.deserializeNBT(compound.getCompound("items"));
2018-11-20 19:59:18 +01:00
if (type != SaveType.SYNC) {
this.itemsToSpawn.clear();
2021-12-15 16:30:22 +01:00
var list = compound.getList("items_to_spawn", 10);
for (var base : list)
2021-12-08 00:31:29 +01:00
this.itemsToSpawn.add(ItemStack.of((CompoundTag) base));
2018-11-20 19:59:18 +01:00
}
2018-11-19 20:18:22 +01:00
}
}
@Override
2020-10-19 21:26:32 +02:00
public IItemHandlerModifiable getItemHandler() {
2018-11-19 20:18:22 +01:00
return this.items;
}
2023-07-25 14:12:29 +02:00
2018-11-19 20:18:22 +01:00
}