Add BauSystem module

Fix ci java version
Fix LinkageProcessor
This commit is contained in:
2024-08-05 13:28:50 +02:00
parent 41d31e6c9c
commit 3366a30b0c
526 changed files with 43550 additions and 149479 deletions
@@ -0,0 +1,109 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.lobby.portal;
import de.steamwar.lobby.LobbySystem;
import de.steamwar.lobby.command.ModifyCommand;
import de.steamwar.lobby.listener.Portals;
import de.steamwar.network.NetworkSender;
import de.steamwar.network.packets.client.ExecuteCommandPacket;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
public class CommandPortal implements PortalHandler {
private final String command;
public CommandPortal(Map<String, Object> section, Portal portal) {
this.command = (String) section.get("command");
}
public CommandPortal(String command) {
this.command = command;
}
@Override
public void handle(Player player, Location loc) {
String[] parts = command.split("\\\\");
int[] stackIds = new int[parts.length-1];
int maxId = 0;
for(int i = 1; i < parts.length; i++) {
stackIds[i-1] = Integer.parseInt(parts[i].substring(0, 1));
if(stackIds[i-1] > maxId)
maxId = stackIds[i-1];
}
Iterator<Portal> stack = Portals.getStack(player).iterator();
String[] pieces = new String[stackIds.length];
while (maxId > 0) {
if(!stack.hasNext()) {
LobbySystem.getPlugin().getLogger().log(Level.WARNING, "Stackportal with missing elements: " + player.getName() + " /" + command);
player.sendMessage("§cEigentlich solltest du gerade gar nicht durch dieses Portal durchgehen können...");
return;
}
Portal portal = stack.next();
if(portal.type() == PortalType.STACK) {
for(int i = 0; i < stackIds.length; i++) {
if(stackIds[i] == maxId) {
pieces[i] = ((StackPortal) portal.getHandler()).getText();
}
}
maxId--;
}
}
StringBuilder cmd = new StringBuilder(parts[0]);
for(int i = 0; i < pieces.length; i++) {
cmd.append(pieces[i]).append(parts[i+1].substring(1));
}
if(ModifyCommand.modifying(player))
player.sendMessage("/" + cmd);
NetworkSender.send(new ExecuteCommandPacket(SteamwarUser.get(player.getUniqueId()).getId(), cmd.toString()));
}
@Override
public void serialize(Map<String, Object> map) {
map.put("command", command);
}
@Override
public PortalType type() {
return PortalType.COMMAND;
}
@Override
public void delete() {
// Nothing to remove
}
@Override
public String toString() {
return "Command: /" + command;
}
}
@@ -0,0 +1,61 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.lobby.portal;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.Map;
public class DummyPortal implements PortalHandler {
public DummyPortal() {
// Inits nothing
}
public DummyPortal(Map<String, Object> section, Portal portal) {
// Deserializes nothing
}
@Override
public void handle(Player player, Location from) {
// Does nothing
}
@Override
public void serialize(Map<String, Object> map) {
// Serializes nothing
}
@Override
public PortalType type() {
return PortalType.DUMMY;
}
@Override
public void delete() {
// Delets nothing
}
@Override
public String toString() {
return "Dummy";
}
}
@@ -0,0 +1,244 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.lobby.portal;
import de.steamwar.lobby.Fightserver;
import de.steamwar.lobby.LobbySystem;
import de.steamwar.lobby.display.Hologram;
import de.steamwar.lobby.display.NPC;
import de.steamwar.network.packets.common.FightInfoPacket;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class FightserverPortal implements PortalHandler, Comparable<FightserverPortal> {
private static final Map<String, List<FightserverPortal>> portals = new HashMap<>();
public static FightserverPortal findFree(String gamemode) {
List<FightserverPortal> list = portals.getOrDefault(gamemode, Collections.emptyList());
for(FightserverPortal portal : list) {
if(portal.server == null)
return portal;
}
return null;
}
private final Portal portal;
private final String gamemode;
private final int order;
private final String target;
private final List<Location> bluePlayers;
private final List<Location> redPlayers;
private Fightserver server = null;
private PortalHandler handler = new DummyPortal();
private final Hologram hologram;
private final List<NPC> blueNPCs = new ArrayList<>();
private final List<NPC> redNPCs = new ArrayList<>();
public FightserverPortal(Map<String, Object> section, Portal portal) {
this(
portal,
(String) section.get("group"),
(int) section.get("order"),
(String) section.get("target"),
(List<Location>) section.getOrDefault("bluePlayers", new ArrayList<>()),
(List<Location>) section.getOrDefault("redPlayers", new ArrayList<>())
);
}
public FightserverPortal(Portal portal, String gamemode, int order, String target, List<Location> bluePlayers, List<Location> redPlayers) {
this.portal = portal;
this.gamemode = gamemode;
this.order = order;
this.target = target;
this.bluePlayers = bluePlayers;
this.redPlayers = redPlayers;
hologram = new Hologram(null, portal.denormalize(new Vector(0.5, 0.5, 0.5)).toLocation(portal.getPos1().getWorld()), "", false);
setServer(null);
List<FightserverPortal> list = portals.computeIfAbsent(gamemode, mode -> new ArrayList<>());
list.add(this);
list.sort(null);
}
public void setServer(Fightserver server) {
this.server = server;
if (server == null) {
setHandler(new TeleportPortal(portal, target));
} else {
setHandler(new CommandPortal("arena " + server.getServerName()));
}
updateText();
updateBluePlayers();
updateRedPlayers();
}
public void updateText() {
if(server == null) {
hologram.updateText("§7Neuen Kampf starten");
return;
}
FightInfoPacket info = server.current();
if (fightStateCountdown(info.getFightState()))
hologram.updateText(String.format("§7%s §e%s §7%d§8:§7%02d", server.getServerName(), fightStateMapper(info.getFightState()), info.getCountdown() / 60, info.getCountdown() % 60));
else
hologram.updateText(String.format("§7%s §e%s", server.getServerName(), fightStateMapper(info.getFightState())));
}
public void updateBluePlayers() {
updateNPCs(bluePlayers, blueNPCs, server != null ? server.current().getBluePlayers() : Collections.emptyList());
}
public void updateRedPlayers() {
updateNPCs(redPlayers, redNPCs, server != null ? server.current().getRedPlayers() : Collections.emptyList());
}
private void updateNPCs(List<Location> locations, List<NPC> npcs, List<Integer> players) {
List<SteamwarUser> remainingPlayers = players.stream().map(SteamwarUser::get).collect(Collectors.toList());
List<Location> remainingLocations = new ArrayList<>(locations);
npcs.removeIf(npc -> {
SteamwarUser user = SteamwarUser.get(npc.getUuid());
if(remainingPlayers.contains(user)) {
remainingPlayers.remove(user);
remainingLocations.remove(npc.getLocation());
return false;
} else {
npc.delete();
return true;
}
});
for(SteamwarUser user : remainingPlayers) {
if(remainingLocations.isEmpty())
break;
npcs.add(new NPC(remainingLocations.remove(0), user.getUUID(), user.getUserName()));
}
}
public void addBlue(Location location) {
bluePlayers.add(location);
LobbySystem.config().save();
}
public void addRed(Location location) {
redPlayers.add(location);
LobbySystem.config().save();
}
public void removeBlue(int i) {
bluePlayers.remove(i);
LobbySystem.config().save();
}
public void removeRed(int i) {
redPlayers.remove(i);
LobbySystem.config().save();
}
private void setHandler(PortalHandler handler) {
handler.delete();
this.handler = handler;
}
@Override
public void handle(Player player, Location from, Location to) {
handler.handle(player, from, to);
}
@Override
public void serialize(Map<String, Object> map) {
map.put("group", gamemode);
map.put("order", order);
map.put("target", target);
map.put("bluePlayers", bluePlayers);
map.put("redPlayers", redPlayers);
}
@Override
public PortalType type() {
return PortalType.FIGHTSERVER;
}
@Override
public void delete() {
portals.get(gamemode).remove(this);
hologram.delete();
blueNPCs.forEach(NPC::delete);
redNPCs.forEach(NPC::delete);
handler.delete();
}
@Override
public int compareTo(FightserverPortal other) {
return order - other.order;
}
private boolean fightStateCountdown(String state) {
switch (state) {
case "waiting":
case "PRE_LEADER_SETUP":
case "end":
case "SPECTATE":
return false;
default:
return true;
}
}
private String fightStateMapper(String state) {
switch (state) {
case "waiting":
case "PRE_LEADER_SETUP":
return "Warten auf Spieler";
case "PRE_SCHEM_SETUP":
return "Schemauswahl";
case "POST_SCHEM_SETUP":
return "Vorbereitung";
case "PRE_RUNNING":
return "Kampfbeginn in";
case "fighting":
case "RUNNING":
return "Kampf läuft";
case "end":
case "SPECTATE":
return "Zuschauerphase";
default:
LobbySystem.getPlugin().getLogger().log(Level.SEVERE, "Unknown FightState " + state + " encountered");
return "Programmierfehler";
}
}
@Override
public String toString() {
return "Fightserver";
}
}
@@ -0,0 +1,273 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.lobby.portal;
import de.steamwar.lobby.LobbySystem;
import de.steamwar.lobby.display.Hologram;
import de.steamwar.lobby.listener.Portals;
import org.bukkit.Location;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.util.*;
import java.util.function.Function;
public class Portal implements PortalHandler, ConfigurationSerializable {
private static final Map<String, Portal> portals = new HashMap<>();
private static final Map<ChunkCoords, List<Portal>> chunkPortals = new HashMap<>();
public static List<Portal> getPortals() {
return new ArrayList<>(portals.values());
}
public static Set<String> getPortalNames() {
return portals.keySet();
}
public static Portal getPortal(Location from, Location to) {
ChunkCoords in = new ChunkCoords(from);
ChunkCoords out = new ChunkCoords(to);
for(ChunkCoords coords : perChunk(Math.min(in.x, out.x), Math.max(in.x, out.x), Math.min(in.z, out.z), Math.max(in.z, out.z))) {
for(Portal portal : chunkPortals.getOrDefault(coords, Collections.emptyList())) {
Vector normalizedFrom = portal.normalize(from);
Vector normalizedTo = portal.normalize(to);
if(portal.depth == 0.0) {
normalizedFrom.setX(normalizedFrom.getX() > 0 ? 2 : -1);
normalizedTo.setX(normalizedTo.getX() > 0 ? 2 : -1);
}
if(inside(normalizedFrom.getX(), normalizedTo.getX()) && inside(normalizedFrom.getY(), normalizedTo.getY()) && inside(normalizedFrom.getZ(), normalizedTo.getZ())) {
return portal;
}
}
}
return null;
}
public static Portal getPortal(String id) {
return portals.get(id);
}
private static boolean inside(double v1, double v2) {
return (v1 >= 0.0 || v2 >= 0.0) && (v1 <= 1.0 || v2 <= 1.0);
}
private final Location pos1;
private final Location pos2;
private final double depth;
private final Vector pos1Vector;
private final double yRotation;
private final Vector rotatedShape;
private final int minChunkX;
private final int minChunkZ;
private final int maxChunkX;
private final int maxChunkZ;
private final String id;
private final PortalType type;
private final PortalHandler handler;
private final Hologram debugPos1;
private final Hologram debugPos2;
private final Hologram debugPos3;
public Portal(Map<String, Object> map) {
this(
(String) map.get("id"),
(Location) map.get("pos1"),
(Location) map.get("pos2"),
(double) map.getOrDefault("depth", 0.0),
portal -> PortalType.valueOf((String) map.get("type")).deserialize(map, portal)
);
}
public Portal(String id, Location pos1, Location pos2, Function<Portal, PortalHandler> handlerConstructor) {
this(id, pos1, pos2, 0.0, handlerConstructor);
LobbySystem.config().save();
}
public Portal(String id, Location pos1, Location pos2, double depth, Function<Portal, PortalHandler> handlerConstructor) {
this.id = id;
this.pos1 = pos1;
this.pos2 = pos2;
this.depth = depth;
this.minChunkX = Math.min(pos1.getBlockX(), pos2.getBlockX()) >> 4;
this.minChunkZ = Math.min(pos1.getBlockZ(), pos2.getBlockZ()) >> 4;
this.maxChunkX = Math.max(pos1.getBlockX(), pos2.getBlockX()) >> 4;
this.maxChunkZ = Math.max(pos1.getBlockZ(), pos2.getBlockZ()) >> 4;
Vector orientation = pos2.toVector().subtract(pos1.toVector());
this.yRotation = Math.atan2(orientation.getX(), orientation.getZ());
this.pos1Vector = pos1.toVector().subtract(new Vector(depth/2, 0, 0).rotateAroundY(yRotation));
this.rotatedShape = new Vector(depth == 0.0 ? 1.0 : depth, orientation.getY(), orientation.clone().setY(0).length());
this.handler = handlerConstructor.apply(this);
this.type = handler.type();
this.debugPos1 = new Hologram(null, pos1, id + " POS1", true);
this.debugPos2 = new Hologram(null, pos2, id + " POS2", true);
this.debugPos3 = new Hologram(null, pos1.clone().add(pos2).toVector().divide(new Vector(2, 2, 2)).toLocation(pos1.getWorld()), handler.toString(), true);
portals.put(id, this);
perChunk(minChunkX, maxChunkX, minChunkZ, maxChunkZ).forEach(coords -> chunkPortals.computeIfAbsent(coords, coords1 -> new ArrayList<>()).add(this));
}
public Vector normalize(Location location) {
return location.toVector().subtract(pos1Vector).rotateAroundY(-yRotation).divide(rotatedShape);
}
public Vector denormalize(Vector vector) {
return vector.multiply(rotatedShape).rotateAroundY(yRotation).add(pos1Vector);
}
public double getYrotation() {
return yRotation;
}
public double getDepth() {
return depth;
}
private static Iterable<ChunkCoords> perChunk(int minChunkX, int maxChunkX, int minChunkZ, int maxChunkZ) {
return () -> new Iterator<ChunkCoords>() {
private int x = minChunkX;
private int z = minChunkZ;
@Override
public boolean hasNext() {
return z <= maxChunkZ;
}
@Override
public ChunkCoords next() {
ChunkCoords coords = new ChunkCoords(x++, z);
if (x > maxChunkX) {
x = minChunkX;
z++;
}
return coords;
}
};
}
@Override
public void handle(Player player, Location from, Location to) {
handler.handle(player, from, to);
Portals.getStack(player).push(this);
}
@Override
public void serialize(Map<String, Object> map) {
map.put("id", id);
map.put("pos1", pos1);
map.put("pos2", pos2);
map.put("depth", depth);
map.put("type", type.name());
handler.serialize(map);
}
@Override
public PortalType type() {
return handler.type();
}
@Override
public void delete() {
debugPos1.delete();
debugPos2.delete();
debugPos3.delete();
handler.delete();
perChunk(minChunkX, maxChunkX, minChunkZ, maxChunkZ).forEach(coords -> chunkPortals.getOrDefault(coords, new ArrayList<>()).remove(this));
portals.remove(id);
LobbySystem.config().save();
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>();
serialize(map);
return map;
}
public Location getPos1() {
return pos1;
}
public Location getPos2() {
return pos2;
}
public String getId() {
return id;
}
public void setDepth(double depth) {
delete();
Map<String, Object> handlerMap = new HashMap<>();
handler.serialize(handlerMap);
new Portal(id, pos1, pos2, depth, portal -> handler.type().deserialize(handlerMap, portal));
LobbySystem.config().save();
}
public PortalHandler getHandler() {
return handler;
}
@Override
public String toString() {
return getId() + " " + type().name();
}
private static class ChunkCoords {
private final int x;
private final int z;
private ChunkCoords(Location location) {
this.x = location.getBlockX() >> 4;
this.z = location.getBlockZ() >> 4;
}
private ChunkCoords(int x, int z) {
this.x = x;
this.z = z;
}
@Override
public int hashCode() {
return x << 16 + z;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof ChunkCoords))
return false;
ChunkCoords coords = (ChunkCoords) obj;
return x == coords.x && z == coords.z;
}
}
}
@@ -0,0 +1,41 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.lobby.portal;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.Map;
public interface PortalHandler {
default void handle(Player player, Location to) {
throw new UnsupportedOperationException();
}
default void handle(Player player, Location from, Location to) {
handle(player, to);
}
void serialize(Map<String, Object> map);
PortalType type();
void delete();
}
@@ -0,0 +1,41 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.lobby.portal;
import java.util.Map;
import java.util.function.BiFunction;
public enum PortalType {
TELEPORT(TeleportPortal::new),
COMMAND(CommandPortal::new),
FIGHTSERVER(FightserverPortal::new),
DUMMY(DummyPortal::new),
STACK(StackPortal::new);
private final BiFunction<Map<String, Object>, Portal, PortalHandler> deserializer;
PortalType(BiFunction<Map<String, Object>, Portal, PortalHandler> deserializer) {
this.deserializer = deserializer;
}
public PortalHandler deserialize(Map<String, Object> map, Portal portal) {
return deserializer.apply(map, portal);
}
}
@@ -0,0 +1,57 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.lobby.portal;
import java.util.Map;
public class StackPortal extends TeleportPortal {
private final String text;
public StackPortal(Map<String, Object> section, Portal portal) {
super(section, portal);
this.text = (String) section.get("text");
}
public StackPortal(Portal portal, String target, String text) {
super(portal, target);
this.text = text;
}
@Override
public void serialize(Map<String, Object> map) {
super.serialize(map);
map.put("text", text);
}
@Override
public PortalType type() {
return PortalType.STACK;
}
public String getText() {
return text;
}
@Override
public String toString() {
return "Stack: " + text;
}
}
@@ -0,0 +1,109 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.lobby.portal;
import de.steamwar.lobby.LobbySystem;
import de.steamwar.lobby.command.ModifyCommand;
import de.steamwar.lobby.listener.Portals;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.util.Vector;
import java.util.*;
import java.util.logging.Level;
public class TeleportPortal implements PortalHandler {
private static final List<TeleportPortal> portals = new ArrayList<>();
private final Portal portal;
private final String target;
private final Set<TeleportPortal> sources = new HashSet<>();
public TeleportPortal(Map<String, Object> section, Portal portal) {
this.portal = portal;
this.target = (String) section.get("target");
init();
}
public TeleportPortal(Portal portal, String target) {
this.portal = portal;
this.target = target;
init();
}
private void init() {
portals.stream().filter(p -> p.target.equals(portal.getId())).forEach(sources::add);
portals.stream().filter(p -> p.portal.getId().equals(target)).forEach(p -> p.sources.add(this));
portals.add(this);
}
@Override
public void handle(Player player, Location from, Location to) {
Deque<Portal> stack = Portals.getStack(player);
if(!stack.isEmpty() && sources.contains(stack.peek().getHandler())) {
teleport(player, from, to, stack.pop());
} else {
teleport(player, from, to, Portal.getPortal(target));
}
}
private void teleport(Player player, Location from, Location to, Portal target) {
if(target == null) {
LobbySystem.getPlugin().getLogger().log(Level.WARNING, "Portal with unknown target: " + portal.getId());
player.sendMessage("§cAus unbekannten Gründen führt dieses Portal zurzeit in den Limbus");
return;
}
if(ModifyCommand.modifying(player))
player.sendMessage("teleport " + portal.getId() + " -> " + target.getId());
Vector normalized = portal.normalize(to);
if (target.getDepth() != 0.0) {
normalized.setX(1 - portal.normalize(from).getX());
}
Location portalTo = target.denormalize(normalized).toLocation(to.getWorld(), (float) (to.getYaw() - Math.toDegrees(target.getYrotation() - portal.getYrotation())), to.getPitch());
player.teleport(portalTo, PlayerTeleportEvent.TeleportCause.PLUGIN);
player.playSound(portalTo, Sound.BLOCK_ENCHANTMENT_TABLE_USE, 0.25f, 1.0f);
}
@Override
public void serialize(Map<String, Object> map) {
map.put("target", target);
}
@Override
public PortalType type() {
return PortalType.TELEPORT;
}
@Override
public void delete() {
portals.remove(this);
portals.forEach(p -> p.sources.remove(this));
}
@Override
public String toString() {
return "Teleport: " + target;
}
}