}
@Override
public void trigger (ChipState chip) {
Playlist playlist = RadioStation.getPlaylist(band);
if(playlist == null)
return;
if(chip.getInput(0)) {
if(area.getPlayersInArea().size() != listening.size()) {
Map<String, SearchArea> removals = new HashMap<String, SearchArea>();
for(Entry<String, SearchArea> key : listening.entrySet()) {
boolean found = false;
for(Player p : area.getPlayersInArea()) {
if(p.getName().equals(key.getKey())) {
found = true;
break;
}
}
if (!found) removals.put(key.getKey(), key.getValue());
}
if(removals.size() > 0) {
playlist.getPlaylistInterpreter().removePlayers(removals);
for(String key : removals.keySet())
listening.remove(key);
}
boolean changed = false;
for(Player player : area.getPlayersInArea())
if(!listening.containsKey(player.getName())) {
listening.put(player.getName(), area);
changed = true;
}
if(changed)
playlist.getPlaylistInterpreter().addPlayers(listening);
CraftBookPlugin.logDebugMessage("Reset listener list! Size of: " + listening.size(), "ic-mc1277");
}
} else if(listening.size() > 0) {
playlist.getPlaylistInterpreter().removePlayers(listening);
listening.clear();
CraftBookPlugin.logDebugMessage("Cleared listener list!", "ic-mc1277");
}
chip.setOutput(0, playlist.isPlaying() && !listening.isEmpty());
}