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

124 lines
4.9 KiB
Java
Raw Normal View History

2020-10-24 15:58:38 +02:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.misc.WeatherType;
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.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
2020-10-24 15:58:38 +02:00
import org.apache.commons.lang3.tuple.Pair;
import java.util.List;
import java.util.Map;
2021-12-04 15:40:09 +01:00
public class BlockEntityWeatherChanger extends BlockEntityImpl implements ITickableBlockEntity {
2020-10-24 15:58:38 +02:00
private int processTime;
private WeatherType type;
private int itemAmount;
2021-12-08 00:31:29 +01:00
public BlockEntityWeatherChanger(BlockPos pos, BlockState state) {
super(ModTileEntities.WEATHER_CHANGER, pos, state);
2020-10-24 15:58:38 +02:00
}
@Override
public void tick() {
2021-12-04 15:40:09 +01:00
if (this.level.isClientSide) {
if (this.level.getGameTime() % 10 != 0)
2020-10-24 15:58:38 +02:00
return;
if (this.processTime <= 0)
return;
int color = this.type == WeatherType.SUN ? 0xf5d742 : this.type == WeatherType.RAIN ? 0x4d5ae3 : 0x373247;
for (int r = 0; r < 360; r += 20) {
double xOff = Math.cos(Math.toRadians(r)) * 3F;
double zOff = Math.sin(Math.toRadians(r)) * 3F;
2021-12-08 00:31:29 +01:00
for (int i = this.level.random.nextInt(3); i > 0; i--) {
2020-10-24 15:58:38 +02:00
NaturesAuraAPI.instance().spawnMagicParticle(
2021-12-04 15:40:09 +01:00
this.worldPosition.getX() + 0.5F + xOff,
this.worldPosition.getY(),
this.worldPosition.getZ() + 0.5F + zOff,
2021-12-08 00:31:29 +01:00
this.level.random.nextGaussian() * 0.02F,
this.level.random.nextFloat() * 0.1F + 0.1F,
this.level.random.nextGaussian() * 0.02F,
color, this.level.random.nextFloat() * 2 + 1, this.level.random.nextInt(80) + 80, 0, false, true);
2020-10-24 15:58:38 +02:00
}
}
return;
}
if (this.processTime > 0) {
if (this.processTime % 20 == 0) {
2021-12-04 15:40:09 +01:00
BlockPos spot = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 35, this.worldPosition);
IAuraChunk.getAuraChunk(this.level, spot).drainAura(spot, 30000 * this.itemAmount);
2020-10-24 15:58:38 +02:00
}
this.processTime--;
if (this.processTime <= 0) {
this.sendToClients();
int time = 6000 * this.itemAmount;
2021-12-04 15:40:09 +01:00
ServerLevel server = (ServerLevel) this.level;
2020-10-24 15:58:38 +02:00
switch (this.type) {
2021-12-08 00:31:29 +01:00
case SUN -> server.setWeatherParameters(time, 0, false, false);
case RAIN -> server.setWeatherParameters(0, time, true, false);
case THUNDERSTORM -> server.setWeatherParameters(0, time, true, true);
2020-10-24 15:58:38 +02:00
}
}
} else {
2021-12-04 15:40:09 +01:00
if (this.level.getGameTime() % 20 != 0)
2020-10-24 15:58:38 +02:00
return;
Pair<WeatherType, Integer> type = this.getNextWeatherType();
if (type == null)
return;
this.type = type.getLeft();
this.itemAmount = type.getRight();
this.processTime = 100;
this.sendToClients();
}
}
@Override
2021-12-04 15:40:09 +01:00
public void writeNBT(CompoundTag compound, SaveType type) {
2020-10-24 15:58:38 +02:00
super.writeNBT(compound, type);
if (type != SaveType.BLOCK) {
compound.putInt("time", this.processTime);
if (this.type != null)
compound.putInt("weather", this.type.ordinal());
compound.putInt("amount", this.itemAmount);
}
}
@Override
2021-12-04 15:40:09 +01:00
public void readNBT(CompoundTag compound, SaveType type) {
2020-10-24 15:58:38 +02:00
super.readNBT(compound, type);
if (type != SaveType.BLOCK) {
this.processTime = compound.getInt("time");
this.type = WeatherType.values()[compound.getInt("weather")];
this.itemAmount = compound.getInt("amount");
}
}
private Pair<WeatherType, Integer> getNextWeatherType() {
2021-12-08 00:31:29 +01:00
AABB area = new AABB(this.worldPosition).inflate(2);
List<ItemEntity> items = this.level.getEntitiesOfClass(ItemEntity.class, area, Entity::isAlive);
2020-10-24 15:58:38 +02:00
for (ItemEntity entity : items) {
2021-12-08 00:31:29 +01:00
if (entity.hasPickUpDelay())
2020-10-24 15:58:38 +02:00
continue;
ItemStack stack = entity.getItem();
for (Map.Entry<ItemStack, WeatherType> entry : NaturesAuraAPI.WEATHER_CHANGER_CONVERSIONS.entrySet()) {
if (!Helper.areItemsEqual(stack, entry.getKey(), true))
continue;
entity.setItem(ItemStack.EMPTY);
2021-12-08 00:31:29 +01:00
entity.kill();
2020-10-24 15:58:38 +02:00
return Pair.of(entry.getValue(), stack.getCount());
}
}
return null;
}
}