Skip to content

Commit

Permalink
Make StargateCreateEvent accept OfflinePlayers
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorinwasher committed Jun 3, 2024
1 parent 8416ded commit 7edc790
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -47,7 +48,7 @@ public interface BlockHandlerInterface {
* @param portal The affected portal
* @return Whether to claim the block
*/
boolean registerBlock(Location blockLocation, @Nullable Player player, Portal portal, Metadata metaData);
boolean registerBlock(Location blockLocation, @Nullable OfflinePlayer player, Portal portal, Metadata metaData);

/**
* Called if a Stargate is removed, and the add-on has registered this location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void removeBlockHandlerInterfaces(Plugin plugin) {
* @param material The material of the block
* @param player The player that placed the block
*/
public void registerPlacement(@NotNull RegistryAPI registry,@NotNull Location location,@NotNull List<RealPortal> portals,@NotNull Material material, Player player) {
public void registerPlacement(@NotNull RegistryAPI registry,@NotNull Location location,@NotNull List<RealPortal> portals,@NotNull Material material, OfflinePlayer player) {
if (!blockHandlerMap.containsKey(material)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.sgrewritten.stargate.api.event.portal;

import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.sgrewritten.stargate.api.network.portal.Portal;
Expand All @@ -12,10 +14,12 @@
* <p>This event can be used to deny or change the cost of a stargate creation.</p>
*/
@SuppressWarnings("unused")
public class StargateCreatePortalEvent extends DeniableStargatePortalEvent {
public class StargateCreatePortalEvent extends StargatePortalEvent{

private static final HandlerList handlers = new HandlerList();
private final String[] lines;
private boolean deny;
private String denyReason;
private double cost;

/**
Expand All @@ -30,12 +34,13 @@ public class StargateCreatePortalEvent extends DeniableStargatePortalEvent {
* @param denyReason <p>The reason stargate creation was denied</p>
* @param cost <p>The cost of creating the new star gate</p>
*/
public StargateCreatePortalEvent(@NotNull Player player, @NotNull Portal portal, @NotNull String[] lines, boolean deny,
public StargateCreatePortalEvent(@NotNull OfflinePlayer player, @NotNull Portal portal, @NotNull String[] lines, boolean deny,
String denyReason, double cost) {
super(portal, player, deny, denyReason, false);

super(portal, false);
this.lines = lines;
this.cost = cost;
this.deny = deny;
this.denyReason = denyReason;
}

/**
Expand Down Expand Up @@ -100,4 +105,43 @@ public HandlerList getHandlers() {
return handlers;
}



/**
* Gets whether the entity should be denied access
*
* @return <p>Whether the entity should be denied access</p>
*/
public boolean getDeny() {
return this.deny;
}

/**
* Sets whether to deny access to the entity
*
* @param deny <p>Whether to deny access to the entity</p>
*/
public void setDeny(boolean deny) {
this.deny = deny;
}

/**
* Gets the reason the stargate access was denied
*
* @return <p>The reason the stargate access was denied</p>
*/
public String getDenyReason() {
return denyReason;
}

/**
* Sets the reason the stargate access was denied
*
* <p>Set to null for a generic message. Set to empty for no message.</p>
*
* @param denyReason <p>The new reason why the stargate access was denied</p>
*/
public void setDenyReason(String denyReason) {
this.denyReason = denyReason;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class PortalBuilder {
private double cost = 0;
private @Nullable String destinationName;
private @Nullable String serverName;
private @Nullable Player eventTarget;
private @Nullable OfflinePlayer eventTarget;
private @Nullable Entity messageTarget;
private @Nullable Player economyTarget;
private boolean adaptiveGatePositionGeneration = false;
Expand Down Expand Up @@ -123,7 +123,7 @@ public PortalBuilder setCost(double cost, @NotNull Player economyTarget) {
* @param eventTarget <p>The target of the event</p>
* @return <p>This portal builder</p>
*/
public PortalBuilder addEventHandling(@Nullable Player eventTarget) {
public PortalBuilder addEventHandling(@Nullable OfflinePlayer eventTarget) {
this.eventTarget = eventTarget;
return this;
}
Expand Down Expand Up @@ -421,7 +421,9 @@ private void permissionAndEventHandling(RealPortal portal, Network network) thro
} else if (!portalCreateEvent.getDenyReason().isEmpty()) {
message = portalCreateEvent.getDenyReason();
}
MessageUtils.sendMessage(eventTarget, message);
if(eventTarget instanceof Player player) {
MessageUtils.sendMessage(player, message);
}
throw new LocalisedMessageException(message, portal, MessageType.DENY);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
Expand All @@ -24,7 +25,7 @@ public class BlockHandlerInterfaceMock implements BlockHandlerInterface {
private final Plugin plugin;
private final Priority priority;
private final PortalFlag flag;
private final Map<BlockLocation, TwoTuple<Player, Portal>> registeredBlocks = new HashMap<>();
private final Map<BlockLocation, TwoTuple<OfflinePlayer, Portal>> registeredBlocks = new HashMap<>();
private boolean isRegisterPlacedBlock = true;

public BlockHandlerInterfaceMock(PositionType interfaceType, Material handledMaterial, Plugin plugin, Priority priority, Character flag) {
Expand Down Expand Up @@ -62,7 +63,7 @@ public BlockHandlerInterfaceMock(PositionType interfaceType, Material handledMat
}

@Override
public boolean registerBlock(Location blockLocation, @Nullable Player player, Portal portal, Metadata data) {
public boolean registerBlock(Location blockLocation, @Nullable OfflinePlayer player, Portal portal, Metadata data) {
if (isRegisterPlacedBlock) {
registeredBlocks.put(new BlockLocation(blockLocation), new TwoTuple<>(player, portal));
}
Expand All @@ -74,7 +75,7 @@ public void unRegisterBlock(Location blockLocation, Portal portal) {
registeredBlocks.remove(new BlockLocation(blockLocation));
}

public boolean blockIsRegistered(Location blockLocation, @Nullable Player player, Portal portal) {
public boolean blockIsRegistered(Location blockLocation, @Nullable OfflinePlayer player, Portal portal) {
BlockLocation key = new BlockLocation(blockLocation);
return registeredBlocks.containsKey(key) && registeredBlocks.get(key).getFirstValue() == player
&& registeredBlocks.get(key).getSecondValue() == portal;
Expand Down

0 comments on commit 7edc790

Please sign in to comment.