Implement a flag that rules all flags. (#1927)

Add new Protection Flag: CHANGE_SETTINGS. This flag allows set which rank can change island settings. By default, it is set to OWNER rank and minimal value is MEMBER rank.

Fixes #1493
This commit is contained in:
BONNe 2022-01-29 04:45:50 +02:00 committed by GitHub
parent 90ebe103c4
commit 09ab327551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 114 additions and 55 deletions

View File

@ -4,6 +4,8 @@ import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.event.inventory.ClickType;
import java.util.Objects;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.events.flags.FlagProtectionChangeEvent;
@ -13,6 +15,7 @@ import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.TabbedPanel;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.panels.settings.SettingsTab;
import world.bentobox.bentobox.util.Util;
@ -75,7 +78,7 @@ public class CycleClick implements PanelItem.ClickHandler {
// Left clicking increases the rank required
// Right clicking decreases the rank required
// Shift Left Click toggles player visibility
if (island != null && (user.isOp() || user.getUniqueId().equals(island.getOwner()) || user.hasPermission(prefix + "admin.settings"))) {
if (island != null && (user.isOp() || island.isAllowed(user, Flags.CHANGE_SETTINGS) || user.hasPermission(prefix + "admin.settings"))) {
changeOccurred = true;
RanksManager rm = plugin.getRanksManager();
plugin.getFlagsManager().getFlag(id).ifPresent(flag -> {
@ -124,8 +127,16 @@ public class CycleClick implements PanelItem.ClickHandler {
}
});
} else {
// Player is not the owner of the island.
user.sendMessage("general.errors.not-owner");
if (island == null) {
// Island is not targeted.
user.sendMessage("general.errors.not-on-island");
} else {
// Player is not the allowed to change settings.
user.sendMessage("general.errors.insufficient-rank",
TextVariables.RANK,
user.getTranslation(plugin.getRanksManager().getRank(Objects.requireNonNull(island).getRank(user))));
}
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_METAL_HIT, 1F, 1F);
}
return true;

View File

@ -4,6 +4,8 @@ import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.event.inventory.ClickType;
import java.util.Objects;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.events.flags.FlagSettingChangeEvent;
@ -13,6 +15,7 @@ import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
import world.bentobox.bentobox.api.panels.TabbedPanel;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.panels.settings.SettingsTab;
import world.bentobox.bentobox.util.Util;
@ -51,21 +54,30 @@ public class IslandToggleClick implements ClickHandler {
}
// Get the island for this tab
Island island = st.getIsland();
if (island != null && (user.isOp() || user.getUniqueId().equals(island.getOwner()) || user.hasPermission(prefix + "admin.settings"))) {
plugin.getFlagsManager().getFlag(id).ifPresent(flag -> {
if (click.equals(ClickType.SHIFT_LEFT) && user.isOp()) {
if (!plugin.getIWM().getHiddenFlags(user.getWorld()).contains(flag.getID())) {
if (island != null && (user.isOp() || island.isAllowed(user, Flags.CHANGE_SETTINGS) || user.hasPermission(prefix + "admin.settings")))
{
plugin.getFlagsManager().getFlag(id).ifPresent(flag ->
{
if (click.equals(ClickType.SHIFT_LEFT) && user.isOp())
{
if (!plugin.getIWM().getHiddenFlags(user.getWorld()).contains(flag.getID()))
{
plugin.getIWM().getHiddenFlags(user.getWorld()).add(flag.getID());
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_GLASS_BREAK, 1F, 1F);
} else {
}
else
{
plugin.getIWM().getHiddenFlags(user.getWorld()).remove(flag.getID());
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1F, 1F);
}
// Save changes
plugin.getIWM().getAddon(user.getWorld()).ifPresent(GameModeAddon::saveWorldSettings);
} else {
}
else
{
// Check cooldown
if (!user.isOp() && island.isCooldown(flag)) {
if (!user.isOp() && island.isCooldown(flag))
{
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_BEACON_DEACTIVATE, 1F, 1F);
user.notify("protection.panel.flag-item.setting-cooldown");
return;
@ -76,17 +88,32 @@ public class IslandToggleClick implements ClickHandler {
// Set cooldown
island.setCooldown(flag);
// Fire event
Bukkit.getPluginManager().callEvent(new FlagSettingChangeEvent(island, user.getUniqueId(), flag, island.isAllowed(flag)));
Bukkit.getPluginManager().callEvent(new FlagSettingChangeEvent(island,
user.getUniqueId(),
flag,
island.isAllowed(flag)));
if (flag.hasSubflags()) {
if (flag.hasSubflags())
{
// Fire events for all subflags as well
flag.getSubflags().forEach(subflag -> Bukkit.getPluginManager().callEvent(new FlagSettingChangeEvent(island, user.getUniqueId(), subflag, island.isAllowed(subflag))));
flag.getSubflags().forEach(subflag -> Bukkit.getPluginManager()
.callEvent(new FlagSettingChangeEvent(island,
user.getUniqueId(),
subflag,
island.isAllowed(subflag))));
}
}
});
} else {
// Player is not the owner of the island.
user.sendMessage("general.errors.not-owner");
if (island == null) {
user.sendMessage("general.errors.not-on-island");
} else {
// Player is not the allowed to change settings.
user.sendMessage("general.errors.insufficient-rank",
TextVariables.RANK,
user.getTranslation(plugin.getRanksManager().getRank(Objects.requireNonNull(island).getRank(user))));
}
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_METAL_HIT, 1F, 1F);
}
return true;

View File

@ -9,6 +9,7 @@ import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.RanksManager;
/**
@ -36,7 +37,7 @@ public class CommandCycleClick implements ClickHandler {
// Get the user's island for the game world
World world = panel.getWorld().orElse(user.getWorld());
Island island = plugin.getIslands().getIsland(world, user.getUniqueId());
if (island != null && island.getOwner() != null && island.getOwner().equals(user.getUniqueId())) {
if (island != null && island.getOwner() != null && island.isAllowed(user, Flags.CHANGE_SETTINGS)) {
RanksManager rm = plugin.getRanksManager();
int currentRank = island.getRankCommand(command);
if (click.equals(ClickType.LEFT)) {

View File

@ -57,8 +57,11 @@ public class CommandRankClickListener implements ClickHandler {
// Get the user's island
Island island = plugin.getIslands().getIsland(panel.getWorld().orElse(user.getWorld()), user.getUniqueId());
if (island == null || island.getOwner() == null || !island.getOwner().equals(user.getUniqueId())) {
user.sendMessage("general.errors.not-owner");
if (island == null || island.getOwner() == null || !island.isAllowed(user, Flags.CHANGE_SETTINGS)) {
user.sendMessage("general.errors.insufficient-rank",
TextVariables.RANK,
user.getTranslation(plugin.getRanksManager().getRank(Objects.requireNonNull(island).getRank(user))));
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_METAL_HIT, 1F, 1F);
return true;
}

View File

@ -304,6 +304,16 @@ public final class Flags {
.clickHandler(new CycleClick("LOCK", RanksManager.VISITOR_RANK, RanksManager.MEMBER_RANK))
.mode(Flag.Mode.TOP_ROW).build();
/**
* This flag allows choosing which island members can change island settings values.
*
* @since 1.20.0
*/
public static final Flag CHANGE_SETTINGS = new Flag.Builder("CHANGE_SETTINGS", Material.CRAFTING_TABLE).defaultSetting(true)
.defaultRank(RanksManager.OWNER_RANK)
.clickHandler(new CycleClick("CHANGE_SETTINGS", RanksManager.MEMBER_RANK, RanksManager.OWNER_RANK))
.mode(Flag.Mode.TOP_ROW).build();
/*
* Settings flags (not protection flags)
*/

View File

@ -132,6 +132,7 @@ public class SettingsTab implements Tab, ClickHandler {
Map<Integer, PanelItem> icons = new HashMap<>();
// Add the lock icon - we want it to be displayed no matter the tab
if (island != null) {
icons.put(4, Flags.CHANGE_SETTINGS.toPanelItem(plugin, user, island, false));
icons.put(5, Flags.LOCK.toPanelItem(plugin, user, island, false));
}
// Add the mode icon

View File

@ -37,6 +37,7 @@ general:
wrong-world: "&c You are not in the right world to do that!"
you-must-wait: "&c You must wait [number]s before you can do that command again."
must-be-positive-number: "&c [number] is not a valid positive number."
not-on-island: "&c You are not on island!"
worlds:
overworld: "Overworld"
nether: "Nether"
@ -1103,6 +1104,11 @@ protection:
LOCK:
description: "Toggle lock"
name: "Lock island"
CHANGE_SETTINGS:
name: "Change Settings"
description: |-
&a Allow to switch which member
&a role can change island settings.
MILKING:
description: "Toggle cow milking"
name: "Milking"