NaturesAura/src/main/java/de/ellpeck/naturesaura/InternalHooks.java

186 lines
7.6 KiB
Java
Raw Normal View History

2018-11-11 13:26:19 +01:00
package de.ellpeck.naturesaura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.api.misc.ILevelData;
import de.ellpeck.naturesaura.api.multiblock.IMultiblock;
import de.ellpeck.naturesaura.blocks.multi.Multiblock;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.misc.LevelData;
2021-12-08 00:31:29 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
2019-02-20 23:49:21 +01:00
import net.minecraft.util.Tuple;
2021-12-08 00:31:29 +01:00
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.apache.commons.lang3.mutable.MutableFloat;
2018-11-11 13:26:19 +01:00
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.mutable.MutableObject;
import org.apache.commons.lang3.tuple.Pair;
2018-11-11 13:26:19 +01:00
import java.util.ArrayList;
import java.util.List;
2018-11-11 13:26:19 +01:00
import java.util.function.BiConsumer;
public class InternalHooks implements NaturesAuraAPI.IInternalHooks {
@Override
2021-12-04 15:40:09 +01:00
public boolean extractAuraFromPlayer(Player player, int amount, boolean simulate) {
return this.auraPlayerInteraction(player, amount, true, simulate);
}
@Override
2021-12-04 15:40:09 +01:00
public boolean insertAuraIntoPlayer(Player player, int amount, boolean simulate) {
return this.auraPlayerInteraction(player, amount, false, simulate);
}
2021-12-04 15:40:09 +01:00
private boolean auraPlayerInteraction(Player player, int amount, boolean extract, boolean simulate) {
2019-11-04 19:08:49 +01:00
if (extract && player.isCreative())
return true;
var stack = Helper.getEquippedItem(s -> s.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER).isPresent(), player, false);
2020-01-26 19:26:50 +01:00
if (!stack.isEmpty()) {
2021-12-23 13:27:52 +01:00
var container = stack.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER).orElse(null);
2020-01-26 19:26:50 +01:00
if (extract) {
return container.drainAura(amount, simulate) > 0;
2020-01-26 19:26:50 +01:00
} else {
return container.storeAura(amount, simulate) > 0;
}
}
return false;
}
2018-11-11 13:26:19 +01:00
@Override
2018-11-13 00:36:47 +01:00
public void spawnMagicParticle(double posX, double posY, double posZ, double motionX, double motionY, double motionZ, int color, float scale, int maxAge, float gravity, boolean collision, boolean fade) {
NaturesAura.proxy.spawnMagicParticle(posX, posY, posZ, motionX, motionY, motionZ, color, scale, maxAge, gravity, collision, fade);
}
@Override
public void spawnParticleStream(float startX, float startY, float startZ, float endX, float endY, float endZ, float speed, int color, float scale) {
2021-12-15 16:30:22 +01:00
var dir = new Vec3(endX - startX, endY - startY, endZ - startZ);
var length = dir.length();
2019-03-31 14:17:29 +02:00
if (length > 0) {
2020-01-23 20:57:56 +01:00
dir = dir.normalize();
2018-11-13 00:36:47 +01:00
this.spawnMagicParticle(startX, startY, startZ,
dir.x * speed, dir.y * speed, dir.z * speed,
2019-03-31 14:17:29 +02:00
color, scale, (int) (length / speed), 0F, false, false);
2018-11-13 00:36:47 +01:00
}
2018-11-11 13:26:19 +01:00
}
2019-02-19 17:23:03 +01:00
@Override
public void setParticleDepth(boolean depth) {
NaturesAura.proxy.setParticleDepth(depth);
}
@Override
public void setParticleSpawnRange(int range) {
NaturesAura.proxy.setParticleSpawnRange(range);
}
2020-02-28 15:36:12 +01:00
@Override
public void setParticleCulling(boolean cull) {
NaturesAura.proxy.setParticleCulling(cull);
}
@Override
public IMultiblock createMultiblock(ResourceLocation name, String[][] pattern, Object... rawMatchers) {
return new Multiblock(name, pattern, rawMatchers);
}
@Override
2021-12-08 00:31:29 +01:00
public List<Tuple<Vec3, Integer>> getActiveEffectPowders(Level level, AABB area, ResourceLocation name) {
List<Tuple<Vec3, Integer>> found = new ArrayList<>();
2021-12-15 16:30:22 +01:00
for (var powder : ((LevelData) ILevelData.getLevelData(level)).effectPowders.get(name))
2020-01-21 23:02:39 +01:00
if (area.contains(powder.getA()))
found.add(powder);
return found;
2019-02-20 23:49:21 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public boolean isEffectPowderActive(Level level, BlockPos pos, ResourceLocation name) {
2021-12-15 16:30:22 +01:00
var posVec = new Vec3(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5);
var powders = this.getActiveEffectPowders(level, new AABB(pos).inflate(64), name);
for (var powder : powders) {
var bounds = Helper.aabb(powder.getA()).inflate(powder.getB());
2020-01-21 23:02:39 +01:00
if (bounds.contains(posVec))
2019-02-20 23:49:21 +01:00
return true;
}
return false;
}
2018-11-11 13:26:19 +01:00
@Override
2021-12-04 15:40:09 +01:00
public void getAuraSpotsInArea(Level level, BlockPos pos, int radius, BiConsumer<BlockPos, Integer> consumer) {
Helper.getAuraChunksWithSpotsInArea(level, pos, radius, chunk -> chunk.getSpots(pos, radius, consumer));
2018-11-11 13:26:19 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public int getSpotAmountInArea(Level level, BlockPos pos, int radius) {
2021-12-15 16:30:22 +01:00
var result = new MutableInt();
Helper.getAuraChunksWithSpotsInArea(level, pos, radius, chunk -> result.add(chunk.getAuraAndSpotAmount(pos, radius).getRight()));
return result.intValue();
}
2018-11-11 13:26:19 +01:00
@Override
2021-12-04 15:40:09 +01:00
public int getAuraInArea(Level level, BlockPos pos, int radius) {
2021-12-15 16:30:22 +01:00
var result = new MutableInt(IAuraChunk.DEFAULT_AURA);
Helper.getAuraChunksWithSpotsInArea(level, pos, radius, chunk -> result.add(chunk.getAuraAndSpotAmount(pos, radius).getLeft()));
2018-11-11 13:26:19 +01:00
return result.intValue();
}
@Override
2021-12-04 15:40:09 +01:00
public Pair<Integer, Integer> getAuraAndSpotAmountInArea(Level level, BlockPos pos, int radius) {
2021-12-15 16:30:22 +01:00
var spots = new MutableInt();
var aura = new MutableInt(IAuraChunk.DEFAULT_AURA);
Helper.getAuraChunksWithSpotsInArea(level, pos, radius, chunk -> {
var auraAndSpots = chunk.getAuraAndSpotAmount(pos, radius);
aura.add(auraAndSpots.getLeft());
spots.add(auraAndSpots.getRight());
});
return Pair.of(aura.intValue(), spots.intValue());
}
@Override
2021-12-04 15:40:09 +01:00
public int triangulateAuraInArea(Level level, BlockPos pos, int radius) {
2021-12-15 16:30:22 +01:00
var result = new MutableFloat(IAuraChunk.DEFAULT_AURA);
2021-12-04 15:40:09 +01:00
IAuraChunk.getSpotsInArea(level, pos, radius, (blockPos, spot) -> {
2021-12-15 16:30:22 +01:00
var percentage = 1F - (float) Math.sqrt(pos.distSqr(blockPos)) / radius;
result.add(spot * percentage);
});
return result.intValue();
}
2018-11-11 13:26:19 +01:00
@Override
2021-12-04 15:40:09 +01:00
public BlockPos getLowestAuraDrainSpot(Level level, BlockPos pos, int radius, BlockPos defaultSpot) {
2021-12-15 16:30:22 +01:00
var lowestAmount = new MutableInt(Integer.MAX_VALUE);
var lowestSpot = new MutableObject<BlockPos>();
Helper.getAuraChunksWithSpotsInArea(level, pos, radius, c -> {
var spot = c.getLowestAndHighestSpots(pos, radius)[0];
if (spot.getRight() < lowestAmount.intValue()) {
lowestAmount.setValue(spot.getRight());
lowestSpot.setValue(spot.getLeft());
2018-11-11 13:26:19 +01:00
}
});
2021-12-15 16:30:22 +01:00
var lowest = lowestSpot.getValue();
if (lowest == null || lowestAmount.intValue() >= 0)
2018-11-11 13:26:19 +01:00
lowest = defaultSpot;
return lowest;
}
@Override
2021-12-04 15:40:09 +01:00
public BlockPos getHighestAuraDrainSpot(Level level, BlockPos pos, int radius, BlockPos defaultSpot) {
2021-12-15 16:30:22 +01:00
var highestAmount = new MutableInt(Integer.MIN_VALUE);
var highestSpot = new MutableObject<BlockPos>();
Helper.getAuraChunksWithSpotsInArea(level, pos, radius, c -> {
var spot = c.getLowestAndHighestSpots(pos, radius)[1];
if (spot.getRight() > highestAmount.intValue()) {
highestAmount.setValue(spot.getRight());
highestSpot.setValue(spot.getLeft());
2018-11-11 13:26:19 +01:00
}
});
2021-12-15 16:30:22 +01:00
var highest = highestSpot.getValue();
if (highest == null || highestAmount.intValue() <= 0)
2018-11-11 13:26:19 +01:00
highest = defaultSpot;
return highest;
}
2018-11-11 13:26:19 +01:00
}