Skip to content

Commit

Permalink
Implement StargatePortalBuilderEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorinwasher committed Feb 4, 2024
1 parent 09d2ea2 commit 7a10e26
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p>Is triggered before the {@link org.sgrewritten.stargate.api.event.portal.StargateCreatePortalEvent}.</p>
*/
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 <p>The portal builder that is going to build the portal</p>
* @param gateBuilder <p>The gate builder which is going to build the gate</p>
* @param args <p>The sign arguments, has to have an length of aat least 4</p>
* @param player <p>The player that initiated the event</p>
*/
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 <p>The name for portal in the portal creation attempt</p>
*/
public @NotNull String getPortalName() {
return args[0];
}

/**
* The name of the destination, might be empty if no destination was selected
*
* @return <p>The name of the destination</p>
*/
public @NotNull String getDestinationName() {
return args[1];
}

/**
* Do note that this network name will be heavily modified from permission checks.
*
* @return <p>The initial name of the network</p>
*/
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 <p>The initial string representing flags</p>
*/
public @NotNull String getFlagString() {
return args[3];
}

/**
* @return <p>The player that initiated the event</p>
*/
public @Nullable Player getPlayer() {
return player;
}

@Override
public @NotNull HandlerList getHandlers() {
return handlerList;
}

/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlerList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 7a10e26

Please sign in to comment.