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;
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();
}

Datei anzeigen

@ -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<CommandPortal> portals = new ArrayList<>();
public static List<CommandPortal> getPortals() {
return portals;
}
public class CommandPortal implements PortalHandler {
private final Portal portal;
private final String command;
public CommandPortal(Map<String, Object> section) {
super(section);
public CommandPortal(Map<String, Object> 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<String, Object> serialize() {
Map<String, Object> map = super.serialize();
public void serialize(Map<String, Object> 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
}
}

Datei anzeigen

@ -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<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.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<String, Portal> portals = 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) {
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;
public enum PortalType {
TELEPORT(deserializer),
COMMAND(deserializer),
FIGHTSERVER(deserializer);
TELEPORT(TeleportPortal::new),
COMMAND(CommandPortal::new),
FIGHTSERVER(FightserverPortal::new);
private final BiFunction<Map<String, Object>, Portal, PortalHandler> deserializer;

Datei anzeigen

@ -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<TeleportPortal> portals = new ArrayList<>();
public static List<TeleportPortal> getPortals() {
return portals;
}
private final Set<TeleportPortal> sources = new HashSet<>();
private final Portal portal;
private final String target;
protected TeleportPortal(Map<String, Object> section) {
super(section);
private final Set<TeleportPortal> sources = new HashSet<>();
protected TeleportPortal(Map<String, Object> 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<String, Object> serialize() {
Map<String, Object> map = super.serialize();
public void serialize(Map<String, Object> 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));
}
}