Skip to content

Commit

Permalink
Merge pull request #5432 from TownyAdvanced/feature/spawn-set-events
Browse files Browse the repository at this point in the history
Add events for when a town/nation spawn is set.
  • Loading branch information
LlmDl authored Nov 9, 2021
2 parents c811dd8 + 3b3667c commit ac5ace6
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 10 deletions.
22 changes: 16 additions & 6 deletions src/com/palmergames/bukkit/towny/command/NationCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.palmergames.bukkit.towny.event.NationPreRemoveEnemyEvent;
import com.palmergames.bukkit.towny.event.nation.NationRankAddEvent;
import com.palmergames.bukkit.towny.event.nation.NationRankRemoveEvent;
import com.palmergames.bukkit.towny.event.nation.NationSetSpawnEvent;
import com.palmergames.bukkit.towny.event.nation.NationTownLeaveEvent;
import com.palmergames.bukkit.towny.event.NationRemoveEnemyEvent;
import com.palmergames.bukkit.towny.event.NationRequestAllyNationEvent;
Expand Down Expand Up @@ -2198,7 +2199,7 @@ public static void nationSet(CommandSender sender, String[] split, boolean admin
} else if (split[0].equalsIgnoreCase("spawn")){

if (sender instanceof Player player)
parseNationSetSpawnCommand(player, nation);
parseNationSetSpawnCommand(player, nation, admin);
else
throw new TownyException("Not meant for console!");
}
Expand Down Expand Up @@ -2437,13 +2438,22 @@ else if (split[1].equalsIgnoreCase("clear")) {
}
}

