util + borders work

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

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;
}
}