2019-03-19 17:21:06 +01:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
2020-01-24 20:49:09 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2020-01-21 21:04:44 +01:00
|
|
|
import net.minecraft.tileentity.ITickableTileEntity;
|
2019-03-19 17:21:06 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2020-01-24 20:49:09 +01:00
|
|
|
import net.minecraft.util.math.ChunkPos;
|
2019-03-19 17:21:06 +01:00
|
|
|
import net.minecraft.util.math.MathHelper;
|
2020-01-24 20:49:09 +01:00
|
|
|
import net.minecraft.world.server.ServerWorld;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Collectors;
|
2019-03-19 17:21:06 +01:00
|
|
|
|
2020-01-21 21:04:44 +01:00
|
|
|
public class TileEntityChunkLoader extends TileEntityImpl implements ITickableTileEntity {
|
2020-01-24 20:49:09 +01:00
|
|
|
|
|
|
|
private final List<ChunkPos> forcedChunks = new ArrayList<>();
|
|
|
|
|
2020-01-22 01:32:26 +01:00
|
|
|
public TileEntityChunkLoader() {
|
|
|
|
super(ModTileEntities.CHUNK_LOADER);
|
2020-01-21 21:04:44 +01:00
|
|
|
}
|
2019-03-19 17:21:06 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void validate() {
|
|
|
|
super.validate();
|
2020-01-24 20:49:09 +01:00
|
|
|
this.loadChunks(false);
|
2019-03-19 17:21:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-24 20:49:09 +01:00
|
|
|
public void remove() {
|
|
|
|
super.remove();
|
|
|
|
this.loadChunks(true);
|
2019-03-19 17:21:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRedstonePowerChange(int newPower) {
|
|
|
|
super.onRedstonePowerChange(newPower);
|
|
|
|
if (!this.world.isRemote) {
|
2020-01-24 20:49:09 +01:00
|
|
|
this.loadChunks(false);
|
2019-03-19 17:21:06 +01:00
|
|
|
this.sendToClients();
|
|
|
|
}
|
2020-01-24 20:49:09 +01:00
|
|
|
}
|
2019-03-19 17:21:06 +01:00
|
|
|
|
|
|
|
public int range() {
|
|
|
|
return this.redstonePower * 2;
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:49:09 +01:00
|
|
|
private void loadChunks(boolean unload) {
|
|
|
|
if (this.world.isRemote)
|
2019-03-19 17:21:06 +01:00
|
|
|
return;
|
2020-01-24 20:49:09 +01:00
|
|
|
ServerWorld world = (ServerWorld) this.world;
|
|
|
|
|
|
|
|
List<ChunkPos> shouldBeForced = new ArrayList<>();
|
|
|
|
if (!unload) {
|
|
|
|
int range = this.range();
|
|
|
|
if (range > 0) {
|
|
|
|
for (int x = (this.pos.getX() - range) >> 4; x <= (this.pos.getX() + range) >> 4; x++) {
|
|
|
|
for (int z = (this.pos.getZ() - range) >> 4; z <= (this.pos.getZ() + range) >> 4; z++) {
|
|
|
|
ChunkPos pos = new ChunkPos(x, z);
|
|
|
|
// Only force chunks that we're already forcing or that nobody else is forcing
|
|
|
|
if (this.forcedChunks.contains(pos) || !world.getForcedChunks().contains(pos.asLong()))
|
|
|
|
shouldBeForced.add(pos);
|
|
|
|
}
|
2019-03-19 17:21:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 20:49:09 +01:00
|
|
|
|
|
|
|
// Unforce all of the chunks that shouldn't be forced anymore
|
|
|
|
for (ChunkPos pos : this.forcedChunks) {
|
|
|
|
if (!shouldBeForced.contains(pos))
|
|
|
|
world.forceChunk(pos.x, pos.z, false);
|
|
|
|
}
|
|
|
|
this.forcedChunks.clear();
|
|
|
|
|
|
|
|
// Force all chunks that should be forced
|
|
|
|
for (ChunkPos pos : shouldBeForced) {
|
|
|
|
world.forceChunk(pos.x, pos.z, true);
|
|
|
|
this.forcedChunks.add(pos);
|
|
|
|
}
|
|
|
|
}
|
2019-03-19 17:21:06 +01:00
|
|
|
|
|
|
|
@Override
|
2020-01-21 21:04:44 +01:00
|
|
|
public void tick() {
|
2019-03-19 17:21:06 +01:00
|
|
|
if (!this.world.isRemote) {
|
2020-01-21 21:04:44 +01:00
|
|
|
if (this.world.getGameTime() % 20 != 0)
|
2019-03-19 17:21:06 +01:00
|
|
|
return;
|
|
|
|
int toUse = MathHelper.ceil(this.range() / 2F);
|
|
|
|
if (toUse > 0) {
|
|
|
|
BlockPos spot = IAuraChunk.getHighestSpot(this.world, this.pos, 35, this.pos);
|
|
|
|
IAuraChunk.getAuraChunk(this.world, spot).drainAura(spot, toUse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 20:49:09 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeNBT(CompoundNBT compound, SaveType type) {
|
|
|
|
super.writeNBT(compound, type);
|
|
|
|
if (type == SaveType.TILE)
|
|
|
|
compound.putLongArray("forced_chunks", this.forcedChunks.stream().map(ChunkPos::asLong).collect(Collectors.toList()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readNBT(CompoundNBT compound, SaveType type) {
|
|
|
|
super.readNBT(compound, type);
|
|
|
|
|
|
|
|
if (type == SaveType.TILE) {
|
|
|
|
this.forcedChunks.clear();
|
|
|
|
Arrays.stream(compound.getLongArray("forced_chunks")).mapToObj(ChunkPos::new).forEach(this.forcedChunks::add);
|
|
|
|
}
|
|
|
|
}
|
2019-03-19 17:21:06 +01:00
|
|
|
}
|