13
0

Portal refactoring

Dieser Commit ist enthalten in:
Lixfel 2021-10-04 11:46:17 +02:00
Ursprung e6eb416e45
Commit 074119c1de
6 geänderte Dateien mit 76 neuen und 54 gelöschten Zeilen

Datei anzeigen

@ -19,8 +19,7 @@
package de.steamwar.lobby; package de.steamwar.lobby;
import de.steamwar.lobby.portal.CommandPortal; import de.steamwar.lobby.portal.Portal;
import de.steamwar.lobby.portal.TeleportPortal;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.configuration.serialization.ConfigurationSerialization;
@ -31,15 +30,12 @@ public class Config {
public Config(FileConfiguration yml) { public Config(FileConfiguration yml) {
this.yml = yml; this.yml = yml;
ConfigurationSerialization.registerClass(CommandPortal.class); ConfigurationSerialization.registerClass(Portal.class);
ConfigurationSerialization.registerClass(TeleportPortal.class); yml.getList("portals", Portal.getPortals());
yml.getList("commandPortals", CommandPortal.getPortals());
yml.getList("teleportPortals", TeleportPortal.getPortals());
} }
public void save() { public void save() {
yml.set("commandPortals", CommandPortal.getPortals()); yml.set("portals", Portal.getPortals());
yml.set("teleportPortals", TeleportPortal.getPortals());
LobbySystem.getPlugin().saveConfig(); LobbySystem.getPlugin().saveConfig();
} }

Datei anzeigen

@ -22,34 +22,21 @@ package de.steamwar.lobby.portal;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
public class CommandPortal extends Portal { public class CommandPortal implements PortalHandler {
private static final List<CommandPortal> portals = new ArrayList<>();
public static List<CommandPortal> getPortals() {
return portals;
}
private final Portal portal;
private final String command; private final String command;
public CommandPortal(Map<String, Object> section) { public CommandPortal(Map<String, Object> section, Portal portal) {
super(section); this.portal = portal;
this.command = (String) section.get("command"); this.command = (String) section.get("command");
init();
} }
public CommandPortal(String id, Location pos1, Location pos2, String command) { public CommandPortal(Portal portal, String command) {
super(id, pos1, pos2); this.portal = portal;
this.command = command; this.command = command;
init();
}
private void init() {
portals.add(this);
} }
@Override @Override
@ -58,15 +45,17 @@ public class CommandPortal extends Portal {
} }
@Override @Override
public Map<String, Object> serialize() { public void serialize(Map<String, Object> map) {
Map<String, Object> map = super.serialize();
map.put("command", command); map.put("command", command);
return map; }
@Override
public PortalType type() {
return PortalType.COMMAND;
} }
@Override @Override
public void delete() { public void delete() {
portals.remove(this); // Nothing to remove
super.delete();
} }
} }

Datei anzeigen

@ -19,5 +19,36 @@
package de.steamwar.lobby.portal; 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<String, Object> section, Portal portal) {
this.portal = portal;
}
@Override
public void handle(Player player, Location from) {
}
@Override
public void serialize(Map<String, Object> map) {
}
@Override
public PortalType type() {
return PortalType.FIGHTSERVER;
}
@Override
public void delete() {
}
} }

Datei anzeigen

@ -24,7 +24,9 @@ import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
@ -33,6 +35,10 @@ public class Portal implements PortalHandler, ConfigurationSerializable {
private static final Map<String, Portal> portals = new HashMap<>(); private static final Map<String, Portal> portals = new HashMap<>();
private static final Map<Vector, Portal> posMap = new HashMap<>(); private static final Map<Vector, Portal> posMap = new HashMap<>();
public static List<Portal> getPortals() {
return new ArrayList<>(portals.values());
}
public static Portal getPortal(Location loc) { public static Portal getPortal(Location loc) {
return posMap.get(new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); return posMap.get(new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
} }

Datei anzeigen

@ -23,9 +23,9 @@ import java.util.Map;
import java.util.function.BiFunction; import java.util.function.BiFunction;
public enum PortalType { public enum PortalType {
TELEPORT(deserializer), TELEPORT(TeleportPortal::new),
COMMAND(deserializer), COMMAND(CommandPortal::new),
FIGHTSERVER(deserializer); FIGHTSERVER(FightserverPortal::new);
private final BiFunction<Map<String, Object>, Portal, PortalHandler> deserializer; private final BiFunction<Map<String, Object>, Portal, PortalHandler> deserializer;

Datei anzeigen

@ -25,31 +25,29 @@ import org.bukkit.entity.Player;
import java.util.*; import java.util.*;
public class TeleportPortal extends Portal { public class TeleportPortal implements PortalHandler {
private static final List<TeleportPortal> portals = new ArrayList<>(); private static final List<TeleportPortal> portals = new ArrayList<>();
public static List<TeleportPortal> getPortals() { private final Portal portal;
return portals;
}
private final Set<TeleportPortal> sources = new HashSet<>();
private final String target; private final String target;
protected TeleportPortal(Map<String, Object> section) { private final Set<TeleportPortal> sources = new HashSet<>();
super(section);
protected TeleportPortal(Map<String, Object> section, Portal portal) {
this.portal = portal;
this.target = (String) section.get("target"); this.target = (String) section.get("target");
init(); init();
} }
protected TeleportPortal(String id, Location pos1, Location pos2, String target) { protected TeleportPortal(Portal portal, String target) {
super(id, pos1, pos2); this.portal = portal;
this.target = target; this.target = target;
init(); init();
} }
private void 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); portals.add(this);
} }
@ -59,25 +57,27 @@ public class TeleportPortal extends Portal {
if(!stack.isEmpty() && sources.contains(stack.peek())) { if(!stack.isEmpty() && sources.contains(stack.peek())) {
teleport(player, loc, stack.pop()); teleport(player, loc, stack.pop());
} else { } else {
teleport(player, loc, getPortal(target)); teleport(player, loc, Portal.getPortal(target));
} }
} }
protected void teleport(Player player, Location from, Portal 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 @Override
public Map<String, Object> serialize() { public void serialize(Map<String, Object> map) {
Map<String, Object> map = super.serialize();
map.put("target", target); map.put("target", target);
return map; }
@Override
public PortalType type() {
return PortalType.TELEPORT;
} }
@Override @Override
public void delete() { public void delete() {
portals.remove(this); portals.remove(this);
portals.forEach(portal -> portal.sources.remove(this)); portals.forEach(p -> p.sources.remove(this));
super.delete();
} }
} }