Cleanups in mana network

This commit is contained in:
Vincent Lee committed 2020-07-17 12:10:47 -05:00
1 parent 32bf8054ce
commit 4a9976aa91
3 files changed
+41 -28

No files matched your search

@@ -8,12 +8,11 @@
*/
package vazkii.botania.api.internal;
import com.google.common.collect.ImmutableSet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.Collections;
import java.util.Set;
public class DummyManaNetwork implements IManaNetwork {
@@ -35,12 +34,12 @@ public class DummyManaNetwork implements IManaNetwork {
@Override
public Set<TileEntity> getAllCollectorsInWorld(World world) {
return ImmutableSet.of();
return Collections.emptySet();
}
@Override
public Set<TileEntity> getAllPoolsInWorld(World world) {
return ImmutableSet.of();
return Collections.emptySet();
}
}
@@ -12,12 +12,12 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import javax.annotation.Nullable;
import java.util.Set;
/**
* A basic interface for a world's Mana Network.
*
* @see IInternalMethodHandler#getManaNetworkInstance()
*/
public interface IManaNetwork {
@@ -25,43 +25,40 @@ public interface IManaNetwork {
* Clears the entire Mana Network of all it's contents, you probably
* don't want to call this unless you have a very good reason.
*/
public void clear();
void clear();
/**
* Gets the closest Mana Collector (eg. Mana Spreader) in the network to the Chunk
* Coordinates passed in, in the given dimension.<br>
* A way of getting the dimension is via world.provider.dimensionId<br>
* Note that this function *can* get performance intensive, it's reccomended you
* call it sparingly and take cache of the value returned.
*
* @param limit The maximum distance the closest block can be, if the closest block
* is farther away than that, null will be returned instead.
*/
public TileEntity getClosestCollector(BlockPos pos, World world, int limit);
@Nullable
TileEntity getClosestCollector(BlockPos pos, World world, int limit);
/**
* Gets the closest Mana Pool in the network to the Chunk Coordinates passed in,
* in the given dimension.<br>
* A way of getting the dimension is via world.provider.dimensionId<br>
* Note that this function *can* get performance intensive, it's reccomended you
* call it sparingly and take cache of the value returned.
*
* @param limit The maximum distance the closest block can be, if the closest block
* is farther away than that, null will be returned instead.
*/
public TileEntity getClosestPool(BlockPos pos, World world, int limit);
@Nullable
TileEntity getClosestPool(BlockPos pos, World world, int limit);
/**
* Gets the set of all Mana Collectors (eg. Mana Spreader) in the dimension
* passed in. Note that this is the actual set and not a copy, make sure to
* clone the set if you intend to change it in any way.
* Gets read-only view of all Mana Collectors (eg. Mana Spreader) in the dimension
* passed in.
*/
public Set<TileEntity> getAllCollectorsInWorld(World world);
Set<TileEntity> getAllCollectorsInWorld(World world);
/**
* Gets the set of all Mana Pools in the dimension passed in. Note that this
* is the actual set and not a copy, make sure to clone the set if you intend
* to change it in any way.
* Gets read-only view of all Mana Pools in the dimension passed in.
*/
public Set<TileEntity> getAllPoolsInWorld(World world);
Set<TileEntity> getAllPoolsInWorld(World world);
}
@@ -17,6 +17,8 @@ import vazkii.botania.api.mana.ManaNetworkEvent;
import vazkii.botania.api.mana.ManaNetworkEvent.Action;
import vazkii.botania.api.mana.ManaNetworkEvent.ManaBlockType;
import javax.annotation.Nullable;
import java.util.*;
import java.util.function.BinaryOperator;
@@ -45,7 +47,7 @@ public final class ManaNetworkHandler implements IManaNetwork {
@Override
public TileEntity getClosestPool(BlockPos pos, World world, int limit) {
if (manaPools.containsKey(world)) {
return getClosest(manaPools.get(world), pos, world.isRemote, limit);
return getClosest(manaPools.get(world), pos, limit);
}
return null;
}
@@ -53,7 +55,7 @@ public final class ManaNetworkHandler implements IManaNetwork {
@Override
public TileEntity getClosestCollector(BlockPos pos, World world, int limit) {
if (manaCollectors.containsKey(world)) {
return getClosest(manaCollectors.get(world), pos, world.isRemote, limit);
return getClosest(manaCollectors.get(world), pos, limit);
}
return null;
}
@@ -71,12 +73,22 @@ public final class ManaNetworkHandler implements IManaNetwork {
return set != null && set.contains(tile);
}
private TileEntity getClosest(Set<TileEntity> tiles, BlockPos pos, boolean remoteCheck, int limit) {
return tiles.stream()
.filter(t -> t.getWorld().isRemote == remoteCheck && !t.isRemoved()
&& t.getPos().distanceSq(pos) <= limit * limit)
.reduce(BinaryOperator.minBy(Comparator.comparing(t -> t.getPos().distanceSq(pos), Double::compare)))
.orElse(null);
@Nullable
private TileEntity getClosest(Set<TileEntity> tiles, BlockPos pos, int limit) {
double minDist = Double.MAX_VALUE;
TileEntity closest = null;
for (TileEntity te : tiles) {
if (!te.isRemoved()) {
double distance = te.getPos().distanceSq(pos);
if (distance <= limit * limit && distance < minDist) {
minDist = distance;
closest = te;
}
}
}
return closest;
}
private void remove(Map<World, Set<TileEntity>> map, TileEntity tile) {
@@ -105,7 +117,12 @@ public final class ManaNetworkHandler implements IManaNetwork {
}
private Set<TileEntity> getAllInWorld(Map<World, Set<TileEntity>> map, World world) {
return map.getOrDefault(world, new HashSet<>());
Set<TileEntity> ret = map.get(world);
if (ret == null) {
return Collections.emptySet();
} else {
return Collections.unmodifiableSet(ret);
}
}
}