Skip to content

Commit

Permalink
feat: actually set the world spawn on /setspawn, close #622
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed May 30, 2024
1 parent 386ea57 commit b555b43
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import net.william278.huskhomes.network.Broker;
import net.william278.huskhomes.network.PluginMessageBroker;
import net.william278.huskhomes.network.RedisBroker;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
import net.william278.huskhomes.random.NormalDistributionEngine;
import net.william278.huskhomes.random.RandomTeleportEngine;
Expand Down Expand Up @@ -264,6 +265,13 @@ public Audience getAudience(@NotNull UUID user) {
return audiences.player(user);
}

@Override
public void setWorldSpawn(@NotNull Position position) {
BukkitAdapter.adaptLocation(position).ifPresent(location -> Objects
.requireNonNull(location.getWorld(), "World is null")
.setSpawnLocation(location));
}

@NotNull
@Override
public Broker getMessenger() {
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/net/william278/huskhomes/HuskHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ default Optional<Position> getSpawn() {
: getServerSpawn().map(spawn -> spawn.getPosition(getServerName()));
}

/**
* Update the spawn position of a world on the server.
*
* @param position The new spawn world and coordinates.
*/
void setWorldSpawn(@NotNull Position position);

/**
* Returns the {@link Server} the plugin is on.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@

import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.regex.Pattern;

public class EditHomeCommand extends SavedPositionCommand<Home> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class RtpCommand extends Command implements UserListTabProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void execute(@NotNull OnlineUser executor, @NotNull String[] args) {
} else {
plugin.setServerSpawn(position);
}
plugin.runSync(() -> plugin.setWorldSpawn(position));
} catch (ValidationException e) {
e.dispatchWarpError(executor, plugin, global.getWarpName());
return;
Expand Down
14 changes: 14 additions & 0 deletions fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.fabric.FabricServerAudiences;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.william278.desertwell.util.Version;
import net.william278.huskhomes.command.Command;
import net.william278.huskhomes.command.FabricCommand;
Expand All @@ -53,6 +54,7 @@
import net.william278.huskhomes.network.FabricPluginMessage;
import net.william278.huskhomes.network.PluginMessageBroker;
import net.william278.huskhomes.network.RedisBroker;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
import net.william278.huskhomes.random.NormalDistributionEngine;
import net.william278.huskhomes.random.RandomTeleportEngine;
Expand Down Expand Up @@ -238,6 +240,18 @@ public Audience getAudience(@NotNull UUID user) {
return audiences.player(user);
}

@Override
public void setWorldSpawn(@NotNull Position position) {
minecraftServer.getWorlds().forEach(world -> {
if (!world.getRegistryKey().getValue().toString().equals(position.getWorld().getName())) {
return;
}
world.setSpawnPos(new BlockPos(
(int) position.getX(), (int) position.getY(), (int) position.getZ()
), position.getYaw());
});
}

@Override
public Optional<Spawn> getServerSpawn() {
return Optional.ofNullable(serverSpawn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

package net.william278.huskhomes.network;

import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@
import net.william278.huskhomes.network.Broker;
import net.william278.huskhomes.network.PluginMessageBroker;
import net.william278.huskhomes.network.RedisBroker;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
import net.william278.huskhomes.random.NormalDistributionEngine;
import net.william278.huskhomes.random.RandomTeleportEngine;
import net.william278.huskhomes.user.ConsoleUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.user.SpongeUser;
import net.william278.huskhomes.util.SpongeSafetyResolver;
import net.william278.huskhomes.util.SpongeTask;
import net.william278.huskhomes.util.UnsafeBlocks;
import net.william278.huskhomes.util.Validator;
import net.william278.huskhomes.util.*;
import org.bstats.charts.SimplePie;
import org.bstats.sponge.Metrics;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -232,6 +230,13 @@ public Audience getAudience(@NotNull UUID user) {
return game.server().player(user).map(player -> (Audience) player).orElse(Audience.empty());
}

@Override
public void setWorldSpawn(@NotNull Position position) {
SpongeAdapter.adaptLocation(position).ifPresent(
loc -> loc.world().properties().setSpawnPosition(loc.blockPosition())
);
}

@Override
public Optional<Spawn> getServerSpawn() {
return Optional.ofNullable(serverSpawn);
Expand Down

0 comments on commit b555b43

Please sign in to comment.