commit 4064f28caf031b0c4b4bf1caab41f6416a915ad8 Author: Lixfel Date: Mon Oct 4 11:27:58 2021 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..760fca2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + +/.idea +/target +/lib diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7decdca --- /dev/null +++ b/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + de.steamwar + LobbySystem + 1.0 + jar + https://maven.apache.org + + + UTF-8 + ${project.basedir} + + + + src + + + src + + **/*.java + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + 8 + 8 + + + + lobbysystem + + + + + steamwar + Spigot + 1.15 + system + ${main.basedir}/lib/Spigot-1.15.jar + + + steamwar + WorldEdit + 1.15 + system + ${main.basedir}/lib/WorldEdit-1.15.jar + + + steamwar + SpigotCore + 1.0 + system + ${main.basedir}/lib/SpigotCore.jar + + + diff --git a/src/de/steamwar/lobby/Config.java b/src/de/steamwar/lobby/Config.java new file mode 100644 index 0000000..de61270 --- /dev/null +++ b/src/de/steamwar/lobby/Config.java @@ -0,0 +1,46 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby; + +import de.steamwar.lobby.portal.CommandPortal; +import de.steamwar.lobby.portal.TeleportPortal; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.serialization.ConfigurationSerialization; + +public class Config { + + private final FileConfiguration yml; + + public Config(FileConfiguration yml) { + this.yml = yml; + + ConfigurationSerialization.registerClass(CommandPortal.class); + ConfigurationSerialization.registerClass(TeleportPortal.class); + yml.getList("commandPortals", CommandPortal.getPortals()); + yml.getList("teleportPortals", TeleportPortal.getPortals()); + } + + public void save() { + yml.set("commandPortals", CommandPortal.getPortals()); + yml.set("teleportPortals", TeleportPortal.getPortals()); + + LobbySystem.getPlugin().saveConfig(); + } +} diff --git a/src/de/steamwar/lobby/LobbySystem.java b/src/de/steamwar/lobby/LobbySystem.java new file mode 100644 index 0000000..30064af --- /dev/null +++ b/src/de/steamwar/lobby/LobbySystem.java @@ -0,0 +1,52 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby; + +import de.steamwar.lobby.listener.Fightservers; +import de.steamwar.lobby.listener.Join; +import de.steamwar.lobby.listener.Portals; +import org.bukkit.plugin.java.JavaPlugin; + +public class LobbySystem extends JavaPlugin { + + private static LobbySystem plugin; + private static Config config; + + @Override + public void onLoad() { + plugin = this; + } + + @Override + public void onEnable() { + new Join(); + new Fightservers(); + new Portals(); + config = new Config(getConfig()); + } + + public static LobbySystem getPlugin() { + return plugin; + } + + public static Config config() { + return config; + } +} diff --git a/src/de/steamwar/lobby/listener/BasicListener.java b/src/de/steamwar/lobby/listener/BasicListener.java new file mode 100644 index 0000000..1979b46 --- /dev/null +++ b/src/de/steamwar/lobby/listener/BasicListener.java @@ -0,0 +1,31 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.listener; + +import de.steamwar.lobby.LobbySystem; +import org.bukkit.Bukkit; +import org.bukkit.event.Listener; + +public class BasicListener implements Listener { + + public BasicListener () { + Bukkit.getPluginManager().registerEvents(this, LobbySystem.getPlugin()); + } +} diff --git a/src/de/steamwar/lobby/listener/Fightservers.java b/src/de/steamwar/lobby/listener/Fightservers.java new file mode 100644 index 0000000..2352952 --- /dev/null +++ b/src/de/steamwar/lobby/listener/Fightservers.java @@ -0,0 +1,34 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.listener; + +import de.steamwar.comms.BungeeReceiver; +import de.steamwar.comms.PacketIdManager; +import de.steamwar.lobby.LobbySystem; +import de.steamwar.lobby.servers.Fightserver; +import org.bukkit.Bukkit; + +public class Fightservers { + + public Fightservers() { + BungeeReceiver.registerHandler(PacketIdManager.FIGHT_INFO, Fightserver::newFightInfo); + Bukkit.getScheduler().runTaskTimer(LobbySystem.getPlugin(), Fightserver::removeStopped, 20, 20); + } +} diff --git a/src/de/steamwar/lobby/listener/Join.java b/src/de/steamwar/lobby/listener/Join.java new file mode 100644 index 0000000..4cdd8fa --- /dev/null +++ b/src/de/steamwar/lobby/listener/Join.java @@ -0,0 +1,38 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.listener; + +import org.bukkit.GameMode; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerJoinEvent; + +public class Join extends BasicListener { + + @EventHandler + public void onJoin(PlayerJoinEvent e) { + Player player = e.getPlayer(); + + player.setGameMode(GameMode.ADVENTURE); + player.setWalkSpeed(5.0f); + + //new ImALobbyPacket().send(player); + } +} diff --git a/src/de/steamwar/lobby/listener/Portals.java b/src/de/steamwar/lobby/listener/Portals.java new file mode 100644 index 0000000..a462458 --- /dev/null +++ b/src/de/steamwar/lobby/listener/Portals.java @@ -0,0 +1,69 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.listener; + +import de.steamwar.lobby.portal.Portal; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.HashMap; +import java.util.Map; + +public class Portals extends BasicListener { + + private static final Map> portalStack = new HashMap<>(); + + public static Deque getStack(Player player) { + return portalStack.get(player); + } + + @EventHandler + public void onJoin(PlayerJoinEvent e) { + portalStack.put(e.getPlayer(), new ArrayDeque<>()); + } + + @EventHandler + public void onMove(PlayerMoveEvent e) { + Location to = e.getTo(); + assert to != null; + + Portal portal = Portal.getPortal(to); + if (portal == null) + return; + + Player player = e.getPlayer(); + Deque lastPortals = portalStack.get(player); + if(!lastPortals.isEmpty() && lastPortals.peek() == portal) + return; + + portal.handle(player, to); + } + + @EventHandler + public void onQuit(PlayerQuitEvent e) { + portalStack.remove(e.getPlayer()); + } +} diff --git a/src/de/steamwar/lobby/portal/CommandPortal.java b/src/de/steamwar/lobby/portal/CommandPortal.java new file mode 100644 index 0000000..df01cc8 --- /dev/null +++ b/src/de/steamwar/lobby/portal/CommandPortal.java @@ -0,0 +1,72 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.portal; + +import org.bukkit.Location; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class CommandPortal extends Portal { + + private static final List portals = new ArrayList<>(); + + public static List getPortals() { + return portals; + } + + private final String command; + + public CommandPortal(Map section) { + super(section); + this.command = (String) section.get("command"); + init(); + } + + public CommandPortal(String id, Location pos1, Location pos2, String command) { + super(id, pos1, pos2); + this.command = command; + init(); + } + + private void init() { + portals.add(this); + } + + @Override + public void handle(Player player, Location loc) { + player.sendMessage("Folgender Command wäre jetzt ausgeführt worden, könnte der Bungee das schon: " + command); + } + + @Override + public Map serialize() { + Map map = super.serialize(); + map.put("command", command); + return map; + } + + @Override + public void delete() { + portals.remove(this); + super.delete(); + } +} diff --git a/src/de/steamwar/lobby/portal/FightserverPortal.java b/src/de/steamwar/lobby/portal/FightserverPortal.java new file mode 100644 index 0000000..ae27d56 --- /dev/null +++ b/src/de/steamwar/lobby/portal/FightserverPortal.java @@ -0,0 +1,23 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.portal; + +public class FightserverPortal extends TeleportPortal { +} diff --git a/src/de/steamwar/lobby/portal/Portal.java b/src/de/steamwar/lobby/portal/Portal.java new file mode 100644 index 0000000..5c1ea03 --- /dev/null +++ b/src/de/steamwar/lobby/portal/Portal.java @@ -0,0 +1,127 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.portal; + +import org.bukkit.Location; +import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +public class Portal implements PortalHandler, ConfigurationSerializable { + + private static final Map portals = new HashMap<>(); + private static final Map posMap = new HashMap<>(); + + public static Portal getPortal(Location loc) { + return posMap.get(new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + } + + public static Portal getPortal(String id) { + return portals.get(id); + } + + private final Location pos1; + private final Location pos2; + private final String id; + private final PortalType type; + private final PortalHandler handler; + + public Portal(Map map) { + this.id = (String) map.get("id"); + this.pos1 = (Location) map.get("pos1"); + this.pos2 = (Location) map.get("pos2"); + this.type = PortalType.valueOf((String) map.get("type")); + this.handler = type.deserialize(map, this); + + init(); + } + + public Portal(String id, Location pos1, Location pos2, Function handlerConstructor) { + this.id = id; + this.pos1 = pos1; + this.pos2 = pos2; + this.handler = handlerConstructor.apply(this); + this.type = handler.type(); + + init(); + } + + private void init() { + portals.put(id, this); + Vector diff = pos2.subtract(pos1).toVector(); + diff.divide(new Vector(Math.abs(diff.getBlockX()), Math.abs(diff.getBlockY()), Math.abs(diff.getBlockZ()))); + for(int x = pos1.getBlockX(); x <= pos2.getBlockX(); x += diff.getBlockX()) { + for(int y = pos1.getBlockY(); y <= pos2.getBlockY(); y += diff.getBlockY()) { + for(int z = pos1.getBlockZ(); z <= pos2.getBlockZ(); z += diff.getBlockZ()) { + posMap.put(new Vector(x, y, z), this); + } + } + } + } + + @Override + public void handle(Player player, Location from) { + handler.handle(player, from); + } + + @Override + public void serialize(Map map) { + map.put("id", id); + map.put("pos1", pos1); + map.put("pos2", pos2); + map.put("type", type.name()); + handler.serialize(map); + } + + @Override + public PortalType type() { + return handler.type(); + } + + @Override + public void delete() { + portals.remove("id"); + posMap.values().removeIf(this::equals); + } + + @Override + public Map serialize() { + Map map = new HashMap<>(); + serialize(map); + return map; + } + + public Location getPos1() { + return pos1; + } + + public Location getPos2() { + return pos2; + } + + public String getId() { + return id; + } + +} diff --git a/src/de/steamwar/lobby/portal/PortalHandler.java b/src/de/steamwar/lobby/portal/PortalHandler.java new file mode 100644 index 0000000..4e5e023 --- /dev/null +++ b/src/de/steamwar/lobby/portal/PortalHandler.java @@ -0,0 +1,35 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.portal; + +import org.bukkit.Location; +import org.bukkit.entity.Player; + +import java.util.Map; + +public interface PortalHandler { + void handle(Player player, Location from); + + void serialize(Map map); + + PortalType type(); + + void delete(); +} diff --git a/src/de/steamwar/lobby/portal/PortalType.java b/src/de/steamwar/lobby/portal/PortalType.java new file mode 100644 index 0000000..39ac52a --- /dev/null +++ b/src/de/steamwar/lobby/portal/PortalType.java @@ -0,0 +1,39 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.portal; + +import java.util.Map; +import java.util.function.BiFunction; + +public enum PortalType { + TELEPORT(deserializer), + COMMAND(deserializer), + FIGHTSERVER(deserializer); + + private final BiFunction, Portal, PortalHandler> deserializer; + + PortalType(BiFunction, Portal, PortalHandler> deserializer) { + this.deserializer = deserializer; + } + + public PortalHandler deserialize(Map map, Portal portal) { + return deserializer.apply(map, portal); + } +} diff --git a/src/de/steamwar/lobby/portal/TeleportPortal.java b/src/de/steamwar/lobby/portal/TeleportPortal.java new file mode 100644 index 0000000..a5b92d1 --- /dev/null +++ b/src/de/steamwar/lobby/portal/TeleportPortal.java @@ -0,0 +1,83 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.portal; + +import de.steamwar.lobby.listener.Portals; +import org.bukkit.Location; +import org.bukkit.entity.Player; + +import java.util.*; + +public class TeleportPortal extends Portal { + + private static final List portals = new ArrayList<>(); + + public static List getPortals() { + return portals; + } + + private final Set sources = new HashSet<>(); + private final String target; + + protected TeleportPortal(Map section) { + super(section); + this.target = (String) section.get("target"); + init(); + } + + protected TeleportPortal(String id, Location pos1, Location pos2, String target) { + super(id, pos1, pos2); + this.target = target; + init(); + } + + private void init() { + portals.stream().filter(portal -> portal.target.equals(this.id)).forEach(sources::add); + portals.add(this); + } + + @Override + public void handle(Player player, Location loc) { + Deque stack = Portals.getStack(player); + if(!stack.isEmpty() && sources.contains(stack.peek())) { + teleport(player, loc, stack.pop()); + } else { + teleport(player, loc, getPortal(target)); + } + } + + protected void teleport(Player player, Location from, Portal target) { + player.sendMessage("Symbolisierter teleport zu " + target.id); + } + + @Override + public Map serialize() { + Map map = super.serialize(); + map.put("target", target); + return map; + } + + @Override + public void delete() { + portals.remove(this); + portals.forEach(portal -> portal.sources.remove(this)); + super.delete(); + } +} diff --git a/src/de/steamwar/lobby/servers/Fightserver.java b/src/de/steamwar/lobby/servers/Fightserver.java new file mode 100644 index 0000000..3c3d468 --- /dev/null +++ b/src/de/steamwar/lobby/servers/Fightserver.java @@ -0,0 +1,90 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.servers; + +import com.google.common.io.ByteArrayDataInput; +import de.steamwar.comms.packets.FightInfoPacket; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.*; +import java.util.function.Consumer; + +public class Fightserver { + + private static final List> listeners = new ArrayList<>(); + + public static void registerListener(Consumer listener) { + listeners.add(listener); + } + + private static final Map servers = new HashMap<>(); + + private Instant lastUpdate; + private final String serverName; + private final String gameMode; + private final Observable> bluePlayers = new Observable<>(); + private final Observable> redPlayers = new Observable<>(); + private final Observable playerCount = new Observable<>(); + + private Fightserver(FightInfoPacket fightInfo) { + serverName = fightInfo.getServerName(); + gameMode = fightInfo.getGameMode(); + + update(fightInfo); + listeners.forEach(listener -> listener.accept(this)); + + servers.put(serverName, this); + } + + private void update(FightInfoPacket fightInfo) { + bluePlayers.update(new HashSet<>(fightInfo.getBluePlayers())); + redPlayers.update(new HashSet<>(fightInfo.getRedPlayers())); + playerCount.update(fightInfo.getBluePlayers().size() + fightInfo.getRedPlayers().size() + fightInfo.getSpectators().size()); + lastUpdate = Instant.now(); + } + + private void remove() { + + } + + public static void newFightInfo(ByteArrayDataInput in) { + FightInfoPacket fightInfo = new FightInfoPacket(in); + Fightserver server = servers.get(fightInfo.getServerName()); + if (server == null) { + new Fightserver(fightInfo); + } else { + server.update(fightInfo); + } + } + + public static void removeStopped() { + Instant timeout = Instant.now().minus(5, ChronoUnit.SECONDS); + + Iterator> it = servers.entrySet().iterator(); + while(it.hasNext()) { + Map.Entry server = it.next(); + if(timeout.isBefore(server.getValue().lastUpdate)) { + server.getValue().remove(); + it.remove(); + } + } + } +} diff --git a/src/de/steamwar/lobby/servers/Observable.java b/src/de/steamwar/lobby/servers/Observable.java new file mode 100644 index 0000000..314070a --- /dev/null +++ b/src/de/steamwar/lobby/servers/Observable.java @@ -0,0 +1,50 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.lobby.servers; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +public class Observable { + + private final List> observers = new ArrayList<>(); + + private T value; + + public Observable() { + value = null; + } + + public Observable(T initial) { + value = initial; + } + + public void register(Consumer observer) { + observers.add(observer); + } + + public void update(T value) { + if(!value.equals(this.value)) { + this.value = value; + observers.forEach(observer -> observer.accept(value)); + } + } +} diff --git a/src/plugin.yml b/src/plugin.yml new file mode 100644 index 0000000..76163e9 --- /dev/null +++ b/src/plugin.yml @@ -0,0 +1,10 @@ +name: LobbySystem +version: "1.0" +authors: + - Lixfel +main: de.steamwar.lobby.LobbySystem +depend: [SpigotCore, WorldEdit] +api-version: "1.13" + +commands: + ak: