2020-05-05 19:43:27 +02:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
|
|
|
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
|
|
|
|
import de.ellpeck.naturesaura.items.ItemAuraBottle;
|
|
|
|
import de.ellpeck.naturesaura.items.ModItems;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
2021-12-06 14:38:12 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
2021-12-06 14:38:12 +01:00
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
2020-05-05 19:43:27 +02:00
|
|
|
import net.minecraftforge.items.IItemHandlerModifiable;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
public class BlockEntityAuraTimer extends BlockEntityImpl implements ITickableBlockEntity {
|
2020-05-05 19:43:27 +02:00
|
|
|
|
|
|
|
private static final Map<IAuraType, Integer> TIMES = ImmutableMap.<IAuraType, Integer>builder()
|
|
|
|
.put(NaturesAuraAPI.TYPE_OVERWORLD, 20)
|
|
|
|
.put(NaturesAuraAPI.TYPE_NETHER, 20 * 60)
|
|
|
|
.put(NaturesAuraAPI.TYPE_END, 20 * 60 * 60).build();
|
|
|
|
private final ItemStackHandlerNA itemHandler = new ItemStackHandlerNA(1, this, true) {
|
|
|
|
@Override
|
|
|
|
protected boolean canInsert(ItemStack stack, int slot) {
|
|
|
|
return stack.getItem() == ModItems.AURA_BOTTLE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private int timer;
|
|
|
|
|
2021-12-06 14:38:12 +01:00
|
|
|
public BlockEntityAuraTimer(BlockPos pos, BlockState state) {
|
2021-12-19 15:32:45 +01:00
|
|
|
super(ModBlockEntities.AURA_TIMER, pos, state);
|
2020-05-05 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRedstonePowerChange(int newPower) {
|
|
|
|
if (this.redstonePower <= 0 && newPower > 0) {
|
|
|
|
this.timer = 0;
|
2021-12-15 16:30:22 +01:00
|
|
|
var color = ItemAuraBottle.getType(this.itemHandler.getStackInSlot(0)).getColor();
|
2021-12-04 15:40:09 +01:00
|
|
|
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32, new PacketParticles(this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), PacketParticles.Type.TIMER_RESET, color));
|
2020-05-05 19:43:27 +02:00
|
|
|
this.sendToClients();
|
|
|
|
}
|
|
|
|
super.onRedstonePowerChange(newPower);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void tick() {
|
2021-12-15 16:30:22 +01:00
|
|
|
var total = this.getTotalTime();
|
2020-05-05 19:43:27 +02:00
|
|
|
if (total <= 0) {
|
|
|
|
this.timer = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
if (this.level.isClientSide) {
|
|
|
|
if (this.level.getGameTime() % 8 == 0) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var color = ItemAuraBottle.getType(this.itemHandler.getStackInSlot(0)).getColor();
|
2020-05-05 19:43:27 +02:00
|
|
|
NaturesAuraAPI.instance().spawnMagicParticle(
|
2021-12-06 14:38:12 +01:00
|
|
|
this.worldPosition.getX() + 1 / 16F + this.level.random.nextFloat() * 14 / 16F,
|
|
|
|
this.worldPosition.getY() + 1 / 16F + this.level.random.nextFloat() * 14 / 16F,
|
|
|
|
this.worldPosition.getZ() + 1 / 16F + this.level.random.nextFloat() * 14 / 16F,
|
|
|
|
0, 0, 0, color, 1, 80 + this.level.random.nextInt(50), 0, false, true);
|
2020-05-05 19:43:27 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.timer++;
|
|
|
|
if (this.timer >= total) {
|
|
|
|
this.timer = 0;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var state = this.getBlockState();
|
2021-12-06 14:38:12 +01:00
|
|
|
this.level.setBlock(this.worldPosition, state.setValue(BlockStateProperties.POWERED, true), 1);
|
|
|
|
this.level.scheduleTick(this.worldPosition, state.getBlock(), 4);
|
2020-05-05 19:43:27 +02:00
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var color = ItemAuraBottle.getType(this.itemHandler.getStackInSlot(0)).getColor();
|
2021-12-04 15:40:09 +01:00
|
|
|
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32, new PacketParticles(this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), PacketParticles.Type.TIMER_RESET, color));
|
2020-05-05 19:43:27 +02:00
|
|
|
}
|
|
|
|
if (this.timer % 2 == 0)
|
|
|
|
this.sendToClients();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getTotalTime() {
|
2021-12-15 16:30:22 +01:00
|
|
|
var stack = this.itemHandler.getStackInSlot(0);
|
2020-05-05 19:43:27 +02:00
|
|
|
if (stack.isEmpty())
|
|
|
|
return 0;
|
2021-12-15 16:30:22 +01:00
|
|
|
var amount = TIMES.get(ItemAuraBottle.getType(stack));
|
2020-05-05 19:43:27 +02:00
|
|
|
if (amount == null)
|
|
|
|
return 0;
|
|
|
|
return amount * stack.getCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getTimeLeft() {
|
|
|
|
return this.getTotalTime() - this.timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getTimerPercentage() {
|
|
|
|
return this.timer / (float) this.getTotalTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-19 21:26:32 +02:00
|
|
|
public IItemHandlerModifiable getItemHandler() {
|
2020-05-05 19:43:27 +02:00
|
|
|
return this.itemHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void writeNBT(CompoundTag compound, SaveType type) {
|
2020-05-05 19:43:27 +02:00
|
|
|
super.writeNBT(compound, type);
|
|
|
|
if (type != SaveType.BLOCK) {
|
|
|
|
compound.put("items", this.itemHandler.serializeNBT());
|
|
|
|
compound.putInt("timer", this.timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void readNBT(CompoundTag compound, SaveType type) {
|
2020-05-05 19:43:27 +02:00
|
|
|
super.readNBT(compound, type);
|
|
|
|
if (type != SaveType.BLOCK) {
|
|
|
|
this.itemHandler.deserializeNBT(compound.getCompound("items"));
|
|
|
|
this.timer = compound.getInt("timer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|