NaturesAura/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java

170 lines
5.5 KiB
Java
Raw Normal View History

package de.ellpeck.naturesaura.chunk;
2018-11-12 22:04:40 +01:00
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
2018-11-11 13:26:19 +01:00
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.chunk.IDrainSpotEffect;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import de.ellpeck.naturesaura.packet.PacketAuraChunk;
import de.ellpeck.naturesaura.packet.PacketHandler;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import org.apache.commons.lang3.mutable.MutableInt;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
2018-11-11 13:26:19 +01:00
public class AuraChunk implements IAuraChunk {
private final Chunk chunk;
private final IAuraType type;
2018-10-24 13:06:24 +02:00
private final Map<BlockPos, MutableInt> drainSpots = new HashMap<>();
private final List<IDrainSpotEffect> effects = new ArrayList<>();
private boolean needsSync;
public AuraChunk(Chunk chunk, IAuraType type) {
this.chunk = chunk;
this.type = type;
for (Supplier<IDrainSpotEffect> supplier : NaturesAuraAPI.DRAIN_SPOT_EFFECTS.values()) {
IDrainSpotEffect effect = supplier.get();
if (effect.appliesHere(this.chunk, this, this.type))
this.effects.add(effect);
}
}
2018-11-11 13:26:19 +01:00
@Override
2018-10-24 13:06:24 +02:00
public void getSpotsInArea(BlockPos pos, int radius, BiConsumer<BlockPos, MutableInt> consumer) {
for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
BlockPos drainPos = entry.getKey();
if (drainPos.distanceSq(pos) <= radius * radius) {
consumer.accept(drainPos, entry.getValue());
}
}
}
2018-11-11 13:26:19 +01:00
@Override
public void drainAura(BlockPos pos, int amount) {
if (amount <= 0)
return;
this.getDrainSpot(pos).subtract(amount);
this.markDirty();
}
2018-11-11 13:26:19 +01:00
@Override
public void storeAura(BlockPos pos, int amount) {
if (amount <= 0)
return;
this.getDrainSpot(pos).add(amount);
this.markDirty();
}
2018-11-11 13:26:19 +01:00
@Override
public MutableInt getDrainSpot(BlockPos pos) {
2018-10-24 13:06:24 +02:00
MutableInt spot = this.drainSpots.get(pos);
if (spot == null) {
2018-10-24 13:06:24 +02:00
spot = new MutableInt();
this.drainSpots.put(pos, spot);
}
return spot;
}
2018-10-24 13:06:24 +02:00
public void setSpots(Map<BlockPos, MutableInt> spots) {
this.drainSpots.clear();
this.drainSpots.putAll(spots);
}
2018-11-11 13:26:19 +01:00
@Override
public IAuraType getType() {
return this.type;
}
2018-11-11 13:26:19 +01:00
@Override
public void markDirty() {
this.needsSync = true;
}
public void update() {
World world = this.chunk.getWorld();
Set<BlockPos> toClear = null;
for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
BlockPos pos = entry.getKey();
MutableInt amount = entry.getValue();
for (IDrainSpotEffect effect : this.effects) {
world.profiler.func_194340_a(() -> effect.getName().toString());
effect.update(world, this.chunk, this, pos, amount);
2018-10-31 12:16:42 +01:00
world.profiler.endSection();
}
if (amount.intValue() == 0) {
if (toClear == null)
toClear = new HashSet<>();
toClear.add(pos);
}
}
if (toClear != null) {
for (BlockPos spot : toClear)
this.drainSpots.remove(spot);
this.markDirty();
}
if (this.needsSync) {
PacketHandler.sendToAllLoaded(world,
new BlockPos(this.chunk.x * 16, 0, this.chunk.z * 16),
this.makePacket());
this.needsSync = false;
}
}
public IMessage makePacket() {
return new PacketAuraChunk(this.chunk.x, this.chunk.z, this.drainSpots);
}
@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
2018-11-12 22:04:40 +01:00
return capability == NaturesAuraAPI.capAuraChunk;
}
@Nullable
@Override
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
2018-11-12 22:04:40 +01:00
return capability == NaturesAuraAPI.capAuraChunk ? (T) this : null;
}
@Override
public NBTTagCompound serializeNBT() {
NBTTagList list = new NBTTagList();
2018-10-24 13:06:24 +02:00
for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
NBTTagCompound tag = new NBTTagCompound();
tag.setLong("pos", entry.getKey().toLong());
2018-10-24 13:06:24 +02:00
tag.setInteger("amount", entry.getValue().intValue());
list.appendTag(tag);
}
NBTTagCompound compound = new NBTTagCompound();
compound.setTag("drain_spots", list);
return compound;
}
@Override
public void deserializeNBT(NBTTagCompound compound) {
this.drainSpots.clear();
NBTTagList list = compound.getTagList("drain_spots", 10);
for (NBTBase base : list) {
NBTTagCompound tag = (NBTTagCompound) base;
this.drainSpots.put(
BlockPos.fromLong(tag.getLong("pos")),
2018-10-24 13:06:24 +02:00
new MutableInt(tag.getInteger("amount")));
}
}
}