🚧 Start KOTH type

This commit is contained in:
Maxlego08 2022-03-03 15:08:21 +01:00
parent 691b01b2a6
commit 5fde905e24
11 changed files with 319 additions and 82 deletions

View File

@ -1,7 +1,7 @@
name: zKoth
author: Maxlego08
main: fr.maxlego08.zkoth.ZKothPlugin
version: 2.1.0.0
version: 2.1.0.1
softdepend: [PlaceholderAPI, Guilds, Factions, FactionsX, SuperiorSkyblock2, LegacyFactions, FeatherBoard, TAB, TitleManager, UltimateFactions]
commands:
api-version: 1.13

View File

@ -2,7 +2,9 @@ package fr.maxlego08.zkoth;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@ -21,6 +23,7 @@ import org.bukkit.util.Vector;
import fr.maxlego08.zkoth.api.FactionListener;
import fr.maxlego08.zkoth.api.Koth;
import fr.maxlego08.zkoth.api.enums.KothType;
import fr.maxlego08.zkoth.api.enums.LootType;
import fr.maxlego08.zkoth.api.event.events.KothCapEvent;
import fr.maxlego08.zkoth.api.event.events.KothCatchEvent;
@ -40,6 +43,7 @@ import fr.maxlego08.zkoth.zcore.utils.interfaces.CollectionConsumer;
public class ZKoth extends ZUtils implements Koth {
private final String name;
private KothType kothType;
private int captureSeconds;
private Location minLocation;
private Location maxLocation;
@ -47,6 +51,9 @@ public class ZKoth extends ZUtils implements Koth {
private List<String> commands = new ArrayList<String>();
private List<String> itemStacks = new ArrayList<String>();
private int maxSecondsCap;
private int maxPoints;
private transient boolean isEnable = false;
private transient boolean isCooldown = false;
private transient TimerTask timerTask;
@ -54,6 +61,8 @@ public class ZKoth extends ZUtils implements Koth {
private transient FactionListener factionListener;
private transient AtomicInteger currentCaptureSeconds;
private transient Map<Player, Long> playersValues = new HashMap<Player, Long>();
/**
* @param name
* @param captureSeconds
@ -67,6 +76,14 @@ public class ZKoth extends ZUtils implements Koth {
this.minLocation = minLocation;
this.maxLocation = maxLocation;
this.type = LootType.NONE;
this.kothType = KothType.CLASSIC;
this.maxPoints = 60;
this.maxSecondsCap = 60;
}
@Override
public void setType(KothType type) {
this.kothType = type;
}
@Override
@ -406,8 +423,9 @@ public class ZKoth extends ZUtils implements Koth {
if (this.currentPlayer != null) {
if (!this.currentPlayer.isValid() || !this.currentPlayer.isOnline()
|| !cuboid.contains(this.currentPlayer.getLocation()))
|| !cuboid.contains(this.currentPlayer.getLocation())) {
this.currentPlayer = null;
}
}
if (this.currentPlayer == null) {
@ -419,8 +437,9 @@ public class ZKoth extends ZUtils implements Koth {
return;
}
if (this.timerTask != null)
if (this.timerTask != null) {
this.timerTask.cancel();
}
this.timerTask = null;
this.currentPlayer = null;
@ -431,79 +450,15 @@ public class ZKoth extends ZUtils implements Koth {
return;
}
if (Config.displayMessageKothCap.contains(tmpCapture)) {
broadcast(Message.ZKOHT_EVENT_TIMER);
}
if (tmpCapture <= 0) {
KothWinEvent kothWinEvent = new KothWinEvent(this, this.currentPlayer);
kothWinEvent.callEvent();
this.endKoth(task, cuboid, player);
if (kothWinEvent.isCancelled()) {
return;
}
task.cancel();
broadcast(Message.ZKOTH_EVENT_WIN);
/* Gestion des loots */
this.commands.forEach(command -> {
command = replaceMessage(command);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), papi(command, player));
});
Location center = cuboid.getCenter();
World world = center.getWorld();
while (center.getBlock().getRelative(BlockFace.DOWN).getType().equals(Material.AIR)) {
center = center.getBlock().getRelative(BlockFace.DOWN).getLocation();
}
Location location = center;
if (this.itemStacks.size() != 0)
switch (this.type) {
case CHEST:
location.getBlock().setType(Material.CHEST);
Chest chest = (Chest) location.getBlock().getState();
this.getItemStacks().forEach(itemStack -> chest.getInventory().addItem(itemStack));
Bukkit.getScheduler().runTaskLater(ZPlugin.z(), () -> {
location.getBlock().setType(Material.AIR);
}, 20 * Config.removeChestSec);
break;
case DROP:
location.add(0.5, 0.3, 0.5);
this.getItemStacks().forEach(itemStack -> {
Item item = world.dropItem(location, itemStack);
Vector vector = item.getVelocity();
vector.setZ(0);
vector.setY(0.5);
vector.setX(0);
item.setVelocity(vector);
});
break;
case INVENTORY:
this.getItemStacks().forEach(itemStack -> give(this.currentPlayer, itemStack));
break;
case NONE:
break;
default:
break;
}
/* FIN Gestion des loots */
this.isEnable = false;
this.isCooldown = false;
this.currentPlayer = null;
this.timerTask = null;
this.currentCaptureSeconds = null;
} else {
KothCapEvent capEvent = new KothCapEvent(this, player, this.currentCaptureSeconds.get(),
@ -515,6 +470,93 @@ public class ZKoth extends ZUtils implements Koth {
});
}
@Override
public boolean hasWin() {
switch (this.getType()) {
case CLASSIC:
return this.currentCaptureSeconds != null && this.currentCaptureSeconds.get() <= 0;
case POINT:
return this.currentPlayer != null ? false : this.getValue(this.currentPlayer) >= this.maxPoints;
case TIMER:
return this.currentPlayer != null ? false : this.getValue(this.currentPlayer) >= this.maxSecondsCap;
default:
return false;
}
}
@Override
public void endKoth(TimerTask task, Cuboid cuboid, Player player) {
KothWinEvent kothWinEvent = new KothWinEvent(this, this.currentPlayer);
kothWinEvent.callEvent();
if (kothWinEvent.isCancelled()) {
return;
}
task.cancel();
broadcast(Message.ZKOTH_EVENT_WIN);
/* Gestion des loots */
this.commands.forEach(command -> {
command = replaceMessage(command);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), papi(command, player));
});
Location center = cuboid.getCenter();
World world = center.getWorld();
while (center.getBlock().getRelative(BlockFace.DOWN).getType().equals(Material.AIR)) {
center = center.getBlock().getRelative(BlockFace.DOWN).getLocation();
}
Location location = center;
if (this.itemStacks.size() != 0)
switch (this.type) {
case CHEST:
location.getBlock().setType(Material.CHEST);
Chest chest = (Chest) location.getBlock().getState();
this.getItemStacks().forEach(itemStack -> chest.getInventory().addItem(itemStack));
Bukkit.getScheduler().runTaskLater(ZPlugin.z(), () -> {
location.getBlock().setType(Material.AIR);
}, 20 * Config.removeChestSec);
break;
case DROP:
location.add(0.5, 0.3, 0.5);
this.getItemStacks().forEach(itemStack -> {
Item item = world.dropItem(location, itemStack);
Vector vector = item.getVelocity();
vector.setZ(0);
vector.setY(0.5);
vector.setX(0);
item.setVelocity(vector);
});
break;
case INVENTORY:
this.getItemStacks().forEach(itemStack -> give(this.currentPlayer, itemStack));
break;
case NONE:
break;
default:
break;
}
/* FIN Gestion des loots */
this.isEnable = false;
this.isCooldown = false;
this.currentPlayer = null;
this.timerTask = null;
this.currentCaptureSeconds = null;
this.playersValues.clear();
}
@Override
public CollectionConsumer<Player> onScoreboard() {
if (this.isCooldown) {
@ -563,14 +605,14 @@ public class ZKoth extends ZUtils implements Koth {
if (this.timerTask != null) {
this.timerTask.cancel();
}
this.timerTask = null;
this.isEnable = false;
this.isCooldown = false;
this.currentPlayer = null;
this.timerTask = null;
this.currentCaptureSeconds = null;
this.playersValues.clear();
}
@Override
@ -594,4 +636,44 @@ public class ZKoth extends ZUtils implements Koth {
: this.factionListener.getFactionTag(this.currentPlayer);
}
@Override
public KothType getType() {
return this.kothType == null ? this.kothType = KothType.CLASSIC : this.kothType;
}
@Override
public int getMaxSecondsCap() {
return this.maxSecondsCap;
}
@Override
public void setMaxSecondsCap(int seconds) {
this.maxSecondsCap = seconds;
}
@Override
public int getMaxPoints() {
return this.maxPoints;
}
@Override
public void setMaxPoints(int points) {
this.maxPoints = points;
}
@Override
public Map<Player, Long> getValues() {
return this.playersValues;
}
@Override
public long getValue(Player player) {
return this.playersValues.getOrDefault(player, 0l);
}
@Override
public void onPlayerLeave(Player player) {
this.playersValues.remove(player);
}
}

View File

@ -28,6 +28,7 @@ import fr.maxlego08.zkoth.api.FactionListener;
import fr.maxlego08.zkoth.api.Koth;
import fr.maxlego08.zkoth.api.KothManager;
import fr.maxlego08.zkoth.api.Selection;
import fr.maxlego08.zkoth.api.enums.KothType;
import fr.maxlego08.zkoth.api.enums.LootType;
import fr.maxlego08.zkoth.api.event.events.KothCreateEvent;
import fr.maxlego08.zkoth.api.event.events.KothHookEvent;
@ -266,6 +267,9 @@ public class ZKothManager extends ListenerAdapter implements KothManager {
if (Config.enableScoreboard) {
this.manager.delete(player);
}
List<Koth> koths = this.getActiveKoths();
koths.forEach(koth -> koth.onPlayerLeave(player));
}
@Override
@ -444,6 +448,7 @@ public class ZKothManager extends ListenerAdapter implements KothManager {
message(sender, "§fName: §b%s", koth.getName());
message(sender, "§fCoordinate: §b%s", location);
message(sender, "§fType: §b%s", koth.getType().name());
message(sender, "§fLoot type: §b%s", koth.getLootType().name());
message(sender, "§fCommands §8(§7" + koth.getCommands().size() + "§8):");
if (sender instanceof ConsoleCommandSender) {
@ -515,7 +520,23 @@ public class ZKothManager extends ListenerAdapter implements KothManager {
Koth koth = optional.get();
koth.setLootType(type);
message(sender, Message.ZKOTH_LOOT_EDIT, "%type%", name(type.name()));
message(sender, Message.ZKOTH_LOOT_EDIT, "%type%", name(type.name()), "%name%", koth.getName());
this.save(this.plugin.getPersist());
}
@Override
public void setKothType(CommandSender sender, String name, KothType kothType) {
Optional<Koth> optional = getKoth(name);
if (!optional.isPresent()) {
message(sender, Message.ZKOTH_DOESNT_EXIST, "%name%", name);
return;
}
Koth koth = optional.get();
koth.setType(kothType);
message(sender, Message.ZKOTH_TYPE_EDIT, "%type%", name(kothType.name()), "%name%", koth.getName());
this.save(this.plugin.getPersist());
}

View File

@ -1,12 +1,15 @@
package fr.maxlego08.zkoth.api;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import fr.maxlego08.zkoth.api.enums.KothType;
import fr.maxlego08.zkoth.api.enums.LootType;
import fr.maxlego08.zkoth.zcore.utils.Cuboid;
import fr.maxlego08.zkoth.zcore.utils.interfaces.CollectionConsumer;
@ -101,7 +104,7 @@ public interface Koth {
* @param now
*/
public void spawn(CommandSender sender, boolean now);
/**
*
* @param now
@ -150,22 +153,88 @@ public interface Koth {
* @param second
*/
public void setCapture(int second);
/**
*
* @return
*/
public int getCurrentSecond();
/**
*
* @return
*/
public String getCurrentPlayer();
/**
*
* @return
*/
public String getCurrentFaction();
/**
*
* @return
*/
public KothType getType();
/**
*
* @return
*/
public int getMaxSecondsCap();
/**
*
* @param seconds
*/
public void setMaxSecondsCap(int seconds);
/**
*
* @return
*/
public int getMaxPoints();
/**
*
* @param points
*/
public void setMaxPoints(int points);
/**
*
* @param task
* @param cuboid
* @param player
*/
public void endKoth(TimerTask task, Cuboid cuboid, Player player);
/**
*
* @return
*/
public Map<Player, Long> getValues();
/**
*
* @param player
* @return
*/
public long getValue(Player player);
/**
*
* @param player
*/
public void onPlayerLeave(Player player);
public void setType(KothType type);
/**
*
* @return
*/
public boolean hasWin();
}

View File

@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import fr.maxlego08.zkoth.api.enums.KothType;
import fr.maxlego08.zkoth.api.enums.LootType;
import fr.maxlego08.zkoth.zcore.utils.storage.Saveable;
@ -153,4 +154,12 @@ public interface KothManager extends Saveable {
*/
public List<String> getKothNames();
/**
*
* @param sender
* @param name
* @param kothType
*/
public void setKothType(CommandSender sender, String name, KothType kothType);
}

View File

@ -0,0 +1,9 @@
package fr.maxlego08.zkoth.api.enums;
public enum KothType {
CLASSIC,
POINT,
TIMER,
}

View File

@ -25,6 +25,7 @@ public class CommandKoth extends VCommand {
this.addSubCommand(new CommandKothAdd(plugin));
this.addSubCommand(new CommandKothRemove(plugin));
this.addSubCommand(new CommandKothSetType(plugin));
this.addSubCommand(new CommandKothSetLootType(plugin));
this.addSubCommand(new CommandKothLoot(plugin));
this.addSubCommand(new CommandKothSetCapture(plugin));
this.addSubCommand(new CommandKothAxe(plugin));

View File

@ -0,0 +1,43 @@
package fr.maxlego08.zkoth.command.commands;
import java.util.Arrays;
import java.util.stream.Collectors;
import fr.maxlego08.zkoth.ZKothPlugin;
import fr.maxlego08.zkoth.api.enums.LootType;
import fr.maxlego08.zkoth.command.VCommand;
import fr.maxlego08.zkoth.zcore.enums.Message;
import fr.maxlego08.zkoth.zcore.enums.Permission;
import fr.maxlego08.zkoth.zcore.utils.commands.CommandType;
public class CommandKothSetLootType extends VCommand {
public CommandKothSetLootType(ZKothPlugin plugin) {
super(plugin);
this.addCompletion(0, (a, b) -> plugin.getKothManager().getKothNames());
this.addCompletion(1, (a, b) -> Arrays.asList(LootType.values()).stream().map(e -> e.name().toLowerCase())
.collect(Collectors.toList()));
this.setPermission(Permission.ZKOTH_LOOT_TYPE);
this.addSubCommand("setloottype", "setltype");
this.setDescription(Message.DESCRIPTION_LOOTTYPE);
this.addRequireArg("type");
this.setTabCompletor();
}
@Override
protected CommandType perform(ZKothPlugin plugin) {
String name = argAsString(0);
String type = argAsString(1);
try {
LootType lootType = LootType.valueOf(type.toUpperCase());
this.manager.setKothLoot(sender, name, lootType);
} catch (Exception e) {
return CommandType.SYNTAX_ERROR;
}
return CommandType.SUCCESS;
}
}

View File

@ -4,7 +4,7 @@ import java.util.Arrays;
import java.util.stream.Collectors;
import fr.maxlego08.zkoth.ZKothPlugin;
import fr.maxlego08.zkoth.api.enums.LootType;
import fr.maxlego08.zkoth.api.enums.KothType;
import fr.maxlego08.zkoth.command.VCommand;
import fr.maxlego08.zkoth.zcore.enums.Message;
import fr.maxlego08.zkoth.zcore.enums.Permission;
@ -15,7 +15,7 @@ public class CommandKothSetType extends VCommand {
public CommandKothSetType(ZKothPlugin plugin) {
super(plugin);
this.addCompletion(0, (a, b) -> plugin.getKothManager().getKothNames());
this.addCompletion(1, (a, b) -> Arrays.asList(LootType.values()).stream().map(e -> e.name().toLowerCase())
this.addCompletion(1, (a, b) -> Arrays.asList(KothType.values()).stream().map(e -> e.name().toLowerCase())
.collect(Collectors.toList()));
this.setPermission(Permission.ZKOTH_TYPE);
this.addSubCommand("settype");
@ -31,8 +31,8 @@ public class CommandKothSetType extends VCommand {
String type = argAsString(1);
try {
LootType lootType = LootType.valueOf(type.toUpperCase());
manager.setKothLoot(sender, name, lootType);
KothType kothType = KothType.valueOf(type.toUpperCase());
this.manager.setKothType(this.sender, name, kothType);
} catch (Exception e) {
return CommandType.SYNTAX_ERROR;
}

View File

@ -97,7 +97,8 @@ public enum Message {
ZKOTH_COMMAND_CREATE("§7You have just added the command §8\"§f%command%§8\""),
ZKOTH_COMMAND_DELETE("§aYou have just deleted a command."),
ZKOTH_LOOT_EDIT("§aYou have just set the type to §f%type%§a."),
ZKOTH_LOOT_EDIT("§aYou have just set the loot type to §f%type%§a."),
ZKOTH_TYPE_EDIT("§aYou have just set the type to §f%type%§a."),
ZKOTH_LOOT_INVENTORY("§7Loots §b%name%"),
ZKOTH_LOOT_CHANGE("§aYou have just modified the loots of the koth §2%name%§a."),
ZKOTH_CAPUTRE_EDIT("§aYou have just modified the capture time of the koth §n%name%§a to §f%seconds%§a."),
@ -129,7 +130,8 @@ public enum Message {
DESCRIPTION_STOP("Stop a koth"),
DESCRIPTION_SPAWN("Spawn a koth with cooldown"),
DESCRIPTION_CAPTURE("Set capture time for a koth"),
DESCRIPTION_TYPE("Set loot type for a koth"),
DESCRIPTION_TYPE("Set koth type (classic, point, timer)"),
DESCRIPTION_LOOTTYPE("Set loot type for a koth"),
;

View File

@ -17,6 +17,7 @@ public enum Permission {
ZKOTH_COMMAND_REMOVE,
ZKOTH_LOOT,
ZKOTH_TYPE,
ZKOTH_LOOT_TYPE,
ZKOTH_SETCAPTURE,
ZKOTH_SCHEDULER,