From 4a9976aa91b15707454e95394f4745f0d783c14b Mon Sep 17 00:00:00 2001 From: Vincent Lee Date: Fri, 17 Jul 2020 12:10:47 -0500 Subject: [PATCH] Cleanups in mana network --- .../api/internal/DummyManaNetwork.java | 7 ++-- .../botania/api/internal/IManaNetwork.java | 27 +++++++------- .../core/handler/ManaNetworkHandler.java | 35 ++++++++++++++----- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/main/java/vazkii/botania/api/internal/DummyManaNetwork.java b/src/main/java/vazkii/botania/api/internal/DummyManaNetwork.java index 434ecdbc4..41c8b0519 100644 --- a/src/main/java/vazkii/botania/api/internal/DummyManaNetwork.java +++ b/src/main/java/vazkii/botania/api/internal/DummyManaNetwork.java @@ -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 getAllCollectorsInWorld(World world) { - return ImmutableSet.of(); + return Collections.emptySet(); } @Override public Set getAllPoolsInWorld(World world) { - return ImmutableSet.of(); + return Collections.emptySet(); } } diff --git a/src/main/java/vazkii/botania/api/internal/IManaNetwork.java b/src/main/java/vazkii/botania/api/internal/IManaNetwork.java index eeced744e..bf6d874fa 100644 --- a/src/main/java/vazkii/botania/api/internal/IManaNetwork.java +++ b/src/main/java/vazkii/botania/api/internal/IManaNetwork.java @@ -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.
- * A way of getting the dimension is via world.provider.dimensionId
* 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.
- * A way of getting the dimension is via world.provider.dimensionId
* 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 getAllCollectorsInWorld(World world); + Set 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 getAllPoolsInWorld(World world); + Set getAllPoolsInWorld(World world); } diff --git a/src/main/java/vazkii/botania/common/core/handler/ManaNetworkHandler.java b/src/main/java/vazkii/botania/common/core/handler/ManaNetworkHandler.java index 98dc59785..a69175f29 100644 --- a/src/main/java/vazkii/botania/common/core/handler/ManaNetworkHandler.java +++ b/src/main/java/vazkii/botania/common/core/handler/ManaNetworkHandler.java @@ -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 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 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> map, TileEntity tile) { @@ -105,7 +117,12 @@ public final class ManaNetworkHandler implements IManaNetwork { } private Set getAllInWorld(Map> map, World world) { - return map.getOrDefault(world, new HashSet<>()); + Set ret = map.get(world); + if (ret == null) { + return Collections.emptySet(); + } else { + return Collections.unmodifiableSet(ret); + } } }