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

149 lines
5.7 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;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
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;
2020-01-21 21:04:44 +01:00
import net.minecraft.tileentity.ITickableTileEntity;
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;
2020-01-21 21:04:44 +01:00
public class TileEntityOfferingTable extends TileEntityImpl implements ITickableTileEntity {
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<>();
2020-01-22 01:32:26 +01:00
public TileEntityOfferingTable() {
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) {
for (OfferingRecipe recipe : this.world.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() {
2018-11-20 19:59:18 +01:00
if (!this.world.isRemote) {
2020-01-21 21:04:44 +01:00
if (this.world.getGameTime() % 20 == 0) {
2018-11-20 19:59:18 +01:00
if (!Multiblocks.OFFERING_TABLE.isComplete(this.world, this.pos))
return;
ItemStack stack = this.items.getStackInSlot(0);
if (stack.isEmpty())
return;
2019-10-20 22:30:49 +02:00
List<ItemEntity> items = this.world.getEntitiesWithinAABB(ItemEntity.class, new AxisAlignedBB(this.pos).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-01-14 23:15:02 +01:00
if (Multiblocks.OFFERING_TABLE.forEach(this.pos, 'R', (pos, m) -> this.world.getBlockState(pos).getBlock() == Blocks.WITHER_ROSE)) {
for (int i = this.world.rand.nextInt(5) + 3; i >= 0; i--)
this.itemsToSpawn.add(new ItemStack(Items.BLACK_DYE));
}
2020-09-22 03:17:02 +02:00
LightningBoltEntity lightningboltentity = EntityType.LIGHTNING_BOLT.create(this.world);
lightningboltentity.setEffectOnly(true);
2020-09-22 03:17:02 +02:00
lightningboltentity.moveForced(Vector3d.copyCenteredHorizontally(this.pos));
this.world.addEntity(lightningboltentity);
2020-01-23 16:05:52 +01:00
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(
2020-01-28 18:08:56 +01:00
(float) item.getPosX(), (float) item.getPosY(), (float) item.getPosZ(), PacketParticles.Type.OFFERING_TABLE,
2020-01-22 23:21:52 +01:00
this.pos.getX(), this.pos.getY(), this.pos.getZ()));
2018-11-21 01:05:50 +01:00
2018-11-20 19:59:18 +01:00
break;
}
2020-01-21 21:04:44 +01:00
} else if (this.world.getGameTime() % 3 == 0) {
2018-11-20 19:59:18 +01:00
if (!this.itemsToSpawn.isEmpty())
2020-01-21 21:04:44 +01:00
this.world.addEntity(new ItemEntity(
2018-11-20 19:59:18 +01:00
this.world,
2018-11-21 01:05:50 +01:00
this.pos.getX() + 0.5F, 256, this.pos.getZ() + 0.5F,
2018-11-20 19:59:18 +01:00
this.itemsToSpawn.remove()));
}
}
}
2018-11-19 20:18:22 +01:00
@Override
2019-10-20 22:30:49 +02:00
public void writeNBT(CompoundNBT 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
2019-10-20 22:30:49 +02:00
public void readNBT(CompoundNBT 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) {
this.itemsToSpawn.add(ItemStack.read((CompoundNBT) 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;
}
}