44import com .jaimemartz .playerbalancer .connection .ConnectionIntent ;
55import com .jaimemartz .playerbalancer .manager .PlayerLocker ;
66import com .jaimemartz .playerbalancer .section .ServerSection ;
7+ import com .jaimemartz .playerbalancer .settings .props .KickHandlerProps ;
8+ import com .jaimemartz .playerbalancer .settings .props .MessagesProps ;
79import com .jaimemartz .playerbalancer .utils .MessageUtils ;
810import net .md_5 .bungee .api .Callback ;
911import net .md_5 .bungee .api .chat .TextComponent ;
1921import java .util .concurrent .TimeUnit ;
2022
2123public class ServerKickListener implements Listener {
24+ private final KickHandlerProps props ;
2225 private final PlayerBalancer plugin ;
26+ private final MessagesProps messages ;
2327
2428 public ServerKickListener (PlayerBalancer plugin ) {
29+ this .props = plugin .getSettings ().getKickHandlerProps ();
30+ this .messages = plugin .getSettings ().getMessagesProps ();
2531 this .plugin = plugin ;
2632 }
2733
@@ -30,35 +36,60 @@ public void onKick(ServerKickEvent event) {
3036 ProxiedPlayer player = event .getPlayer ();
3137 ServerInfo from = event .getKickedFrom ();
3238
33- ServerSection section = getSection (player , from );
34-
35- if (section != null ) {
36- List <ServerInfo > servers = new ArrayList <>();
37- servers .addAll (section .getMappedServers ());
38- servers .remove (from );
39-
40- new ConnectionIntent (plugin , player , section , servers ) {
41- @ Override
42- public void connect (ServerInfo server , Callback <Boolean > callback ) {
43- PlayerLocker .lock (player );
44- MessageUtils .send (player , plugin .getSettings ().getMessagesProps ().getKickMessage (),
45- (str ) -> str .replace ("{from}" , from .getName ())
46- .replace ("{to}" , server .getName ())
47- .replace ("{reason}" , event .getKickReason ()));
48- event .setCancelled (true );
49- event .setCancelServer (server );
50- plugin .getProxy ().getScheduler ().schedule (plugin , () -> {
51- PlayerLocker .unlock (player );
52- }, 5 , TimeUnit .SECONDS );
53- callback .done (true , null );
54- }
55- };
39+ boolean matches = false ;
40+ String reason = TextComponent .toPlainText (event .getKickReasonComponent ());
41+
42+ for (String string : props .getReasons ()) {
43+ if (reason .matches (string ) || reason .contains (string )) { //todo improve this
44+ matches = true ;
45+ break ;
46+ }
47+ }
48+
49+ if (props .isInverted ()) {
50+ matches = !matches ;
51+ }
52+
53+ if (props .isDebug ()) {
54+ plugin .getLogger ().info (String .format ("The player %s got kicked from %s, reason: %s. Matched reasons: %s" ,
55+ player .getName (),
56+ from .getName (),
57+ TextComponent .toPlainText (event .getKickReasonComponent ()),
58+ matches
59+ ));
60+ }
61+
62+ if (matches ) {
63+ ServerSection section = getSection (player , from );
64+
65+ if (section != null ) {
66+ List <ServerInfo > servers = new ArrayList <>();
67+ servers .addAll (section .getMappedServers ());
68+ servers .remove (from );
69+
70+ new ConnectionIntent (plugin , player , section , servers ) {
71+ @ Override
72+ public void connect (ServerInfo server , Callback <Boolean > callback ) {
73+ PlayerLocker .lock (player );
74+ MessageUtils .send (player , messages .getKickMessage (), (str ) ->
75+ str .replace ("{from}" , from .getName ())
76+ .replace ("{to}" , server .getName ())
77+ .replace ("{reason}" , event .getKickReason ()));
78+ event .setCancelled (true );
79+ event .setCancelServer (server );
80+ plugin .getProxy ().getScheduler ().schedule (plugin , () -> {
81+ PlayerLocker .unlock (player );
82+ }, 5 , TimeUnit .SECONDS );
83+ callback .done (true , null );
84+ }
85+ };
86+ }
5687 }
5788 }
5889
5990 private ServerSection getSection (ProxiedPlayer player , ServerInfo from ) {
6091 if (player .getServer () == null ) {
61- if (plugin . getSettings (). getKickHandlerProps () .isForcePrincipal ()) {
92+ if (props .isForcePrincipal ()) {
6293 return plugin .getSectionManager ().getPrincipal ();
6394 } else {
6495 return null ;
@@ -72,52 +103,32 @@ private ServerSection getSection(ProxiedPlayer player, ServerInfo from) {
72103 ServerSection current = plugin .getSectionManager ().getByServer (from );
73104
74105 if (current != null ) {
75- if (plugin . getSettings (). getKickHandlerProps () .getExcludedSections ().contains (current .getName ())) {
106+ if (props .getExcludedSections ().contains (current .getName ())) {
76107 return null ;
77108 }
78109 }
79110
80- boolean matches = false ;
81- String reason = TextComponent .toPlainText (event .getKickReasonComponent ());
82-
83- for (String string : settings .getProperty (ReconnectorProperties .REASONS )) {
84- if (reason .matches (string ) || reason .contains (string )) {
85- matches = true ;
86- break ;
111+ if (current != null ) {
112+ ServerSection target = plugin .getSectionManager ().getBind (props .getRules (), current )
113+ .orElse (current .getParent ());
114+ if (target == null ) {
115+ MessageUtils .send (player , messages .getUnavailableServerMessage ());
116+ return null ;
87117 }
88- }
89-
90- if (settings .getProperty (ReconnectorProperties .INVERTED )) {
91- matches .set (!matches .get ());
92- }
93-
94- if (settings .getProperty (ReconnectorProperties .DEBUG )) {
95- plugin .getLogger ().info (String .format ("Kick Reason: \" %s\" , Found Match: %s" , TextComponent .toPlainText (event .getKickReasonComponent ()), matches ));
96- }
97118
98- if (matches .get ()) {
99- if (current != null ) {
100- MapBean rules = settings .getProperty (CommandProperties .RULES );
101- String bind = rules .getMap ().get (current .getName ());
102- ServerSection target = holder .getByName (bind );
103-
104- if (target == null ) {
105- target = current .getParentName ();
106- }
107-
108- if (settings .getProperty (ReconnectorProperties .RESTRICTED )) {
109- if (current .getPosition () >= 0 && target .getPosition () < 0 ) {
110- return null ;
111- }
119+ if (props .isRestricted ()) {
120+ if (current .getPosition () >= 0 && target .getPosition () < 0 ) {
121+ return null ;
112122 }
123+ }
113124
114- return target ;
115- } else {
116- if (settings .getProperty (GeneralProperties .FALLBACK_PRINCIPAL )) {
117- return holder .getPrincipal ();
118- }
125+ return target ;
126+ } else {
127+ if (plugin .getSettings ().getGeneralProps ().isFallbackPrincipal ()) {
128+ return plugin .getSectionManager ().getPrincipal ();
119129 }
120130 }
131+
121132 return null ;
122133 }
123134}
0 commit comments