Skip to content

Commit

Permalink
Add plot group creation/deletion events (#7476)
Browse files Browse the repository at this point in the history
  • Loading branch information
Warriorrrr authored Jun 19, 2024
1 parent 770ad6c commit de07e43
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import com.palmergames.bukkit.towny.event.plot.PlotSetForSaleEvent;
import com.palmergames.bukkit.towny.event.plot.PlotTrustAddEvent;
import com.palmergames.bukkit.towny.event.plot.PlotTrustRemoveEvent;
import com.palmergames.bukkit.towny.event.plot.group.PlotGroupAddEvent;
import com.palmergames.bukkit.towny.event.plot.group.PlotGroupCreatedEvent;
import com.palmergames.bukkit.towny.event.plot.group.PlotGroupDeletedEvent;
import com.palmergames.bukkit.towny.event.plot.toggle.PlotToggleExplosionEvent;
import com.palmergames.bukkit.towny.event.plot.toggle.PlotToggleFireEvent;
import com.palmergames.bukkit.towny.event.plot.toggle.PlotToggleMobsEvent;
Expand Down Expand Up @@ -1318,42 +1321,53 @@ public void parsePlotGroupAdd(String[] split, TownBlock townBlock, Player player
Confirmation.runOnAccept( ()-> {
PlotGroup oldGroup = townBlock.getPlotObjectGroup();
oldGroup.removeTownBlock(townBlock);
if (oldGroup.getTownBlocks().isEmpty()) {
if (oldGroup.getTownBlocks().isEmpty() && !BukkitTools.isEventCancelled(new PlotGroupDeletedEvent(oldGroup, player, PlotGroupDeletedEvent.Cause.NO_TOWNBLOCKS))) {
String oldName = oldGroup.getName();
town.removePlotGroup(oldGroup);
TownyUniverse.getInstance().getDataSource().removePlotGroup(oldGroup);
TownyMessaging.sendMsg(player, Translatable.of("msg_plotgroup_deleted", oldName));
} else
oldGroup.save();
createOrAddOnToPlotGroup(townBlock, town, name);
resident.setPlotGroupName(name);
TownyMessaging.sendMsg(player, Translatable.of("msg_townblock_transferred_from_x_to_x_group", oldGroup.getName(), townBlock.getPlotObjectGroup().getName()));

try {
createOrAddOnToPlotGroup(townBlock, town, player, name);
resident.setPlotGroupName(name);
TownyMessaging.sendMsg(player, Translatable.of("msg_townblock_transferred_from_x_to_x_group", oldGroup.getName(), townBlock.getPlotObjectGroup().getName()));
} catch (TownyException e) {
TownyMessaging.sendErrorMsg(player, e.getMessage(player));
}
})
.setTitle(Translatable.of("msg_plot_group_already_exists_did_you_want_to_transfer", townBlock.getPlotObjectGroup().getName(), split[1]))
.sendTo(player);
} else {
// Create a brand new plot group.
createOrAddOnToPlotGroup(townBlock, town, plotGroupName);
createOrAddOnToPlotGroup(townBlock, town, player, plotGroupName);
resident.setPlotGroupName(plotGroupName);
TownyMessaging.sendMsg(player, Translatable.of("msg_plot_was_put_into_group_x", townBlock.getX(), townBlock.getZ(), townBlock.getPlotObjectGroup().getName()));
}
}

private void createOrAddOnToPlotGroup(TownBlock townBlock, Town town, String plotGroupName) {
PlotGroup newGroup = null;
private void createOrAddOnToPlotGroup(TownBlock townBlock, Town town, Player player, String plotGroupName) throws TownyException {
PlotGroup newGroup;

// Don't add the group to the town data if it's already there.
if (town.hasPlotGroupName(plotGroupName)) {
newGroup = town.getPlotObjectGroupFromName(plotGroupName);

BukkitTools.ifCancelledThenThrow(new PlotGroupAddEvent(newGroup, townBlock, player));

townBlock.setPermissions(newGroup.getPermissions().toString());
townBlock.setChanged(!townBlock.getPermissions().toString().equals(town.getPermissions().toString()));
} else {
} else {
// This is a brand new PlotGroup, register it.
newGroup = new PlotGroup(UUID.randomUUID(), plotGroupName, town);
TownyUniverse.getInstance().registerGroup(newGroup);
newGroup.setPermissions(townBlock.getPermissions());
newGroup.setTrustedResidents(townBlock.getTrustedResidents());
newGroup.setPermissionOverrides(townBlock.getPermissionOverrides());

BukkitTools.ifCancelledThenThrow(new PlotGroupCreatedEvent(newGroup, townBlock, player));

TownyUniverse.getInstance().registerGroup(newGroup);
}

// Add group to townblock, this also adds the townblock to the group.
Expand All @@ -1378,9 +1392,11 @@ public void parsePlotGroupDelete(TownBlock townBlock, Player player, Town town)

Confirmation.runOnAccept(()-> {
String name = group.getName();
town.removePlotGroup(group);
TownyUniverse.getInstance().getDataSource().removePlotGroup(group);
TownyMessaging.sendMsg(player, Translatable.of("msg_plotgroup_deleted", name));
if (!BukkitTools.isEventCancelled(new PlotGroupDeletedEvent(group, player, PlotGroupDeletedEvent.Cause.DELETED))) {
town.removePlotGroup(group);
TownyUniverse.getInstance().getDataSource().removePlotGroup(group);
TownyMessaging.sendMsg(player, Translatable.of("msg_plotgroup_deleted", name));
}
}).sendTo(player);
}

Expand Down Expand Up @@ -1483,7 +1499,7 @@ public void parsePlotGroupRemove(TownBlock townBlock, Player player, Town town)
townBlock.save();
TownyMessaging.sendMsg(player, Translatable.of("msg_plot_was_removed_from_group_x", townBlock.getX(), townBlock.getZ(), name));

if (group.getTownBlocks().isEmpty()) {
if (group.getTownBlocks().isEmpty() && !BukkitTools.isEventCancelled(new PlotGroupDeletedEvent(group, player, PlotGroupDeletedEvent.Cause.NO_TOWNBLOCKS))) {
town.removePlotGroup(group);
TownyUniverse.getInstance().getDataSource().removePlotGroup(group);
TownyMessaging.sendMsg(player, Translatable.of("msg_plotgroup_empty_deleted", name));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.palmergames.bukkit.towny.event.plot.group;

import com.palmergames.bukkit.towny.event.CancellableTownyEvent;
import com.palmergames.bukkit.towny.object.PlotGroup;
import com.palmergames.bukkit.towny.object.TownBlock;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

/**
* Called when a townblock is added into a plot group
*/
public class PlotGroupAddEvent extends CancellableTownyEvent {
private static final HandlerList HANDLER_LIST = new HandlerList();
private final PlotGroup plotGroup;
private final TownBlock townBlock;
private final Player player;

public PlotGroupAddEvent(final PlotGroup group, final TownBlock townBlock, final Player player) {
this.plotGroup = group;
this.townBlock = townBlock;
this.player = player;
}

@NotNull
public PlotGroup getPlotGroup() {
return plotGroup;
}

@NotNull
public TownBlock getTownBlock() {
return townBlock;
}

@NotNull
public Player getPlayer() {
return player;
}

@NotNull
public static HandlerList getHandlerList() {
return HANDLER_LIST;
}

@NotNull
@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.palmergames.bukkit.towny.event.plot.group;

import com.palmergames.bukkit.towny.object.PlotGroup;
import com.palmergames.bukkit.towny.object.TownBlock;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

/**
* Called when a plot group is created.
*/
public class PlotGroupCreatedEvent extends PlotGroupAddEvent {
public PlotGroupCreatedEvent(PlotGroup group, TownBlock townBlock, Player player) {
super(group, townBlock, player);
}

/**
* @return The initial townblock that this plot group is being created with.
*/
@Override
@NotNull
public TownBlock getTownBlock() {
return super.getTownBlock();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.palmergames.bukkit.towny.event.plot.group;

import com.palmergames.bukkit.towny.event.CancellableTownyEvent;
import com.palmergames.bukkit.towny.object.PlotGroup;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PlotGroupDeletedEvent extends CancellableTownyEvent {
private static final HandlerList HANDLER_LIST = new HandlerList();
private final PlotGroup plotGroup;
private final Player player;
private final Cause deletionCause;

public PlotGroupDeletedEvent(@NotNull PlotGroup group, @Nullable Player player, @NotNull Cause deletionCause) {
this.plotGroup = group;
this.player = player;
this.deletionCause = deletionCause;
}

/**
* @return The plot group that is being deleted.
*/
@NotNull
public PlotGroup getPlotGroup() {
return plotGroup;
}

/**
* @return The player associated with the deletion, if applicable.
*/
@Nullable
public Player getPlayer() {
return player;
}

public Cause getDeletionCause() {
return deletionCause;
}

public static HandlerList getHandlerList() {
return HANDLER_LIST;
}

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

public enum Cause {
UNKNOWN,
/**
* The plot group was deleted by a player via the /plot group delete command.
*/
DELETED,
/**
* The plot group was deleted because all of its townblocks were removed.
*/
NO_TOWNBLOCKS,
/**
* The plot group was deleted because the town it was in being deleted/ruined.
*/
TOWN_DELETED,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.palmergames.bukkit.towny.confirmations.Confirmation;
import com.palmergames.bukkit.towny.confirmations.ConfirmationTransaction;
import com.palmergames.bukkit.towny.event.DeleteTownEvent;
import com.palmergames.bukkit.towny.event.plot.group.PlotGroupDeletedEvent;
import com.palmergames.bukkit.towny.event.town.TownPreReclaimEvent;
import com.palmergames.bukkit.towny.event.town.TownReclaimedEvent;
import com.palmergames.bukkit.towny.event.town.TownRuinedEvent;
Expand Down Expand Up @@ -124,7 +125,8 @@ public static void putTownIntoRuinedState(Town town) {
// Unregister the now empty plotgroups.
if (town.getPlotGroups() != null)
for (PlotGroup group : new ArrayList<>(town.getPlotGroups()))
TownyUniverse.getInstance().getDataSource().removePlotGroup(group);
if (!BukkitTools.isEventCancelled(new PlotGroupDeletedEvent(group, null, PlotGroupDeletedEvent.Cause.TOWN_DELETED)))
TownyUniverse.getInstance().getDataSource().removePlotGroup(group);

// Check if Town has more residents than it should be allowed (if it were the capital of a nation.)
if (TownySettings.getMaxResidentsPerTown() > 0)
Expand Down

0 comments on commit de07e43

Please sign in to comment.