88package org .mvplugins .multiverse .signportals .listeners ;
99
1010import com .dumptruckman .minecraft .util .Logging ;
11- import org .jetbrains .annotations .NotNull ;
11+ import org .bukkit .Location ;
12+ import org .bukkit .permissions .Permission ;
13+ import org .bukkit .permissions .PermissionDefault ;
14+ import org .bukkit .plugin .PluginManager ;
15+ import org .mvplugins .multiverse .core .api .destination .DestinationInstance ;
16+ import org .mvplugins .multiverse .core .api .destination .DestinationsProvider ;
17+ import org .mvplugins .multiverse .core .api .teleportation .SafetyTeleporter ;
1218import org .mvplugins .multiverse .external .jakarta .inject .Inject ;
19+ import org .mvplugins .multiverse .external .jetbrains .annotations .NotNull ;
1320import org .mvplugins .multiverse .external .jvnet .hk2 .annotations .Service ;
1421import org .mvplugins .multiverse .signportals .MultiverseSignPortals ;
1522import org .mvplugins .multiverse .signportals .exceptions .MoreThanOneSignFoundException ;
2431import org .bukkit .event .player .PlayerInteractEvent ;
2532import org .bukkit .event .player .PlayerPortalEvent ;
2633
34+ import static org .mvplugins .multiverse .core .permissions .PermissionUtils .hasPermission ;
35+
2736@ Service
2837public class MVSPPlayerListener implements SignPortalsListener {
2938
3039 private static final String USE_PERMISSION = "multiverse.signportal.use" ;
3140 private static final String VALIDATE_PERMISSION = "multiverse.signportal.validate" ;
32- private final MultiverseSignPortals plugin ;
33- // private final MVPermissions permissions ;
41+ private final DestinationsProvider destinationsProvider ;
42+ private final SafetyTeleporter safetyTeleporter ;
3443 private final PortalDetector pd ;
3544
3645 @ Inject
37- MVSPPlayerListener (@ NotNull MultiverseSignPortals plugin , @ NotNull PortalDetector pd ) {
38- this .plugin = plugin ;
39- // this.permissions = this.plugin.getCore().getMVPerms();
40- // this.permissions.addPermission(VALIDATE_PERMISSION, PermissionDefault.OP);
41- // this.permissions.addPermission(USE_PERMISSION, PermissionDefault.TRUE);
46+ MVSPPlayerListener (@ NotNull PortalDetector pd ,
47+ @ NotNull PluginManager pluginManager ,
48+ @ NotNull DestinationsProvider destinationsProvider ,
49+ @ NotNull SafetyTeleporter safetyTeleporter ) {
50+ this .destinationsProvider = destinationsProvider ;
51+ this .safetyTeleporter = safetyTeleporter ;
52+ pluginManager .addPermission (new Permission (VALIDATE_PERMISSION , PermissionDefault .OP ));
53+ pluginManager .addPermission (new Permission (USE_PERMISSION , PermissionDefault .TRUE ));
4254 this .pd = pd ;
4355 }
4456
@@ -55,21 +67,26 @@ public void playerPortal(PlayerPortalEvent event) {
5567 String destString = pd .getNotchPortalDestination (event .getPlayer (), event .getFrom ());
5668 if (destString != null ) {
5769 Logging .finer ("Found a Multiverse Sign" );
58- // DestinationFactory df = this.plugin.getCore().getDestFactory();
59- // destString = ChatColor.stripColor(destString);
60- // MVDestination dest = df.getDestination(destString);
61- // MVSPTravelAgent travelAgent = new MVSPTravelAgent(this.plugin.getCore(), dest, event.getPlayer());
62- // travelAgent.setPortalEventTravelAgent(event);
63- // event.setTo(dest.getLocation(event.getPlayer()));
70+ destString = ChatColor .stripColor (destString );
71+ DestinationInstance <?, ?> dest = destinationsProvider .parseDestination (destString ).getOrNull ();
72+ if (dest == null ) {
73+ Logging .warning ("Could not find destination: " + destString );
74+ return ;
75+ }
76+ Location destLocation = dest .getLocation (event .getPlayer ()).getOrNull ();
77+ if (destLocation == null ) {
78+ Logging .warning ("Could not find Location for destination: " + destString );
79+ return ;
80+ }
81+ event .setTo (destLocation );
6482 }
6583
6684 } catch (NoMultiverseSignFoundException e ) {
6785 // This will simply act as a notch portal.
6886 Logging .finer ("Did NOT find a Multiverse Sign" );
6987 } catch (MoreThanOneSignFoundException e ) {
70- // this.plugin.getCore().getMessaging().sendMessage(event.getPlayer(),
71- // String.format("%sSorry %sbut more than 1 sign was found where the second line was [mv] or [multiverse]. Please remove one of the signs.",
72- // ChatColor.RED, ChatColor.WHITE), false);
88+ event .getPlayer ().sendMessage (String .format ("%sSorry %sbut more than 1 sign was found where the second line was [mv] or [multiverse]. Please remove one of the signs." ,
89+ ChatColor .RED , ChatColor .WHITE ));
7390 event .setCancelled (true );
7491 }
7592 }
@@ -102,12 +119,12 @@ public void playerInteract(PlayerInteractEvent event) {
102119 Player player = event .getPlayer ();
103120 switch (status ) {
104121 case SignPortal :
105- // if (permissions. hasPermission(player, USE_PERMISSION, false )) {
106- // String destString = this.pd.processSign(s);
107- // this.takePlayerToDestination(player, destString);
108- // } else {
109- // player.sendMessage(ChatColor.RED + "You do not have the required permission to use SignPortals (" + USE_PERMISSION + ")");
110- // }
122+ if (hasPermission (player , USE_PERMISSION )) {
123+ String destString = this .pd .processSign (s );
124+ this .takePlayerToDestination (player , destString );
125+ } else {
126+ player .sendMessage (ChatColor .RED + "You do not have the required permission to use SignPortals (" + USE_PERMISSION + ")" );
127+ }
111128 event .setCancelled (true );
112129 break ;
113130 case Legacy :
@@ -124,23 +141,23 @@ public void playerInteract(PlayerInteractEvent event) {
124141 }
125142
126143 private void takePlayerToDestination (Player player , String destString ) {
127- // if (destString != null) {
128- // Logging.finer("Found a SignPortal! (" + destString + ")");
129- // SafeTTeleporter teleporter = this.plugin.getCore().getSafeTTeleporter ();
130- // DestinationFactory df = this.plugin.getCore().getDestFactory();
131- //
132- // MVDestination d = df.getDestination(destString) ;
133- // Logging.finer("Found a Destination! (" + d + ")");
134- // if (this.pd.playerCanGoToDestination(player, d)) {
135- // TeleportResult result = teleporter.safelyTeleport (player, player, d);
136- // if (result == TeleportResult.FAIL_UNSAFE) {
137- // player.sendMessage("The Destination was not safe! (" + ChatColor.RED + d + ChatColor.WHITE + ")");
138- // }
139- // } else {
140- // Logging.finer("Denied permission to go to destination!");
141- // }
142- // } else {
143- // player.sendMessage("The Destination was not set on the sign!");
144- // }
144+ if (destString != null ) {
145+ Logging .finer ("Found a SignPortal! (" + destString + ")" );
146+ DestinationInstance <?, ?> d = destinationsProvider . parseDestination ( destString ). getOrNull ();
147+ if ( d == null ) {
148+ Logging . warning ( "Could not find destination: " + destString );
149+ return ;
150+ }
151+ Logging . finer ( "Found a Destination! (" + d + ")" );
152+ if ( this . pd . playerCanGoToDestination (player , d )) {
153+ safetyTeleporter . to ( d ). teleport ( player )
154+ . onSuccess (() -> player . sendMessage ( "Teleported " + player . getName () + " to: " + ChatColor .GREEN + d ))
155+ . onFailure ( result -> player . sendMessage ( "Could not teleport " + player . getName () + " to: " + ChatColor . RED + d ));
156+ } else {
157+ Logging .finer ("Denied permission to go to destination!" );
158+ }
159+ } else {
160+ player .sendMessage ("The Destination was not set on the sign!" );
161+ }
145162 }
146163}
0 commit comments