Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Austin L Mayes <[email protected]>
  • Loading branch information
AustinLMayes committed Jan 31, 2021
1 parent 96d7f39 commit 207b16d
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 53 deletions.
1 change: 0 additions & 1 deletion core/src/main/java/net/avicus/atlas/core/Atlas.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ public void onPlayerLogin(PlayerLoginEvent event) {
PlayerSettings.register(DeathMessage.SETTING);
PlayerSettings.register(VoteModule.SETTING);


try {
this.matchManager.start();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@

public class ObserverListener implements Listener {

private final GroupsModule module;

private static final Set<Material> TOOL_TYPES =
Sets.newHashSet(Material.COMPASS, Material.WOOD_AXE);
private final GroupsModule module;

public ObserverListener(GroupsModule module) {
this.module = module;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public void check() {
}

for (Competitor competitor : groups.getCompetitors()) {
CompetitorCompletionState completionState = new CompetitorCompletionState(match, competitor);
CompetitorCompletionState completionState = new CompetitorCompletionState(match,
competitor);
states.add(completionState);
if (completionState.shouldWin()) {
winners.add(competitor);
Expand Down
34 changes: 25 additions & 9 deletions core/src/main/java/net/avicus/atlas/core/module/spawns/Spawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ private Spawn(Optional<Group> group, List<SpawnRegion> regions, Optional<Loadout
this.ensureSafe = ensureSafe;
}

/** Select a region, spreading players out evenly regardless of relation **/
/**
* Select a region, spreading players out evenly regardless of relation
**/
private static SpawnRegion selectSpread(Match match, Player player, List<SpawnRegion> regions) {
Map<SpawnRegion, Double> minimumDistances = new HashMap<>();

Expand Down Expand Up @@ -106,7 +108,9 @@ private static SpawnRegion selectSpread(Match match, Player player, List<SpawnRe
return selected;
}

/** Select a region, spreading players out so they are furthest away from enemies **/
/**
* Select a region, spreading players out so they are furthest away from enemies
**/
private static SpawnRegion selectSafe(Match match, Player player, List<SpawnRegion> regions) {
SpawnRegion selected = null;

Expand Down Expand Up @@ -145,7 +149,9 @@ private static SpawnRegion selectRandom(List<SpawnRegion> regions) {
return regions.get(RANDOM.nextInt(regions.size()));
}

/** Find a position in a region which is safe. Will return null if region has no safe places **/
/**
* Find a position in a region which is safe. Will return null if region has no safe places
**/
private static Vector findSafePosition(Match match, SpawnRegion region) {
int attempts = 0;
AtomicInteger regionSize = new AtomicInteger();
Expand All @@ -156,7 +162,9 @@ private static Vector findSafePosition(Match match, SpawnRegion region) {
Location loc = position.toLocation(match.getWorld());
boolean safe = false;

if (loc.getBlock().getRelative(BlockFace.UP).getRelative(BlockFace.UP).getType().isSolid()) continue;
if (loc.getBlock().getRelative(BlockFace.UP).getRelative(BlockFace.UP).getType().isSolid()) {
continue;
}

for (int y = loc.getBlockY(); y >= loc.getBlockY() - 10; y--) {
if (match.getWorld().getBlockAt(new Location(match.getWorld(), loc.getX(), y, loc.getZ()))
Expand All @@ -166,16 +174,21 @@ private static Vector findSafePosition(Match match, SpawnRegion region) {
}
}

if (safe) break;
if (safe) {
break;
}
position = null;
attempts++;
}

return position;
}

/** Try to find a safe place in the pre-selected region first, then fallback to checking the rest **/
private static Vector ensureSafe(Match match, SpawnRegion selected, List<SpawnRegion> regions, Player player) {
/**
* Try to find a safe place in the pre-selected region first, then fallback to checking the rest
**/
private static Vector ensureSafe(Match match, SpawnRegion selected, List<SpawnRegion> regions,
Player player) {
Vector position = null;

List<SpawnRegion> regionsToTry = new ArrayList<>(regions);
Expand All @@ -185,11 +198,14 @@ private static Vector ensureSafe(Match match, SpawnRegion selected, List<SpawnRe

for (SpawnRegion region : regionsToTry) {
position = findSafePosition(match, region);
if (position != null) break;
if (position != null) {
break;
}
}

if (position == null) {
StaffChannels.MAPDEV_CHANNEL.send(null, new TextComponent("Failed to find suitable spawn position for " + player.getName()));
StaffChannels.MAPDEV_CHANNEL.send(null,
new TextComponent("Failed to find suitable spawn position for " + player.getName()));
position = selectRandom(regions).randomPosition(RANDOM);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ public static void start(final CommandContext args, final CommandSender source)
ChatColor.GRAY.toString());
source.sendMessage(Messages.VOTE_SET.with(ChatColor.GRAY, new UnlocalizedText(mapNames)));
if (args.hasFlag('c')) {
StaffChannels.MAPDEV_CHANNEL.send(null, new TextComponent(source.getName() + " queued a vote for " + ChatColor.stripColor(mapNames)));
StaffChannels.MAPDEV_CHANNEL.send(null, new TextComponent(
source.getName() + " queued a vote for " + ChatColor.stripColor(mapNames)));
source.sendMessage(Messages.VOTE_DELAY.with(ChatColor.GREEN));
} else {
StaffChannels.MAPDEV_CHANNEL.send(null, new TextComponent(source.getName() + " started a vote for " + ChatColor.stripColor(mapNames)));
StaffChannels.MAPDEV_CHANNEL.send(null, new TextComponent(
source.getName() + " started a vote for " + ChatColor.stripColor(mapNames)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@

public class Main extends ModuleSet {

@Setter
AvicusCommandsRegistration registrar;
@Setter
private Atlas atlas;
@Setter
private MatchFactory matchFactory;
@Setter
private Logger logger;
@Setter
AvicusCommandsRegistration registrar;

@Override
public void onEnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ public class ObjectivesBridge extends ObjectivesModuleBridge implements Listener
private final List<WoolObjective> wools;
private final List<FlagObjective> flags;
private final List<LeakableObjective> leakables;
List<Objective> objectives;
private Match match;
private ObjectivesModule module;

// Phase caches
private final List<DestroyableObjective> objectivesWithPhases = new ArrayList<>();
private final Map<DestroyablePhase, List<DestroyableObjective>> phases = new HashMap<>();
List<Objective> objectives;
private Match match;
private ObjectivesModule module;

public ObjectivesBridge(ObjectivesModule module) {
this.module = module;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

public class PhaseCommands {

private static final String LINE = ChatColor.GOLD + ChatColor.STRIKETHROUGH.toString() + "------" + ChatColor.RESET;
private static final String LINE =
ChatColor.GOLD + ChatColor.STRIKETHROUGH.toString() + "------" + ChatColor.RESET;
private static final String INDENT = " ";

private static Match getMatch(CommandSender sender) throws CommandException {
Expand Down Expand Up @@ -68,42 +69,60 @@ public static void list(CommandContext args, CommandSender sender)
throws CommandException {
List<BaseComponent> rows = Lists.newArrayList();
Map<DestroyablePhase, List<DestroyableObjective>> phases = getBridge(sender).getPhases();
if (phases.keySet().isEmpty()) throw new TranslatableCommandWarningException(new UnlocalizedFormat("This map doesn't have any registered phases"));
if (phases.keySet().isEmpty()) {
throw new TranslatableCommandWarningException(
new UnlocalizedFormat("This map doesn't have any registered phases"));
}

rows.add(new TextComponent(LINE + ChatColor.YELLOW + " Destroyable Phases " + LINE));
phases.forEach((phase, obs) -> {
rows.addAll(describePhase("", phase, sender));
rows.add(new TextComponent(INDENT + ChatColor.GOLD + "Applies to: " + StringUtil
.listToEnglishCompound(obs.stream().map(o -> o.getName(sender)).collect(Collectors.toList()))));
.listToEnglishCompound(
obs.stream().map(o -> o.getName(sender)).collect(Collectors.toList()))));
});

rows.forEach(sender::sendMessage);
}

private static List<BaseComponent> describePhase(String prefix, DestroyablePhase phase, CommandSender viewer) {
private static List<BaseComponent> describePhase(String prefix, DestroyablePhase phase,
CommandSender viewer) {
List<BaseComponent> rows = Lists.newArrayList();

rows.add(new UnlocalizedFormat(prefix + "{0} ({1})").with(phase.getName().toText(), new UnlocalizedText(phase.getId())).translate(viewer));
rows.add(new TextComponent(prefix + INDENT + ChatColor.YELLOW + StringUtil.listToEnglishCompound(phase.describeReplacementStrategy())));
rows.add(new TextComponent(prefix + INDENT + ChatColor.BLUE + "Applied After: " + ChatColor.AQUA + StringUtil.secondsToClock((int) phase.getDelay().getStandardSeconds())));
rows.add(new UnlocalizedFormat(prefix + "{0} ({1})")
.with(phase.getName().toText(), new UnlocalizedText(phase.getId())).translate(viewer));
rows.add(new TextComponent(prefix + INDENT + ChatColor.YELLOW + StringUtil
.listToEnglishCompound(phase.describeReplacementStrategy())));
rows.add(new TextComponent(
prefix + INDENT + ChatColor.BLUE + "Applied After: " + ChatColor.AQUA + StringUtil
.secondsToClock((int) phase.getDelay().getStandardSeconds())));
if (phase.getPassPhase().isPresent()) {
rows.add(new TextComponent(prefix + INDENT + ChatColor.GREEN + "Phase Applied if Application Check Passes:"));
rows.add(new TextComponent(
prefix + INDENT + ChatColor.GREEN + "Phase Applied if Application Check Passes:"));
rows.addAll(describePhase(prefix + INDENT + INDENT, phase.getPassPhase().get(), viewer));
}
if (phase.getFailPhase().isPresent()) {
rows.add(new TextComponent(prefix + INDENT + ChatColor.RED + "Phase Applied if Application Check Fails:"));
rows.add(new TextComponent(
prefix + INDENT + ChatColor.RED + "Phase Applied if Application Check Fails:"));
rows.addAll(describePhase(prefix + INDENT + INDENT, phase.getFailPhase().get(), viewer));
}
return rows;
}

private static DestroyablePhase getPhase(CommandSender sender, String id) throws CommandException {
private static DestroyablePhase getPhase(CommandSender sender, String id)
throws CommandException {
Match match = getMatch(sender);
MatchRegistry registry = match.getRegistry();

if (!registry.has(id)) throw new TranslatableCommandErrorException(new UnlocalizedFormat("Object with ID \"" + id + "\" not found."));
if (!registry.has(id)) {
throw new TranslatableCommandErrorException(
new UnlocalizedFormat("Object with ID \"" + id + "\" not found."));
}

if (!registry.isOfType(id, DestroyablePhase.class)) throw new TranslatableCommandErrorException(new UnlocalizedFormat("Object with ID \"" + id + "\" is not a destroyable phase."));
if (!registry.isOfType(id, DestroyablePhase.class)) {
throw new TranslatableCommandErrorException(
new UnlocalizedFormat("Object with ID \"" + id + "\" is not a destroyable phase."));
}

return registry.get(DestroyablePhase.class, id, true).get();
}
Expand All @@ -114,7 +133,9 @@ public static void remove(CommandContext args, CommandSender sender) throws Comm
String id = args.getString(0);

DestroyablePhase found = getPhase(sender, id);
CompendiumPlugin.getInstance().getCountdownManager().cancelAll(c -> c instanceof PhaseApplyCountdown && ((PhaseApplyCountdown) c).getPhase().equals(found));
CompendiumPlugin.getInstance().getCountdownManager().cancelAll(
c -> c instanceof PhaseApplyCountdown && ((PhaseApplyCountdown) c).getPhase()
.equals(found));

ObjectivesBridge bridge = getBridge(sender);
bridge.getPhases().remove(found);
Expand All @@ -124,7 +145,9 @@ public static void remove(CommandContext args, CommandSender sender) throws Comm
}

for (DestroyableObjective objective : bridge.getObjectivesWithPhases()) {
if (objective.getPhase().get().equals(found)) objective.setPhase(Optional.empty());
if (objective.getPhase().get().equals(found)) {
objective.setPhase(Optional.empty());
}
}

bridge.populatePhaseCache();
Expand All @@ -142,8 +165,12 @@ public static void modtime(CommandContext args, CommandSender sender) throws Com
found.setDelay(delay);
for (Entry<Countdown, CountdownTask> entry : CompendiumPlugin
.getInstance().getCountdownManager().getCountdowns().entrySet()) {
if (!(entry.getKey() instanceof PhaseApplyCountdown)) continue;
if (!((PhaseApplyCountdown) entry.getKey()).getPhase().equals(found)) continue;
if (!(entry.getKey() instanceof PhaseApplyCountdown)) {
continue;
}
if (!((PhaseApplyCountdown) entry.getKey()).getPhase().equals(found)) {
continue;
}

entry.getValue().setNewDuration(delay);
}
Expand All @@ -156,6 +183,7 @@ public static void modtime(CommandContext args, CommandSender sender) throws Com
}

public static class Super {

@Command(aliases = "phase", usage = "<>", desc = ".", min = 1)
@NestedCommand(PhaseCommands.class)
public static void phase(CommandContext cmd, CommandSender sender) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ public class DestroyablePhase implements RegisterableObject<DestroyablePhase> {
* Material that the blocks will be changed to.
*/
private final LinkedHashMap<MultiMaterialMatcher, SingleMaterialMatcher> materials;

/**
* Delay before the phase is applied. (The countdown will be started at match start OR when the
* preceding phase is applied.)
*/
@Setter
private Duration delay;

/**
* Check to be ran before the phase is applied.
*/
Expand All @@ -85,6 +77,13 @@ public class DestroyablePhase implements RegisterableObject<DestroyablePhase> {
* Delay between retry attempts.
*/
private final Optional<Duration> retryDelay;
private final HashMap<DestroyableObjective, AtomicInteger> failures = new HashMap<>();
/**
* Delay before the phase is applied. (The countdown will be started at match start OR when the
* preceding phase is applied.)
*/
@Setter
private Duration delay;
/**
* Phase that will be applied if the check fails and there are no retry attempts remaining.
*/
Expand All @@ -94,8 +93,6 @@ public class DestroyablePhase implements RegisterableObject<DestroyablePhase> {
*/
private Optional<DestroyablePhase> passPhase;

private final HashMap<DestroyableObjective, AtomicInteger> failures = new HashMap<>();

/**
* Constructor.
*
Expand Down Expand Up @@ -251,12 +248,18 @@ public List<String> describeReplacementStrategy() {

public void removePhase(DestroyablePhase phase) {
if (this.passPhase.isPresent()) {
if (this.passPhase.get().equals(phase)) this.passPhase = Optional.empty();
else this.passPhase.get().removePhase(phase);
if (this.passPhase.get().equals(phase)) {
this.passPhase = Optional.empty();
} else {
this.passPhase.get().removePhase(phase);
}
}
if (this.failPhase.isPresent()) {
if (this.failPhase.get().equals(phase)) this.failPhase = Optional.empty();
else this.failPhase.get().removePhase(phase);
if (this.failPhase.get().equals(phase)) {
this.failPhase = Optional.empty();
} else {
this.failPhase.get().removePhase(phase);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ protected void onTick(Duration elapsedTime, Duration remainingTime) {
Localizable message = timeRemainingMessage(elapsedTime, remainingTime);

// Boss bar
if (remainingTime.isShorterThan(BOSS_SHOWN_AT)) updateBossBar(message, elapsedTime);
else clearBossBars();
if (remainingTime.isShorterThan(BOSS_SHOWN_AT)) {
updateBossBar(message, elapsedTime);
} else {
clearBossBars();
}

// Periodic chat broadcast
if (shouldBroadcast(sec)) {
Expand Down

0 comments on commit 207b16d

Please sign in to comment.