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

90 lines
3.1 KiB
Java
Raw Normal View History

2020-09-28 16:45:37 +02:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2021-12-08 00:31:29 +01:00
import net.minecraft.core.BlockPos;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-08 00:31:29 +01:00
import net.minecraft.world.entity.monster.MagmaCube;
import net.minecraft.world.entity.monster.Slime;
import net.minecraft.world.level.block.state.BlockState;
2020-09-28 16:45:37 +02:00
2021-12-04 15:40:09 +01:00
public class BlockEntitySlimeSplitGenerator extends BlockEntityImpl implements ITickableBlockEntity {
2020-09-28 16:45:37 +02:00
private int generationTimer;
private int amountToRelease;
private int color;
2021-12-08 00:31:29 +01:00
public BlockEntitySlimeSplitGenerator(BlockPos pos, BlockState state) {
super(ModTileEntities.SLIME_SPLIT_GENERATOR, pos, state);
2020-09-28 16:45:37 +02:00
}
@Override
public void tick() {
2021-12-04 15:40:09 +01:00
if (this.level.isClientSide || this.level.getGameTime() % 10 != 0)
2020-09-28 16:45:37 +02:00
return;
if (this.generationTimer > 0) {
2021-12-15 16:30:22 +01:00
var amount = this.amountToRelease * 10;
2021-03-30 15:44:31 +02:00
if (this.canGenerateRightNow(amount)) {
this.generateAura(amount);
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.SLIME_SPLIT_GEN_CREATE, this.color));
2020-09-28 16:45:37 +02:00
}
this.generationTimer -= 10;
}
}
@Override
public boolean wantsLimitRemover() {
return true;
}
public boolean isBusy() {
return this.generationTimer > 0;
}
2021-12-08 00:31:29 +01:00
public void startGenerating(Slime slime) {
2021-12-15 16:30:22 +01:00
var size = slime.getSize();
2020-09-28 16:45:37 +02:00
this.generationTimer = size * 30;
this.amountToRelease = (size * this.getGenerationAmount(slime)) / this.generationTimer;
this.color = this.getSlimeColor(slime);
2021-12-08 00:31:29 +01:00
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32, new PacketParticles((float) slime.getX(), (float) slime.getY(), (float) slime.getZ(), PacketParticles.Type.SLIME_SPLIT_GEN_START,
2021-12-04 15:40:09 +01:00
this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), this.color));
2020-09-28 16:45:37 +02:00
}
@Override
2021-12-04 15:40:09 +01:00
public void writeNBT(CompoundTag compound, SaveType type) {
2020-09-28 16:45:37 +02:00
super.writeNBT(compound, type);
if (type == SaveType.TILE) {
compound.putInt("timer", this.generationTimer);
compound.putInt("amount", this.amountToRelease);
compound.putInt("color", this.color);
}
}
@Override
2021-12-04 15:40:09 +01:00
public void readNBT(CompoundTag compound, SaveType type) {
2020-09-28 16:45:37 +02:00
super.readNBT(compound, type);
if (type == SaveType.TILE) {
this.generationTimer = compound.getInt("timer");
this.amountToRelease = compound.getInt("amount");
this.color = compound.getInt("color");
}
}
2021-12-08 00:31:29 +01:00
private int getSlimeColor(Slime slime) {
if (slime instanceof MagmaCube) {
2020-09-28 16:45:37 +02:00
return 0x942516;
} else {
2020-09-28 17:18:40 +02:00
return 0x4da84f;
2020-09-28 16:45:37 +02:00
}
}
2021-12-08 00:31:29 +01:00
private int getGenerationAmount(Slime slime) {
if (slime instanceof MagmaCube) {
2020-09-28 16:45:37 +02:00
return 45000;
} else {
return 25000;
}
}
}