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

149 lines
5.8 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;
import net.minecraft.block.Blocks;
2020-09-22 03:17:02 +02:00
import net.minecraft.entity.EntityType;
2020-01-23 16:05:52 +01:00
import net.minecraft.entity.effect.LightningBoltEntity;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.item.ItemEntity;
2018-11-20 19:59:18 +01:00
import net.minecraft.item.ItemStack;
2021-01-14 23:15:02 +01:00
import net.minecraft.item.Items;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2020-01-21 21:04:44 +01:00
import net.minecraft.nbt.INBT;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.ListNBT;
2021-12-04 15:40:09 +01:00
import net.minecraft.tileentity.ITickableBlockEntity;
2018-11-20 19:59:18 +01:00
import net.minecraft.util.math.AxisAlignedBB;
2020-09-22 03:17:02 +02:00
import net.minecraft.util.math.vector.Vector3d;
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.List;
import java.util.Queue;
2021-12-04 15:40:09 +01:00
public class BlockEntityOfferingTable extends BlockEntityImpl implements ITickableBlockEntity {
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-04 15:40:09 +01:00
public BlockEntityOfferingTable() {
2020-01-22 01:32:26 +01:00
super(ModTileEntities.OFFERING_TABLE);
2020-01-21 21:04:44 +01:00
}
2020-04-29 16:38:50 +02:00
private OfferingRecipe getRecipe(ItemStack input) {
2021-12-04 15:40:09 +01:00
for (OfferingRecipe recipe : this.level.getRecipeManager().getRecipes(ModRecipes.OFFERING_TYPE, null, null))
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;
ItemStack stack = this.items.getStackInSlot(0);
if (stack.isEmpty())
return;
2021-12-04 15:40:09 +01:00
List<ItemEntity> items = this.level.getEntitiesWithinAABB(ItemEntity.class, new AxisAlignedBB(this.worldPosition).grow(1));
2018-11-20 19:59:18 +01:00
if (items.isEmpty())
return;
2020-04-29 16:38:50 +02:00
OfferingRecipe recipe = this.getRecipe(stack);
2018-11-20 19:59:18 +01:00
if (recipe == null)
return;
2019-10-20 22:30:49 +02:00
for (ItemEntity item : items) {
2020-01-21 21:04:44 +01:00
if (!item.isAlive() || item.cannotPickup())
2018-11-20 19:59:18 +01:00
continue;
ItemStack itemStack = item.getItem();
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;
2018-12-30 20:37:00 +01:00
int amount = Helper.getIngredientAmount(recipe.input);
int recipeCount = stack.getCount() / amount;
stack.shrink(recipeCount * amount);
2020-01-21 21:04:44 +01:00
item.remove();
2018-11-20 19:59:18 +01:00
this.sendToClients();
for (int i = 0; i < recipeCount; i++)
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)) {
for (int i = this.level.rand.nextInt(5) + 3; i >= 0; i--)
2021-01-14 23:15:02 +01:00
this.itemsToSpawn.add(new ItemStack(Items.BLACK_DYE));
}
2021-12-04 15:40:09 +01:00
LightningBoltEntity lightningboltentity = EntityType.LIGHTNING_BOLT.create(this.level);
lightningboltentity.setEffectOnly(true);
2021-12-04 15:40:09 +01:00
lightningboltentity.moveForced(Vector3d.copyCenteredHorizontally(this.worldPosition));
this.level.addEntity(lightningboltentity);
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32, new PacketParticles(
2020-01-28 18:08:56 +01:00
(float) item.getPosX(), (float) item.getPosY(), (float) item.getPosZ(), 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-04 15:40:09 +01:00
this.level.addEntity(new ItemEntity(
this.level,
this.worldPosition.getX() + 0.5F, 256, this.worldPosition.getZ() + 0.5F,
2018-11-20 19:59:18 +01:00
this.itemsToSpawn.remove()));
}
}
}
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) {
2019-10-20 22:30:49 +02:00
ListNBT list = new ListNBT();
2018-11-20 19:59:18 +01:00
for (ItemStack stack : this.itemsToSpawn) {
2020-01-21 21:04:44 +01:00
list.add(stack.serializeNBT());
2018-11-20 19:59:18 +01:00
}
2020-01-21 21:04:44 +01:00
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();
2020-01-21 21:04:44 +01:00
ListNBT list = compound.getList("items_to_spawn", 10);
for (INBT base : list) {
2021-12-04 15:40:09 +01:00
this.itemsToSpawn.add(ItemStack.read((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;
}
}