🚧 Add koth create

This commit is contained in:
Maxlego08 2024-02-22 11:33:50 +01:00
parent 075734a6aa
commit b65b59b3b7
18 changed files with 416 additions and 10 deletions

View File

@ -1,21 +1,33 @@
package fr.maxlego08.koth;
import fr.maxlego08.koth.api.Koth;
import fr.maxlego08.koth.api.events.KothCreateEvent;
import fr.maxlego08.koth.loader.KothLoader;
import fr.maxlego08.koth.zcore.enums.Message;
import fr.maxlego08.koth.zcore.utils.ZUtils;
import fr.maxlego08.koth.zcore.utils.builder.ItemBuilder;
import fr.maxlego08.koth.zcore.utils.loader.Loader;
import fr.maxlego08.koth.zcore.utils.storage.Persist;
import fr.maxlego08.koth.zcore.utils.storage.Savable;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
public class KothManager extends ZUtils implements Savable {
private final Loader<Koth> kothLoader = new KothLoader();
private final List<Koth> koths = new ArrayList<>();
private final Map<UUID, Selection> selections = new HashMap<>();
private final KothPlugin plugin;
private final File folder;
@ -34,6 +46,16 @@ public class KothManager extends ZUtils implements Savable {
@Override
public void load(Persist persist) {
File[] files = this.folder.listFiles((dir, name) -> name.endsWith(".yml"));
if (files == null) return;
for (File file : files) {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
Koth koth = this.kothLoader.load(configuration, "", file);
this.koths.add(koth);
}
System.out.println(koths);
}
public Optional<Selection> getSelection(UUID uuid) {
@ -49,4 +71,60 @@ public class KothManager extends ZUtils implements Savable {
public void createSelection(UUID uniqueId, Selection selection) {
this.selections.put(uniqueId, selection);
}
public void saveKoth(Koth koth) {
File file = new File(this.folder, koth.getFileName() + ".yml");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException exception) {
exception.printStackTrace();
}
}
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
this.kothLoader.save(koth, configuration, "");
try {
configuration.save(file);
} catch (IOException exception) {
exception.printStackTrace();
}
}
public void createKoth(Player player, String name, Location minLocation, Location maxLocation, int captureSeconds) {
Optional<Koth> optional = getKoth(name);
if (optional.isPresent()) {
message(player, Message.ALREADY_EXIST, "%name%", name);
return;
}
int distance = Math.abs(minLocation.getBlockX() - maxLocation.getBlockY());
if (distance <= 0) {
message(player, Message.KOTH_SIZE);
return;
}
String fileName = name.replace(" ", "_");
Koth koth = new ZKoth(fileName, name, captureSeconds, minLocation, maxLocation, new ArrayList<>(), new ArrayList<>());
KothCreateEvent event = new KothCreateEvent(koth);
event.call();
if (event.isCancelled()) return;
Optional<Selection> optionalSelection = getSelection(player.getUniqueId());
optionalSelection.ifPresent(Selection::clear);
this.koths.add(koth);
message(player, Message.CREATE_SUCCESS, "%name%", name);
this.saveKoth(koth);
}
private Optional<Koth> getKoth(String name) {
return this.koths.stream().filter(koth -> name != null && koth.getName().equalsIgnoreCase(name)).findFirst();
}
}

View File

@ -0,0 +1,91 @@
package fr.maxlego08.koth;
import fr.maxlego08.koth.api.Koth;
import fr.maxlego08.koth.zcore.utils.Cuboid;
import org.bukkit.Location;
import java.util.ArrayList;
import java.util.List;
public class ZKoth implements Koth {
private final String fileName;
private String name;
private int captureSeconds;
private Location minLocation;
private Location maxLocation;
private List<String> startCommands = new ArrayList<>();
private List<String> endCommands = new ArrayList<>();
public ZKoth(String fileName, String name, int captureSeconds, Location minLocation, Location maxLocation, List<String> startCommands, List<String> endCommands) {
this.fileName = fileName;
this.name = name;
this.captureSeconds = captureSeconds;
this.minLocation = minLocation;
this.maxLocation = maxLocation;
this.startCommands = startCommands;
this.endCommands = endCommands;
}
@Override
public String getFileName() {
return this.fileName;
}
@Override
public String getName() {
return this.name;
}
@Override
public Location getMinLocation() {
return this.minLocation;
}
@Override
public Location getMaxLocation() {
return this.maxLocation;
}
@Override
public Cuboid getCuboid() {
return new Cuboid(this.maxLocation, this.minLocation);
}
@Override
public Location getCenter() {
Cuboid cuboid = getCuboid();
return cuboid.getCenter();
}
@Override
public List<String> getStartCommands() {
return this.startCommands;
}
@Override
public List<String> getEndCommands() {
return this.endCommands;
}
@Override
public void move(Location minLocation, Location maxLocation) {
this.minLocation = minLocation;
this.maxLocation = maxLocation;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public void setCaptureSeconds(int captureSeconds) {
this.captureSeconds = captureSeconds;
}
@Override
public int getCaptureSeconds() {
return captureSeconds;
}
}

View File

@ -7,8 +7,12 @@ import java.util.List;
public interface Koth {
String getFileName();
String getName();
void setName(String name);
Location getMinLocation();
Location getMaxLocation();
@ -23,4 +27,7 @@ public interface Koth {
void move(Location minLocation, Location maxLocation);
int getCaptureSeconds();
void setCaptureSeconds(int captureSeconds);
}

View File

@ -0,0 +1,24 @@
package fr.maxlego08.koth.api.events;
import org.bukkit.event.Cancellable;
public class CancelledKothEvent extends KothEvent implements Cancellable {
private boolean cancelled;
/**
* @return the cancelled
*/
public boolean isCancelled() {
return cancelled;
}
/**
* @param cancelled
* the cancelled to set
*/
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}

View File

@ -0,0 +1,24 @@
package fr.maxlego08.koth.api.events;
import fr.maxlego08.koth.api.Koth;
public class KothCreateEvent extends CancelledKothEvent {
private final Koth koth;
/**
* @param koth
*/
public KothCreateEvent(Koth koth) {
super();
this.koth = koth;
}
/**
* @return the koth
*/
public Koth getKoth() {
return koth;
}
}

View File

@ -0,0 +1,26 @@
package fr.maxlego08.koth.api.events;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class KothEvent extends Event {
private final static HandlerList handlers = new HandlerList();
/**
* @return the handlers
*/
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public void call(){
Bukkit.getPluginManager().callEvent(this);
}
}

View File

@ -11,6 +11,8 @@ public class CommandKoth extends VCommand {
super(plugin);
this.setPermission(Permission.ZKOTH_USE);
this.addSubCommand(new CommandKothReload(plugin));
this.addSubCommand(new CommandKothAxe(plugin));
this.addSubCommand(new CommandKothCreate(plugin));
}
@Override

View File

@ -5,6 +5,7 @@ import fr.maxlego08.koth.command.VCommand;
import fr.maxlego08.koth.zcore.enums.Message;
import fr.maxlego08.koth.zcore.enums.Permission;
import fr.maxlego08.koth.zcore.utils.commands.CommandType;
import org.bukkit.inventory.ItemStack;
public class CommandKothAxe extends VCommand {
@ -18,9 +19,9 @@ public class CommandKothAxe extends VCommand {
@Override
protected CommandType perform(KothPlugin plugin) {
plugin.reloadConfig();
plugin.reloadFiles();
message(sender, Message.RELOAD);
ItemStack itemStack = this.manager.getKothAxe();
this.player.getInventory().addItem(itemStack);
message(this.sender, Message.AXE_RECEIVE);
return CommandType.SUCCESS;
}

View File

@ -0,0 +1,61 @@
package fr.maxlego08.koth.command.commands;
import fr.maxlego08.koth.KothPlugin;
import fr.maxlego08.koth.Selection;
import fr.maxlego08.koth.command.VCommand;
import fr.maxlego08.koth.zcore.enums.Message;
import fr.maxlego08.koth.zcore.enums.Permission;
import fr.maxlego08.koth.zcore.utils.commands.CommandType;
import org.bukkit.Location;
import java.util.Optional;
public class CommandKothCreate extends VCommand {
public CommandKothCreate(KothPlugin plugin) {
super(plugin);
this.setPermission(Permission.ZKOTH_CREATE);
this.addSubCommand("create");
this.setDescription(Message.DESCRIPTION_CREATE);
this.setConsoleCanUse(false);
this.addRequireArg("name");
this.addOptionalArg("capture/score");
}
@Override
protected CommandType perform(KothPlugin plugin) {
String name = argAsString(0);
int captureSeconds = argAsInteger(1, 30);
Optional<Selection> optional = this.manager.getSelection(this.player.getUniqueId());
if (!optional.isPresent()) {
message(this.sender, Message.CREATE_ERROR_SELECTION);
return CommandType.DEFAULT;
}
Selection selection = optional.get();
if (!selection.isValid()) {
message(this.sender, Message.CREATE_ERROR_SELECTION);
return CommandType.DEFAULT;
}
if (!selection.isCorrect()) {
message(this.sender, Message.CREATE_ERROR_SIZE);
return CommandType.DEFAULT;
}
if (name == null) {
return CommandType.SYNTAX_ERROR;
}
Location minLocation = selection.getRightLocation();
Location maxLocation = selection.getLeftLocation();
this.manager.createKoth(this.player, name, minLocation, maxLocation, captureSeconds);
return CommandType.SUCCESS;
}
}

View File

@ -19,9 +19,9 @@ public class CommandKothReload extends VCommand {
@Override
protected CommandType perform(KothPlugin plugin) {
ItemStack itemStack = this.manager.getKothAxe();
this.player.getInventory().addItem(itemStack);
message(this.sender, Message.AXE_RECEIVE);
plugin.reloadConfig();
plugin.reloadFiles();
message(sender, Message.RELOAD);
return CommandType.SUCCESS;
}

View File

@ -0,0 +1,42 @@
package fr.maxlego08.koth.loader;
import fr.maxlego08.koth.ZKoth;
import fr.maxlego08.koth.api.Koth;
import fr.maxlego08.koth.zcore.utils.ZUtils;
import fr.maxlego08.koth.zcore.utils.loader.Loader;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.List;
public class KothLoader extends ZUtils implements Loader<Koth> {
private final Loader<Location> locationLoader = new LocationLoader();
@Override
public Koth load(YamlConfiguration configuration, String path, File file) {
String fileName = getFileNameWithoutExtension(file);
String name = configuration.getString("name");
int captureSeconds = configuration.getInt("capture");
List<String> startCommands = configuration.getStringList("startCommands");
List<String> endCommands = configuration.getStringList("endCommands");
Location minLocation = locationLoader.load(configuration, "minLocation.", file);
Location manLocation = locationLoader.load(configuration, "maxLocation.", file);
return new ZKoth(fileName, name, captureSeconds, minLocation, manLocation, startCommands, endCommands);
}
@Override
public void save(Koth object, YamlConfiguration configuration, String path) {
configuration.set("name", object.getName());
configuration.set("capture", object.getCaptureSeconds());
configuration.set("startCommands", object.getStartCommands());
configuration.set("endCommands", object.getEndCommands());
locationLoader.save(object.getMinLocation(), configuration, "minLocation.");
locationLoader.save(object.getMaxLocation(), configuration, "manLocation.");
}
}

View File

@ -0,0 +1,31 @@
package fr.maxlego08.koth.loader;
import fr.maxlego08.koth.zcore.utils.loader.Loader;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
public class LocationLoader implements Loader<Location> {
@Override
public Location load(YamlConfiguration configuration, String path, File file) {
String worldName = configuration.getString(path + "world", "world");
int x = configuration.getInt(path + "x");
int y = configuration.getInt(path + "y");
int z = configuration.getInt(path + "z");
return new Location(Bukkit.getWorld(worldName), x, y, z);
}
@Override
public void save(Location location, YamlConfiguration configuration, String path) {
configuration.set(path + "world", location.getWorld().getName());
configuration.set(path + "x", location.getBlockX());
configuration.set(path + "y", location.getBlockY());
configuration.set(path + "z", location.getBlockZ());
}
}

View File

@ -45,6 +45,7 @@ public enum Message {
DESCRIPTION_RELOAD("Reload configuration files"),
DESCRIPTION_AXE("Getting the selection axe"),
DESCRIPTION_CREATE("Create new koth"),
AXE_RECEIVE("§7You have just received the axe for zone selection."),
AXE_NAME("§6✤ §7zKoth axe §6✤"),
@ -61,6 +62,14 @@ public enum Message {
AXE_ERROR("§cYour selection is invalid, you must have at least 2 blocks of height."),
AXE_VALID("§aYour selection is valid, you can create a koth with the command §f/koth create <name>§a."),
CREATE_SUCCESS("§7You just created the koth §f%name%§7."),
CREATE_ERROR_SELECTION("§cYou must select a zone with the command §b/zkoth axe§c."),
CREATE_ERROR_SIZE("§cYour selection is invalid, you must have at least 2 blocks of height."),
ALREADY_EXIST("§cThe koth §f%name% §calready exists."),
KOTH_SIZE("§cYour koth is too small, you can't create one that small. Then you will come on discord for support when the problem comes from you."),
DOESNT_EXIST("§cThe koth §f%name% §cdoesnt exists."),
;

View File

@ -6,7 +6,7 @@ public enum Permission {
ZKOTH_RELOAD,
ZKOTH_AXE,
;
ZKOTH_CREATE;
private String permission;

View File

@ -1,5 +1,6 @@
package fr.maxlego08.koth.zcore.utils;
import java.io.File;
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
@ -1256,4 +1257,9 @@ public abstract class ZUtils extends MessageUtils {
return false;
}
protected String getFileNameWithoutExtension(File file) {
Pattern pattern = Pattern.compile("(?<=.)\\.[^.]+$");
return pattern.matcher(file.getName()).replaceAll("").replace(" ", "_");
}
}

View File

@ -1,5 +1,6 @@
package fr.maxlego08.koth.zcore.utils.loader;
import java.io.File;
import java.util.List;
import fr.maxlego08.koth.zcore.utils.ZUtils;
@ -12,7 +13,7 @@ import org.bukkit.material.MaterialData;
public class ButtonLoader extends ZUtils implements Loader<Button> {
@Override
public Button load(YamlConfiguration configuration, String path) {
public Button load(YamlConfiguration configuration, String path, File file) {
String name = configuration.getString(path + "name") == null ? null
: color(configuration.getString(path + "name"));

View File

@ -1,5 +1,6 @@
package fr.maxlego08.koth.zcore.utils.loader;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@ -26,7 +27,7 @@ public class ItemStackLoader extends ZUtils implements Loader<ItemStack> {
/**
* Load ItemStack
*/
public ItemStack load(YamlConfiguration configuration, String path) {
public ItemStack load(YamlConfiguration configuration, String path, File file) {
int data = configuration.getInt(path + "data", 0);
int amount = configuration.getInt(path + "amount", 1);

View File

@ -2,6 +2,8 @@ package fr.maxlego08.koth.zcore.utils.loader;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
public interface Loader<T> {
/**
@ -11,7 +13,7 @@ public interface Loader<T> {
* @param path
* @return element
*/
T load(YamlConfiguration configuration, String path);
T load(YamlConfiguration configuration, String path, File file);
/**
* Save object to yml