Skip to content

Commit df4fa4e

Browse files
amyavimathiascode
authored andcommitted
fix: allow spawning in places other than default world
1 parent 80d30fd commit df4fa4e

3 files changed

Lines changed: 20 additions & 24 deletions

File tree

src/main/java/pw/kaboom/extras/commands/CommandSpawn.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package pw.kaboom.extras.commands;
22

33
import net.kyori.adventure.text.Component;
4-
import org.bukkit.Bukkit;
5-
import org.bukkit.Location;
6-
import org.bukkit.World;
7-
import org.bukkit.block.Block;
8-
import org.bukkit.block.BlockFace;
94
import org.bukkit.command.Command;
105
import org.bukkit.command.CommandExecutor;
116
import org.bukkit.command.CommandSender;
127
import org.bukkit.entity.Player;
8+
import org.bukkit.event.player.PlayerTeleportEvent;
9+
import pw.kaboom.extras.util.Utility;
1310

1411
import javax.annotation.Nonnull;
1512

@@ -24,23 +21,7 @@ public boolean onCommand(final @Nonnull CommandSender sender,
2421
return true;
2522
}
2623

27-
final World defaultWorld = Bukkit.getWorld("world");
28-
final World world = (defaultWorld == null) ? Bukkit.getWorlds().get(0) : defaultWorld;
29-
final Location spawnLocation = world.getSpawnLocation();
30-
final int maxWorldHeight = 256;
31-
32-
for (double y = spawnLocation.getY(); y <= maxWorldHeight; y++) {
33-
final Location yLocation = new Location(world, spawnLocation.getX(), y,
34-
spawnLocation.getZ());
35-
final Block coordBlock = world.getBlockAt(yLocation);
36-
37-
if (!coordBlock.getType().isSolid()
38-
&& !coordBlock.getRelative(BlockFace.UP).getType().isSolid()) {
39-
player.teleportAsync(yLocation);
40-
break;
41-
}
42-
}
43-
24+
Utility.teleportToSpawn(player, PlayerTeleportEvent.TeleportCause.COMMAND);
4425
player.sendMessage(Component
4526
.text("Successfully moved to spawn"));
4627
return true;

src/main/java/pw/kaboom/extras/modules/player/PlayerDamage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.bukkit.event.entity.EntityRegainHealthEvent;
2020
import org.bukkit.event.entity.FoodLevelChangeEvent;
2121
import org.bukkit.event.entity.PlayerDeathEvent;
22+
import org.bukkit.event.player.PlayerTeleportEvent;
2223
import org.bukkit.inventory.ItemStack;
2324
import org.bukkit.potion.PotionEffect;
2425
import pw.kaboom.extras.util.Utility;
@@ -103,8 +104,7 @@ void onPlayerDeath(final PlayerDeathEvent event) {
103104
if (player.getRespawnLocation() != null) {
104105
player.teleportAsync(player.getRespawnLocation());
105106
} else {
106-
final World world = Bukkit.getWorld("world");
107-
player.teleportAsync(world.getSpawnLocation());
107+
Utility.teleportToSpawn(player, PlayerTeleportEvent.TeleportCause.UNKNOWN);
108108
}
109109
} catch (Exception exception) {
110110
Utility.resetAttribute(player, Attribute.MAX_HEALTH);

src/main/java/pw/kaboom/extras/util/Utility.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
package pw.kaboom.extras.util;
22

33
import org.bukkit.Bukkit;
4+
import org.bukkit.Location;
5+
import org.bukkit.World;
46
import org.bukkit.attribute.Attributable;
57
import org.bukkit.attribute.Attribute;
68
import org.bukkit.attribute.AttributeInstance;
79
import org.bukkit.attribute.AttributeModifier;
810
import org.bukkit.entity.Entity;
911
import org.bukkit.entity.Player;
12+
import org.bukkit.event.player.PlayerTeleportEvent;
1013

1114
import javax.annotation.Nonnull;
1215
import javax.annotation.Nullable;
1316
import java.util.concurrent.Callable;
1417
import java.util.function.Function;
1518

1619
public final class Utility {
20+
public static void teleportToSpawn(final Player player,
21+
final PlayerTeleportEvent.TeleportCause cause) {
22+
final World world = player.getServer().getRespawnWorld();
23+
final Location spawnLocation = world.getSpawnLocation();
24+
25+
final int y = world.getHighestBlockYAt(spawnLocation);
26+
final Location location = new Location(world,
27+
spawnLocation.x(), y + 1, spawnLocation.z(),
28+
spawnLocation.getYaw(), spawnLocation.getPitch());
29+
player.teleportAsync(location, cause);
30+
}
31+
1732
public static @Nullable Player getPlayerExactIgnoreCase(final String username) {
1833
return Bukkit.getOnlinePlayers()
1934
.stream()

0 commit comments

Comments
 (0)