diff --git a/src/main/java/vazkii/botania/api/internal/DummyManaNetwork.java b/src/main/java/vazkii/botania/api/internal/DummyManaNetwork.java index e1425597a..111cd93af 100644 --- a/src/main/java/vazkii/botania/api/internal/DummyManaNetwork.java +++ b/src/main/java/vazkii/botania/api/internal/DummyManaNetwork.java @@ -11,12 +11,14 @@ package vazkii.botania.api.internal; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import vazkii.botania.api.mana.TileSignature; import java.util.List; +import java.util.Set; public class DummyManaNetwork implements IManaNetwork { @@ -38,13 +40,13 @@ public class DummyManaNetwork implements IManaNetwork { } @Override - public List getAllCollectorsInWorld(World world) { - return ImmutableList.of(); + public Set getAllCollectorsInWorld(World world) { + return ImmutableSet.of(); } @Override - public List getAllPoolsInWorld(World world) { - return ImmutableList.of(); + public Set getAllPoolsInWorld(World world) { + return ImmutableSet.of(); } } diff --git a/src/main/java/vazkii/botania/api/internal/IManaNetwork.java b/src/main/java/vazkii/botania/api/internal/IManaNetwork.java index 7f1488fea..2a44e7a15 100644 --- a/src/main/java/vazkii/botania/api/internal/IManaNetwork.java +++ b/src/main/java/vazkii/botania/api/internal/IManaNetwork.java @@ -16,6 +16,7 @@ import net.minecraft.world.World; import vazkii.botania.api.mana.TileSignature; import java.util.List; +import java.util.Set; /** * A basic interface for a world's Mana Network. @@ -52,16 +53,16 @@ public interface IManaNetwork { public TileEntity getClosestPool(BlockPos pos, World world, int limit); /** - * Gets the list of all Mana Collectors (eg. Mana Spreader) in the dimension - * passed in. Note that this is the actual list and not a copy, make sure to - * clone the list if you intend to change it in any way. + * 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. */ - public List getAllCollectorsInWorld(World world); + public Set getAllCollectorsInWorld(World world); /** - * Gets the list of all Mana Pools in the dimension passed in. Note that this - * is the actual list and not a copy, make sure to clone the list if you intend + * 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. */ - public List getAllPoolsInWorld(World world); + public 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 23ae8fc87..e61b7d533 100644 --- a/src/main/java/vazkii/botania/common/core/handler/ManaNetworkHandler.java +++ b/src/main/java/vazkii/botania/common/core/handler/ManaNetworkHandler.java @@ -11,8 +11,10 @@ package vazkii.botania.common.core.handler; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.WeakHashMap; import net.minecraft.tileentity.TileEntity; @@ -30,12 +32,12 @@ public final class ManaNetworkHandler implements IManaNetwork { public static final ManaNetworkHandler instance = new ManaNetworkHandler(); - private final WeakHashMap> manaPools = new WeakHashMap<>(); - private final WeakHashMap> manaCollectors = new WeakHashMap<>(); + private final WeakHashMap> manaPools = new WeakHashMap<>(); + private final WeakHashMap> manaCollectors = new WeakHashMap<>(); @SubscribeEvent public void onNetworkEvent(ManaNetworkEvent event) { - Map> map = event.type == ManaBlockType.COLLECTOR ? manaCollectors : manaPools; + Map> map = event.type == ManaBlockType.COLLECTOR ? manaCollectors : manaPools; if(event.action == Action.ADD) add(map, event.tile); else remove(map, event.tile); @@ -69,19 +71,12 @@ public final class ManaNetworkHandler implements IManaNetwork { return isIn(tile, manaPools); } - private synchronized boolean isIn(TileEntity tile, Map> map) { - List list = map.get(tile.getWorld()); - if(list == null) - return false; - - for(TileSignature sig : list) - if(sig.tile == tile) - return true; - - return false; + private synchronized boolean isIn(TileEntity tile, Map> map) { + Set set = map.get(tile.getWorld()); + return set != null && set.contains(new TileSignature(tile, tile.getWorld().isRemote)); } - private synchronized TileEntity getClosest(List tiles, BlockPos pos, boolean remoteCheck, int limit) { + private synchronized TileEntity getClosest(Set tiles, BlockPos pos, boolean remoteCheck, int limit) { float closest = Float.MAX_VALUE; TileEntity closestTile = null; @@ -105,46 +100,38 @@ public final class ManaNetworkHandler implements IManaNetwork { return closestTile; } - private synchronized void remove(Map> map, TileEntity tile) { + private synchronized void remove(Map> map, TileEntity tile) { World world = tile.getWorld(); if(!map.containsKey(world)) return; - List sigs = map.get(world); - for(TileSignature sig : sigs) - if(sig.tile.equals(tile)) { - sigs.remove(sig); - break; - } + map.get(world).remove(new TileSignature(tile, tile.getWorld().isRemote)); } - private synchronized void add(Map> map, TileEntity tile) { + private synchronized void add(Map> map, TileEntity tile) { World world = tile.getWorld(); - List tiles; + Set tiles; if(!map.containsKey(world)) - map.put(world, new ArrayList<>()); + map.put(world, new HashSet<>()); - tiles = map.get(world); - - if(!tiles.contains(tile)) - tiles.add(new TileSignature(tile, tile.getWorld().isRemote)); + map.get(world).add(new TileSignature(tile, tile.getWorld().isRemote)); } @Override - public List getAllCollectorsInWorld(World world) { + public Set getAllCollectorsInWorld(World world) { return getAllInWorld(manaCollectors, world); } @Override - public List getAllPoolsInWorld(World world) { + public Set getAllPoolsInWorld(World world) { return getAllInWorld(manaPools, world); } - private List getAllInWorld(Map> map, World world) { + private Set getAllInWorld(Map> map, World world) { if(!map.containsKey(world)) - return new ArrayList<>(); + return new HashSet<>(); return map.get(world); }