From f014c83e99018b5f387ba7b222a0de4ab28305bc Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 27 Oct 2021 18:14:43 +0200 Subject: [PATCH 1/8] Implement reset after win! Add FightWorld --- src/de/steamwar/misslewars/FightWorld.java | 102 ++++++++++++++++++ src/de/steamwar/misslewars/MissileWars.java | 9 ++ .../misslewars/countdowns/EndCountdown.java | 12 +-- .../misslewars/listener/JoinListener.java | 14 +++ 4 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 src/de/steamwar/misslewars/FightWorld.java diff --git a/src/de/steamwar/misslewars/FightWorld.java b/src/de/steamwar/misslewars/FightWorld.java new file mode 100644 index 0000000..87b0db7 --- /dev/null +++ b/src/de/steamwar/misslewars/FightWorld.java @@ -0,0 +1,102 @@ +/* + * + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * / + */ + +package de.steamwar.misslewars; + +import de.steamwar.core.events.ChunkListener; +import net.minecraft.server.v1_15_R1.Chunk; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.WorldCreator; +import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; + +import java.util.function.ObjIntConsumer; + +public class FightWorld { + + private static final World world = Bukkit.getWorlds().get(0); + private static final boolean paper = Bukkit.getVersion().contains("git-Paper"); + + public static boolean isPaper(){ + return paper; + } + + public static double posToChunk(int pos){ + return pos / 16.0; + } + + private static int getMinChunkX(){ + return (int) Math.floor(posToChunk(Config.ArenaMinX)); + } + + private static int getMaxChunkX(){ + return (int) Math.ceil(posToChunk(Config.ArenaMaxX)); + } + + private static int getMinChunkZ(){ + return (int) Math.floor(posToChunk(Config.ArenaMinZ)); + } + + private static int getMaxChunkZ(){ + return (int) Math.ceil(posToChunk(Config.ArenaMaxZ)); + } + + public static void forEachChunk(ObjIntConsumer executor) { + for(int x = getMinChunkX(); x <= getMaxChunkX(); x++) + for(int z = getMinChunkZ(); z <= getMaxChunkZ(); z++) + executor.accept(x, z); + } + + public static void resetWorld(){ + for(Entity entity : world.getEntities()){ + if(entity.getType() != EntityType.PLAYER){ + entity.remove(); + } + } + + World backup = new WorldCreator(world.getName() + "/backup").createWorld(); + assert backup != null; + forEachChunk((x, z) -> resetChunk(world, backup, x, z)); + Bukkit.unloadWorld(backup, false); + } + + public static void resetChunk(World world, World backup, int x, int z) { + net.minecraft.server.v1_15_R1.World w = ((CraftWorld) world).getHandle(); + Chunk chunk = w.getChunkAt(x, z); + Chunk backupChunk = ((CraftWorld) backup).getHandle().getChunkAt(x, z); + + System.arraycopy(backupChunk.getSections(), 0, chunk.getSections(), 0, chunk.getSections().length); + w.tileEntityListTick.removeAll(chunk.tileEntities.values()); + if (!FightWorld.isPaper()) { + w.tileEntityList.removeAll(chunk.tileEntities.values()); + } + chunk.tileEntities.clear(); + chunk.tileEntities.putAll(backupChunk.tileEntities); + chunk.heightMap.clear(); + chunk.heightMap.putAll(backupChunk.heightMap); + for(Player p : Bukkit.getOnlinePlayers()){ + ChunkListener.sendChunk(p, x, z); + } + } +} diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index adf509f..00ea3a6 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -88,6 +88,15 @@ public class MissileWars extends JavaPlugin { Bukkit.getScheduler().runTaskTimer(this, new FightInfoPacketSender(), 20, 20); } + public static void waiting() { + if (fightState == FightState.END) { + fightState = FightState.WAITING; + + redTeam = new MWTeam(ChatColor.RED, Config.RedSpawn, "Rot", Config.RedPortalZ); + blueTeam = new MWTeam(ChatColor.DARK_AQUA, Config.BlueSpawn, "Blau", Config.BluePortalZ); + } + } + /** * call to change fightstate from WAITING to INGAME */ diff --git a/src/de/steamwar/misslewars/countdowns/EndCountdown.java b/src/de/steamwar/misslewars/countdowns/EndCountdown.java index 155cceb..3430fbe 100644 --- a/src/de/steamwar/misslewars/countdowns/EndCountdown.java +++ b/src/de/steamwar/misslewars/countdowns/EndCountdown.java @@ -23,6 +23,7 @@ import de.steamwar.misslewars.Config; import de.steamwar.misslewars.FightState; import de.steamwar.misslewars.MissileWars; import de.steamwar.misslewars.StateDependent; +import de.steamwar.misslewars.listener.JoinListener; import org.bukkit.Bukkit; import org.bukkit.scheduler.BukkitTask; @@ -38,16 +39,11 @@ public class EndCountdown extends StateDependent { @Override public void enable() { - task = Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), this::kickPlayer, Config.EndTime); + task = Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), this::restart, Config.EndTime); } - private void kickPlayer(){ - if(Bukkit.getOnlinePlayers().isEmpty()){ - Bukkit.shutdown(); - }else{ - Bukkit.getOnlinePlayers().iterator().next().kickPlayer(null); - task = Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), this::kickPlayer, 10); - } + private void restart(){ + Bukkit.getOnlinePlayers().forEach(JoinListener::join); } @Override diff --git a/src/de/steamwar/misslewars/listener/JoinListener.java b/src/de/steamwar/misslewars/listener/JoinListener.java index 0758ece..d528e32 100644 --- a/src/de/steamwar/misslewars/listener/JoinListener.java +++ b/src/de/steamwar/misslewars/listener/JoinListener.java @@ -22,6 +22,7 @@ package de.steamwar.misslewars.listener; import de.steamwar.misslewars.Config; import de.steamwar.misslewars.FightState; import de.steamwar.misslewars.MissileWars; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerJoinEvent; @@ -50,4 +51,17 @@ public class JoinListener extends BasicListener { MissileWars.join(e.getPlayer()); } + public static void join(Player p) { + if (Config.isChallenge()) { + if (Config.RedLeader.equals(p.getUniqueId())) { + MissileWars.getRedTeam().join(p); + } else if (Config.BlueLeader.equals(p.getUniqueId())) { + MissileWars.getBlueTeam().join(p); + } else { + p.teleport(MissileWars.getRedTeam().getSpawn().toVector().midpoint(MissileWars.getBlueTeam().getSpawn().toVector()).toLocation(p.getWorld())); + } + return; + } + MissileWars.join(p); + } } From 8050f8d455b6c60077eb3eda6ed2df58759cbd16 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 27 Oct 2021 18:17:44 +0200 Subject: [PATCH 2/8] Fix EndCountdown --- src/de/steamwar/misslewars/countdowns/EndCountdown.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/de/steamwar/misslewars/countdowns/EndCountdown.java b/src/de/steamwar/misslewars/countdowns/EndCountdown.java index 3430fbe..3b3e218 100644 --- a/src/de/steamwar/misslewars/countdowns/EndCountdown.java +++ b/src/de/steamwar/misslewars/countdowns/EndCountdown.java @@ -19,10 +19,7 @@ package de.steamwar.misslewars.countdowns; -import de.steamwar.misslewars.Config; -import de.steamwar.misslewars.FightState; -import de.steamwar.misslewars.MissileWars; -import de.steamwar.misslewars.StateDependent; +import de.steamwar.misslewars.*; import de.steamwar.misslewars.listener.JoinListener; import org.bukkit.Bukkit; import org.bukkit.scheduler.BukkitTask; @@ -43,6 +40,8 @@ public class EndCountdown extends StateDependent { } private void restart(){ + FightWorld.resetWorld(); + MissileWars.waiting(); Bukkit.getOnlinePlayers().forEach(JoinListener::join); } From 8457e4f7f2d92b56cabc51c3046241cd63723fb7 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 27 Oct 2021 18:18:53 +0200 Subject: [PATCH 3/8] Fix MissileWars StateDependant code --- src/de/steamwar/misslewars/MissileWars.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index 00ea3a6..985a524 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -91,6 +91,7 @@ public class MissileWars extends JavaPlugin { public static void waiting() { if (fightState == FightState.END) { fightState = FightState.WAITING; + StateDependent.setupState(fightState); redTeam = new MWTeam(ChatColor.RED, Config.RedSpawn, "Rot", Config.RedPortalZ); blueTeam = new MWTeam(ChatColor.DARK_AQUA, Config.BlueSpawn, "Blau", Config.BluePortalZ); From 2d6e89549ea12efa910888b143b6aa17a167bb7f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 27 Oct 2021 20:22:10 +0200 Subject: [PATCH 4/8] Fix Indentation --- src/de/steamwar/misslewars/FightWorld.java | 8 ++--- .../misslewars/countdowns/EndCountdown.java | 34 +++++++++---------- .../misslewars/listener/JoinListener.java | 12 +------ 3 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/de/steamwar/misslewars/FightWorld.java b/src/de/steamwar/misslewars/FightWorld.java index 87b0db7..48f5f80 100644 --- a/src/de/steamwar/misslewars/FightWorld.java +++ b/src/de/steamwar/misslewars/FightWorld.java @@ -38,11 +38,11 @@ public class FightWorld { private static final World world = Bukkit.getWorlds().get(0); private static final boolean paper = Bukkit.getVersion().contains("git-Paper"); - public static boolean isPaper(){ + private static boolean isPaper(){ return paper; } - public static double posToChunk(int pos){ + private static double posToChunk(int pos){ return pos / 16.0; } @@ -62,7 +62,7 @@ public class FightWorld { return (int) Math.ceil(posToChunk(Config.ArenaMaxZ)); } - public static void forEachChunk(ObjIntConsumer executor) { + private static void forEachChunk(ObjIntConsumer executor) { for(int x = getMinChunkX(); x <= getMaxChunkX(); x++) for(int z = getMinChunkZ(); z <= getMaxChunkZ(); z++) executor.accept(x, z); @@ -81,7 +81,7 @@ public class FightWorld { Bukkit.unloadWorld(backup, false); } - public static void resetChunk(World world, World backup, int x, int z) { + private static void resetChunk(World world, World backup, int x, int z) { net.minecraft.server.v1_15_R1.World w = ((CraftWorld) world).getHandle(); Chunk chunk = w.getChunkAt(x, z); Chunk backupChunk = ((CraftWorld) backup).getHandle().getChunkAt(x, z); diff --git a/src/de/steamwar/misslewars/countdowns/EndCountdown.java b/src/de/steamwar/misslewars/countdowns/EndCountdown.java index 3b3e218..af4170c 100644 --- a/src/de/steamwar/misslewars/countdowns/EndCountdown.java +++ b/src/de/steamwar/misslewars/countdowns/EndCountdown.java @@ -28,26 +28,26 @@ import java.util.EnumSet; public class EndCountdown extends StateDependent { - private BukkitTask task; + private BukkitTask task; - public EndCountdown() { - super(EnumSet.of(FightState.END)); - } + public EndCountdown() { + super(EnumSet.of(FightState.END)); + } - @Override - public void enable() { - task = Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), this::restart, Config.EndTime); - } + @Override + public void enable() { + task = Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), this::restart, Config.EndTime); + } - private void restart(){ + private void restart() { FightWorld.resetWorld(); - MissileWars.waiting(); - Bukkit.getOnlinePlayers().forEach(JoinListener::join); - } + MissileWars.waiting(); + Bukkit.getOnlinePlayers().forEach(JoinListener::join); + } - @Override - public void disable() { - if(task != null && !task.isCancelled()) - task.cancel(); - } + @Override + public void disable() { + if (task != null && !task.isCancelled()) + task.cancel(); + } } diff --git a/src/de/steamwar/misslewars/listener/JoinListener.java b/src/de/steamwar/misslewars/listener/JoinListener.java index d528e32..6858b01 100644 --- a/src/de/steamwar/misslewars/listener/JoinListener.java +++ b/src/de/steamwar/misslewars/listener/JoinListener.java @@ -38,17 +38,7 @@ public class JoinListener extends BasicListener { @EventHandler(priority = EventPriority.HIGHEST) public void onJoin(PlayerJoinEvent e){ e.setJoinMessage("§a» " + e.getPlayer().getDisplayName()); - if (Config.isChallenge()) { - if (Config.RedLeader.equals(e.getPlayer().getUniqueId())) { - MissileWars.getRedTeam().join(e.getPlayer()); - } else if (Config.BlueLeader.equals(e.getPlayer().getUniqueId())) { - MissileWars.getBlueTeam().join(e.getPlayer()); - } else { - e.getPlayer().teleport(MissileWars.getRedTeam().getSpawn().toVector().midpoint(MissileWars.getBlueTeam().getSpawn().toVector()).toLocation(e.getPlayer().getWorld())); - } - return; - } - MissileWars.join(e.getPlayer()); + join(e.getPlayer()); } public static void join(Player p) { From b8343b5aa6323a0cde63ec2c565d393b106b9539 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 27 Oct 2021 20:23:41 +0200 Subject: [PATCH 5/8] Update MissileWars.waiting --- src/de/steamwar/misslewars/MWTeam.java | 5 +++++ src/de/steamwar/misslewars/MissileWars.java | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index a540212..886b05a 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -161,6 +161,11 @@ public class MWTeam { MissileWars.end(WinReasons.NO_ENEMY, enemy()); } + public void reset() { + players.clear(); + openInvitations.clear(); + } + public void invitePlayer(Player p) { if (enemy().openInvitations.contains(p)) return; openInvitations.add(p); diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index 985a524..6194811 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -93,8 +93,8 @@ public class MissileWars extends JavaPlugin { fightState = FightState.WAITING; StateDependent.setupState(fightState); - redTeam = new MWTeam(ChatColor.RED, Config.RedSpawn, "Rot", Config.RedPortalZ); - blueTeam = new MWTeam(ChatColor.DARK_AQUA, Config.BlueSpawn, "Blau", Config.BluePortalZ); + redTeam.reset(); + blueTeam.reset(); } } From ba7baf4dea2ee461218a19cec71f4e9458907d04 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 27 Oct 2021 20:26:49 +0200 Subject: [PATCH 6/8] Update config.yml --- src/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.yml b/src/config.yml index 49abd82..5e6b695 100644 --- a/src/config.yml +++ b/src/config.yml @@ -2,7 +2,7 @@ WaitingTime: 600 ItemTime: 300 ShieldFlyTime: 100 PlatformTime: 30 -EndTime: 600 +EndTime: 200 Arena: MinX: 0 From ad02b84a8bd6f3a405e273fd7168d49b1f98a02c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 27 Oct 2021 20:35:14 +0200 Subject: [PATCH 7/8] Update Config --- src/de/steamwar/misslewars/Config.java | 4 ++-- src/de/steamwar/misslewars/MissileWars.java | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/misslewars/Config.java b/src/de/steamwar/misslewars/Config.java index dbd5fff..c3cfc1a 100644 --- a/src/de/steamwar/misslewars/Config.java +++ b/src/de/steamwar/misslewars/Config.java @@ -51,8 +51,8 @@ public class Config { public static final double MissileChance; - public static final UUID BlueLeader; - public static final UUID RedLeader; + public static UUID BlueLeader; + public static UUID RedLeader; private static final int EventKampfID; diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index 6194811..cb93539 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -93,6 +93,9 @@ public class MissileWars extends JavaPlugin { fightState = FightState.WAITING; StateDependent.setupState(fightState); + Config.RedLeader = null; + Config.BlueLeader = null; + redTeam.reset(); blueTeam.reset(); } From d54ec631d0336b5a4d08df0b00323b6de5d6234e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 27 Oct 2021 20:43:30 +0200 Subject: [PATCH 8/8] Update MWTeam --- src/de/steamwar/misslewars/MWTeam.java | 10 +++++----- .../steamwar/misslewars/countdowns/EndCountdown.java | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 886b05a..25e3f00 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -31,10 +31,7 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Team; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Objects; -import java.util.Set; +import java.util.*; public class MWTeam { private static final ItemStack bow = new ItemStack(Material.BOW); @@ -162,8 +159,11 @@ public class MWTeam { } public void reset() { - players.clear(); openInvitations.clear(); + + List tempPlayer = new ArrayList<>(players); + players.clear(); + tempPlayer.forEach(this::join); } public void invitePlayer(Player p) { diff --git a/src/de/steamwar/misslewars/countdowns/EndCountdown.java b/src/de/steamwar/misslewars/countdowns/EndCountdown.java index af4170c..4b9c2fc 100644 --- a/src/de/steamwar/misslewars/countdowns/EndCountdown.java +++ b/src/de/steamwar/misslewars/countdowns/EndCountdown.java @@ -42,7 +42,6 @@ public class EndCountdown extends StateDependent { private void restart() { FightWorld.resetWorld(); MissileWars.waiting(); - Bukkit.getOnlinePlayers().forEach(JoinListener::join); } @Override