diff --git a/src/main/java/org/sgrewritten/stargate/api/event/StargatePortalBuilderEvent.java b/src/main/java/org/sgrewritten/stargate/api/event/StargatePortalBuilderEvent.java new file mode 100644 index 00000000..0b6fecbd --- /dev/null +++ b/src/main/java/org/sgrewritten/stargate/api/event/StargatePortalBuilderEvent.java @@ -0,0 +1,105 @@ +package org.sgrewritten.stargate.api.event; + +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.sgrewritten.stargate.api.gate.GateBuilder; +import org.sgrewritten.stargate.api.network.PortalBuilder; + +import java.util.Objects; + +/** + *

Is triggered before the {@link org.sgrewritten.stargate.api.event.portal.StargateCreatePortalEvent}.

+ */ +public class StargatePortalBuilderEvent extends Event { + private static final HandlerList handlerList = new HandlerList(); + private final PortalBuilder portalBuilder; + private final GateBuilder gateBuilder; + private final String[] args; + private final Player player; + + + /** + * + * @param portalBuilder

The portal builder that is going to build the portal

+ * @param gateBuilder

The gate builder which is going to build the gate

+ * @param args

The sign arguments, has to have an length of aat least 4

+ * @param player

The player that initiated the event

+ */ + public StargatePortalBuilderEvent(@NotNull PortalBuilder portalBuilder, @NotNull GateBuilder gateBuilder, @NotNull String[] args, @Nullable Player player) { + this.portalBuilder = Objects.requireNonNull(portalBuilder); + this.gateBuilder = Objects.requireNonNull(gateBuilder); + if (args.length < 4) { + throw new IllegalArgumentException("Expected at least 4 input arguments"); + } + this.args = Objects.requireNonNull(args); + this.player = player; + } + + public @NotNull PortalBuilder getPortalBuilder() { + return this.portalBuilder; + } + + public @NotNull GateBuilder getGateBuilder() { + return this.gateBuilder; + } + + /** + * @return

The name for portal in the portal creation attempt

+ */ + public @NotNull String getPortalName() { + return args[0]; + } + + /** + * The name of the destination, might be empty if no destination was selected + * + * @return

The name of the destination

+ */ + public @NotNull String getDestinationName() { + return args[1]; + } + + /** + * Do note that this network name will be heavily modified from permission checks. + * + * @return

The initial name of the network

+ */ + public @NotNull String getNetworkName() { + return args[2]; + } + + /** + * Note that the resulting flags might heavily be changed from this string. Any string + * matching the pattern (\{.*?\}) will be ignored, but can be used for addons as flag + * arguments. + * + * @return

The initial string representing flags

+ */ + public @NotNull String getFlagString() { + return args[3]; + } + + /** + * @return

The player that initiated the event

+ */ + public @Nullable Player getPlayer() { + return player; + } + + @Override + public @NotNull HandlerList getHandlers() { + return handlerList; + } + + /** + * Gets a handler-list containing all event handlers + * + * @return

A handler-list with all event handlers

+ */ + public static HandlerList getHandlerList() { + return handlerList; + } +} diff --git a/src/main/java/org/sgrewritten/stargate/listener/BlockEventListener.java b/src/main/java/org/sgrewritten/stargate/listener/BlockEventListener.java index 622b469b..93b1049b 100644 --- a/src/main/java/org/sgrewritten/stargate/listener/BlockEventListener.java +++ b/src/main/java/org/sgrewritten/stargate/listener/BlockEventListener.java @@ -37,6 +37,7 @@ import org.sgrewritten.stargate.api.BlockHandlerResolver; import org.sgrewritten.stargate.api.StargateAPI; import org.sgrewritten.stargate.api.config.ConfigurationOption; +import org.sgrewritten.stargate.api.event.StargatePortalBuilderEvent; import org.sgrewritten.stargate.api.event.portal.message.MessageType; import org.sgrewritten.stargate.api.formatting.LanguageManager; import org.sgrewritten.stargate.api.formatting.TranslatableMessage; @@ -182,10 +183,12 @@ public void onSignChange(SignChangeEvent event) { try { GateBuilder gateBuilder = new ImplicitGateBuilder(event.getBlock().getLocation(), registry); - PortalBuilder portalBuilder = new PortalBuilder(stargateAPI, player, portalName).setGateBuilder(gateBuilder).setFlags(flagsString); - portalBuilder.setNetwork(networkOrServerName); + PortalBuilder portalBuilder = new PortalBuilder(stargateAPI, player, portalName).setFlags(flagsString); + portalBuilder.setNetwork(networkOrServerName).setGateBuilder(gateBuilder); portalBuilder.addEventHandling(player).addMessageReceiver(player).addPermissionCheck(player).setCost(ConfigurationHelper.getDouble(ConfigurationOption.CREATION_COST), player); portalBuilder.setDestination(destinationName).setAdaptiveGatePositionGeneration(true).setDestinationServerName(networkOrServerName); + StargatePortalBuilderEvent builderEvent = new StargatePortalBuilderEvent(portalBuilder, gateBuilder, event.getLines(), player); + builderEvent.callEvent(); portalBuilder.build(); } catch (NoFormatFoundException noFormatFoundException) { Stargate.log(Level.FINER, "No Gate format matches");