2018-10-16 11:49:30 +02:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
2018-11-11 13:26:19 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
2018-11-19 21:55:00 +01:00
|
|
|
import de.ellpeck.naturesaura.api.recipes.TreeRitualRecipe;
|
2018-11-07 13:33:49 +01:00
|
|
|
import de.ellpeck.naturesaura.blocks.multi.Multiblocks;
|
2018-10-16 11:49:30 +02:00
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticleStream;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
|
|
|
import net.minecraft.block.BlockLeaves;
|
|
|
|
import net.minecraft.block.BlockLog;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
2018-10-16 17:48:36 +02:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
2018-10-24 12:42:04 +02:00
|
|
|
import net.minecraft.init.SoundEvents;
|
2018-10-16 11:49:30 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2018-11-20 11:44:07 +01:00
|
|
|
import net.minecraft.item.crafting.Ingredient;
|
2018-10-16 11:49:30 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2018-10-16 18:07:03 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2018-11-07 13:33:49 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.ITickable;
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraft.util.SoundCategory;
|
2018-10-16 11:49:30 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2018-10-18 13:34:37 +02:00
|
|
|
import net.minecraftforge.items.IItemHandlerModifiable;
|
|
|
|
import net.minecraftforge.items.ItemStackHandler;
|
2018-10-16 11:49:30 +02:00
|
|
|
|
2018-10-27 01:49:57 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2018-10-16 11:49:30 +02:00
|
|
|
|
|
|
|
public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
|
|
|
|
|
2018-10-18 13:34:37 +02:00
|
|
|
public final ItemStackHandler items = new ItemStackHandlerNA(1, this, true) {
|
|
|
|
@Override
|
|
|
|
public int getSlotLimit(int slot) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
2018-10-16 11:49:30 +02:00
|
|
|
|
2018-10-27 01:49:57 +02:00
|
|
|
private TreeRitualRecipe recipe;
|
2018-10-16 11:49:30 +02:00
|
|
|
private BlockPos ritualPos;
|
|
|
|
private int timer;
|
|
|
|
|
2018-10-27 01:49:57 +02:00
|
|
|
public void setRitual(BlockPos pos, TreeRitualRecipe recipe) {
|
2018-10-16 11:49:30 +02:00
|
|
|
this.ritualPos = pos;
|
2018-10-27 01:49:57 +02:00
|
|
|
this.recipe = recipe;
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update() {
|
|
|
|
if (!this.world.isRemote) {
|
2018-10-27 01:49:57 +02:00
|
|
|
if (this.ritualPos != null && this.recipe != null) {
|
|
|
|
if (this.world.getTotalWorldTime() % 5 == 0) {
|
|
|
|
if (this.isRitualOkay()) {
|
|
|
|
boolean wasOverHalf = this.timer >= this.recipe.time / 2;
|
|
|
|
this.timer += 5;
|
|
|
|
boolean isOverHalf = this.timer >= this.recipe.time / 2;
|
|
|
|
|
|
|
|
if (!isOverHalf)
|
2018-11-07 13:33:49 +01:00
|
|
|
Multiblocks.TREE_RITUAL.forEach(this.ritualPos, 'W', (pos, matcher) -> {
|
2018-10-27 01:49:57 +02:00
|
|
|
TileEntity tile = this.world.getTileEntity(pos);
|
|
|
|
if (tile instanceof TileEntityWoodStand && !((TileEntityWoodStand) tile).items.getStackInSlot(0).isEmpty()) {
|
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticleStream(
|
|
|
|
(float) pos.getX() + 0.2F + this.world.rand.nextFloat() * 0.6F,
|
|
|
|
(float) pos.getY() + 0.85F,
|
|
|
|
(float) pos.getZ() + 0.2F + this.world.rand.nextFloat() * 0.6F,
|
|
|
|
this.ritualPos.getX() + 0.5F, this.ritualPos.getY() + this.world.rand.nextFloat() * 3F + 2F, this.ritualPos.getZ() + 0.5F,
|
2018-11-04 18:42:35 +01:00
|
|
|
this.world.rand.nextFloat() * 0.04F + 0.04F, 0x89cc37, this.world.rand.nextFloat() * 1F + 1F
|
2018-10-27 01:49:57 +02:00
|
|
|
));
|
|
|
|
}
|
2018-11-07 13:33:49 +01:00
|
|
|
return true;
|
2018-10-27 01:49:57 +02:00
|
|
|
});
|
|
|
|
|
2018-10-16 17:48:36 +02:00
|
|
|
PacketHandler.sendToAllAround(this.world, this.ritualPos, 32,
|
|
|
|
new PacketParticles(this.ritualPos.getX(), this.ritualPos.getY(), this.ritualPos.getZ(), 0));
|
2018-10-16 11:49:30 +02:00
|
|
|
|
2018-10-27 01:49:57 +02:00
|
|
|
if (this.timer >= this.recipe.time) {
|
|
|
|
this.recurseTreeDestruction(this.ritualPos, this.ritualPos);
|
2018-11-07 13:33:49 +01:00
|
|
|
Multiblocks.TREE_RITUAL.forEach(this.ritualPos, 'G', (pos, matcher) -> {
|
|
|
|
this.world.setBlockToAir(pos);
|
|
|
|
return true;
|
|
|
|
});
|
2018-10-27 01:49:57 +02:00
|
|
|
|
|
|
|
EntityItem item = new EntityItem(this.world,
|
|
|
|
this.ritualPos.getX() + 0.5, this.ritualPos.getY() + 4.5, this.ritualPos.getZ() + 0.5,
|
|
|
|
this.recipe.result.copy());
|
|
|
|
this.world.spawnEntity(item);
|
|
|
|
|
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32,
|
|
|
|
new PacketParticles((float) item.posX, (float) item.posY, (float) item.posZ, 3));
|
|
|
|
this.world.playSound(null, this.pos.getX() + 0.5, this.pos.getY() + 0.5, this.pos.getZ() + 0.5,
|
|
|
|
SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.BLOCKS, 0.65F, 1F);
|
|
|
|
|
|
|
|
this.ritualPos = null;
|
|
|
|
this.recipe = null;
|
|
|
|
this.timer = 0;
|
|
|
|
} else if (isOverHalf && !wasOverHalf) {
|
2018-11-07 13:33:49 +01:00
|
|
|
Multiblocks.TREE_RITUAL.forEach(this.ritualPos, 'W', (pos, matcher) -> {
|
2018-10-27 01:49:57 +02:00
|
|
|
TileEntity tile = this.world.getTileEntity(pos);
|
|
|
|
if (tile instanceof TileEntityWoodStand) {
|
|
|
|
TileEntityWoodStand stand = (TileEntityWoodStand) tile;
|
|
|
|
if (!stand.items.getStackInSlot(0).isEmpty()) {
|
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32,
|
|
|
|
new PacketParticles(stand.pos.getX(), stand.pos.getY(), stand.pos.getZ(), 1));
|
|
|
|
this.world.playSound(null, stand.pos.getX() + 0.5, stand.pos.getY() + 0.5, stand.pos.getZ() + 0.5,
|
|
|
|
SoundEvents.BLOCK_WOOD_STEP, SoundCategory.BLOCKS, 0.5F, 1F);
|
|
|
|
|
|
|
|
stand.items.setStackInSlot(0, ItemStack.EMPTY);
|
|
|
|
stand.sendToClients();
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 13:33:49 +01:00
|
|
|
return true;
|
2018-10-27 01:49:57 +02:00
|
|
|
});
|
2018-10-16 17:48:36 +02:00
|
|
|
}
|
2018-10-27 01:49:57 +02:00
|
|
|
} else {
|
2018-10-16 11:49:30 +02:00
|
|
|
this.ritualPos = null;
|
2018-10-27 01:49:57 +02:00
|
|
|
this.recipe = null;
|
2018-10-16 11:49:30 +02:00
|
|
|
this.timer = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-27 01:49:57 +02:00
|
|
|
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void recurseTreeDestruction(BlockPos pos, BlockPos start) {
|
2018-10-16 21:10:38 +02:00
|
|
|
if (Math.abs(pos.getX() - start.getX()) >= 6
|
|
|
|
|| Math.abs(pos.getZ() - start.getZ()) >= 6
|
|
|
|
|| Math.abs(pos.getY() - start.getY()) >= 16) {
|
2018-10-16 11:49:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-23 17:15:42 +01:00
|
|
|
for (int x = -1; x <= 1; x++) {
|
|
|
|
for (int y = -1; y <= 1; y++) {
|
|
|
|
for (int z = -1; z <= 1; z++) {
|
|
|
|
BlockPos offset = pos.add(x, y, z);
|
|
|
|
IBlockState state = this.world.getBlockState(offset);
|
|
|
|
if (state.getBlock() instanceof BlockLog || state.getBlock() instanceof BlockLeaves) {
|
|
|
|
this.world.setBlockToAir(offset);
|
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(offset.getX(), offset.getY(), offset.getZ(), 2));
|
|
|
|
|
|
|
|
this.recurseTreeDestruction(offset, start);
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:02:29 +02:00
|
|
|
private boolean isRitualOkay() {
|
2018-11-07 13:33:49 +01:00
|
|
|
if (!Multiblocks.TREE_RITUAL.isComplete(this.world, this.ritualPos)) {
|
2018-10-27 01:49:57 +02:00
|
|
|
return false;
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|
2018-10-27 01:49:57 +02:00
|
|
|
if (this.timer < this.recipe.time / 2) {
|
2018-11-20 11:44:07 +01:00
|
|
|
List<Ingredient> required = new ArrayList<>(Arrays.asList(this.recipe.ingredients));
|
2018-11-07 13:33:49 +01:00
|
|
|
boolean fine = Multiblocks.TREE_RITUAL.forEach(this.ritualPos, 'W', (pos, matcher) -> {
|
2018-10-27 01:49:57 +02:00
|
|
|
TileEntity tile = this.world.getTileEntity(pos);
|
|
|
|
if (tile instanceof TileEntityWoodStand) {
|
|
|
|
ItemStack stack = ((TileEntityWoodStand) tile).items.getStackInSlot(0);
|
|
|
|
if (!stack.isEmpty()) {
|
2018-11-19 21:55:00 +01:00
|
|
|
for (int i = required.size() - 1; i >= 0; i--) {
|
2018-11-20 11:44:07 +01:00
|
|
|
Ingredient req = required.get(i);
|
|
|
|
if (req.apply(stack)) {
|
2018-11-19 21:55:00 +01:00
|
|
|
required.remove(i);
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-27 01:49:57 +02:00
|
|
|
}
|
2018-11-19 21:55:00 +01:00
|
|
|
return false;
|
2018-10-27 01:49:57 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-07 13:33:49 +01:00
|
|
|
return true;
|
2018-10-27 01:49:57 +02:00
|
|
|
});
|
2018-11-07 13:33:49 +01:00
|
|
|
return fine && required.isEmpty();
|
2018-10-27 01:49:57 +02:00
|
|
|
} else
|
|
|
|
return true;
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-11-04 16:38:09 +01:00
|
|
|
public void writeNBT(NBTTagCompound compound, SaveType type) {
|
|
|
|
super.writeNBT(compound, type);
|
2018-11-05 16:36:10 +01:00
|
|
|
if (type != SaveType.BLOCK)
|
2018-11-20 11:48:45 +01:00
|
|
|
compound.setTag("items", this.items.serializeNBT());
|
2018-10-16 18:07:03 +02:00
|
|
|
|
2018-11-04 16:38:09 +01:00
|
|
|
if (type == SaveType.TILE) {
|
2018-10-27 01:49:57 +02:00
|
|
|
if (this.ritualPos != null && this.recipe != null) {
|
2018-10-16 18:08:32 +02:00
|
|
|
compound.setLong("ritual_pos", this.ritualPos.toLong());
|
|
|
|
compound.setInteger("timer", this.timer);
|
2018-10-27 01:49:57 +02:00
|
|
|
compound.setString("recipe", this.recipe.name.toString());
|
2018-10-16 18:07:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-11-04 16:38:09 +01:00
|
|
|
public void readNBT(NBTTagCompound compound, SaveType type) {
|
|
|
|
super.readNBT(compound, type);
|
2018-11-05 16:36:10 +01:00
|
|
|
if (type != SaveType.BLOCK)
|
2018-11-20 11:48:45 +01:00
|
|
|
this.items.deserializeNBT(compound.getCompoundTag("items"));
|
2018-10-16 18:07:03 +02:00
|
|
|
|
2018-11-04 16:38:09 +01:00
|
|
|
if (type == SaveType.TILE) {
|
2018-10-27 01:49:57 +02:00
|
|
|
if (compound.hasKey("recipe")) {
|
2018-10-16 18:08:32 +02:00
|
|
|
this.ritualPos = BlockPos.fromLong(compound.getLong("ritual_pos"));
|
|
|
|
this.timer = compound.getInteger("timer");
|
2018-11-11 13:26:19 +01:00
|
|
|
this.recipe = NaturesAuraAPI.TREE_RITUAL_RECIPES.get(new ResourceLocation(compound.getString("recipe")));
|
2018-10-16 18:07:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|
2018-10-18 13:34:37 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public IItemHandlerModifiable getItemHandler(EnumFacing facing) {
|
|
|
|
return this.items;
|
|
|
|
}
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|