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

238 lines
8.4 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;
2020-01-23 16:05:52 +01:00
import de.ellpeck.naturesaura.api.aura.chunk.IDrainSpotEffect.ActiveType;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import de.ellpeck.naturesaura.api.misc.IWorldData;
import de.ellpeck.naturesaura.misc.WorldData;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.packet.PacketAuraChunk;
import de.ellpeck.naturesaura.packet.PacketHandler;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
2020-01-21 21:04:44 +01:00
import net.minecraft.nbt.INBT;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Tuple;
import net.minecraft.util.math.BlockPos;
2020-01-22 23:21:52 +01:00
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import org.apache.commons.lang3.mutable.MutableInt;
2018-12-03 00:45:34 +01:00
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
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-12-03 00:45:34 +01:00
private final Map<BlockPos, MutableInt> drainSpots = new ConcurrentHashMap<>();
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
public int drainAura(BlockPos pos, int amount, boolean aimForZero, boolean simulate) {
if (amount <= 0)
return 0;
MutableInt spot = this.getActualDrainSpot(pos, !simulate);
int curr = spot != null ? spot.intValue() : 0;
2019-01-27 19:12:48 +01:00
if (curr < 0 && curr - amount > 0) // Underflow protection
return this.drainAura(pos.up(), amount, aimForZero, simulate);
if (aimForZero) {
if (curr > 0 && curr - amount < 0)
amount = curr;
}
if (!simulate) {
spot.subtract(amount);
2018-12-03 00:45:34 +01:00
if (spot.intValue() == 0)
this.drainSpots.remove(pos);
this.markDirty();
}
return amount;
}
2018-11-11 13:26:19 +01:00
@Override
public int drainAura(BlockPos pos, int amount) {
return this.drainAura(pos, amount, false, false);
}
@Override
public int storeAura(BlockPos pos, int amount, boolean aimForZero, boolean simulate) {
if (amount <= 0)
return 0;
MutableInt spot = this.getActualDrainSpot(pos, !simulate);
int curr = spot != null ? spot.intValue() : 0;
if (curr > 0 && curr + amount < 0) // Overflow protection
return this.storeAura(pos.up(), amount, aimForZero, simulate);
if (aimForZero) {
if (curr < 0 && curr + amount > 0) {
amount = -curr;
}
}
if (!simulate) {
spot.add(amount);
2018-12-03 00:45:34 +01:00
if (spot.intValue() == 0)
this.drainSpots.remove(pos);
this.markDirty();
}
return amount;
}
@Override
public int storeAura(BlockPos pos, int amount) {
return this.storeAura(pos, amount, true, false);
}
private MutableInt getActualDrainSpot(BlockPos pos, boolean make) {
2018-10-24 13:06:24 +02:00
MutableInt spot = this.drainSpots.get(pos);
if (spot == null && make) {
2018-10-24 13:06:24 +02:00
spot = new MutableInt();
this.addDrainSpot(pos, spot);
}
return spot;
}
@Override
public int getDrainSpot(BlockPos pos) {
MutableInt spot = this.getActualDrainSpot(pos, false);
return spot == null ? 0 : spot.intValue();
}
private void addDrainSpot(BlockPos pos, MutableInt spot) {
int expX = pos.getX() >> 4;
int expZ = pos.getZ() >> 4;
ChunkPos myPos = this.chunk.getPos();
2020-01-22 23:21:52 +01:00
if (expX != myPos.x || expZ != myPos.z)
throw new IllegalArgumentException("Tried to add drain spot " + pos + " to chunk at " + myPos.x + ", " + myPos.z + " when it should've been added to chunk at " + expX + ", " + expZ);
this.drainSpots.put(pos, spot);
}
2018-10-24 13:06:24 +02:00
public void setSpots(Map<BlockPos, MutableInt> spots) {
this.drainSpots.clear();
for (Map.Entry<BlockPos, MutableInt> entry : spots.entrySet())
this.addDrainSpot(entry.getKey(), entry.getValue());
this.addOrRemoveAsActive();
}
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.chunk.markDirty();
this.needsSync = true;
this.addOrRemoveAsActive();
}
public void update() {
World world = this.chunk.getWorld();
for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
BlockPos pos = entry.getKey();
MutableInt amount = entry.getValue();
2020-01-21 21:04:44 +01:00
for (IDrainSpotEffect effect : this.effects)
effect.update(world, this.chunk, this, pos, amount.intValue());
}
if (this.needsSync) {
ChunkPos pos = this.chunk.getPos();
2020-01-22 23:21:52 +01:00
PacketHandler.sendToAllLoaded(world,
new BlockPos(pos.x * 16, 0, pos.z * 16),
this.makePacket());
this.needsSync = false;
}
}
2020-01-22 23:21:52 +01:00
public PacketAuraChunk makePacket() {
ChunkPos pos = this.chunk.getPos();
2020-01-22 23:21:52 +01:00
return new PacketAuraChunk(pos.x, pos.z, this.drainSpots);
}
public void getSpotsInArea(BlockPos pos, int radius, BiConsumer<BlockPos, Integer> 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().intValue());
}
}
}
2019-10-20 22:30:49 +02:00
public void getActiveEffectIcons(PlayerEntity player, Map<ResourceLocation, Tuple<ItemStack, Boolean>> icons) {
for (IDrainSpotEffect effect : this.effects) {
Tuple<ItemStack, Boolean> alreadyThere = icons.get(effect.getName());
2020-01-21 21:04:44 +01:00
if (alreadyThere != null && alreadyThere.getB())
continue;
for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
BlockPos pos = entry.getKey();
MutableInt amount = entry.getValue();
2020-01-23 16:05:52 +01:00
ActiveType state = effect.isActiveHere(player, this.chunk, this, pos, amount.intValue());
if (state == ActiveType.INACTIVE)
continue;
ItemStack stack = effect.getDisplayIcon();
if (stack.isEmpty())
continue;
2020-01-23 16:05:52 +01:00
icons.put(effect.getName(), new Tuple<>(stack, state == ActiveType.INHIBITED));
}
}
}
@Override
2019-10-20 22:30:49 +02:00
public CompoundNBT serializeNBT() {
ListNBT list = new ListNBT();
2018-10-24 13:06:24 +02:00
for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
2019-10-20 22:30:49 +02:00
CompoundNBT tag = new CompoundNBT();
2020-01-21 21:04:44 +01:00
tag.putLong("pos", entry.getKey().toLong());
tag.putInt("amount", entry.getValue().intValue());
list.add(tag);
}
2019-10-20 22:30:49 +02:00
CompoundNBT compound = new CompoundNBT();
2020-01-21 21:04:44 +01:00
compound.put("drain_spots", list);
return compound;
}
@Override
2019-10-20 22:30:49 +02:00
public void deserializeNBT(CompoundNBT compound) {
this.drainSpots.clear();
2020-01-21 21:04:44 +01:00
ListNBT list = compound.getList("drain_spots", 10);
for (INBT base : list) {
2019-10-20 22:30:49 +02:00
CompoundNBT tag = (CompoundNBT) base;
this.addDrainSpot(
BlockPos.fromLong(tag.getLong("pos")),
2020-01-21 21:04:44 +01:00
new MutableInt(tag.getInt("amount")));
}
this.addOrRemoveAsActive();
}
private void addOrRemoveAsActive() {
long chunkPos = this.chunk.getPos().asLong();
WorldData data = (WorldData) IWorldData.getWorldData(this.chunk.getWorld());
if (this.drainSpots.size() > 0) {
data.auraChunksWithSpots.put(chunkPos, this);
} else {
data.auraChunksWithSpots.remove(chunkPos);
}
}
}