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

139 lines
5 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.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.recipes.OfferingRecipe;
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.entity.item.ItemEntity;
2018-11-20 19:59:18 +01:00
import net.minecraft.item.ItemStack;
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;
import net.minecraft.tileentity.TileEntityType;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Direction;
2018-11-20 19:59:18 +01:00
import net.minecraft.util.math.AxisAlignedBB;
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
}
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;
OfferingRecipe recipe = getRecipe(stack);
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());
2020-01-21 21:04:44 +01:00
// TODO weather effects
//this.world.addWeatherEffect(new LightningBoltEntity(this.world, this.pos.getX(), this.pos.getY(), this.pos.getZ(), true));
2020-01-22 23:21:52 +01:00
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(
2018-11-21 01:05:50 +01:00
(float) item.posX, (float) item.posY, (float) item.posZ, 13,
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()));
}
}
}
private static OfferingRecipe getRecipe(ItemStack input) {
for (OfferingRecipe recipe : NaturesAuraAPI.OFFERING_RECIPES.values())
2020-01-21 21:04:44 +01:00
if (recipe.input.test(input))
2018-11-20 19:59:18 +01:00
return recipe;
return null;
}
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
2019-10-20 22:30:49 +02:00
public IItemHandlerModifiable getItemHandler(Direction facing) {
2018-11-19 20:18:22 +01:00
return this.items;
}
}