Skip to content

Commit

Permalink
Refactor offset command
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Sep 22, 2023
1 parent 6c0b4f1 commit 7efb488
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,74 @@
package de.btegermany.terraplusminus.commands;

import de.btegermany.terraplusminus.Terraplusminus;
import de.btegermany.terraplusminus.gen.RealWorldGenerator;
import net.buildtheearth.terraminusminus.projection.GeographicProjection;
import net.buildtheearth.terraminusminus.projection.transform.OffsetProjectionTransform;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.generator.ChunkGenerator;
import org.jetbrains.annotations.NotNull;

import static java.lang.Math.round;
import static org.bukkit.ChatColor.GRAY;
import static org.bukkit.ChatColor.RED;

public class OffsetCommand implements CommandExecutor {

@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
if (command.getName().equalsIgnoreCase("offset")) {
Player player = (Player) commandSender;
if (player.hasPermission("t+-.offset")) {
player.sendMessage(Terraplusminus.config.getString("prefix") + "§7Offsets:");
player.sendMessage(Terraplusminus.config.getString("prefix") + "§7 | X: §8" + Terraplusminus.config.getInt("terrain_offset.x"));
player.sendMessage(Terraplusminus.config.getString("prefix") + "§7 | Y: §8" + Terraplusminus.config.getInt("terrain_offset.y"));
player.sendMessage(Terraplusminus.config.getString("prefix") + "§7 | Z: §8" + Terraplusminus.config.getInt("terrain_offset.z"));
} else {
player.sendMessage(Terraplusminus.config.getString("prefix") + "§7No permission for /offset");
return true;
}
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String... arguments) {

String prefix = Terraplusminus.config.getString("prefix");

if (!sender.hasPermission("t+-.offset")) {
sender.sendMessage(Terraplusminus.config.getString("prefix") + "§7No permission for /offset");
return true;
}

World world;
if (arguments.length == 1) {
String worldName = arguments[0];
world = Bukkit.getWorld(worldName);
} else if (arguments.length == 0 && sender instanceof Player) {
Player player = (Player) sender;
world = player.getWorld();
} else {
return false;
}

if (world == null) {
sender.sendMessage(prefix + RED + "No such world");
return true;
}

ChunkGenerator generator = world.getGenerator();
if (!(generator instanceof RealWorldGenerator)) {
sender.sendMessage(prefix + GRAY + world.getName() + RED + " is not a Terra+- world");
return true;
}
RealWorldGenerator realWorldGenerator = (RealWorldGenerator) generator;
GeographicProjection projection = realWorldGenerator.getSettings().projection();

int offsetY = realWorldGenerator.getYOffset();
int offsetX, offsetZ;
if (projection instanceof OffsetProjectionTransform) {
OffsetProjectionTransform transform = (OffsetProjectionTransform) projection;
// We assume there is ony one offset transform, or that it is the only one the user cares about
// We can safely round to int as we are only dealing with blocks at that scale
offsetX = (int) round(transform.dx());
offsetZ = (int) round(transform.dy());
} else {
offsetX = offsetZ = 0;
}

sender.sendMessage(prefix + "§7Offsets for world" + world.getName() + ":");
sender.sendMessage(prefix + "§7 | X: §8" + offsetX);
sender.sendMessage(prefix + "§7 | Y: §8" + offsetY);
sender.sendMessage(prefix + "§7 | Z: §8" + offsetZ);
return true;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ commands:
usage: /where
offset:
description: Displays the x,y and z offset of your world
usage: /offset
usage: /offset [world]

0 comments on commit 7efb488

Please sign in to comment.