mirror of
https://github.com/Maxlego08/zKoth.git
synced 2025-02-21 02:33:13 +01:00
Ajout de deux events, modification des class Manager et koth
This commit is contained in:
parent
bd8391d119
commit
15ccd51498
@ -1,22 +1,43 @@
|
||||
package fr.maxlego08.koth;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import fr.maxlego08.koth.event.KothSpawnEvent;
|
||||
import fr.maxlego08.koth.event.KothStartEvent;
|
||||
import fr.maxlego08.koth.save.Config;
|
||||
import fr.maxlego08.koth.zcore.enums.Message;
|
||||
import fr.maxlego08.koth.zcore.utils.Cuboid;
|
||||
import fr.maxlego08.koth.zcore.utils.ZUtils;
|
||||
import fr.maxlego08.koth.zcore.utils.builder.TimerBuilder;
|
||||
|
||||
public class Koth {
|
||||
public class Koth extends ZUtils {
|
||||
|
||||
private final String name;
|
||||
private int capSec;
|
||||
private Location pos1;
|
||||
private Location pos2;
|
||||
private transient boolean isEnable;
|
||||
private transient boolean isCooldown;
|
||||
private transient String currentPlayer;
|
||||
private transient int cooldown;
|
||||
private transient Player currentPlayer;
|
||||
private transient Cuboid cuboid;
|
||||
|
||||
public Koth(String name) {
|
||||
public Koth(String name, int capSec) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.capSec = capSec;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the capSec
|
||||
*/
|
||||
public int getCapSec() {
|
||||
return capSec;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -73,7 +94,7 @@ public class Koth {
|
||||
/**
|
||||
* @return the currentPlayer
|
||||
*/
|
||||
public String getCurrentPlayer() {
|
||||
public Player getCurrentPlayer() {
|
||||
return currentPlayer;
|
||||
}
|
||||
|
||||
@ -100,4 +121,218 @@ public class Koth {
|
||||
return pos2.getBlockX() + ", " + pos2.getBlockY() + ", " + pos2.getBlockZ();
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de stop un koth
|
||||
*/
|
||||
public void stop() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de start un koth
|
||||
*
|
||||
* @param sender
|
||||
* @param now
|
||||
*/
|
||||
public void spawn(CommandSender sender, boolean now) {
|
||||
|
||||
if (pos1 == null) {
|
||||
message(sender, Message.KOTH_SET_FIRST_POSITION_NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
if (pos2 == null) {
|
||||
message(sender, Message.KOTH_SET_SECOND_POSITION_NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isCooldown) {
|
||||
message(sender, Message.KOTH_SPAWN_COOLDOWN);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isEnable) {
|
||||
message(sender, Message.KOTH_SPAWN_ALREADY);
|
||||
return;
|
||||
}
|
||||
|
||||
if (cuboid == null)
|
||||
buildCuboid();
|
||||
|
||||
if (now)
|
||||
spawnNow(sender);
|
||||
else
|
||||
spawn(sender);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de créer le Cuboid
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Cuboid buildCuboid() {
|
||||
if (pos1 == null || pos2 == null)
|
||||
return null;
|
||||
return cuboid = new Cuboid(pos1, pos2);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param sender
|
||||
*/
|
||||
private void spawn(CommandSender sender) {
|
||||
|
||||
isCooldown = true;
|
||||
|
||||
cooldown = Config.cooldownInSecond;
|
||||
|
||||
scheduleFix(1000, (task, canRun) -> {
|
||||
if (!canRun)
|
||||
return;
|
||||
|
||||
if (!isCooldown) {
|
||||
task.cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
// Si on doit avetir
|
||||
if (Config.displayMessageCooldown.contains(cooldown))
|
||||
broadcast(Message.KOTH_SPAWN_MESSAGE_COOLDOWN, null, null);
|
||||
|
||||
// On fait spawn le totem
|
||||
if (cooldown <= 0) {
|
||||
task.cancel();
|
||||
isCooldown = false;
|
||||
spawnNow(sender);
|
||||
}
|
||||
|
||||
cooldown--;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de faire spawn le totem sans cooldown
|
||||
*
|
||||
* @param sender
|
||||
*/
|
||||
private void spawnNow(CommandSender sender) {
|
||||
|
||||
KothSpawnEvent event = new KothSpawnEvent(cuboid, this);
|
||||
event.callEvent();
|
||||
|
||||
if (event.isCancelled())
|
||||
return;
|
||||
|
||||
isEnable = true;
|
||||
currentPlayer = null;
|
||||
broadcast(Message.KOTH_SPAWN_MESSAGE, null, null);
|
||||
|
||||
}
|
||||
|
||||
public void startCap(Player player, FactionListener listener) {
|
||||
|
||||
KothStartEvent event = new KothStartEvent(player, this);
|
||||
event.callEvent();
|
||||
|
||||
if (event.isCancelled())
|
||||
return;
|
||||
|
||||
broadcast(Message.KOHT_CATCH, player, listener.getFactionTag(player));
|
||||
|
||||
if (capSec <= 0)
|
||||
capSec = Config.defaultCap;
|
||||
|
||||
AtomicInteger timer = new AtomicInteger(capSec);
|
||||
|
||||
scheduleFix(0, 1000, (task, isCancelled) -> {
|
||||
|
||||
if (!isCancelled) {
|
||||
task.cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
int tmpTimer = timer.getAndDecrement();
|
||||
|
||||
if (currentPlayer == null) {
|
||||
task.cancel();
|
||||
broadcast(Message.KOHT_LOOSE, player, listener.getFactionTag(player));
|
||||
return;
|
||||
}
|
||||
|
||||
if (Config.displayMessageKothCap.contains(tmpTimer))
|
||||
broadcast(Message.KOHT_TIMER, player, listener.getFactionTag(player));
|
||||
|
||||
if (tmpTimer == 0) {
|
||||
|
||||
task.cancel();
|
||||
broadcast(Message.KOHT_END, player, listener.getFactionTag(player));
|
||||
// Méthode pour win
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param message
|
||||
* @param player
|
||||
* @param currentFaction
|
||||
* @param cooldown
|
||||
*/
|
||||
private void broadcast(Message message, Player player, String currentFaction) {
|
||||
String msg = message.getMessage();
|
||||
|
||||
Location location = cuboid.getCenter();
|
||||
msg = msg.replace("%centerX%", String.valueOf(location.getBlockX()));
|
||||
msg = msg.replace("%centerY%", String.valueOf(location.getBlockY()));
|
||||
msg = msg.replace("%centerZ%", String.valueOf(location.getBlockZ()));
|
||||
msg = msg.replace("%pos1X%", String.valueOf(pos1.getBlockX()));
|
||||
msg = msg.replace("%pos1Y%", String.valueOf(pos1.getBlockY()));
|
||||
msg = msg.replace("%pos1Z%", String.valueOf(pos1.getBlockZ()));
|
||||
msg = msg.replace("%pos2X%", String.valueOf(pos2.getBlockX()));
|
||||
msg = msg.replace("%pos2Y%", String.valueOf(pos2.getBlockY()));
|
||||
msg = msg.replace("%pos2Z%", String.valueOf(pos2.getBlockZ()));
|
||||
msg = msg.replace("%player%", player == null ? "NULL" : player.getName());
|
||||
msg = msg.replace("%sec%", String.valueOf(cooldown));
|
||||
msg = msg.replace("%cooldown%", TimerBuilder.getStringTime(cooldown));
|
||||
msg = msg.replace("%currentFaction%", currentFaction == null ? "NULL" : currentFaction);
|
||||
msg = msg.replace("%currentPlayer%",
|
||||
this.currentPlayer == null ? Message.KOTH_NOONE.getMessage() : this.currentPlayer.getName());
|
||||
msg = msg.replace("%name%", String.valueOf(name));
|
||||
|
||||
Bukkit.broadcastMessage(Message.PREFIX.getMessage() + " " + msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Quand un joueur bouge
|
||||
*
|
||||
* @param player
|
||||
* @param factionListener
|
||||
*/
|
||||
public void playerMove(Player player, FactionListener factionListener) {
|
||||
|
||||
if (!isEnable)
|
||||
return;
|
||||
|
||||
if (cuboid == null)
|
||||
return;
|
||||
|
||||
// Si le mec est dans le koth
|
||||
if (currentPlayer == null && cuboid.contains(player.getLocation())) {
|
||||
|
||||
currentPlayer = player;
|
||||
startCap(player, factionListener);
|
||||
|
||||
// Le mec sort du koth
|
||||
} else if (currentPlayer == player && !cuboid.contains(player.getLocation())) {
|
||||
|
||||
currentPlayer = null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -84,18 +84,18 @@ public class KothManager extends ListenerAdapter implements Saveable {
|
||||
|
||||
/**
|
||||
* Permet de créer un koth
|
||||
*
|
||||
* @param sender
|
||||
* @param name
|
||||
* @param capSec
|
||||
*/
|
||||
public void create(CommandSender sender, String name) {
|
||||
public void create(CommandSender sender, String name, int capSec) {
|
||||
|
||||
if (exist(name)) {
|
||||
message(sender, Message.KOTH_ALREADY_EXIST, name);
|
||||
return;
|
||||
}
|
||||
|
||||
Koth koth = new Koth(name);
|
||||
Koth koth = new Koth(name, capSec);
|
||||
koths.add(koth);
|
||||
|
||||
message(sender, Message.KOTH_CREATE, name);
|
||||
@ -186,11 +186,11 @@ public class KothManager extends ListenerAdapter implements Saveable {
|
||||
TextComponent spawn = buildTextComponent(" §8(§2spawn§8)");
|
||||
setClickAction(spawn, Action.SUGGEST_COMMAND, "/koth spawn " + koth.getName());
|
||||
setHoverMessage(spawn, "§f» §7Click for spawn koth");
|
||||
|
||||
|
||||
TextComponent now = buildTextComponent(" §8(§2now§8)");
|
||||
setClickAction(now, Action.SUGGEST_COMMAND, "/koth now " + koth.getName());
|
||||
setHoverMessage(now, "§f» §7Click for spawn koth now");
|
||||
|
||||
|
||||
TextComponent delete = buildTextComponent(" §8(§cdelete§8)");
|
||||
setClickAction(delete, Action.SUGGEST_COMMAND, "/koth delete " + koth.getName());
|
||||
setHoverMessage(delete, "§f» §7Click for delete koth");
|
||||
@ -198,13 +198,48 @@ public class KothManager extends ListenerAdapter implements Saveable {
|
||||
component.addExtra(spawn);
|
||||
component.addExtra(now);
|
||||
component.addExtra(delete);
|
||||
|
||||
|
||||
player.spigot().sendMessage(component);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param sender
|
||||
* @param name
|
||||
*/
|
||||
public void stop(CommandSender sender, String name) {
|
||||
|
||||
if (!exist(name)) {
|
||||
message(sender, Message.KOTH_DOESNT_EXIST, name);
|
||||
return;
|
||||
}
|
||||
|
||||
Koth koth = get(name);
|
||||
|
||||
if (!koth.isEnable() && !koth.isCooldown()) {
|
||||
message(sender, Message.KOTH_NO_ENABLE, name);
|
||||
return;
|
||||
}
|
||||
|
||||
koth.stop();
|
||||
|
||||
}
|
||||
|
||||
public void spawn(CommandSender sender, String name, boolean now){
|
||||
|
||||
if (!exist(name)) {
|
||||
message(sender, Message.KOTH_DOESNT_EXIST, name);
|
||||
return;
|
||||
}
|
||||
|
||||
Koth koth = get(name);
|
||||
koth.spawn(sender, now);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de charger la class {@link FactionListener} en fonction du plugin
|
||||
* Faction
|
||||
@ -241,9 +276,19 @@ public class KothManager extends ListenerAdapter implements Saveable {
|
||||
ZPlugin.z().addListener(factionListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Event quand un joueur va bouger
|
||||
*/
|
||||
@Override
|
||||
protected void onMove(PlayerMoveEvent event, Player player) {
|
||||
|
||||
if (!hasKothEnable())
|
||||
return;
|
||||
|
||||
//On récup tout les koths actif
|
||||
Collection<Koth> collection = getKoths();
|
||||
collection.forEach(koth -> koth.playerMove(player, factionListener));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
31
src/fr/maxlego08/koth/event/KothSpawnEvent.java
Normal file
31
src/fr/maxlego08/koth/event/KothSpawnEvent.java
Normal file
@ -0,0 +1,31 @@
|
||||
package fr.maxlego08.koth.event;
|
||||
|
||||
import fr.maxlego08.koth.Koth;
|
||||
import fr.maxlego08.koth.zcore.utils.Cuboid;
|
||||
|
||||
public class KothSpawnEvent extends KothEvent {
|
||||
|
||||
private final Cuboid location;
|
||||
private final Koth koth;
|
||||
|
||||
public KothSpawnEvent(Cuboid location, Koth koth) {
|
||||
super();
|
||||
this.location = location;
|
||||
this.koth = koth;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the location
|
||||
*/
|
||||
public Cuboid getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the koth
|
||||
*/
|
||||
public Koth getKoth() {
|
||||
return koth;
|
||||
}
|
||||
|
||||
}
|
32
src/fr/maxlego08/koth/event/KothStartEvent.java
Normal file
32
src/fr/maxlego08/koth/event/KothStartEvent.java
Normal file
@ -0,0 +1,32 @@
|
||||
package fr.maxlego08.koth.event;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import fr.maxlego08.koth.Koth;
|
||||
|
||||
public class KothStartEvent extends KothEvent {
|
||||
|
||||
private final Player player;
|
||||
private final Koth koth;
|
||||
|
||||
public KothStartEvent(Player player, Koth koth) {
|
||||
super();
|
||||
this.player = player;
|
||||
this.koth = koth;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the player
|
||||
*/
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the koth
|
||||
*/
|
||||
public Koth getKoth() {
|
||||
return koth;
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
package fr.maxlego08.koth.save;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import fr.maxlego08.koth.zcore.utils.storage.Persist;
|
||||
import fr.maxlego08.koth.zcore.utils.storage.Saveable;
|
||||
|
||||
@ -10,6 +13,10 @@ public class Config implements Saveable {
|
||||
* static Singleton instance.
|
||||
*/
|
||||
private static volatile Config instance;
|
||||
public static int cooldownInSecond = 300;
|
||||
public static List<Integer> displayMessageCooldown = Arrays.asList(300, 120, 60, 30, 10, 5, 4, 3, 2, 1);
|
||||
public static List<Integer> displayMessageKothCap = Arrays.asList(300, 120, 60, 30, 10, 5, 4, 3, 2, 1);
|
||||
public static int defaultCap = 60;
|
||||
|
||||
/**
|
||||
* Private constructor for singleton.
|
||||
|
@ -27,19 +27,39 @@ public enum Message {
|
||||
COMMAND_NO_ARG("§cImpossible de trouver la commande avec ses arguments.", true),
|
||||
COMMAND_SYNTAXE_HELP("§a%s §b» §7%s", true),
|
||||
|
||||
KOTH_NOONE("Noboby"),
|
||||
KOTH_LIST("§eAvailable koths§8:"),
|
||||
KOTH_EMPTY("§cNo koth available."),
|
||||
KOTH_CREATE("§eYou have just created the koth §6%s§e. Now you must do §f/koth set pos1/pos2 <koth name>§e."),
|
||||
KOTH_DELETE("§eYou have just deleted the koth §6%s§e."),
|
||||
KOTH_ALREADY_EXIST("§cThe koth §6%s §calready exist."),
|
||||
KOTH_NO_ENABLE("§cThe koth §6%s §cis not enable."),
|
||||
KOTH_DOESNT_EXIST("§cThe koth §6%s §cdoesn't exist."),
|
||||
KOTH_SET_FIRST_POSITION("§eYou just put the first position for the koth §6%s§e."),
|
||||
KOTH_SET_SECOND_POSITION("§eYou just put the second position for the koth §6%s§e."),
|
||||
KOTH_SET_FIRST_POSITION_NULL("§cThe first location cannot be found, made §f/koth set pos1 %s§c."),
|
||||
KOTH_SET_SECOND_POSITION_NULL("§cThe second location cannot be found, made §f/koth set pos2 %s§c."),
|
||||
KOTH_SPAWN_COOLDOWN("§cthe countdown to the appearance of the koth is already underway"),
|
||||
KOTH_SPAWN_ALREADY("§cThe koth is running."),
|
||||
KOTH_SPAWN_MESSAGE_COOLDOWN("§eKoth §6%name% §ewill appear in §6%cooldown% §8(§7%centerX%, %centerY%, %centerZ%§8)"),
|
||||
KOTH_SPAWN_MESSAGE("§eKoth §6%name% §eappeared in §6%centerX%, %centerY%, %centerZ%§e."),
|
||||
KOHT_CATCH("§6%player% §ejust started capturing the koth §6%name%§e. §8(§7%centerX%, %centerY%, %centerZ%§8)"),
|
||||
KOHT_LOOSE("§6%player% §ejust loose koth §6%name%§e. §8(§7%centerX%, %centerY%, %centerZ%§8)"),
|
||||
KOHT_TIMER("§eAnother §6%cooldown% §ebefore §6%player% §ewins the koth §6%name%§e. §8(§7%centerX%, %centerY%, %centerZ%§8)"),
|
||||
KOHT_END("§eThe §6%currentFaction% §efaction has just won the koth §6%name%§e."),
|
||||
|
||||
KOTH_LOCATION_NULL("You did not put this location"),
|
||||
KOTH_FIRST("First location"),
|
||||
KOTH_SECOND("Second location"),
|
||||
|
||||
KOTH_SCHEDULER_CREATE("§eYou have just created a scheduler for the koth §6%s§e."),
|
||||
KOTH_SCHEDULER_EMPTY("§cNo scheduler available"),
|
||||
KOTH_SCHEDULER_REMOVE_HOVER("§7Click to delete the scheduler"),
|
||||
KOTH_SCHEDULER_LIST("§6%totemName%§7, §f%type%§7, §e%day%§7, §e%hour%§7, §e%minute%"),
|
||||
KOTH_SCHEDULER_LIST_REPEAT("§6%totemName%§7, §f%type%§7, §e%timer%"),
|
||||
KOTH_SCHEDULER_REMOVE_ERROR("§cYou must make /koth scheduler list and then click on one of the koths to be able to delete it"),
|
||||
KOTH_SCHEDULER_REMOVE_SUCCESS("§eYou have just deleted the scheduler"),
|
||||
|
||||
DESCRIPTION_SPAWN("Spawn a koth"),
|
||||
DESCRIPTION_CREATE("Create a koth"),
|
||||
DESCRIPTION_DELETE("Delete a koth"),
|
||||
|
@ -671,7 +671,8 @@ public abstract class ZUtils {
|
||||
* @param message
|
||||
*/
|
||||
protected void message(CommandSender player, Message message) {
|
||||
player.sendMessage(Message.PREFIX.msg() + " " + message.msg());
|
||||
if (player != null)
|
||||
player.sendMessage(Message.PREFIX.msg() + " " + message.msg());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -680,7 +681,8 @@ public abstract class ZUtils {
|
||||
* @param message
|
||||
*/
|
||||
protected void message(CommandSender player, String message) {
|
||||
player.sendMessage(Message.PREFIX.msg() + " " + message);
|
||||
if (player != null)
|
||||
player.sendMessage(Message.PREFIX.msg() + " " + message);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -689,7 +691,8 @@ public abstract class ZUtils {
|
||||
* @param message
|
||||
*/
|
||||
protected void messageWO(CommandSender player, Message message) {
|
||||
player.sendMessage(message.msg());
|
||||
if (player != null)
|
||||
player.sendMessage(message.msg());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -699,7 +702,8 @@ public abstract class ZUtils {
|
||||
* @param args
|
||||
*/
|
||||
protected void messageWO(CommandSender player, Message message, Object... args) {
|
||||
player.sendMessage(String.format(message.msg(), args));
|
||||
if (player != null)
|
||||
player.sendMessage(String.format(message.msg(), args));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -780,6 +784,24 @@ public abstract class ZUtils {
|
||||
}
|
||||
}, delay, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param delay
|
||||
* @param runnable
|
||||
*/
|
||||
protected void scheduleFix(long start, long delay, BiConsumer<TimerTask, Boolean> runnable) {
|
||||
new Timer().scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!ZPlugin.z().isEnabled()) {
|
||||
cancel();
|
||||
runnable.accept(this, false);
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTask(ZPlugin.z(), () -> runnable.accept(this, true));
|
||||
}
|
||||
}, start, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user