Skip to content

Commit

Permalink
Add convenience methods for getting players in regions
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 50522c2337708a68ec87254d3d4b380b4279e979
  • Loading branch information
RyanHecht committed Jun 5, 2024
1 parent 130c164 commit 9e48994
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/us/mcparks/showscript/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ private void setupCloud() {

}


public RegionListener getRegionListener() {
return regionListener;
}


public void addActiveShow(ShowScheduler show) {
Expand Down
18 changes: 14 additions & 4 deletions src/us/mcparks/showscript/event/region/RegionListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
import org.bukkit.event.player.*;
import org.bukkit.event.vehicle.VehicleExitEvent;
import org.bukkit.event.vehicle.VehicleMoveEvent;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import java.util.*;
import java.util.stream.Collectors;

public class RegionListener implements Listener {
public static Map<Player, Set<ProtectedRegion>> regionMap;
Expand All @@ -32,6 +31,17 @@ public RegionListener(WorldGuardPlugin wgp) {
Bukkit.getPluginManager().registerEvents(this, Main.getPlugin(Main.class));
}

public Collection<Player> getPlayersInRegion(String regionId) {
return regionMap.entrySet().stream()
.filter(entry -> entry.getValue().stream().anyMatch(region -> region.getId().equals(regionId)))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}

public Collection<String> getRegionsForPlayer(Player player) {
return regionMap.get(player).stream().map(ProtectedRegion::getId).collect(Collectors.toList());
}

@EventHandler
public void onMove(PlayerMoveEvent e) {
updateRegions(e.getPlayer(), MovementType.MOVE, e.getTo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public void show(@DelegatesTo(value = LinkedHashMap.class) Closure<?> closure) {
closure.setResolveStrategy(Closure.DELEGATE_FIRST);
closure.setDelegate(map);
closure.call();
System.out.println(map);
ensureMapHasKeys(map, Lists.newArrayList("name"));
actions.add(new GroovyShowAction(ShowActionType.SHOW, map));
}
Expand Down
12 changes: 12 additions & 0 deletions src/us/mcparks/showscript/showscript/groovy/dsl/TimecodeDsl.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ public double tan(double angle) {
return Math.tan(Math.toRadians(angle));
}

public Collection<Player> playersInRegion(String regionName) {
return Main.getPlugin(Main.class).getRegionListener().getPlayersInRegion(regionName);
}

public Collection<String> playerRegions(Player player) {
return Main.getPlugin(Main.class).getRegionListener().getRegionsForPlayer(player);
}

public Collection<String> playerRegions(String playerName) {
return Main.getPlugin(Main.class).getRegionListener().getRegionsForPlayer(player(playerName));
}

public Collection<ShowScheduler> runningShows() {
return Main.getPlugin(Main.class).getActiveShows();
}
Expand Down

0 comments on commit 9e48994

Please sign in to comment.