From 4d73afffcd980520f0e57b8c7cc19a915331150a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 19 Apr 2022 12:04:29 +0200 Subject: [PATCH] Update MWTeam.givePlayerItem Add SlowMoScript and needed utils --- src/de/steamwar/misslewars/MWTeam.java | 2 +- src/de/steamwar/misslewars/MissileWars.java | 7 +- .../steamwar/misslewars/scripts/Script.java | 3 +- .../scripts/implemented/SlowMoScript.java | 44 +++++++++++ .../misslewars/slowmo/SlowMoRunner.java | 55 ++++++++++++++ .../misslewars/slowmo/SlowMoUtils.java | 76 +++++++++++++++++++ 6 files changed, 183 insertions(+), 4 deletions(-) create mode 100644 src/de/steamwar/misslewars/scripts/implemented/SlowMoScript.java create mode 100644 src/de/steamwar/misslewars/slowmo/SlowMoRunner.java create mode 100644 src/de/steamwar/misslewars/slowmo/SlowMoUtils.java diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 5e28c10..94af88a 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -94,7 +94,7 @@ public class MWTeam { Inventory inventory = p.getInventory(); for (int i = 0; i <= 35; i++) { //35 is the last normal inventory slot ItemStack itemStack = inventory.getItem(i); - if (itemStack != null && itemStack.isSimilar(item) && itemStack.getAmount() != itemStack.getMaxStackSize()) { + if (itemStack != null && itemStack.isSimilar(item) && itemStack.getAmount() + item.getAmount() < 64) { itemStack.setAmount(itemStack.getAmount() + item.getAmount()); inventory.setItem(i, itemStack); p.updateInventory(); diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index 69e9670..e179d89 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -25,8 +25,10 @@ import de.steamwar.misslewars.commands.CommandSpectate; import de.steamwar.misslewars.countdowns.EndCountdown; import de.steamwar.misslewars.countdowns.ItemCountdown; import de.steamwar.misslewars.countdowns.WaitingCountdown; -import de.steamwar.misslewars.items.*; +import de.steamwar.misslewars.items.CustomItem; +import de.steamwar.misslewars.items.Missile; import de.steamwar.misslewars.listener.*; +import de.steamwar.misslewars.slowmo.SlowMoRunner; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; @@ -35,7 +37,6 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import java.util.List; import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; @@ -99,6 +100,8 @@ public class MissileWars extends JavaPlugin { fightState = FightState.WAITING; StateDependent.setupState(fightState); + SlowMoRunner.resetSlowMoTime(); + Set uuidList = Bukkit.getOnlinePlayers().stream().map(Entity::getUniqueId).collect(Collectors.toSet()); if (!uuidList.contains(Config.RedLeader) || !uuidList.contains(Config.BlueLeader)) { Config.RedLeader = null; diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java index 0b8ac11..e43fbd0 100644 --- a/src/de/steamwar/misslewars/scripts/Script.java +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -46,7 +46,6 @@ public class Script { if (!runnableScriptList.get(index++).execute(runnableScriptEvent)) return; } } - } public void execute(RunnableScriptEvent runnableScriptEvent) { @@ -84,6 +83,8 @@ public class Script { return new SoundScript(jsonObject); case "summon": return new SummonScript(jsonObject); + case "slowmo": + return new SlowMoScript(jsonObject); default: return null; } diff --git a/src/de/steamwar/misslewars/scripts/implemented/SlowMoScript.java b/src/de/steamwar/misslewars/scripts/implemented/SlowMoScript.java new file mode 100644 index 0000000..54587fd --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/implemented/SlowMoScript.java @@ -0,0 +1,44 @@ +/* + * + * 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.scripts.implemented; + +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import de.steamwar.misslewars.scripts.RunnableScript; +import de.steamwar.misslewars.scripts.RunnableScriptEvent; +import de.steamwar.misslewars.slowmo.SlowMoRunner; + +public class SlowMoScript implements RunnableScript { + + private int slowMoTime = 0; + + public SlowMoScript(JsonObject jsonObject) { + JsonPrimitive jsonPrimitive = jsonObject.getAsJsonPrimitive("time"); + if (jsonPrimitive.isNumber()) slowMoTime = jsonPrimitive.getAsInt(); + } + + @Override + public boolean execute(RunnableScriptEvent runnableScriptEvent) { + SlowMoRunner.addSlowMoTime(slowMoTime); + return true; + } +} diff --git a/src/de/steamwar/misslewars/slowmo/SlowMoRunner.java b/src/de/steamwar/misslewars/slowmo/SlowMoRunner.java new file mode 100644 index 0000000..025b242 --- /dev/null +++ b/src/de/steamwar/misslewars/slowmo/SlowMoRunner.java @@ -0,0 +1,55 @@ +/* + * + * 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.slowmo; + +import de.steamwar.misslewars.MissileWars; +import org.bukkit.Bukkit; + +public class SlowMoRunner { + + private static long currentTime = 0; + private static long current = 0; + + public static void addSlowMoTime(int time) { + currentTime += time; + } + + public static void resetSlowMoTime() { + currentTime = 0; + } + + static { + Bukkit.getScheduler().runTaskTimer(MissileWars.getPlugin(), () -> { + if(currentTime > 0) { + current += 1; + if (current % 5 == 0) { + SlowMoUtils.unfreeze(); + } else { + SlowMoUtils.freeze(); + } + currentTime--; + } else { + SlowMoUtils.unfreeze(); + } + }, 0, 1); + } +} diff --git a/src/de/steamwar/misslewars/slowmo/SlowMoUtils.java b/src/de/steamwar/misslewars/slowmo/SlowMoUtils.java new file mode 100644 index 0000000..dbde1f0 --- /dev/null +++ b/src/de/steamwar/misslewars/slowmo/SlowMoUtils.java @@ -0,0 +1,76 @@ +/* + * + * 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.slowmo; + +import com.comphenix.tinyprotocol.Reflection; +import org.bukkit.Bukkit; +import org.bukkit.World; + +import java.lang.reflect.Field; + +public class SlowMoUtils { + + private static final Field field; + public static final boolean freezeEnabled; + + private static final Reflection.MethodInvoker getWorldHandle = Reflection.getTypedMethod(Reflection.getClass("{obc}.CraftWorld"), "getHandle", null); + private static boolean frozen = false; + + private static final World world; + + static { + Field temp; + try { + temp = Reflection.getClass("{nms.server.level}.WorldServer").getField("freezed"); + } catch (NoSuchFieldException e) { + temp = null; + } + field = temp; + if (field != null) field.setAccessible(true); + freezeEnabled = field != null; + world = Bukkit.getWorlds().get(0); + } + + public static void freeze() { + setFreeze(world, true); + } + + public static void unfreeze() { + setFreeze(world, false); + } + + public static boolean frozen() { + return freezeEnabled && frozen; + } + + private static void setFreeze(World world, boolean state) { + if (freezeEnabled) { + if (frozen == state) return; + try { + field.set(getWorldHandle.invoke(world), state); + frozen = state; + } catch (IllegalAccessException e) { + // Ignored; + } + } + } +}