Implement config file and spawn offset
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Bea 2022-11-12 01:12:45 +01:00
parent dd1425863a
commit 51c178ab4f
9 changed files with 145 additions and 19 deletions

View File

@ -1,8 +1,6 @@
package wtf.beatrice.limbomanager;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.configuration.file.YamlConfiguration;
import wtf.beatrice.limbomanager.objects.Coordinates;
import wtf.beatrice.limbomanager.objects.LocationCheckRunnable;
@ -11,20 +9,18 @@ import java.util.HashMap;
public class Cache
{
public static final Coordinates baseCoords = new Coordinates(1000, 1000);
private static YamlConfiguration configuration;
public static final HashMap<String, Coordinates> playerIslands = new HashMap<>();
public static LocationCheckRunnable locationCheckRunnable;
public static void teleportToOwnIsland(Player player)
{
Coordinates islandCoords = playerIslands.get(player.getName());
Location targetLocation = new Location(player.getWorld(), islandCoords.getX(), 100, islandCoords.getZ());
Bukkit.getScheduler().runTask(LimboManager.getInstance(), () -> {
targetLocation.getWorld().loadChunk(targetLocation.getChunk().getX(), targetLocation.getChunk().getZ(), true);
player.teleport(targetLocation);
});
public static void setConfiguration(YamlConfiguration configuration) {
Cache.configuration = configuration;
}
public static YamlConfiguration getConfiguration() {
return configuration;
}
}

View File

@ -1,6 +1,8 @@
package wtf.beatrice.limbomanager;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import wtf.beatrice.limbomanager.commands.Spawn;
@ -9,8 +11,10 @@ import wtf.beatrice.limbomanager.listeners.PlayerChecker;
import wtf.beatrice.limbomanager.listeners.PlayerTeleporter;
import wtf.beatrice.limbomanager.listeners.RiskyBlocksHandler;
import wtf.beatrice.limbomanager.objects.LocationCheckRunnable;
import wtf.beatrice.limbomanager.utils.FileUtils;
import java.io.File;
import java.io.IOException;
public class LimboManager extends JavaPlugin {
@ -33,10 +37,20 @@ public class LimboManager extends JavaPlugin {
pluginManager.registerEvents(new CommandCanceller(), this);
pluginManager.registerEvents(new RiskyBlocksHandler(), this);
// no need to check if it exists, it will just skip creation.
// no need to check if folder exists, it will just skip creation.
getDataFolder().mkdirs();
schematicsFolderPath = getDataFolder().getAbsolutePath() + File.separator + "schematics";
getSchematicsFolder().mkdirs();
String configPath = getDataFolder().getAbsolutePath() + File.separator + "config.yml";
File config = new File(configPath);
FileUtils.copyFileFromSrc(config);
YamlConfiguration configuration = new YamlConfiguration();
try {
configuration.load(config);
Cache.setConfiguration(configuration);
} catch (IOException | InvalidConfigurationException e) {
throw new RuntimeException(e);
}
// start location check runnable
Cache.locationCheckRunnable = new LocationCheckRunnable();

View File

@ -5,7 +5,7 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.limbomanager.Cache;
import wtf.beatrice.limbomanager.utils.LocationUtils;
public class Spawn implements CommandExecutor
{
@ -20,7 +20,7 @@ public class Spawn implements CommandExecutor
return true;
}
Cache.teleportToOwnIsland((Player) sender);
LocationUtils.teleportToOwnIsland((Player) sender);
sender.sendMessage("Teleported");
return true;
}

View File

@ -22,6 +22,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
import wtf.beatrice.limbomanager.Cache;
import wtf.beatrice.limbomanager.LimboManager;
import wtf.beatrice.limbomanager.objects.Coordinates;
import wtf.beatrice.limbomanager.utils.LocationUtils;
import java.io.File;
import java.io.FileInputStream;
@ -77,7 +78,7 @@ public class PlayerTeleporter implements Listener
}
Cache.teleportToOwnIsland(player);
LocationUtils.teleportToOwnIsland(player);
}
@ -98,7 +99,7 @@ public class PlayerTeleporter implements Listener
we start from (1000, 1000) and we put islands at 500 blocks distance up to 10000. then we move to a new row.
*/
Coordinates islandCoords = new Coordinates(1000, 1000);
Coordinates islandCoords = new Coordinates(Cache.baseCoords);
for(Coordinates currentCoords : Cache.playerIslands.values()) {
// if the two are not the same, it means we found a free spot.

View File

@ -11,6 +11,12 @@ public class Coordinates
this.z = z;
}
public Coordinates(Coordinates old)
{
this.x = old.x;
this.z = old.z;
}
public void setX(int x) { this.x = x; }
public void setZ(int z) { this.z = z; }

