1616import org .bukkit .configuration .file .FileConfiguration ;
1717import org .bukkit .configuration .file .YamlConfiguration ;
1818import org .bukkit .entity .Player ;
19- import org .bukkit .scheduler .BukkitRunnable ;
2019import revxrsal .commands .annotation .Command ;
2120import revxrsal .commands .annotation .Cooldown ;
2221import revxrsal .commands .annotation .Description ;
2322import revxrsal .commands .bukkit .annotation .CommandPermission ;
2423
2524import java .io .File ;
25+ import java .util .concurrent .ThreadLocalRandom ;
2626
2727import static net .kyori .adventure .text .format .NamedTextColor .YELLOW ;
2828
@@ -56,7 +56,7 @@ private void loadRTPConfig() {
5656 @ Description ("Permet de se téléporter à un endroit aléatoire" )
5757 @ CommandPermission ("omc.commands.rtp" )
5858 @ DynamicCooldown (
59- group = "player:rtp" ,
59+ group = "player:rtp" ,
6060 messageKey = "command.utils.rtp.must_wait" )
6161 @ Cooldown (15 )
6262 public void rtp (Player player ) {
@@ -66,57 +66,55 @@ public void rtp(Player player) {
6666 }
6767
6868 private void rtpPlayer (Player player , int tries ) {
69- new BukkitRunnable () {
70- @ Override
71- public void run () {
72- if (tryRtp (player ))
73- return ;
74-
75- if ((tries + 1 ) >= maxTries ) {
76- // On a déjà mis le cooldown au début
77- player .sendActionBar (TranslationManager .translation ("command.utils.rtp.fail" ));
78- return ;
79- }
80-
81- player .sendActionBar (TranslationManager .translation ("command.utils.rtp.try" ,
82- Component .text (tries + 1 ),
83- Component .text (maxTries )));
84-
85- new BukkitRunnable () {
86- @ Override
87- public void run () {
88- rtpPlayer (player , tries + 1 );
69+ Bukkit .getScheduler ().runTaskAsynchronously (OMCPlugin .getInstance (), () -> {
70+ int [] coords = generateRandomCoords ();
71+ int chunkX = coords [0 ] >> 4 ;
72+ int chunkZ = coords [1 ] >> 4 ;
73+
74+ World world = player .getWorld ();
75+
76+ world .getChunkAtAsync (chunkX , chunkZ ).thenAccept (_ -> {
77+ Bukkit .getScheduler ().runTask (OMCPlugin .getInstance (), () -> {
78+ Location loc = world .getHighestBlockAt (coords [0 ], coords [1 ]).getLocation ();
79+
80+ if (isSafe (loc )) {
81+ loc .add (0.5 , 1 , 0.5 );
82+ tpPlayer (player , loc );
83+ return ;
8984 }
90- }.runTaskLaterAsynchronously (OMCPlugin .getInstance (), 20 );
91- }
92- }.runTaskAsynchronously (OMCPlugin .getInstance ());
9385
94- }
86+ int nextTry = tries + 1 ;
87+ if (nextTry >= maxTries ) {
88+ // On a déjà mis le cooldown au début
89+ player .sendActionBar (TranslationManager .translation ("command.utils.rtp.fail" ));
90+ return ;
91+ }
9592
96- public boolean tryRtp (Player player ) {
97- Location loc = generateRandomLocation (player .getWorld ());
98- if (isSafe (loc )) {
99- loc .add (0.5 , 1 , 0.5 );
100- tpPlayer (player , loc );
101- return true ;
102- } else {
103- return false ;
104- }
105- }
93+ player .sendActionBar (TranslationManager .translation ("command.utils.rtp.try" ,
94+ Component .text (nextTry ),
95+ Component .text (maxTries )));
10696
107- public boolean isSafe (Location loc ) {
108- return loc .getBlock ().isSolid () && loc .getBlockY () > 50 ;
97+ Bukkit .getScheduler ().runTaskLater (OMCPlugin .getInstance (),
98+ () -> rtpPlayer (player , nextTry ), 20L );
99+ });
100+ });
101+ });
109102 }
110103
111- public Location generateRandomLocation (World world ) {
112- int radius = (int ) (Math .random () * (maxRadius - minRadius + 1 )) + minRadius ;
113- float angle = (float ) (Math .random () * 2 * Math .PI );
104+ private int [] generateRandomCoords () {
105+ ThreadLocalRandom random = ThreadLocalRandom .current ();
106+ int radius = random .nextInt (minRadius , maxRadius + 1 );
107+ double angle = random .nextDouble () * 2 * Math .PI ;
114108 int x = (int ) (Math .cos (angle ) * radius );
115109 int z = (int ) (Math .sin (angle ) * radius );
116- return world .getHighestBlockAt (x , z ).getLocation ();
110+ return new int []{x , z };
111+ }
112+
113+ private boolean isSafe (Location loc ) {
114+ return loc .getBlock ().isSolid () && loc .getBlockY () > 50 ;
117115 }
118116
119- public void tpPlayer (Player player , Location loc ) {
117+ private void tpPlayer (Player player , Location loc ) {
120118 PlayerUtils .sendFadeTitleTeleport (player , loc );
121119 MessagesManager .sendMessage (player , TranslationManager .translation ("command.utils.rtp.success" ,
122120 Component .text (loc .getBlockX ()).color (YELLOW ),
@@ -127,5 +125,4 @@ public void tpPlayer(Player player, Location loc) {
127125 DynamicCooldownManager .use (player .getUniqueId (), "player:rtp" , rtpCooldown * 1000L )
128126 );
129127 }
130-
131128}
0 commit comments