|
| 1 | +package org.mvplugins.multiverse.portals; |
| 2 | + |
| 3 | +import org.bukkit.Material; |
| 4 | +import org.bukkit.entity.Player; |
| 5 | +import org.mvplugins.multiverse.core.MultiverseCoreApi; |
| 6 | +import org.mvplugins.multiverse.core.config.node.ConfigNode; |
| 7 | +import org.mvplugins.multiverse.core.config.node.Node; |
| 8 | +import org.mvplugins.multiverse.core.config.node.NodeGroup; |
| 9 | +import org.mvplugins.multiverse.core.destination.DestinationInstance; |
| 10 | +import org.mvplugins.multiverse.core.destination.DestinationsProvider; |
| 11 | +import org.mvplugins.multiverse.core.exceptions.MultiverseException; |
| 12 | +import org.mvplugins.multiverse.external.vavr.control.Try; |
| 13 | +import org.mvplugins.multiverse.portals.utils.MultiverseRegion; |
| 14 | + |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +final class MVPortalNodes { |
| 19 | + |
| 20 | + private final NodeGroup nodes = new NodeGroup(); |
| 21 | + |
| 22 | + private MultiversePortals plugin; |
| 23 | + private MVPortal portal; |
| 24 | + private DestinationsProvider destinationsProvider; |
| 25 | + |
| 26 | + MVPortalNodes(MultiversePortals plugin, MVPortal portal) { |
| 27 | + this.plugin = plugin; |
| 28 | + this.portal = portal; |
| 29 | + this.destinationsProvider = MultiverseCoreApi.get().getDestinationsProvider(); |
| 30 | + } |
| 31 | + |
| 32 | + NodeGroup getNodes() { |
| 33 | + return nodes; |
| 34 | + } |
| 35 | + |
| 36 | + private <N extends Node> N node(N node) { |
| 37 | + nodes.add(node); |
| 38 | + return node; |
| 39 | + } |
| 40 | + |
| 41 | + final ConfigNode<Material> currency = node(ConfigNode.builder("currency", Material.class) |
| 42 | + .defaultValue(Material.AIR) |
| 43 | + .aliases("curr") |
| 44 | + .build()); |
| 45 | + |
| 46 | + final ConfigNode<Double> price = node(ConfigNode.builder("price", Double.class) |
| 47 | + .defaultValue(0.0) |
| 48 | + .build()); |
| 49 | + |
| 50 | + final ConfigNode<Boolean> safeTeleport = node(ConfigNode.builder("safe-teleport", Boolean.class) |
| 51 | + .defaultValue(true) |
| 52 | + .aliases("safe") |
| 53 | + .build()); |
| 54 | + |
| 55 | + final ConfigNode<Boolean> teleportNonPlayers = node(ConfigNode.builder("teleport-non-players", Boolean.class) |
| 56 | + .defaultValue(false) |
| 57 | + .aliases("telenonplayers") |
| 58 | + .build()); |
| 59 | + |
| 60 | + final ConfigNode<String> owner = node(ConfigNode.builder("owner", String.class) |
| 61 | + .defaultValue("") |
| 62 | + .build()); |
| 63 | + |
| 64 | + final ConfigNode<String> location = node(ConfigNode.builder("location", String.class) |
| 65 | + .defaultValue("") |
| 66 | + .aliases("loc") |
| 67 | + .suggester((sender, input) -> { |
| 68 | + if (sender instanceof Player player && plugin.getPortalSession(player).getSelectedRegion() != null) { |
| 69 | + return List.of("@selected-region"); |
| 70 | + } |
| 71 | + return Collections.emptyList(); |
| 72 | + }) |
| 73 | + .stringParser((sender, input, type) -> { |
| 74 | + if (input.equals("@selected-region")) { |
| 75 | + if (!(sender instanceof Player player)) { |
| 76 | + return Try.failure(new MultiverseException("You can only use '@selected-region' as a player.")); |
| 77 | + } |
| 78 | + MultiverseRegion region = plugin.getPortalSession(player).getSelectedRegion(); |
| 79 | + if (region == null) { |
| 80 | + return Try.failure(new MultiverseException("You must select a region first. See `/mvp wand` for more info.")); |
| 81 | + } |
| 82 | + return Try.success(region.toString()); |
| 83 | + } |
| 84 | + PortalLocation portalLocation = PortalLocation.parseLocation(input); |
| 85 | + if (!portalLocation.isValidLocation()) { |
| 86 | + return Try.failure(new MultiverseException("Invalid location format. The portal location must be in the format `WORLD:X,Y,Z:X,Y,Z`.")); |
| 87 | + } |
| 88 | + return Try.success(portalLocation.toString()); |
| 89 | + }) |
| 90 | + .onSetValue((oldValue, newValue) -> portal.setPortalLocationInternal(PortalLocation.parseLocation(newValue))) |
| 91 | + .build()); |
| 92 | + |
| 93 | + final ConfigNode<String> destination = node(ConfigNode.builder("destination", String.class) |
| 94 | + .defaultValue("") |
| 95 | + .aliases("dest") |
| 96 | + .suggester((sender, input) -> destinationsProvider.suggestDestinationStrings(sender, input)) |
| 97 | + .stringParser((sender, input, type) -> destinationsProvider.parseDestination(sender, input) |
| 98 | + .map(DestinationInstance::toString) |
| 99 | + .toTry()) |
| 100 | + .build()); |
| 101 | + |
| 102 | + final ConfigNode<Double> version = node(ConfigNode.builder("version", Double.class) |
| 103 | + .defaultValue(0.0) |
| 104 | + .hidden() |
| 105 | + .build()); |
| 106 | +} |
0 commit comments