View File

@ -5,6 +5,7 @@ import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import wtf.beatrice.limbomanager.Cache;
import wtf.beatrice.limbomanager.utils.LocationUtils;
import java.util.ArrayList;
import java.util.List;
@ -49,7 +50,7 @@ public class LocationCheckRunnable implements Runnable
if(distanceX > distanceMax || distanceZ > distanceMax)
{
Cache.teleportToOwnIsland(player);
LocationUtils.teleportToOwnIsland(player);
player.sendMessage("Out of island limits");
}

View File

@ -0,0 +1,40 @@
package wtf.beatrice.limbomanager.utils;
import wtf.beatrice.limbomanager.LimboManager;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
public class FileUtils
{
public static void copyFileFromSrc(File destination)
{
// Check if files already exists and if it doesn't, then create it.
if(!destination.exists())
{
// Load the InputStream of the file in the source folder.
InputStream is = FileUtils.class.getResourceAsStream("/" + destination.getName());
try
{
// Try copying the file to the directory where it's supposed to be, and log it.
Files.copy(is, Paths.get(destination.getAbsolutePath()));
is.close();
LimboManager.getInstance().getLogger().log(Level.INFO, "File " + destination.getName() + " successfully created.");
}
catch (IOException e)
{
// Throw exception if something went wrong
LimboManager.getInstance().getLogger().log(Level.SEVERE, "There were some unexpected errors from " + destination.getName() + " file creation. Please contact the developer and send him this log:");
e.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,57 @@
package wtf.beatrice.limbomanager.utils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import wtf.beatrice.limbomanager.Cache;
import wtf.beatrice.limbomanager.LimboManager;
import wtf.beatrice.limbomanager.objects.Coordinates;
import java.util.logging.Level;
public class LocationUtils {
public static void teleportToOwnIsland(Player player)
{
Location targetLocation = getPlayerSpawnLocation(player);
Bukkit.getScheduler().runTask(LimboManager.getInstance(), () -> {
targetLocation.getWorld().loadChunk(targetLocation.getChunk().getX(), targetLocation.getChunk().getZ(), true);
player.teleport(targetLocation);
});
}
private static Location getPlayerSpawnLocation(Player player) {
String worldName = Cache.getConfiguration().getString("island.world-name");
if(worldName == null)
{
LimboManager.getInstance().getLogger().log(Level.SEVERE, "World name is null!");
return null;
}
World spawnWorld = Bukkit.getWorld(worldName);
if(spawnWorld == null)
{
LimboManager.getInstance().getLogger().log(Level.SEVERE, "World is null!");
return null;
}
String playerName = player.getName();
Coordinates coordinates = Cache.playerIslands.get(playerName);
double offsetX = Cache.getConfiguration().getDouble("island.spawn-offset.x");
double y = Cache.getConfiguration().getDouble("island.spawn-offset.y");
double offsetZ = Cache.getConfiguration().getDouble("island.spawn-offset.z");
double x = coordinates.getX() + offsetX;
double z = coordinates.getZ() + offsetZ;
float yaw = (float) Cache.getConfiguration().getDouble("island.spawn-offset.yaw");
float pitch = (float) Cache.getConfiguration().getDouble("island.spawn-offset.pitch");
return new Location(spawnWorld, x, y, z, yaw, pitch);
}
}

View File

@ -0,0 +1,11 @@
sizing:
island-distance: 500
allowed-range: 100
island:
world-name: world
spawn-offset:
x: 0.0
y: 20.0
z: 0.0
yaw: 0.0
pitch: 0.0