NaturesAura/src/main/java/de/ellpeck/naturesaura/chunk/effect/ReplenishingEffect.java

62 lines
2.3 KiB
Java
Raw Normal View History

package de.ellpeck.naturesaura.chunk.effect;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.NaturesAura;
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.chunk.ISpotDrainable;
import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import java.util.ArrayList;
import java.util.List;
public class ReplenishingEffect implements IDrainSpotEffect {
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "replenishing");
@Override
public void update(World world, Chunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
if (spot < 0) {
List<ISpotDrainable> tiles = new ArrayList<>();
Helper.getTileEntitiesInArea(world, pos, 25, tile -> {
2020-01-21 23:02:39 +01:00
IAuraContainer container = tile.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null);
if (container instanceof ISpotDrainable)
tiles.add((ISpotDrainable) container);
2018-11-14 19:14:03 +01:00
return false;
});
if (!tiles.isEmpty()) {
IAuraType type = IAuraType.forWorld(world);
for (int i = world.rand.nextInt(6); i >= 0; i--) {
ISpotDrainable tile = tiles.get(world.rand.nextInt(tiles.size()));
if (!tile.isAcceptableType(type))
continue;
int drained = tile.drainAuraPassively(-spot, false);
if (drained <= 0)
continue;
auraChunk.storeAura(pos, drained);
spot += drained;
if (spot >= drained) {
break;
}
}
}
}
}
@Override
public boolean appliesHere(Chunk chunk, IAuraChunk auraChunk, IAuraType type) {
return true;
}
@Override
public ResourceLocation getName() {
return NAME;
}
}