util + borders work

This commit is contained in:
Erik Bročko 2018-09-04 01:58:20 +02:00
parent 5aded1e458
commit a4e32ee605
7 changed files with 267 additions and 13 deletions

View File

@ -1,7 +1,22 @@
# Here you can specify the world limits.
# Borders will be calculated automatically from values in the 'points' list.
# These coordinates WILL be inside your border, therefore you must specify at least two.
#world:
# Here you can specify the world limits, defining them either by the center of the allowed
# region and its dimensions or two or more points inside the required area that will be
# automatically sized accordingly (to contain every single point). You can also specify both.
# Coordinates are encoded in X,Y order.
#world: # -500 -500, 500 500
# points:
# - 500, 500
# - -500, -500
# - 500 500
# - -500 -500
#flatroom: # -250 -250, 250 250
# center: 0 0 # <
# radius: 200 # < the only working size-related settings
# size: 500 500
# warningdist: 5
# warningtime: 15
# damage: 0.2
# buffer: 5
#event: # -250 -250, 600 550
# center: 0 0
# size: 500 500
# points:
# - 600 550
# - 350 200

View File

@ -5,6 +5,7 @@ import java.util.HashMap;
import org.bukkit.plugin.java.JavaPlugin;
import cz.marwland.mc.core.config.ConfigManager;
import cz.marwland.mc.core.features.Base;
import cz.marwland.mc.core.features.Feature;
import cz.marwland.mc.essentials.features.Borders;
@ -18,7 +19,8 @@ public class MarwCore extends JavaPlugin {
//getServer().getPluginManager().registerEvents(new EventListener(this), this);
configManager = new ConfigManager(this);
configManager.registerConfig("borders.yml");
this.addFeature(new Base(this));
this.addFeature(new Borders(this));
configManager.load();
@ -27,8 +29,18 @@ public class MarwCore extends JavaPlugin {
@Override
public void onDisable() {
configManager.save();
this.features.forEach((k, v) -> v.onDisable());
configManager.save();
}
@Override
public void reloadConfig() {
this.configManager.load();
}
public void reload() {
this.reloadConfig();
this.features.forEach((k, v) -> v.onReload());
}
public ConfigManager getConfigManager() {

View File

@ -0,0 +1,44 @@
package cz.marwland.mc.core.features;
import java.util.Arrays;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.configuration.file.FileConfiguration;
import cz.marwland.mc.core.MarwCore;
import cz.marwland.mc.core.features.Feature;
public class Base extends Feature {
FileConfiguration cfg;
public Base(MarwCore plugin) {
super(plugin, "marw");
// String name, String description, String usageMessage, List<String> aliases)
this.addCommand(new BukkitCommand("marw", "Core plugin controls.", "/marw", Arrays.asList("marwcore")) {
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (args.length < 1) {
sendHelp(sender);
} else if (args[0].equalsIgnoreCase("reload")) {
plugin.reload();
sender.sendMessage("Reloaded!");
}
return true;
}
public void sendHelp(CommandSender sender) {
sender.sendMessage("Available commands:");
sender.sendMessage("/marw reload - Reload configs.");
}
});
}
@Override
public void onEnable() {
super.onEnable();
}
}

View File

@ -13,7 +13,7 @@ import org.bukkit.event.Listener;
import cz.marwland.mc.core.MarwCore;
public abstract class Feature implements Listener {
private final MarwCore plugin;
protected final MarwCore plugin;
private String name = "";
private ArrayList<BukkitCommand> commands = new ArrayList<>();
private HashMap<String, String> configs = new HashMap<>();
@ -40,10 +40,19 @@ public abstract class Feature implements Listener {
}
plugin.getServer().getPluginManager().registerEvents(this, plugin);
configs.forEach((k, v) -> plugin.getConfigManager().registerConfig(k, v));
//configs.forEach((k, v) -> plugin.getConfigManager().registerConfig(k, v));
}
public void onDisable() {
if (cmap != null) {
for (BukkitCommand cmd : commands) {
cmd.unregister(cmap);
}
}
}
public void onReload() {
}
public String getName() {
@ -63,7 +72,8 @@ public abstract class Feature implements Listener {
}
protected void registerConfig(String template, String path) {
this.configs.put(template, path);
//this.configs.put(template, path);
plugin.getConfigManager().registerConfig(template, path);
}
protected FileConfiguration getConfig(String path) {

View File

@ -0,0 +1,25 @@
package cz.marwland.mc.core.util;
import org.bukkit.util.Vector;
public class LocationUtil {
public static Vector deserializeVector(String str) {
String[] args = str.split(" ");
double[] vals = new double[3];
if (args.length < 2)
return null;
for (int i = 0; i < args.length; i++) {
try {
vals[i] = Double.parseDouble(args[i]);
} catch (NumberFormatException e) {
return null;
}
}
if (args.length == 2) {
vals[2] = vals[1];
vals[1] = 0d;
}
return new Vector(vals[0], vals[1], vals[2]);
}
}

View File

@ -0,0 +1,77 @@
package cz.marwland.mc.core.util;
import org.bukkit.util.Vector;
public class Region {
Vector one = null;
Vector two = null;
public Region() {
}
public Region(Vector one, Vector two) {
this.one = one;
this.two = two;
}
public void ensureInside(Vector v) {
if (one == null) {
one = v;
return;
}
if (two == null) {
two = v;
return;
}
if (v.getX() > one.getX() && v.getX() > two.getX()) {
if (one.getX() < two.getX())
two.setX(v.getX());
else
one.setX(v.getX());
} else if (v.getX() < one.getX() && v.getX() < two.getX()) {
if (one.getX() > two.getX())
two.setX(v.getX());
else
one.setX(v.getX());
}
if (v.getY() > one.getY() && v.getY() > two.getY()) {
if (one.getY() < two.getY())
two.setY(v.getY());
else
one.setY(v.getY());
} else if (v.getY() < one.getY() && v.getY() < two.getY()) {
if (one.getY() > two.getY())
two.setY(v.getY());
else
one.setY(v.getY());
}
if (v.getZ() > one.getZ() && v.getZ() > two.getZ()) {
if (one.getZ() < two.getZ())
two.setZ(v.getZ());
else
one.setZ(v.getZ());
} else if (v.getZ() < one.getZ() && v.getZ() < two.getZ()) {
if (one.getZ() > two.getZ())
two.setZ(v.getZ());
else
one.setZ(v.getZ());
}
}
public Vector getCenter() {
return new Vector((one.getX() + two.getX()) / 2, (one.getY() + two.getY()) / 2, (one.getZ() + two.getZ()) / 2);
}
public boolean isValid() {
return this.one != null && this.two != null;
}
public void setOne(Vector one) {
this.one = one;
}
public void setTwo(Vector two) {
this.two = two;
}
}

View File

@ -1,15 +1,22 @@
package cz.marwland.mc.essentials.features;
import java.util.Arrays;
import java.util.List;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.util.Vector;
import cz.marwland.mc.core.MarwCore;
import cz.marwland.mc.core.features.Feature;
import cz.marwland.mc.core.util.LocationUtil;
import cz.marwland.mc.core.util.Region;
public class Borders extends Feature {
FileConfiguration cfg;
public Borders(MarwCore plugin) {
super(plugin, "heil");
// String name, String description, String usageMessage, List<String> aliases)
@ -26,7 +33,71 @@ public class Borders extends Feature {
@Override
public void onEnable() {
super.onEnable();
cfg = getConfig("borders.yml");
this.loadWorlds();
}
@Override
public void onReload() {
cfg = getConfig("borders.yml");
this.loadWorlds();
}
public void loadWorlds() {
for(World w : plugin.getServer().getWorlds()) {
final String wname = w.getName();
if (!cfg.contains(wname))
continue;
plugin.getLogger().info("Loading borders for " + wname);
Region r = new Region();
if (cfg.contains(wname + ".center") && cfg.contains(wname + ".size")) {
plugin.getLogger().info("should i?");
String centerstr = cfg.getString(wname + ".center");
Vector center = LocationUtil.deserializeVector(centerstr);
String sizestr = cfg.getString(wname + ".size");
if (sizestr.indexOf(' ') == -1) {
w.getWorldBorder().setCenter(center.getX(), center.getZ());
w.getWorldBorder().setSize(cfg.getDouble(wname + ".size", 0));
plugin.getLogger().info("set the ghetto way");
} else {
Vector size = LocationUtil.deserializeVector(sizestr);
if (center == null) {
plugin.getLogger().config("[Borders] Invalid center coordinates for " + wname + ": " + centerstr);
} else if (size == null) {
plugin.getLogger().config("[Borders] Invalid size for " + wname + ": " + sizestr);
} else {
size.divide(new Vector(2f, 2f, 2f));
r.setOne(center.add(size));
r.setTwo(center.subtract(size));
}
}
}
if (cfg.contains(wname + ".points")) {
List<String> points = cfg.getStringList(wname + ".points");
for (String pstr : points) {
Vector p = LocationUtil.deserializeVector(pstr);
if (p == null)
continue;
r.ensureInside(p);
}
}
if (r.isValid()) {
Vector c = r.getCenter();
w.getWorldBorder().setCenter(c.getX(), c.getZ());
}
if (cfg.contains(wname + ".radius"))
w.getWorldBorder().setSize(cfg.getInt(wname + ".radius"));
if (cfg.contains(wname + ".warningdist"))
w.getWorldBorder().setWarningDistance(cfg.getInt(wname + ".warningdist"));
if (cfg.contains(wname + ".warningtime"))
w.getWorldBorder().setWarningTime(cfg.getInt(wname + ".warningtime"));
if (cfg.contains(wname + ".damage"))
w.getWorldBorder().setDamageAmount(cfg.getDouble(wname + ".damage"));
if (cfg.contains(wname + ".buffer"))
w.getWorldBorder().setDamageBuffer(cfg.getDouble(wname + ".buffer"));
}
}
}