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

63 lines
2.4 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.type.IAuraType;
import de.ellpeck.naturesaura.chunk.AuraChunk;
2021-12-15 16:24:53 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.LevelChunk;
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(Level level, LevelChunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot, AuraChunk.DrainSpot actualSpot) {
if (spot < 0) {
List<ISpotDrainable> tiles = new ArrayList<>();
2021-12-04 19:17:21 +01:00
Helper.getBlockEntitiesInArea(level, pos, 25, tile -> {
2024-03-10 15:54:58 +01:00
var container = level.getCapability(NaturesAuraAPI.AURA_CONTAINER_BLOCK_CAPABILITY, tile.getBlockPos(), tile.getBlockState(), tile, null);
2020-01-21 23:02:39 +01:00
if (container instanceof ISpotDrainable)
tiles.add((ISpotDrainable) container);
2018-11-14 19:14:03 +01:00
return false;
});
if (!tiles.isEmpty()) {
2021-12-15 16:30:22 +01:00
var type = IAuraType.forLevel(level);
for (var i = level.random.nextInt(6); i >= 0; i--) {
var tile = tiles.get(level.random.nextInt(tiles.size()));
if (!tile.isAcceptableType(type))
continue;
2021-12-15 16:30:22 +01:00
var drained = tile.drainAuraPassively(-spot, false);
if (drained <= 0)
continue;
auraChunk.storeAura(pos, drained);
spot += drained;
if (spot >= drained) {
break;
}
}
}
}
}
@Override
2021-12-15 16:24:53 +01:00
public boolean appliesHere(LevelChunk chunk, IAuraChunk auraChunk, IAuraType type) {
return true;
}
@Override
public ResourceLocation getName() {
2022-06-27 15:24:04 +02:00
return ReplenishingEffect.NAME;
}
2024-03-10 15:54:58 +01:00
}