package crazypants.enderio.machine.hypercube; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import net.minecraftforge.common.DimensionManager; import crazypants.enderio.Log; public class HyperCubeRegister { private static final String CATEGORY_PUBLIC_CHANNELS = "PublicChannels"; private static final String CATEGORY_PRIVATE_CHANNELS = "PrivateChannels"; private static final String KEY_PUBLIC_CHANNELS = "names"; public static HyperCubeRegister instance; private final Map> channelMapping = new HashMap>(); private final List publicChannels = new ArrayList(); private final List publicChannelsRO = Collections.unmodifiableList(publicChannels); private final Map> userChannels = new HashMap>(); //private Configuration config; private HyperCubeConfig conf; public static void load() { instance = new HyperCubeRegister(); instance.innerLoad(); } public static void unload() { instance = new HyperCubeRegister(); } private void innerLoad() { File f = DimensionManager.getCurrentSaveRootDirectory(); conf = new HyperCubeConfig(new File(DimensionManager.getCurrentSaveRootDirectory(), "enderio/dimensionalTransceiver.cfg")); publicChannels.addAll(conf.getPublicChannels()); userChannels.putAll(conf.getUserChannels()); } public synchronized List getChannelsForUser(String user) { List result = userChannels.get(user); if(result == null) { result = new ArrayList(); userChannels.put(user, result); } return result; } public synchronized void register(TileHyperCube cube) { List cubes = innerGetCubesForChannel(cube.getChannel()); if(cubes != null && !cubes.contains(cube)) { cubes.add(cube); } } public synchronized void deregister(TileHyperCube cube, Channel channel) { List cubes = innerGetCubesForChannel(channel); if(cubes != null) { cubes.remove(cube); } } public synchronized void deregister(TileHyperCube cube) { List cubes = innerGetCubesForChannel(cube.getChannel()); if(cubes != null) { cubes.remove(cube); } } public synchronized List getCubesForChannel(Channel channel) { List chans = innerGetCubesForChannel(channel); if(chans == null) { return Collections.emptyList(); } return new ArrayList(chans); } public synchronized void addChannel(Channel channel) { if(channel == null || channel.name == null) { return; } if(channel.user == null) { if(!publicChannels.contains(channel)) { publicChannels.add(channel); updateConfig(); } } else { List channels = getChannelsForUser(channel.user); if(!channels.contains(channel)) { channels.add(channel); updateConfig(); } } } public void removeChannel(Channel channel) { if(channel == null || channel.name == null) { return; } if(channel.user == null) { if(publicChannels.contains(channel)) { publicChannels.remove(channel); updateConfig(); } } else { List channels = getChannelsForUser(channel.user); if(channels.contains(channel)) { channels.remove(channel); updateConfig(); } } } private void updateConfig() { if(conf == null) { Log.warn("HyperCubeRegister.updateConfig: Config was null."); return; } conf.setPublicChannels(publicChannels); conf.setUserChannels(userChannels); conf.save(); } private List innerGetCubesForChannel(Channel channel) { if(channel == null) { return null; } List result = channelMapping.get(channel); if(result == null) { result = new ArrayList(); channelMapping.put(channel, result); } return result; } public List getPublicChannels() { return publicChannelsRO; } }