Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teleport monitoring (no enderpearl zones) #256

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@
<version>R7</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -250,6 +256,8 @@
<include>com.google.code.gson:gson</include>
<include>org.jdom:jdom2</include>
<include>org.mcstats.bukkit:metrics</include>
<include>org.jgrapht:jgrapht-core</include>
<include>org.jheaps:jheaps</include>
</includes>
</artifactSet>
</configuration>
Expand All @@ -258,4 +266,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
86 changes: 82 additions & 4 deletions src/main/java/org/mctourney/autoreferee/AutoRefMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
Expand All @@ -22,6 +23,8 @@
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.JsonElement;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -61,6 +64,7 @@
import org.bukkit.material.Redstone;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
Expand All @@ -87,6 +91,7 @@
import org.mctourney.autoreferee.listeners.ZoneListener;
import org.mctourney.autoreferee.regions.AutoRefRegion;
import org.mctourney.autoreferee.regions.CuboidRegion;
import org.mctourney.autoreferee.regions.RegionGraph;
import org.mctourney.autoreferee.util.ArmorPoints;
import org.mctourney.autoreferee.util.BlockData;
import org.mctourney.autoreferee.util.BookUtil;
Expand Down Expand Up @@ -930,6 +935,7 @@ public void run()
public AutoRefMatch(World world, boolean tmp, MatchStatus state)
{ this(world, tmp); setCurrentState(state); }

@SuppressWarnings("deprecation")
public AutoRefMatch(World world, boolean tmp)
{
setPrimaryWorld(world);
Expand Down Expand Up @@ -968,7 +974,29 @@ public AutoRefMatch(World world, boolean tmp)

messageReferees("match", getWorld().getName(), "init");
loadWorldConfiguration();


this.createRegionGraphs();

try {
this.loadRegionJSON();
} catch (FileNotFoundException | ClassCastException e) {
AutoReferee.log("Failed to load " + REGION_CFG_FILENAME);
e.printStackTrace();
}

/*if(AutoReferee.getInstance().isExperimentalMode()) { // experimental feature
this.initRegionGraphs();

graphTask =
new BukkitRunnable() {
@Override
public void run() {
computeRegionGraphs();
graphTask = null;
}
}.runTaskAsynchronously(AutoReferee.getInstance());
}*/

messageReferees("match", getWorld().getName(), "map", getMapName());
setCurrentState(MatchStatus.WAITING);

Expand Down Expand Up @@ -1181,7 +1209,7 @@ protected void clearScoreboardData(Scoreboard sb)
}

