Skip to content

Commit

Permalink
store exact locations in (first) join config
Browse files Browse the repository at this point in the history
  • Loading branch information
NonSwag committed Mar 11, 2024
1 parent 6e7e4f4 commit 6c1cc85
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 30 deletions.
5 changes: 4 additions & 1 deletion plugin/src/main/java/net/thenextlvl/worlds/Worlds.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import core.file.format.GsonFile;
import core.i18n.file.ComponentBundle;
import core.io.IO;
import core.paper.adapters.world.LocationAdapter;
import core.paper.adapters.world.WorldAdapter;
import lombok.Getter;
import lombok.experimental.Accessors;
Expand All @@ -24,6 +25,7 @@
import net.thenextlvl.worlds.preset.Presets;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerKickEvent;
Expand Down Expand Up @@ -98,7 +100,8 @@ private void initConfig() {
configFile = new GsonFile<>(
IO.of(getDataFolder(), "config.json"), new Config(),
new GsonBuilder()
.registerTypeHierarchyAdapter(World.class, WorldAdapter.Name.INSTANCE)
.registerTypeHierarchyAdapter(Location.class, LocationAdapter.Simple.INSTANCE)
.registerTypeHierarchyAdapter(World.class, WorldAdapter.Key.INSTANCE)
.setPrettyPrinting()
.serializeNulls()
.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.worlds.Worlds;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -34,32 +33,23 @@ private static void execute(CommandContext<CommandSender> context) {
var location = context.contains("position") ? context.<Location>get("position") : player.getLocation();
var angle = context.contains("angle") ? context.<Float>get("angle") : player.getLocation().getYaw();
player.getWorld().setSpawnLocation(location.getBlockX(), location.getBlockY(), location.getBlockZ(), angle);
boolean firstJoinWorld = context.flags().contains("first-join");
boolean joinWorld = context.flags().contains("on-join");
if (firstJoinWorld) setFirstJoinWorld(location.getWorld());
else if (joinWorld) setJoinWorld(location.getWorld());
var firstJoinWorld = context.flags().contains("first-join");
var joinWorld = context.flags().contains("on-join");
assert plugin.configFile() != null;
if (firstJoinWorld) plugin.configFile().getRoot().setFirstJoinLocation(location);
if (joinWorld) plugin.configFile().getRoot().setJoinLocation(location);
if (firstJoinWorld || joinWorld) plugin.configFile().save();
setSpawn(player, location, angle);
var message = firstJoinWorld ? "world.spawn.set.first" :
joinWorld ? "world.spawn.set.join" : "world.spawn.set";
var message = joinWorld ? "world.spawn.set.join"
: firstJoinWorld ? "world.spawn.set.first"
: "world.spawn.set";
plugin.bundle().sendMessage(player, message,
Placeholder.parsed("x", String.valueOf(location.getBlockX())),
Placeholder.parsed("y", String.valueOf(location.getBlockY())),
Placeholder.parsed("z", String.valueOf(location.getBlockZ())),
Placeholder.parsed("angle", String.valueOf(angle)));
}

private static void setFirstJoinWorld(World world) {
assert plugin.configFile() != null;
plugin.configFile().getRoot().setFirstJoinWorld(world);
plugin.configFile().save();
}

private static void setJoinWorld(World world) {
assert plugin.configFile() != null;
plugin.configFile().getRoot().setJoinWorld(world);
plugin.configFile().save();
}

private static void setSpawn(Player player, Location location, float angle) {
player.teleportAsync(player.getWorld().getSpawnLocation().add(0.5, 0, 0.5), COMMAND);
}
Expand Down
6 changes: 3 additions & 3 deletions plugin/src/main/java/net/thenextlvl/worlds/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.google.gson.annotations.SerializedName;
import core.annotation.FieldsAreNullableByDefault;
import lombok.Data;
import org.bukkit.World;
import org.bukkit.Location;

@Data
@FieldsAreNullableByDefault
public class Config {
private @SerializedName("first-join-world") World firstJoinWorld;
private @SerializedName("join-world") World joinWorld;
private @SerializedName("first-join-location") Location firstJoinLocation;
private @SerializedName("join-location") Location joinLocation;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ public class ConnectionListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onSpawn(PlayerSpawnLocationEvent event) {
assert plugin.configFile() != null;
if (event.getPlayer().hasPlayedBefore()) {
var world = plugin.configFile().getRoot().getJoinWorld();
if (world != null) event.setSpawnLocation(world.getSpawnLocation());
} else {
var world = plugin.configFile().getRoot().getFirstJoinWorld();
if (world != null) event.setSpawnLocation(world.getSpawnLocation());
}
var location = event.getPlayer().hasPlayedBefore()
? plugin.configFile().getRoot().getJoinLocation()
: plugin.configFile().getRoot().getFirstJoinLocation();
if (location != null) event.setSpawnLocation(location);
}
}

0 comments on commit 6c1cc85

Please sign in to comment.