diff --git a/src/de/steamwar/lobby/Config.java b/src/de/steamwar/lobby/Config.java index de61270..e5c055c 100644 --- a/src/de/steamwar/lobby/Config.java +++ b/src/de/steamwar/lobby/Config.java @@ -19,8 +19,7 @@ package de.steamwar.lobby; -import de.steamwar.lobby.portal.CommandPortal; -import de.steamwar.lobby.portal.TeleportPortal; +import de.steamwar.lobby.portal.Portal; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.serialization.ConfigurationSerialization; @@ -31,15 +30,12 @@ public class Config { 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()); + ConfigurationSerialization.registerClass(Portal.class); + yml.getList("portals", Portal.getPortals()); } public void save() { - yml.set("commandPortals", CommandPortal.getPortals()); - yml.set("teleportPortals", TeleportPortal.getPortals()); + yml.set("portals", Portal.getPortals()); LobbySystem.getPlugin().saveConfig(); } diff --git a/src/de/steamwar/lobby/portal/CommandPortal.java b/src/de/steamwar/lobby/portal/CommandPortal.java index df01cc8..411c2f3 100644 --- a/src/de/steamwar/lobby/portal/CommandPortal.java +++ b/src/de/steamwar/lobby/portal/CommandPortal.java @@ -22,34 +22,21 @@ 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; - } +public class CommandPortal implements PortalHandler { + private final Portal portal; private final String command; - public CommandPortal(Map section) { - super(section); + public CommandPortal(Map section, Portal portal) { + this.portal = portal; this.command = (String) section.get("command"); - init(); } - public CommandPortal(String id, Location pos1, Location pos2, String command) { - super(id, pos1, pos2); + public CommandPortal(Portal portal, String command) { + this.portal = portal; this.command = command; - init(); - } - - private void init() { - portals.add(this); } @Override @@ -58,15 +45,17 @@ public class CommandPortal extends Portal { } @Override - public Map serialize() { - Map map = super.serialize(); + public void serialize(Map map) { map.put("command", command); - return map; + } + + @Override + public PortalType type() { + return PortalType.COMMAND; } @Override public void delete() { - portals.remove(this); - super.delete(); + // Nothing to remove } } diff --git a/src/de/steamwar/lobby/portal/FightserverPortal.java b/src/de/steamwar/lobby/portal/FightserverPortal.java index ae27d56..56f5515 100644 --- a/src/de/steamwar/lobby/portal/FightserverPortal.java +++ b/src/de/steamwar/lobby/portal/FightserverPortal.java @@ -19,5 +19,36 @@ package de.steamwar.lobby.portal; -public class FightserverPortal extends TeleportPortal { +import org.bukkit.Location; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class FightserverPortal implements PortalHandler { + + private final Portal portal; + + public FightserverPortal(Map section, Portal portal) { + this.portal = portal; + } + + @Override + public void handle(Player player, Location from) { + + } + + @Override + public void serialize(Map map) { + + } + + @Override + public PortalType type() { + return PortalType.FIGHTSERVER; + } + + @Override + public void delete() { + + } } diff --git a/src/de/steamwar/lobby/portal/Portal.java b/src/de/steamwar/lobby/portal/Portal.java index 5c1ea03..a3c4eb5 100644 --- a/src/de/steamwar/lobby/portal/Portal.java +++ b/src/de/steamwar/lobby/portal/Portal.java @@ -24,7 +24,9 @@ import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.entity.Player; import org.bukkit.util.Vector; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.function.Function; @@ -33,6 +35,10 @@ public class Portal implements PortalHandler, ConfigurationSerializable { private static final Map portals = new HashMap<>(); private static final Map posMap = new HashMap<>(); + public static List getPortals() { + return new ArrayList<>(portals.values()); + } + public static Portal getPortal(Location loc) { return posMap.get(new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); } diff --git a/src/de/steamwar/lobby/portal/PortalType.java b/src/de/steamwar/lobby/portal/PortalType.java index 39ac52a..f27a8f9 100644 --- a/src/de/steamwar/lobby/portal/PortalType.java +++ b/src/de/steamwar/lobby/portal/PortalType.java @@ -23,9 +23,9 @@ import java.util.Map; import java.util.function.BiFunction; public enum PortalType { - TELEPORT(deserializer), - COMMAND(deserializer), - FIGHTSERVER(deserializer); + TELEPORT(TeleportPortal::new), + COMMAND(CommandPortal::new), + FIGHTSERVER(FightserverPortal::new); private final BiFunction, Portal, PortalHandler> deserializer; diff --git a/src/de/steamwar/lobby/portal/TeleportPortal.java b/src/de/steamwar/lobby/portal/TeleportPortal.java index a5b92d1..a8018bb 100644 --- a/src/de/steamwar/lobby/portal/TeleportPortal.java +++ b/src/de/steamwar/lobby/portal/TeleportPortal.java @@ -25,31 +25,29 @@ import org.bukkit.entity.Player; import java.util.*; -public class TeleportPortal extends Portal { +public class TeleportPortal implements PortalHandler { private static final List portals = new ArrayList<>(); - public static List getPortals() { - return portals; - } - - private final Set sources = new HashSet<>(); + private final Portal portal; private final String target; - protected TeleportPortal(Map section) { - super(section); + private final Set sources = new HashSet<>(); + + protected TeleportPortal(Map section, Portal portal) { + this.portal = portal; this.target = (String) section.get("target"); init(); } - protected TeleportPortal(String id, Location pos1, Location pos2, String target) { - super(id, pos1, pos2); + protected TeleportPortal(Portal portal, String target) { + this.portal = portal; this.target = target; init(); } private void init() { - portals.stream().filter(portal -> portal.target.equals(this.id)).forEach(sources::add); + portals.stream().filter(p -> p.target.equals(portal.getId())).forEach(sources::add); portals.add(this); } @@ -59,25 +57,27 @@ public class TeleportPortal extends Portal { if(!stack.isEmpty() && sources.contains(stack.peek())) { teleport(player, loc, stack.pop()); } else { - teleport(player, loc, getPortal(target)); + teleport(player, loc, Portal.getPortal(target)); } } protected void teleport(Player player, Location from, Portal target) { - player.sendMessage("Symbolisierter teleport zu " + target.id); + player.sendMessage("Symbolisierter teleport zu " + target.getId()); } @Override - public Map serialize() { - Map map = super.serialize(); + public void serialize(Map map) { map.put("target", target); - return map; + } + + @Override + public PortalType type() { + return PortalType.TELEPORT; } @Override public void delete() { portals.remove(this); - portals.forEach(portal -> portal.sources.remove(this)); - super.delete(); + portals.forEach(p -> p.sources.remove(this)); } }