Skip to content
This repository was archived by the owner on Nov 25, 2019. It is now read-only.

Autojoin feature #57

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Commons/core/src/main/i18n/templates/pgm/PGMUI.properties
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,5 @@ stats.ui.cores = Cores Leaked:
stats.ui.monuments = Monuments Destroyed:
stats.ui.teamkills = TK:

autojoin.starting = Match is starting in {0} seconds! Left click the hat to cancel autojoin!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will join the match in {0} seconds. Left click the helmet to cancel!

autojoin.cancelled = You have cancelled autojoin!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will now observe the match. Right click the helmet if you want to join again!

2 changes: 2 additions & 0 deletions PGM/src/main/java/tc/oc/pgm/PGMModulesManifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import tc.oc.commons.core.inject.HybridManifest;
import tc.oc.pgm.animation.AnimationManifest;
import tc.oc.pgm.autojoin.AutoJoinManifest;
import tc.oc.pgm.broadcast.BroadcastManifest;
import tc.oc.pgm.classes.ClassManifest;
import tc.oc.pgm.controlpoint.ControlPointManifest;
Expand Down Expand Up @@ -42,6 +43,7 @@
public class PGMModulesManifest extends HybridManifest {
@Override
protected void configure() {
install(new AutoJoinManifest());
install(new FilterManifest());
install(new RegionManifest());
install(new KitManifest());
Expand Down
14 changes: 14 additions & 0 deletions PGM/src/main/java/tc/oc/pgm/autojoin/AutoJoinManifest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tc.oc.pgm.autojoin;

import tc.oc.commons.bukkit.settings.SettingBinder;
import tc.oc.commons.core.inject.HybridManifest;
import tc.oc.pgm.match.inject.MatchModuleFixtureManifest;

public class AutoJoinManifest extends HybridManifest {
@Override
protected void configure() {
new SettingBinder(publicBinder()).addBinding().toInstance(AutoJoinSetting.get());
install(new MatchModuleFixtureManifest<AutoJoinMatchModule>(){});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra new line

}
}
101 changes: 101 additions & 0 deletions PGM/src/main/java/tc/oc/pgm/autojoin/AutoJoinMatchModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package tc.oc.pgm.autojoin;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import tc.oc.commons.bukkit.settings.SettingManagerProvider;
import tc.oc.pgm.events.ListenerScope;
import tc.oc.pgm.events.PlayerChangePartyEvent;
import tc.oc.pgm.join.JoinMatchModule;
import tc.oc.pgm.join.JoinMethod;
import tc.oc.pgm.match.MatchModule;
import tc.oc.pgm.match.MatchPlayer;
import tc.oc.pgm.match.MatchScope;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra new line.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the extra new line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️


import javax.inject.Inject;

/**
* New join feature that allows players to join without interfacing with GUI
* with an AutoJoinSetting that allows players to use the legacy join feature
* instead.
*/
@ListenerScope(MatchScope.LOADED)
public class AutoJoinMatchModule extends MatchModule implements Listener {
private Set<MatchPlayer> joiningPlayers;
private final SettingManagerProvider settingManagerProvider;
private final JoinMatchModule joinMatchModule;

@Inject public AutoJoinMatchModule(SettingManagerProvider settingManagerProvider, JoinMatchModule joinMatchModule) {
this.joiningPlayers = new HashSet<>();
this.settingManagerProvider = settingManagerProvider;
this.joinMatchModule = joinMatchModule;
}

@Override
public void disable() {
joiningPlayers.clear();
}

// Checks if the player is eligible when they join
@EventHandler(priority = EventPriority.MONITOR)
public void playerJoin(final PlayerChangePartyEvent event) {
MatchPlayer player = event.getPlayer();

// Ignore if the match has started
if(match.hasStarted()) return;

// Remove the player if the player is leaving
if(event.getNewParty() == null) {
joiningPlayers.remove(player);
return;
}

//Ignore if player is going to participate in a match
if(event.getNewParty().isParticipatingType()) return;

/* Ignore if player is already known; case:
* Player joined a participating team
* Player left a participating team
*/
// Check exists to only handle cases where Match has not started
if(joiningPlayers.contains(player)) return;

// Ignore if player explicitly chooses the legacy join feature

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, only use comments when something isn't obvious. You can remove most of these comments.

if(!settingManagerProvider.getManager(player.getBukkit()).getValue(AutoJoinSetting.get(), Boolean.class, true)) return;

joiningPlayers.add(player);
}

// Public accessor methods

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this comment


public boolean shouldAutoJoin(MatchPlayer player) {
return joiningPlayers.contains(player);
}

// Checks if the player is in participating team when match starts
public boolean shouldAlert(MatchPlayer player) {
return shouldAutoJoin(player) && player.getParty().isParticipatingType();
}

// Player left clicks hat
public void cancelAutojoin(MatchPlayer player) {
joiningPlayers.remove(player);
}

public void requestJoin(MatchPlayer player) {
joinMatchModule.requestJoin(player, JoinMethod.USER);
}

// StartCountdown needs this
public void enterAllPlayers() {
if(!joiningPlayers.isEmpty()) joiningPlayers.forEach(this::requestJoin);
}

public Stream<MatchPlayer> joiningPlayers() {
return joiningPlayers.stream();
}
}
18 changes: 18 additions & 0 deletions PGM/src/main/java/tc/oc/pgm/autojoin/AutoJoinSetting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tc.oc.pgm.autojoin;

import me.anxuiz.settings.Setting;
import me.anxuiz.settings.SettingBuilder;
import me.anxuiz.settings.types.BooleanType;

public class AutoJoinSetting {
private static final Setting INSTANCE = new SettingBuilder()
.name("AutoJoin").alias("aj")
.summary("Toggles the AutoJoin feature")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little more specific than that please.

.type(new BooleanType())
.defaultValue(false)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, auto join is off? Then what's the point?

.get();

public static Setting get() {
return INSTANCE;
}
}
55 changes: 38 additions & 17 deletions PGM/src/main/java/tc/oc/pgm/picker/PickerMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerLocaleChangeEvent;
Expand All @@ -40,6 +39,7 @@
import tc.oc.commons.core.chat.Component;
import tc.oc.commons.core.formatting.StringUtils;
import tc.oc.pgm.PGMTranslations;
import tc.oc.pgm.autojoin.AutoJoinMatchModule;
import tc.oc.pgm.blitz.BlitzEvent;
import tc.oc.pgm.classes.ClassMatchModule;
import tc.oc.pgm.classes.ClassModule;
Expand Down Expand Up @@ -96,15 +96,22 @@ public boolean matches(MaterialData material) {
private final ComponentRenderContext renderer;
private final JoinMatchModule jmm;
private final BlitzMatchModule bmm;
private final AutoJoinMatchModule autoJoinMatchModule;
private final boolean hasTeams;
private final boolean hasClasses;

private final Set<MatchPlayer> picking = new HashSet<>();

@Inject PickerMatchModule(ComponentRenderContext renderer, JoinMatchModule jmm, BlitzMatchModule bmm, Optional<TeamModule> teamModule, Optional<ClassModule> classModule) {
@Inject PickerMatchModule(ComponentRenderContext renderer,
JoinMatchModule jmm,
BlitzMatchModule bmm,
AutoJoinMatchModule autoJoinMatchModule,
Optional<TeamModule> teamModule,
Optional<ClassModule> classModule) {
this.renderer = renderer;
this.jmm = jmm;
this.bmm = bmm;
this.autoJoinMatchModule = autoJoinMatchModule;
this.hasTeams = teamModule.isPresent();
this.hasClasses = classModule.isPresent();
}
Expand Down Expand Up @@ -288,29 +295,43 @@ public void closeMonitoredInventory(final InventoryCloseEvent event) {
}

@EventHandler
public void rightClickIcon(final ObserverInteractEvent event) {
if(event.getClickType() != ClickType.RIGHT) return;

public void handleClickIcon(final ObserverInteractEvent event) {
MatchPlayer player = event.getPlayer();
if(!canUse(player)) return;

ItemStack hand = event.getClickedItem();
if(ItemUtils.isNothing(hand)) return;

String displayName = hand.getItemMeta().getDisplayName();
if(displayName == null) return;

if(hand.getType() == Button.JOIN.material) {
event.setCancelled(true);
if(canOpenWindow(player)) {
showWindow(player);
} else {
// If there is nothing to pick, just join immediately
jmm.requestJoin(player, JoinRequest.user());
}
} else if(hand.getType() == Button.LEAVE.material) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the leave button? Does it still work?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i simply refactored it and commented it out below

event.setCancelled(true);
jmm.requestObserve(player);
if(hand.getType() != Button.JOIN.material) return;

switch(event.getClickType()) {
// Autojoin feature - Player left clicks the hat(cancels Autojoin)
case LEFT:
if(autoJoinMatchModule.shouldAutoJoin(player)) {
autoJoinMatchModule.cancelAutojoin(player);
}
player.sendHotbarMessage(new Component(ChatColor.DARK_PURPLE).translate("autojoin.cancelled").bold(true));
break;
case RIGHT:
if(!canUse(player)) return;

if(hand.getType() == Button.JOIN.material) {
event.setCancelled(true);
if(canOpenWindow(player)) {
showWindow(player);
} else {
// If there is nothing to pick, just join immediately
jmm.requestJoin(player, JoinRequest.user());
}

//} else if(hand.getType() == Button.LEAVE.material) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you going to leave this here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty sure that the button doesn't do anything, so i'll leave this here at the moment until we find some use for it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but add a comment explaining that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep this in, I believe it is for ranked

// event.setCancelled(true);
// jmm.requestObserve(player);
//
}
break;
}
}

Expand Down
12 changes: 12 additions & 0 deletions PGM/src/main/java/tc/oc/pgm/start/StartCountdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.Duration;
import javax.annotation.Nullable;
import javax.inject.Inject;

import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
Expand All @@ -10,6 +11,7 @@
import org.bukkit.entity.Player;
import tc.oc.commons.core.chat.Component;
import tc.oc.commons.core.util.Comparables;
import tc.oc.pgm.autojoin.AutoJoinMatchModule;
import tc.oc.pgm.match.Match;
import tc.oc.pgm.match.MatchState;
import tc.oc.pgm.teams.Team;
Expand All @@ -28,6 +30,7 @@ public class StartCountdown extends PreMatchCountdown {
// or implementing some kind of countdown listener system.
private final @Nullable TeamMatchModule tmm;
private final StartMatchModule smm;
private final AutoJoinMatchModule autoJoinMatchModule;
private final Duration huddle;
private boolean autoBalanced, balanceWarningSent;
protected final boolean forced;
Expand All @@ -38,6 +41,7 @@ public StartCountdown(Match match, boolean forced, Duration huddle) {
this.forced = forced;
this.smm = match.needMatchModule(StartMatchModule.class);
this.tmm = match.getMatchModule(TeamMatchModule.class);
this.autoJoinMatchModule = match.needMatchModule(AutoJoinMatchModule.class);
}

protected boolean willHuddle() {
Expand Down Expand Up @@ -73,11 +77,19 @@ public void onStart(Duration remaining, Duration total) {
public void onTick(Duration remaining, Duration total) {
super.onTick(remaining, total);

if(remaining.getSeconds() <= 10) {
// Autojoin feature - Send the player hotbar messages and alert them that match is starting
autoJoinMatchModule.joiningPlayers()
.forEach(player -> player.sendHotbarMessage(new Component(ChatColor.DARK_PURPLE).translate("autojoin.starting",
String.valueOf(remaining.getSeconds())).bold(true)));
}

if(remaining.getSeconds() >= 1 && remaining.getSeconds() <= 3) {
// Auto-balance runs at match start as well, but try to run it a few seconds in advance
if(this.tmm != null && !this.autoBalanced) {
this.autoBalanced = true;
this.tmm.balanceTeams();
autoJoinMatchModule.enterAllPlayers();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you join players before balancing?

}
}

Expand Down