From d956ee7353ade48ed0ca739c157f906ae88c71d3 Mon Sep 17 00:00:00 2001 From: Yaruma3341 Date: Sun, 17 Feb 2019 22:39:57 +0100 Subject: [PATCH] Registered Listeners for WinConditions (only if enabled); Added PercentSystem (Prozentsystem) as WinCondition; Added PercentSystem to config.yml; Signed-off-by: Yaruma3341 --- config.yml | 2 + src/me/yaruma/fightsystem/FightSystem.java | 32 ++++++++++++ .../yaruma/fightsystem/utils/WorldEdit.java | 13 +++++ .../WinconditionPercentSystem.java | 50 +++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 src/me/yaruma/fightsystem/winconditions/WinconditionPercentSystem.java diff --git a/config.yml b/config.yml index 6174d9c..8654c37 100644 --- a/config.yml +++ b/config.yml @@ -36,10 +36,12 @@ WinConditions: Timeout: boolean AllDead: boolean CaptainDead: boolean + PercentSystem: boolean WinConditionParams: TimeoutTime: 0 EnterPhaseBegin: 0 + PercentWin: 0 Fight: Entern: boolean diff --git a/src/me/yaruma/fightsystem/FightSystem.java b/src/me/yaruma/fightsystem/FightSystem.java index 3c03a2a..302fe5d 100644 --- a/src/me/yaruma/fightsystem/FightSystem.java +++ b/src/me/yaruma/fightsystem/FightSystem.java @@ -8,6 +8,9 @@ import me.yaruma.fightsystem.listener.*; import me.yaruma.fightsystem.manager.FileManager; import me.yaruma.fightsystem.utils.countdown.Countdown; import me.yaruma.fightsystem.utils.countdown.FinishSetupOver; +import me.yaruma.fightsystem.winconditions.WinconditionAllDead; +import me.yaruma.fightsystem.winconditions.WinconditionCaptainDead; +import me.yaruma.fightsystem.winconditions.WinconditionPercentSystem; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.plugin.PluginManager; @@ -51,6 +54,13 @@ public class FightSystem extends JavaPlugin { public int team2cornerY; public int team2cornerZ; + + public int team1SchematicBlocks; + public int team2SchematicBlocks; + + public int team1DestroyedBlocks; + public int team2DestroyedBlocks; + public void onEnable() { plugin = this; @@ -155,6 +165,12 @@ public class FightSystem extends JavaPlugin { pm.registerEvents(new BlockBreakListener(), plugin); pm.registerEvents(new PlayerMoveListener(), plugin); + //WinConditions + if(fileManager.getBooleanFromConfig("WinConditions.AllDead")) pm.registerEvents(new WinconditionAllDead(), plugin); + if(fileManager.getBooleanFromConfig("WinConditions.CaptainDead")) pm.registerEvents(new WinconditionCaptainDead(), plugin); + if(fileManager.getBooleanFromConfig("WinConditions.AllDead")) pm.registerEvents(new WinconditionAllDead(), plugin); + if(fileManager.getBooleanFromConfig("WinConditions.PercentWin")) pm.registerEvents(new WinconditionPercentSystem(), plugin); + getCommand("ak").setExecutor(new AkCommand()); } @@ -315,4 +331,20 @@ public class FightSystem extends JavaPlugin { public int getTeam2cornerZ() { return team2cornerZ; } + + public int getTeam1SchematicBlocks() { + return team1SchematicBlocks; + } + + public int getTeam2SchematicBlocks() { + return team2SchematicBlocks; + } + + public int getTeam1DestroyedBlocks() { + return team1DestroyedBlocks; + } + + public int getTeam2DestroyedBlocks() { + return team2DestroyedBlocks; + } } diff --git a/src/me/yaruma/fightsystem/utils/WorldEdit.java b/src/me/yaruma/fightsystem/utils/WorldEdit.java index 04a9333..097ef04 100644 --- a/src/me/yaruma/fightsystem/utils/WorldEdit.java +++ b/src/me/yaruma/fightsystem/utils/WorldEdit.java @@ -3,6 +3,7 @@ package me.yaruma.fightsystem.utils; import com.boydti.fawe.FaweAPI; import com.boydti.fawe.util.EditSessionBuilder; import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.IncompleteRegionException; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.bukkit.BukkitWorld; @@ -46,5 +47,17 @@ public class WorldEdit { } catch (IOException e) { } } + public static int countBlocks(Player player, int minX, int minY, int minZ, int maxX, int maxY, int maxZ, BaseBlock baseBlock) { + World weWorld = new BukkitWorld(player.getWorld()); + EditSession editSession = new EditSessionBuilder(weWorld).fastmode(true).build(); + CuboidSelection cuboidSelection = new CuboidSelection(player.getWorld(), new Location(player.getWorld(), minX, minY, minZ), new Location(player.getWorld(), maxX, maxY, maxZ)); + Set target = new HashSet<>(); + target.add(baseBlock); + try { + return editSession.countBlocks(cuboidSelection.getRegionSelector().getRegion(), target); + } catch (IncompleteRegionException e) { } + return 0; + } + } diff --git a/src/me/yaruma/fightsystem/winconditions/WinconditionPercentSystem.java b/src/me/yaruma/fightsystem/winconditions/WinconditionPercentSystem.java new file mode 100644 index 0000000..6869e63 --- /dev/null +++ b/src/me/yaruma/fightsystem/winconditions/WinconditionPercentSystem.java @@ -0,0 +1,50 @@ +package me.yaruma.fightsystem.winconditions; + +import me.yaruma.fightsystem.FightSystem; +import me.yaruma.fightsystem.fight.Fight; +import me.yaruma.fightsystem.manager.FileManager; +import me.yaruma.fightsystem.utils.Region; +import me.yaruma.fightsystem.utils.countdown.Countdown; +import me.yaruma.fightsystem.utils.countdown.FinishSpectateOver; +import org.bukkit.entity.Entity; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityExplodeEvent; + +public class WinconditionPercentSystem implements Listener { + + FightSystem instance = FightSystem.getPlugin(); + FileManager fileManager = instance.getFileManager(); + + @EventHandler + public void handleEntityExplode(EntityExplodeEvent event) { + if(!Methods.isEnabled("WinConditions.PercentWin")) return; + Entity entity = event.getEntity(); + + //Team 1 / Rot + if(Region.isInRegion(entity.getLocation(), instance.getTeam1cornerX(), instance.getTeam1cornerY(), instance.getTeam1cornerZ(), instance.getTeam1cornerX() + instance.getSchemsizeX(), instance.getTeam1cornerY() + instance.getSchemsizeY(), instance.getTeam1cornerZ() + instance.getSchemsizeZ(), fileManager.getIntegerFromConfig("Arena.BorderFromSchematic"))) { + instance.team1DestroyedBlocks = instance.getTeam1DestroyedBlocks() + event.blockList().size(); + double destroyPercent = instance.getTeam1DestroyedBlocks() * 100 / instance.getTeam1SchematicBlocks(); + if(destroyPercent >= fileManager.getIntegerFromConfig("WinConditionParams.PercentWin")) { + instance.setSpectateState(Fight.blueTeam); + Countdown countdown = new Countdown(fileManager.getIntegerFromConfig("Times.SpectatorDuration"), new FinishSpectateOver()); + countdown.startTimer(instance); + } + return; + } + + //Team 2 / Blau + if(Region.isInRegion(entity.getLocation(), instance.getTeam2cornerX(), instance.getTeam1cornerY(), instance.getTeam2cornerZ(), instance.getTeam2cornerX() + instance.getSchemsizeX(), instance.getTeam2cornerY() + instance.getSchemsizeY(), instance.getTeam2cornerZ() + instance.getSchemsizeZ(), fileManager.getIntegerFromConfig("Arena.BorderFromSchematic"))) { + instance.team2DestroyedBlocks = instance.getTeam2DestroyedBlocks() + event.blockList().size(); + double destroyPercent = instance.getTeam2DestroyedBlocks() * 100 / instance.getTeam2SchematicBlocks(); + if(destroyPercent >= fileManager.getIntegerFromConfig("WinConditionParams.PercentWin")) { + instance.setSpectateState(Fight.redTeam); + Countdown countdown = new Countdown(fileManager.getIntegerFromConfig("Times.SpectatorDuration"), new FinishSpectateOver()); + countdown.startTimer(instance); + } + return; + } + } + + +}