protected void loadScoreboardData()
{
{
clearScoreboardData(scoreboard);
clearScoreboardData( infoboard);

Expand Down Expand Up @@ -2116,7 +2144,57 @@ public Set<AutoRefRegion> getRegions(AutoRefTeam team)

public boolean addRegion(AutoRefRegion reg)
{ return reg != null && !regions.contains(reg) && regions.add(reg); }


protected void createRegionGraphs() {
for ( AutoRefTeam t : this.getTeams() ) {
t.createRegionGraph();
}
}

protected void initRegionGraphs() {
for ( AutoRefTeam t : this.getTeams() ) {
t.initRegionGraph();
}
}

public void computeRegionGraphs() {
for ( AutoRefTeam t : this.getTeams() ) {
t.computeRegionGraph();
}
}

public boolean regionGraphsLoaded() {
return this.getTeams().stream()
.allMatch(t -> t.getRegGraph().loaded());
}

public static final String REGION_CFG_FILENAME = "regions.json";

public void loadRegionJSON( ) throws FileNotFoundException, ClassCastException {
if(!AutoReferee.getInstance().isExperimentalMode()) return;

File f = new File(this.getWorld().getWorldFolder(), REGION_CFG_FILENAME);
if(!f.exists()) return;

Gson gson = new Gson();
Reader reader = new FileReader(f);

Map<String, Object> data = gson.fromJson(reader, Map.class);

for( String key : data.keySet() ) {
AutoRefTeam team = this.getTeam(key);
if(team == null) continue;

Map<String, Object> data2 = (Map<String, Object>) data.get(key);

List restricted = (List) data2.get("restricted");

if(restricted != null) {
team.setRestrictionRegions( team.getRegGraph().fromInts( restricted ) );
}
}
}

/**
* A redstone mechanism necessary to start a match.
*
Expand Down Expand Up @@ -3624,4 +3702,4 @@ public void sendMatchInfo(CommandSender sender)
: String.format(ChatColor.GRAY + "The current match time is: " +
"%02d:%02d:%02d", timestamp/3600L, (timestamp/60L)%60L, timestamp%60L));
}
}
}
117 changes: 114 additions & 3 deletions src/main/java/org/mctourney/autoreferee/AutoRefTeam.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.mctourney.autoreferee;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Collectors;

import com.google.common.collect.Maps;

Expand All @@ -28,11 +30,13 @@
import org.mctourney.autoreferee.listeners.GoalsInventorySnapshot;
import org.mctourney.autoreferee.listeners.ZoneListener;
import org.mctourney.autoreferee.regions.AutoRefRegion;
import org.mctourney.autoreferee.regions.AutoRefRegion.Flag;
import org.mctourney.autoreferee.regions.RegionGraph;
import org.mctourney.autoreferee.util.BlockData;
import org.mctourney.autoreferee.util.Metadatable;
import org.mctourney.autoreferee.util.PlayerKit;
import org.mctourney.autoreferee.util.PlayerUtil;

import org.mctourney.autoreferee.util.Vec3;
import org.apache.commons.lang.StringUtils;

import com.google.common.collect.Sets;
Expand Down Expand Up @@ -298,7 +302,18 @@ public Location getVictoryMonumentLocation()
*/
public Set<AutoRefRegion> getRegions()
{ return match.getRegions(this); }


/**
* Returns whether a particular Location
* is in team's lane or not
* @author char
*
* @param loc
* @return
*/
public boolean containsLoc(Location loc)
{ return this.getRegions().stream().anyMatch(reg -> reg.contains(loc)); }

public boolean addRegion(AutoRefRegion reg)
{
for (AutoRefRegion ereg : match.getRegions())
Expand Down Expand Up @@ -343,6 +358,102 @@ public Location getSpawnLocation()
return regs[random.nextInt(spawnRegions.size())].getLocation();
}

private RegionGraph graph;
private Set<Set<Vec3>> restrictedRegions;

public RegionGraph getRegGraph() { return this.graph; }
public void setRestrictionRegions(Set<Set<Vec3>> regions)
{ this.restrictedRegions = regions; }

public boolean regGraphLoaded() { return this.getRegGraph().loaded(); }

public void initRegionGraph() {
createRegionGraph();
graph.computeGraph();
}

public void createRegionGraph() {
// this is an experimental feature
if(!AutoReferee.getInstance().isExperimentalMode()) return;

if(this.getMatch() == null) return;
World w = this.getMatch().getWorld();
if(w == null) return;

if(this.getRegions() == null) return;

graph = new RegionGraph(w, this.getRegions(), AutoReferee.getInstance().getLogger(), this)
.regions(this.getRegions());
/*.setDungeonOpenings( this.getRegions().stream()
.filter(r -> r.getFlags().contains(Flag.DUNGEON_BOUNDARY))
.collect(Collectors.toSet()));*/
}

// safe from async thread
public void computeRegionGraph() {
// this is an expiremental feature
if(!AutoReferee.getInstance().isExperimentalMode()) return;
if(this.getRegions() == null) return;

RegionGraph graph = this.getRegGraph();
if(graph == null) return;

if(this.getMatch() == null) return;
World w = this.getMatch().getWorld();
if(w == null) return;

graph.findConnectedRegions();
}

public Set<Location> unrestrictedPts() {
if(this.getRegions() == null) return null;

return this.getRegions().stream()
.filter(reg -> reg.getFlags().contains(Flag.NON_RESTRICTED))
.map(reg -> reg.getBoundingCuboid().getMinimumPoint().getBlock().getLocation())
.collect(Collectors.toSet());
}

/*public Set<AutoRefRegion> dungeonOpenings() {
if(this.getRegions() == null) return null;

return this.regions().stream()
.filter(r -> r.getFlags().contains(Flag.DUNGEON_BOUNDARY))
.collect(Collectors.toSet());
}*/

public Set<Vec3> restrictedRegion(Location l) {
if(this.getRegGraph() == null) return null;

if(this.restrictedRegions != null) {
return this.restrictedRegions.stream()
.filter(reg -> reg.contains( this.getRegGraph().vec(l) ))
.findAny().orElse(null);
}

if(!this.getRegGraph().loaded()) return null;
if(this.getRegGraph().connectedRegions().isEmpty()) return null;

return this.getRegGraph().connectedRegions().stream()
.filter(reg -> reg.contains( this.getRegGraph().vec(l) ))
.findAny().orElse(null);
}

public boolean isRestrictedLoc(Location l) {
boolean def = false;
if(this.getRegGraph() == null) return def;

if(this.restrictedRegions != null) {
return this.getRegGraph()
.isRestricted(l, this.restrictedRegions, this.getRegions());
}

if(!this.getRegGraph().loaded()) return def;
if(this.getRegGraph().connectedRegions().isEmpty()) return def;

return this.getRegGraph().isInRestrictedArea(l, this.unrestrictedPts());
}

private Set<AutoRefGoal> goals = Sets.newHashSet();

/**
Expand Down Expand Up @@ -488,7 +599,7 @@ public static AutoRefTeam create(AutoRefMatch match, String name, ChatColor colo
}

protected void setupScoreboard()
{
{
String sbteam = this.getScoreboardTeamName();

// set team data on spectators' scoreboard
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/mctourney/autoreferee/AutoReferee.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.mctourney.autoreferee.commands.PracticeCommands;
import org.mctourney.autoreferee.commands.ScoreboardCommands;
import org.mctourney.autoreferee.commands.SpectatorCommands;
import org.mctourney.autoreferee.entity.EntityAREnderPearl;
import org.mctourney.autoreferee.listeners.CombatListener;
import org.mctourney.autoreferee.listeners.ObjectiveTracker;
import org.mctourney.autoreferee.listeners.ObjectiveTracer;
Expand Down Expand Up @@ -353,6 +354,16 @@ public void onEnable()
consoleLog = getConfig().getBoolean("console-log", true);
consoleLogInColor = getConfig().getBoolean("console-colors", true);

// experimental mode?
if(this.isExperimentalMode()) {
getLogger().info(this.getName() + " loaded in Experimental Mode. This is not intended for regular use!");

if(EntityAREnderPearl.patch())
getLogger().info("Successfully patched EntityEnderPearl!");
else
getLogger().severe("Failed to patch EntityEnderPearl! Please let a dev know about this.");
}

// setup the map library folder
AutoRefMap.getMapLibrary();

Expand Down Expand Up @@ -478,6 +489,16 @@ public void sendMessageSync(CommandSender recipient, String ...msgs)
catch (IllegalStateException ignored) { }
}

/**
* Get whether server is in experimental mode or not
* @author char
*
* @return Whether server is in experimental mode
*/
public boolean isExperimentalMode() {
return this.getConfig().getBoolean("experimental-mode", false);
}

private class SyncMessageTask extends BukkitRunnable
{
private class RoutedMessage
Expand Down
Loading