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

124 lines
4.7 KiB
Java
Raw Normal View History

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;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2020-05-05 19:43:27 +02:00
import net.minecraft.state.properties.BlockStateProperties;
2021-12-04 15:40:09 +01:00
import net.minecraft.tileentity.ITickableBlockEntity;
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-04 15:40:09 +01:00
public BlockEntityAuraTimer() {
2020-05-05 19:43:27 +02:00
super(ModTileEntities.AURA_TIMER);
}
@Override
public void onRedstonePowerChange(int newPower) {
if (this.redstonePower <= 0 && newPower > 0) {
this.timer = 0;
int 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() {
int total = this.getTotalTime();
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) {
2020-05-05 19:43:27 +02:00
int color = ItemAuraBottle.getType(this.itemHandler.getStackInSlot(0)).getColor();
NaturesAuraAPI.instance().spawnMagicParticle(
2021-12-04 15:40:09 +01:00
this.worldPosition.getX() + 1 / 16F + this.level.rand.nextFloat() * 14 / 16F,
this.worldPosition.getY() + 1 / 16F + this.level.rand.nextFloat() * 14 / 16F,
this.worldPosition.getZ() + 1 / 16F + this.level.rand.nextFloat() * 14 / 16F,
0, 0, 0, color, 1, 80 + this.level.rand.nextInt(50), 0, false, true);
2020-05-05 19:43:27 +02:00
}
return;
}
this.timer++;
if (this.timer >= total) {
this.timer = 0;
BlockState state = this.getBlockState();
2021-12-04 15:40:09 +01:00
this.level.setBlockState(this.worldPosition, state.with(BlockStateProperties.POWERED, true), 1);
this.level.getPendingBlockTicks().scheduleTick(this.worldPosition, state.getBlock(), 4);
2020-05-05 19:43:27 +02:00
int 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() {
ItemStack stack = this.itemHandler.getStackInSlot(0);
if (stack.isEmpty())
return 0;
Integer amount = TIMES.get(ItemAuraBottle.getType(stack));
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");
}
}
}