Skip to content

Commit

Permalink
random spawn option
Browse files Browse the repository at this point in the history
  • Loading branch information
pop4959 committed Jul 2, 2022
1 parent 51ab2af commit 571fb8d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public interface ISettings extends IConf {

boolean getRespawnAtHome();

boolean isRandomRespawn();
String getRandomSpawnLocation();

String getRandomRespawnLocation();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public void updateConfig() {
}
}

public boolean hasLocation(final String name) {
return config.hasProperty("locations." + name);
}

public Location getCenter(final String name) {
try {
final LazyLocation center = config.getLocation(locationKey(name, "center"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ public boolean getRespawnAtHome() {
}

@Override
public boolean isRandomRespawn() {
return config.getBoolean("random-respawn", false);
public String getRandomSpawnLocation() {
return config.getString("random-spawn-location", "none");
}

@Override
public String getRandomRespawnLocation() {
return config.getString("random-respawn-location", "world");
return config.getString("random-respawn-location", "none");
}

@Override
Expand Down
9 changes: 5 additions & 4 deletions Essentials/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1174,10 +1174,11 @@ respawn-at-home-bed: true
# When users die, should EssentialsSpawn respect users' respawn anchors?
respawn-at-anchor: false

# When users die, should they respawn randomly?
# The value of respawn-random-location will be the name of the location (set with /settpr) to teleport from.
random-respawn: false
random-respawn-location: world
# If configured, users will spawn at the random spawn location instead of the newbies spawnpoint.
random-spawn-location: "none"

# If configured, when users die, they will respawn at the random respawn location.
random-respawn-location: "none"

# Teleport all joining players to the spawnpoint
spawn-on-join: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.earth2me.essentials.Kit;
import com.earth2me.essentials.OfflinePlayer;
import com.earth2me.essentials.RandomTeleport;
import com.earth2me.essentials.User;
import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer;
Expand Down Expand Up @@ -66,13 +65,9 @@ void onPlayerRespawn(final PlayerRespawnEvent event) {
event.setRespawnLocation(home);
return;
}
} else if (ess.getSettings().isRandomRespawn()) {
final String name = ess.getSettings().getRandomRespawnLocation();
final RandomTeleport randomTeleport = ess.getRandomTeleport();
randomTeleport.getRandomLocation(name).thenAccept(location -> {
final CompletableFuture<Boolean> future = new CompletableFuture<>();
user.getAsyncTeleport().now(location, false, PlayerTeleportEvent.TeleportCause.COMMAND, future);
});
}
if (tryRandomTeleport(user, ess.getSettings().getRandomRespawnLocation())) {
return;
}
final Location spawn = spawns.getSpawn(user.getGroup());
if (spawn != null) {
Expand Down Expand Up @@ -112,7 +107,9 @@ private void delayedJoin(final Player player) {

final User user = ess.getUser(player);

if (!"none".equalsIgnoreCase(ess.getSettings().getNewbieSpawn())) {
final boolean spawnRandomly = tryRandomTeleport(user, ess.getSettings().getRandomSpawnLocation());

if (!spawnRandomly && !"none".equalsIgnoreCase(ess.getSettings().getNewbieSpawn())) {
ess.scheduleSyncDelayedTask(new NewPlayerTeleport(user), 1L);
}

Expand Down Expand Up @@ -169,4 +166,15 @@ public void run() {
}
}
}

private boolean tryRandomTeleport(final User user, final String name) {
if (!ess.getRandomTeleport().hasLocation(name)) {
return false;
}
ess.getRandomTeleport().getRandomLocation(name).thenAccept(location -> {
final CompletableFuture<Boolean> future = new CompletableFuture<>();
user.getAsyncTeleport().now(location, false, PlayerTeleportEvent.TeleportCause.PLUGIN, future);
});
return true;
}
}

0 comments on commit 571fb8d

Please sign in to comment.