Experiemntally convert mana network to using sets

This commit is contained in:
Vincent Lee committed 2016-04-12 12:28:58 -05:00
1 parent 649dffcf71
commit 7a36e543ad
3 files changed
+33 -43

No files matched your search

@@ -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<TileSignature> getAllCollectorsInWorld(World world) {
return ImmutableList.of();
public Set<TileSignature> getAllCollectorsInWorld(World world) {
return ImmutableSet.of();
}
@Override
public List<TileSignature> getAllPoolsInWorld(World world) {
return ImmutableList.of();
public Set<TileSignature> getAllPoolsInWorld(World world) {
return ImmutableSet.of();
}
}
@@ -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<TileSignature> getAllCollectorsInWorld(World world);
public Set<TileSignature> 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<TileSignature> getAllPoolsInWorld(World world);
public Set<TileSignature> getAllPoolsInWorld(World world);
}
@@ -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<World, List<TileSignature>> manaPools = new WeakHashMap<>();
private final WeakHashMap<World, List<TileSignature>> manaCollectors = new WeakHashMap<>();
private final WeakHashMap<World, Set<TileSignature>> manaPools = new WeakHashMap<>();
private final WeakHashMap<World, Set<TileSignature>> manaCollectors = new WeakHashMap<>();
@SubscribeEvent
public void onNetworkEvent(ManaNetworkEvent event) {
Map<World, List<TileSignature>> map = event.type == ManaBlockType.COLLECTOR ? manaCollectors : manaPools;
Map<World, Set<TileSignature>> 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<World, List<TileSignature>> map) {
List<TileSignature> 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<World, Set<TileSignature>> map) {
Set<TileSignature> set = map.get(tile.getWorld());
return set != null && set.contains(new TileSignature(tile, tile.getWorld().isRemote));
}
private synchronized TileEntity getClosest(List<TileSignature> tiles, BlockPos pos, boolean remoteCheck, int limit) {
private synchronized TileEntity getClosest(Set<TileSignature> 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<World, List<TileSignature>> map, TileEntity tile) {
private synchronized void remove(Map<World, Set<TileSignature>> map, TileEntity tile) {
World world = tile.getWorld();
if(!map.containsKey(world))
return;
List<TileSignature> 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<World, List<TileSignature>> map, TileEntity tile) {
private synchronized void add(Map<World, Set<TileSignature>> map, TileEntity tile) {
World world = tile.getWorld();
List<TileSignature> tiles;
Set<TileSignature> 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<TileSignature> getAllCollectorsInWorld(World world) {
public Set<TileSignature> getAllCollectorsInWorld(World world) {
return getAllInWorld(manaCollectors, world);
}
@Override
public List<TileSignature> getAllPoolsInWorld(World world) {
public Set<TileSignature> getAllPoolsInWorld(World world) {
return getAllInWorld(manaPools, world);
}
private List<TileSignature> getAllInWorld(Map<World, List<TileSignature>> map, World world) {
private Set<TileSignature> getAllInWorld(Map<World, Set<TileSignature>> map, World world) {
if(!map.containsKey(world))
return new ArrayList<>();
return new HashSet<>();
return map.get(world);
}