2018-10-16 11:49:30 +02:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
2023-02-17 14:21:39 +01:00
|
|
|
import de.ellpeck.naturesaura.Helper;
|
2018-11-07 13:33:49 +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.PacketParticleStream;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
2021-01-14 23:15:02 +01:00
|
|
|
import de.ellpeck.naturesaura.recipes.TreeRitualRecipe;
|
2021-12-04 19:17:21 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
2021-12-04 19:17:21 +01:00
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.minecraft.sounds.SoundEvents;
|
|
|
|
import net.minecraft.sounds.SoundSource;
|
2020-09-22 03:17:02 +02:00
|
|
|
import net.minecraft.tags.BlockTags;
|
2021-12-04 19:17:21 +01:00
|
|
|
import net.minecraft.world.entity.item.ItemEntity;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.item.crafting.Ingredient;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.Blocks;
|
|
|
|
import net.minecraft.world.level.block.LeavesBlock;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
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
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
public class BlockEntityWoodStand extends BlockEntityImpl implements ITickableBlockEntity {
|
2018-10-16 11:49:30 +02:00
|
|
|
|
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;
|
|
|
|
|
2021-12-04 19:17:21 +01:00
|
|
|
public BlockEntityWoodStand(BlockPos pos, BlockState state) {
|
2021-12-19 15:32:45 +01:00
|
|
|
super(ModBlockEntities.WOOD_STAND, pos, state);
|
2020-01-21 21:04:44 +01:00
|
|
|
}
|
|
|
|
|
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
|
2020-01-21 21:04:44 +01:00
|
|
|
public void tick() {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!this.level.isClientSide) {
|
2018-10-27 01:49:57 +02:00
|
|
|
if (this.ritualPos != null && this.recipe != null) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (this.level.getGameTime() % 5 == 0) {
|
2018-10-27 01:49:57 +02:00
|
|
|
if (this.isRitualOkay()) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var wasOverHalf = this.timer >= this.recipe.time / 2;
|
2018-10-27 01:49:57 +02:00
|
|
|
this.timer += 5;
|
2021-12-15 16:30:22 +01:00
|
|
|
var isOverHalf = this.timer >= this.recipe.time / 2;
|
2018-10-27 01:49:57 +02:00
|
|
|
|
|
|
|
if (!isOverHalf)
|
2018-11-07 13:33:49 +01:00
|
|
|
Multiblocks.TREE_RITUAL.forEach(this.ritualPos, 'W', (pos, matcher) -> {
|
2021-12-15 16:30:22 +01:00
|
|
|
var tile = this.level.getBlockEntity(pos);
|
2021-12-04 15:40:09 +01:00
|
|
|
if (tile instanceof BlockEntityWoodStand && !((BlockEntityWoodStand) tile).items.getStackInSlot(0).isEmpty()) {
|
|
|
|
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32, new PacketParticleStream(
|
2021-12-04 19:17:21 +01:00
|
|
|
(float) pos.getX() + 0.2F + this.level.random.nextFloat() * 0.6F,
|
2018-10-27 01:49:57 +02:00
|
|
|
(float) pos.getY() + 0.85F,
|
2021-12-04 19:17:21 +01:00
|
|
|
(float) pos.getZ() + 0.2F + this.level.random.nextFloat() * 0.6F,
|
|
|
|
this.ritualPos.getX() + 0.5F, this.ritualPos.getY() + this.level.random.nextFloat() * 3F + 2F, this.ritualPos.getZ() + 0.5F,
|
|
|
|
this.level.random.nextFloat() * 0.04F + 0.04F, 0x89cc37, this.level.random.nextFloat() + 1F
|
2020-01-22 23:21:52 +01:00
|
|
|
));
|
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
|
|
|
});
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
PacketHandler.sendToAllAround(this.level, this.ritualPos, 32,
|
2020-01-26 00:16:06 +01:00
|
|
|
new PacketParticles(this.ritualPos.getX(), this.ritualPos.getY(), this.ritualPos.getZ(), PacketParticles.Type.TR_GOLD_POWDER));
|
2018-10-16 11:49:30 +02:00
|
|
|
|
2018-10-27 01:49:57 +02:00
|
|
|
if (this.timer >= this.recipe.time) {
|
2018-11-07 13:33:49 +01:00
|
|
|
Multiblocks.TREE_RITUAL.forEach(this.ritualPos, 'G', (pos, matcher) -> {
|
2021-12-04 19:17:21 +01:00
|
|
|
this.level.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
|
2018-11-07 13:33:49 +01:00
|
|
|
return true;
|
|
|
|
});
|
2023-12-17 18:07:09 +01:00
|
|
|
Helper.mineRecursively(this.level, this.ritualPos, this.ritualPos, ItemStack.EMPTY, 6, 32, s -> s.is(BlockTags.LOGS) || s.getBlock() instanceof LeavesBlock);
|
2018-10-27 01:49:57 +02:00
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var item = new ItemEntity(this.level,
|
2018-10-27 01:49:57 +02:00
|
|
|
this.ritualPos.getX() + 0.5, this.ritualPos.getY() + 4.5, this.ritualPos.getZ() + 0.5,
|
|
|
|
this.recipe.result.copy());
|
2021-12-04 19:17:21 +01:00
|
|
|
this.level.addFreshEntity(item);
|
2018-10-27 01:49:57 +02:00
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32,
|
2021-12-04 19:17:21 +01:00
|
|
|
new PacketParticles((float) item.getX(), (float) item.getY(), (float) item.getZ(), PacketParticles.Type.TR_SPAWN_RESULT));
|
2021-12-04 15:40:09 +01:00
|
|
|
this.level.playSound(null, this.worldPosition.getX() + 0.5, this.worldPosition.getY() + 0.5, this.worldPosition.getZ() + 0.5,
|
2021-12-04 19:17:21 +01:00
|
|
|
SoundEvents.ENDERMAN_TELEPORT, SoundSource.BLOCKS, 0.65F, 1F);
|
2018-10-27 01:49:57 +02:00
|
|
|
|
|
|
|
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) -> {
|
2021-12-15 16:30:22 +01:00
|
|
|
var tile = this.level.getBlockEntity(pos);
|
2021-12-04 19:17:21 +01:00
|
|
|
if (tile instanceof BlockEntityWoodStand stand && !stand.items.getStackInSlot(0).isEmpty()) {
|
|
|
|
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32,
|
|
|
|
new PacketParticles(stand.worldPosition.getX(), stand.worldPosition.getY(), stand.worldPosition.getZ(), PacketParticles.Type.TR_CONSUME_ITEM));
|
|
|
|
this.level.playSound(null, stand.worldPosition.getX() + 0.5, stand.worldPosition.getY() + 0.5, stand.worldPosition.getZ() + 0.5,
|
|
|
|
SoundEvents.WOOD_STEP, SoundSource.BLOCKS, 0.5F, 1F);
|
|
|
|
|
|
|
|
stand.items.setStackInSlot(0, ItemStack.EMPTY);
|
|
|
|
stand.sendToClients();
|
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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-10-17 15:02:29 +02:00
|
|
|
private boolean isRitualOkay() {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!Multiblocks.TREE_RITUAL.isComplete(this.level, this.ritualPos)) {
|
2018-10-27 01:49:57 +02:00
|
|
|
return false;
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var i = 0; i < 2; i++) {
|
|
|
|
var state = this.level.getBlockState(this.ritualPos.above(i));
|
2022-06-27 15:24:04 +02:00
|
|
|
if (!state.is(BlockTags.LOGS))
|
2019-02-02 14:35:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
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));
|
2021-12-15 16:30:22 +01:00
|
|
|
var fine = Multiblocks.TREE_RITUAL.forEach(this.ritualPos, 'W', (pos, matcher) -> {
|
|
|
|
var tile = this.level.getBlockEntity(pos);
|
2021-12-04 15:40:09 +01:00
|
|
|
if (tile instanceof BlockEntityWoodStand) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var stack = ((BlockEntityWoodStand) tile).items.getStackInSlot(0);
|
2018-10-27 01:49:57 +02:00
|
|
|
if (!stack.isEmpty()) {
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var i = required.size() - 1; i >= 0; i--) {
|
|
|
|
var req = required.get(i);
|
2020-01-21 21:04:44 +01:00
|
|
|
if (req.test(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
|
2021-12-04 15:40:09 +01:00
|
|
|
public void writeNBT(CompoundTag compound, SaveType type) {
|
2018-11-04 16:38:09 +01:00
|
|
|
super.writeNBT(compound, type);
|
2018-11-05 16:36:10 +01:00
|
|
|
if (type != SaveType.BLOCK)
|
2020-01-21 21:04:44 +01:00
|
|
|
compound.put("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) {
|
2021-12-04 19:17:21 +01:00
|
|
|
compound.putLong("ritual_pos", this.ritualPos.asLong());
|
2020-01-21 21:04:44 +01:00
|
|
|
compound.putInt("timer", this.timer);
|
|
|
|
compound.putString("recipe", this.recipe.name.toString());
|
2018-10-16 18:07:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void readNBT(CompoundTag compound, SaveType type) {
|
2018-11-04 16:38:09 +01:00
|
|
|
super.readNBT(compound, type);
|
2018-11-05 16:36:10 +01:00
|
|
|
if (type != SaveType.BLOCK)
|
2020-01-21 21:04:44 +01:00
|
|
|
this.items.deserializeNBT(compound.getCompound("items"));
|
2018-10-16 18:07:03 +02:00
|
|
|
|
2018-11-04 16:38:09 +01:00
|
|
|
if (type == SaveType.TILE) {
|
2020-01-21 21:04:44 +01:00
|
|
|
if (compound.contains("recipe")) {
|
2021-12-04 19:17:21 +01:00
|
|
|
this.ritualPos = BlockPos.of(compound.getLong("ritual_pos"));
|
2020-01-21 21:04:44 +01:00
|
|
|
this.timer = compound.getInt("timer");
|
2021-12-04 15:40:09 +01:00
|
|
|
if (this.hasLevel())
|
2021-12-04 19:17:21 +01:00
|
|
|
this.recipe = (TreeRitualRecipe) this.level.getRecipeManager().byKey(new ResourceLocation(compound.getString("recipe"))).orElse(null);
|
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
|
2020-10-19 21:26:32 +02:00
|
|
|
public IItemHandlerModifiable getItemHandler() {
|
2018-10-18 13:34:37 +02:00
|
|
|
return this.items;
|
|
|
|
}
|
2023-12-17 18:07:09 +01:00
|
|
|
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|