use of com.palmergames.bukkit.towny.event.plot.toggle.PlotToggleFireEvent in project Towny by TownyAdvanced.
the class PlotCommand method plotToggle.
/**
* Toggle the plots flags for pvp/explosion/fire/mobs (if town/world
* permissions allow)
*
* @param player - Player.
* @param townBlock - TownBlock object.
* @param split - Current command arguments.
*/
public void plotToggle(Player player, TownBlock townBlock, String[] split) {
TownyPermissionSource permSource = TownyUniverse.getInstance().getPermissionSource();
if (split.length == 0 || split[0].equalsIgnoreCase("?")) {
HelpMenu.PLOT_TOGGLE.send(player);
} else {
try {
if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_TOGGLE.getNode(split[0].toLowerCase())))
throw new TownyException(Translatable.of("msg_err_command_disable"));
Town town = townBlock.getTownOrNull();
if (town == null)
throw new TownyException(Translatable.of("msg_not_claimed_1"));
Optional<Boolean> choice = Optional.empty();
if (split.length == 2) {
choice = BaseCommand.parseToggleChoice(split[1]);
}
if (split[0].equalsIgnoreCase("pvp")) {
// Make sure we are allowed to set these permissions.
toggleTest(player, townBlock, StringMgmt.join(split, " "));
if (TownySettings.getPVPCoolDownTime() > 0 && !permSource.isTownyAdmin(player)) {
// Test to see if the pvp cooldown timer is active for the town this plot belongs to.
if (CooldownTimerTask.hasCooldown(town.getUUID().toString(), CooldownType.PVP))
throw new TownyException(Translatable.of("msg_err_cannot_toggle_pvp_x_seconds_remaining", CooldownTimerTask.getCooldownRemaining(town.getUUID().toString(), CooldownType.PVP)));
// Test to see if the pvp cooldown timer is active for this plot.
if (CooldownTimerTask.hasCooldown(townBlock.getWorldCoord().toString(), CooldownType.PVP))
throw new TownyException(Translatable.of("msg_err_cannot_toggle_pvp_x_seconds_remaining", CooldownTimerTask.getCooldownRemaining(townBlock.getWorldCoord().toString(), CooldownType.PVP)));
}
// Prevent plot pvp from being enabled if admin pvp is disabled
if (town.isAdminDisabledPVP() && !townBlock.getPermissions().pvp)
throw new TownyException(Translatable.of("msg_err_admin_controlled_pvp_prevents_you_from_changing_pvp", "adminDisabledPVP", "on"));
// Prevent plot pvp from being disabled if admin pvp is enabled
if (town.isAdminEnabledPVP() && townBlock.getPermissions().pvp)
throw new TownyException(Translatable.of("msg_err_admin_controlled_pvp_prevents_you_from_changing_pvp", "adminEnabledPVP", "off"));
if (TownySettings.getOutsidersPreventPVPToggle() && choice.orElse(!townBlock.getPermissions().pvp)) {
for (Player target : Bukkit.getOnlinePlayers()) {
if (!town.hasResident(target) && !player.getName().equals(target.getName()) && townBlock.getWorldCoord().equals(WorldCoord.parseWorldCoord(target)))
throw new TownyException(Translatable.of("msg_cant_toggle_pvp_outsider_in_plot"));
}
}
// Fire cancellable event directly before setting the toggle.
PlotTogglePvpEvent plotTogglePvpEvent = new PlotTogglePvpEvent(townBlock, player, choice.orElse(!townBlock.getPermissions().pvp));
Bukkit.getPluginManager().callEvent(plotTogglePvpEvent);
if (plotTogglePvpEvent.isCancelled())
throw new TownyException(plotTogglePvpEvent.getCancellationMsg());
townBlock.getPermissions().pvp = choice.orElse(!townBlock.getPermissions().pvp);
// Add a cooldown timer for this plot.
if (TownySettings.getPVPCoolDownTime() > 0 && !permSource.isTownyAdmin(player))
CooldownTimerTask.addCooldownTimer(townBlock.getWorldCoord().toString(), CooldownType.PVP);
TownyMessaging.sendMsg(player, Translatable.of("msg_changed_pvp", "Plot", townBlock.getPermissions().pvp ? Translatable.of("enabled") : Translatable.of("disabled")));
} else if (split[0].equalsIgnoreCase("explosion")) {
// Make sure we are allowed to set these permissions.
toggleTest(player, townBlock, StringMgmt.join(split, " "));
// Fire cancellable event directly before setting the toggle.
PlotToggleExplosionEvent plotToggleExplosionEvent = new PlotToggleExplosionEvent(townBlock, player, choice.orElse(!townBlock.getPermissions().explosion));
Bukkit.getPluginManager().callEvent(plotToggleExplosionEvent);
if (plotToggleExplosionEvent.isCancelled())
throw new TownyException(plotToggleExplosionEvent.getCancellationMsg());
townBlock.getPermissions().explosion = choice.orElse(!townBlock.getPermissions().explosion);
TownyMessaging.sendMsg(player, Translatable.of("msg_changed_expl", "the Plot", townBlock.getPermissions().explosion ? Translatable.of("enabled") : Translatable.of("disabled")));
} else if (split[0].equalsIgnoreCase("fire")) {
// Make sure we are allowed to set these permissions.
toggleTest(player, townBlock, StringMgmt.join(split, " "));
// Fire cancellable event directly before setting the toggle.
PlotToggleFireEvent plotToggleFireEvent = new PlotToggleFireEvent(townBlock, player, choice.orElse(!townBlock.getPermissions().fire));
Bukkit.getPluginManager().callEvent(plotToggleFireEvent);
if (plotToggleFireEvent.isCancelled())
throw new TownyException(plotToggleFireEvent.getCancellationMsg());
townBlock.getPermissions().fire = choice.orElse(!townBlock.getPermissions().fire);
TownyMessaging.sendMsg(player, Translatable.of("msg_changed_fire", "the Plot", townBlock.getPermissions().fire ? Translatable.of("enabled") : Translatable.of("disabled")));
} else if (split[0].equalsIgnoreCase("mobs")) {
// Make sure we are allowed to set these permissions.
toggleTest(player, townBlock, StringMgmt.join(split, " "));
// Fire cancellable event directly before setting the toggle.
PlotToggleMobsEvent plotToggleMobsEvent = new PlotToggleMobsEvent(townBlock, player, choice.orElse(!townBlock.getPermissions().mobs));
Bukkit.getPluginManager().callEvent(plotToggleMobsEvent);
if (plotToggleMobsEvent.isCancelled())
throw new TownyException(plotToggleMobsEvent.getCancellationMsg());
townBlock.getPermissions().mobs = choice.orElse(!townBlock.getPermissions().mobs);
TownyMessaging.sendMsg(player, Translatable.of("msg_changed_mobs", "the Plot", townBlock.getPermissions().mobs ? Translatable.of("enabled") : Translatable.of("disabled")));
} else if (TownyCommandAddonAPI.hasCommand(CommandType.PLOT_TOGGLE, split[0])) {
TownyCommandAddonAPI.getAddonCommand(CommandType.PLOT_TOGGLE, split[0]).execute(player, "plot", split);
} else {
TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_invalid_property", "plot"));
return;
}
townBlock.setChanged(true);
// Change settings event
TownBlockSettingsChangedEvent event = new TownBlockSettingsChangedEvent(townBlock);
Bukkit.getServer().getPluginManager().callEvent(event);
} catch (TownyException e) {
TownyMessaging.sendErrorMsg(player, e.getMessage(player));
}
townBlock.save();
}
}
use of com.palmergames.bukkit.towny.event.plot.toggle.PlotToggleFireEvent in project Towny by TownyAdvanced.
the class PlotCommand method plotGroupToggle.
/**
* Toggle the plot group flags for pvp/explosion/fire/mobs (if town/world
* permissions allow)
*
* @param player - Player.
* @param plotGroup - PlotObjectGroup object.
* @param split - Current command arguments.
*/
public void plotGroupToggle(Player player, PlotGroup plotGroup, String[] split) {
TownyPermissionSource permSource = TownyUniverse.getInstance().getPermissionSource();
if (split.length == 0 || split[0].equalsIgnoreCase("?")) {
HelpMenu.PLOT_GROUP_TOGGLE.send(player);
} else {
try {
// We need to keep an ending string to show the message only after the transaction is over,
// to prevent chat log spam.
Translatable endingMessage = null;
Optional<Boolean> choice = Optional.empty();
if (split.length == 2) {
choice = BaseCommand.parseToggleChoice(split[1]);
}
for (TownBlock groupBlock : plotGroup.getTownBlocks()) {
Town town = groupBlock.getTownOrNull();
if (town == null)
continue;
if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_TOGGLE.getNode(split[0].toLowerCase())))
throw new TownyException(Translatable.of("msg_err_command_disable"));
if (split[0].equalsIgnoreCase("pvp")) {
// Make sure we are allowed to set these permissions.
toggleTest(player, groupBlock, StringMgmt.join(split, " "));
if (TownySettings.getPVPCoolDownTime() > 0 && !permSource.isTownyAdmin(player)) {
// Test to see if the pvp cooldown timer is active for the town this plot belongs to.
if (CooldownTimerTask.hasCooldown(town.getUUID().toString(), CooldownType.PVP))
throw new TownyException(Translatable.of("msg_err_cannot_toggle_pvp_x_seconds_remaining", CooldownTimerTask.getCooldownRemaining(town.getUUID().toString(), CooldownType.PVP)));
// Test to see if the pvp cooldown timer is active for this plot.
if (CooldownTimerTask.hasCooldown(groupBlock.getWorldCoord().toString(), CooldownType.PVP))
throw new TownyException(Translatable.of("msg_err_cannot_toggle_pvp_x_seconds_remaining", CooldownTimerTask.getCooldownRemaining(groupBlock.getWorldCoord().toString(), CooldownType.PVP)));
}
if (TownySettings.getOutsidersPreventPVPToggle() && choice.orElse(!groupBlock.getPermissions().pvp)) {
for (Player target : Bukkit.getOnlinePlayers()) {
if (!town.hasResident(target) && !player.getName().equals(target.getName()) && groupBlock.getWorldCoord().equals(WorldCoord.parseWorldCoord(target)))
throw new TownyException(Translatable.of("msg_cant_toggle_pvp_outsider_in_plot"));
}
}
PlotTogglePvpEvent plotTogglePvpEvent = new PlotTogglePvpEvent(groupBlock, player, choice.orElse(!groupBlock.getPermissions().pvp));
Bukkit.getPluginManager().callEvent(plotTogglePvpEvent);
if (plotTogglePvpEvent.isCancelled()) {
TownyMessaging.sendErrorMsg(player, plotTogglePvpEvent.getCancellationMsg());
return;
}
groupBlock.getPermissions().pvp = choice.orElse(!groupBlock.getPermissions().pvp);
// Add a cooldown timer for this plot.
if (TownySettings.getPVPCoolDownTime() > 0 && !permSource.isTownyAdmin(player))
CooldownTimerTask.addCooldownTimer(groupBlock.getWorldCoord().toString(), CooldownType.PVP);
endingMessage = Translatable.of("msg_changed_pvp", Translatable.of("msg_the_plot_group"), groupBlock.getPermissions().pvp ? Translatable.of("enabled") : Translatable.of("disabled"));
} else if (split[0].equalsIgnoreCase("explosion")) {
// Make sure we are allowed to set these permissions.
toggleTest(player, groupBlock, StringMgmt.join(split, " "));
PlotToggleExplosionEvent plotToggleExplosionEvent = new PlotToggleExplosionEvent(groupBlock, player, choice.orElse(!groupBlock.getPermissions().explosion));
Bukkit.getPluginManager().callEvent(plotToggleExplosionEvent);
if (plotToggleExplosionEvent.isCancelled()) {
TownyMessaging.sendErrorMsg(player, plotToggleExplosionEvent.getCancellationMsg());
return;
}
groupBlock.getPermissions().explosion = choice.orElse(!groupBlock.getPermissions().explosion);
endingMessage = Translatable.of("msg_changed_expl", Translatable.of("msg_the_plot_group"), groupBlock.getPermissions().explosion ? Translatable.of("enabled") : Translatable.of("disabled"));
} else if (split[0].equalsIgnoreCase("fire")) {
// Make sure we are allowed to set these permissions.
toggleTest(player, groupBlock, StringMgmt.join(split, " "));
PlotToggleFireEvent plotToggleFireEvent = new PlotToggleFireEvent(groupBlock, player, choice.orElse(!groupBlock.getPermissions().fire));
Bukkit.getPluginManager().callEvent(plotToggleFireEvent);
if (plotToggleFireEvent.isCancelled()) {
TownyMessaging.sendErrorMsg(player, plotToggleFireEvent.getCancellationMsg());
return;
}
groupBlock.getPermissions().fire = choice.orElse(!groupBlock.getPermissions().fire);
endingMessage = Translatable.of("msg_changed_fire", Translatable.of("msg_the_plot_group"), groupBlock.getPermissions().fire ? Translatable.of("enabled") : Translatable.of("disabled"));
} else if (split[0].equalsIgnoreCase("mobs")) {
// Make sure we are allowed to set these permissions.
toggleTest(player, groupBlock, StringMgmt.join(split, " "));
PlotToggleMobsEvent plotToggleMobsEvent = new PlotToggleMobsEvent(groupBlock, player, choice.orElse(!groupBlock.getPermissions().mobs));
Bukkit.getPluginManager().callEvent(plotToggleMobsEvent);
if (plotToggleMobsEvent.isCancelled()) {
TownyMessaging.sendErrorMsg(player, plotToggleMobsEvent.getCancellationMsg());
return;
}
groupBlock.getPermissions().mobs = choice.orElse(!groupBlock.getPermissions().mobs);
endingMessage = Translatable.of("msg_changed_mobs", Translatable.of("msg_the_plot_group"), groupBlock.getPermissions().mobs ? Translatable.of("enabled") : Translatable.of("disabled"));
} else {
TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_invalid_property", "plot"));
return;
}
groupBlock.setChanged(true);
// Change settings event
TownBlockSettingsChangedEvent event = new TownBlockSettingsChangedEvent(groupBlock);
Bukkit.getServer().getPluginManager().callEvent(event);
// Save
groupBlock.save();
}
// Finally send the message.
if (endingMessage != null)
TownyMessaging.sendMsg(player, endingMessage);
} catch (TownyException e) {
TownyMessaging.sendErrorMsg(player, e.getMessage(player));
} catch (Exception e) {
TownyMessaging.sendErrorMsg(player, e.getMessage());
}
}
}
Aggregations