private static void parseNationSetSpawnCommand(Player player, Nation nation) {
try{
Location newSpawn = player.getLocation();

if (TownyAPI.getInstance().isWilderness(newSpawn))
private static void parseNationSetSpawnCommand(Player player, Nation nation, boolean admin) {
try {
if (TownyAPI.getInstance().isWilderness(player.getLocation()))
throw new TownyException(Translatable.of("msg_cache_block_error_wild", "set spawn"));

NationSetSpawnEvent event = new NationSetSpawnEvent(nation, player, player.getLocation());
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled() && !admin) {
if (!event.getCancelMessage().isEmpty())
TownyMessaging.sendErrorMsg(player, event.getCancelMessage());

return;
}

Location newSpawn = admin ? player.getLocation() : event.getNewSpawn();

TownBlock townBlock = TownyAPI.getInstance().getTownBlock(newSpawn);
Town town = townBlock.getTownOrNull();

Expand Down
20 changes: 16 additions & 4 deletions src/com/palmergames/bukkit/towny/command/TownCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.palmergames.bukkit.towny.event.town.TownPreMergeEvent;
import com.palmergames.bukkit.towny.event.town.TownPreSetHomeBlockEvent;
import com.palmergames.bukkit.towny.event.town.TownPreUnclaimCmdEvent;
import com.palmergames.bukkit.towny.event.town.TownSetSpawnEvent;
import com.palmergames.bukkit.towny.event.town.TownTrustAddEvent;
import com.palmergames.bukkit.towny.event.town.TownTrustRemoveEvent;
import com.palmergames.bukkit.towny.event.town.toggle.TownToggleNeutralEvent;
Expand Down Expand Up @@ -2508,7 +2509,7 @@ else if (split[1].equalsIgnoreCase("clear")) {

} else if (split[0].equalsIgnoreCase("spawn")) {

parseTownSetSpawn(player, town);
parseTownSetSpawn(player, town, admin);

} else if (split[0].equalsIgnoreCase("outpost")) {

Expand Down Expand Up @@ -2686,13 +2687,24 @@ private static void parseTownBaltop(Player player, Town town) {
player.openBook(BookFactory.makeBook("Town Baltop", town.getName(), sb.toString()));
}

private static void parseTownSetSpawn(Player player, Town town) {
private static void parseTownSetSpawn(Player player, Town town, boolean admin) {
try {
// Towns can only set their spawn if they have a homeblock.
if (!town.hasHomeBlock())
throw new TownyException(Translatable.of("msg_err_homeblock_has_not_been_set"));

TownBlock tb = TownyAPI.getInstance().getTownBlock(player.getLocation());
TownSetSpawnEvent event = new TownSetSpawnEvent(town, player, player.getLocation());
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled() && !admin) {
if (!event.getCancelMessage().isEmpty())
TownyMessaging.sendErrorMsg(player, event.getCancelMessage());

return;
}

Location newSpawn = admin ? player.getLocation() : event.getNewSpawn();

TownBlock tb = TownyAPI.getInstance().getTownBlock(newSpawn);

// The townblock needs to exist, belong to the town and also be inside of the homeblock.
if (tb == null || !tb.hasTown() || !tb.getTownOrNull().equals(town) || !town.getHomeBlock().getWorldCoord().equals(tb.getWorldCoord()))
Expand All @@ -2703,7 +2715,7 @@ private static void parseTownSetSpawn(Player player, Town town) {
TownyUniverse.getInstance().removeSpawnPoint(town.getSpawnOrNull());

// Set the spawn point and send feedback message.
town.setSpawn(player.getLocation());
town.setSpawn(newSpawn);
TownyMessaging.sendMsg(player, Translatable.of("msg_set_town_spawn"));
} catch (TownyException e) {
TownyMessaging.sendErrorMsg(player, e.getMessage(player));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.palmergames.bukkit.towny.event.nation;

import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Translation;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Fired when a player uses /nation set spawn
*/
public class NationSetSpawnEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();

private final Nation nation;
private final Player player;
private final Location oldSpawn;
private Location newSpawn;

private boolean cancelled = false;
private String cancelMessage = Translation.of("msg_err_command_disable");

public NationSetSpawnEvent(Nation nation, Player player, Location newSpawn) {
this.nation = nation;
this.player = player;
this.oldSpawn = nation.getSpawnOrNull();
this.newSpawn = newSpawn;
}

/**
* @return The nation for which this spawn is being set.
*/
@NotNull
public Nation getNation() {
return nation;
}

/**
* @return The player that is changing the spawn.
*/
@NotNull
public Player getPlayer() {
return player;
}

/**
* @return The old spawn, or {@code null} if none has been set.
*/
@Nullable
public Location getOldSpawn() {
return oldSpawn;
}

/**
* @return The location where the spawn is being set to.
*/
@NotNull
public Location getNewSpawn() {
return newSpawn;
}

/**
* @param newSpawn Sets the location where the new spawn will be set to.
*/
public void setNewSpawn(@NotNull Location newSpawn) {
this.newSpawn = newSpawn;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}

@Override
public boolean isCancelled() {
return cancelled;
}

public void setCancelMessage(String cancelMessage) {
this.cancelMessage = cancelMessage;
}

public String getCancelMessage() {
return cancelMessage;
}

public static HandlerList getHandlerList() {
return handlers;
}

@Override
public @NotNull HandlerList getHandlers() {
return handlers;
}
}
96 changes: 96 additions & 0 deletions src/com/palmergames/bukkit/towny/event/town/TownSetSpawnEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.palmergames.bukkit.towny.event.town;

import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.Translation;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Fired when a player uses /town set spawn
*/
public class TownSetSpawnEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();

private final Town town;
private final Player player;
private final Location oldSpawn;
private Location newSpawn;

private boolean cancelled = false;
private String cancelMessage = Translation.of("msg_err_command_disable");

public TownSetSpawnEvent(Town town, Player player, Location newSpawn) {
this.town = town;
this.player = player;
this.oldSpawn = town.getSpawnOrNull();
this.newSpawn = newSpawn;
}

/**
* @return The town for which this spawn is being set.
*/
@NotNull
public Town getTown() {
return town;
}

/**
* @return The player that ran the command
*/
@NotNull
public Player getPlayer() {
return player;
}

/**
* @return The town's old spawnpoint location.
*/
@Nullable
public Location getOldSpawn() {
return oldSpawn;
}

/**
* @return The new spawn location.
*/
@NotNull
public Location getNewSpawn() {
return newSpawn;
}

public void setNewSpawn(@NotNull Location newSpawn) {
this.newSpawn = newSpawn;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}

public String getCancelMessage() {
return cancelMessage;
}

public void setCancelMessage(String cancelMessage) {
this.cancelMessage = cancelMessage;
}

public static HandlerList getHandlerList() {
return handlers;
}

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

0 comments on commit ac5ace6

Please sign in to comment.