From 43f4d3bb664deab5e7624a910240fbbcfe7d7cca Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 9 May 2021 12:41:05 +0200 Subject: [PATCH] Add WinconditionRelativeRedstonePercent --- .../winconditions/Winconditions.java | 1 + .../de/steamwar/fightsystem/FightSystem.java | 3 + .../WinconditionRelativeRedstonePercent.java | 145 ++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java diff --git a/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java b/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java index 0394ee7..4399515 100644 --- a/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java +++ b/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java @@ -26,6 +26,7 @@ public enum Winconditions { ALL_DEAD, CAPTAIN_DEAD, PERCENT_SYSTEM, + RELATIVE_REDSTONE_PERCENT, RELATIVE_PERCENT, POINTS, TIME_TECH_KO, diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java index 16a8072..dffc3f6 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java @@ -98,6 +98,9 @@ public class FightSystem extends JavaPlugin { new WinconditionWaterTechKO(); new WinconditionPercentSystem(); new WinconditionRelativePercent(); + if (Core.getVersion() >= 15) { + new WinconditionRelativeRedstonePercent(); + } new WinconditionPoints(); new WinconditionTimeout(); new WinconditionHeartRatioTimeout(); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java new file mode 100644 index 0000000..e763385 --- /dev/null +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java @@ -0,0 +1,145 @@ +package de.steamwar.fightsystem.winconditions; + +import de.steamwar.fightsystem.Config; +import de.steamwar.fightsystem.FightSystem; +import de.steamwar.fightsystem.fight.Fight; +import de.steamwar.fightsystem.fight.FightTeam; +import de.steamwar.fightsystem.states.FightState; +import de.steamwar.fightsystem.states.StateDependent; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.block.data.AnaloguePowerable; +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.Powerable; +import org.bukkit.block.data.Rail; +import org.bukkit.block.data.type.TechnicalPiston; +import org.bukkit.scheduler.BukkitTask; + +import java.util.*; + +public class WinconditionRelativeRedstonePercent extends Wincondition implements PrintableWincondition, PercentWincondition { + + private static final World world = Bukkit.getWorlds().get(0); + private static final Set ignoredBlocks; + + static { + Set ignored = new HashSet<>(); + for (String s : Config.IgnoredBlocks) + ignored.add(Material.valueOf(s)); + ignoredBlocks = Collections.unmodifiableSet(ignored); + } + + private final Map teamMap = new HashMap<>(); + + public WinconditionRelativeRedstonePercent() { + super("RelativeRedstonePercent"); + teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam())); + teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam())); + + if (Config.ActiveWinconditions.contains(Winconditions.RELATIVE_REDSTONE_PERCENT)) { + printableWinconditions.add(this); + percentWincondition = this; + } + } + + @Override + public double getPercent(FightTeam team) { + return teamMap.get(team).getPercent(); + } + + @Override + public String getDisplay(FightTeam team) { + return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%"; + } + + public static class TeamPercent extends StateDependent { + private final FightTeam team; + + private int blockCount; + private BukkitTask task; + private int currentBlocks; + + public TeamPercent(FightTeam team) { + super(Winconditions.RELATIVE_PERCENT, FightState.Running); + this.team = team; + this.currentBlocks = 1; + register(); + } + + @Override + public void enable() { + blockCount = currentBlocks(); + task = Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), this::check, 400, 400); + } + + @Override + public void disable() { + task.cancel(); + } + + private void check() { + currentBlocks(); + + if (!Config.ActiveWinconditions.contains(Winconditions.RELATIVE_REDSTONE_PERCENT)) + return; + + if (getPercent() >= Config.PercentWin) { + Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + team.getColoredName() + " §chat zu viel Redstone verloren!"); + FightSystem.setSpectateState(Fight.getOpposite(team), "RelativePercent"); + } + } + + public double getPercent() { + if (currentBlocks > blockCount) + return 0; + return (blockCount - currentBlocks) * 100 / (double) blockCount; + } + + public int getBlockCount() { + return blockCount; + } + + private int currentBlocks() { + // Entern active + if (!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft()) + return currentBlocks; + + currentBlocks = 0; + team.getSchemRegion().forEach((x, y, z) -> { + Block block = world.getBlockAt(x, y, z); + Material material = block.getType(); + if (ignoredBlocks.contains(material)) { + return; + } + BlockData blockData = block.getBlockData(); + if (blockData instanceof Powerable + || blockData instanceof TechnicalPiston + || blockData instanceof AnaloguePowerable + || blockData instanceof Rail) { + currentBlocks++; + return; + } + switch (material) { + case NOTE_BLOCK: + case REDSTONE_BLOCK: + case REDSTONE: + case REPEATER: + case COMPARATOR: + case OBSERVER: + case HOPPER: + case REDSTONE_LAMP: + case REDSTONE_TORCH: + case REDSTONE_WALL_TORCH: + case REDSTONE_WIRE: + currentBlocks++; + break; + default: + break; + } + }); + return currentBlocks; + } + } +}