mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-24 17:11:30 +01:00
Refactor to reduce complexity
This commit is contained in:
parent
35ece03e5b
commit
9ccdcceaba
@ -9,6 +9,7 @@ import org.bukkit.event.inventory.ClickType;
|
|||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||||
import world.bentobox.bentobox.api.events.flags.FlagProtectionChangeEvent;
|
import world.bentobox.bentobox.api.events.flags.FlagProtectionChangeEvent;
|
||||||
|
import world.bentobox.bentobox.api.flags.Flag;
|
||||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||||
import world.bentobox.bentobox.api.panels.Panel;
|
import world.bentobox.bentobox.api.panels.Panel;
|
||||||
import world.bentobox.bentobox.api.panels.PanelItem;
|
import world.bentobox.bentobox.api.panels.PanelItem;
|
||||||
@ -57,13 +58,13 @@ public class CycleClick implements PanelItem.ClickHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onClick(Panel panel, User user, ClickType click, int slot) {
|
public boolean onClick(Panel panel, User user2, ClickType click, int slot) {
|
||||||
// This click listener is used with TabbedPanel and SettingsTabs only
|
// This click listener is used with TabbedPanel and SettingsTabs only
|
||||||
TabbedPanel tp = (TabbedPanel)panel;
|
TabbedPanel tp = (TabbedPanel)panel;
|
||||||
SettingsTab st = (SettingsTab)tp.getActiveTab();
|
SettingsTab st = (SettingsTab)tp.getActiveTab();
|
||||||
// Get the island for this tab
|
// Get the island for this tab
|
||||||
island = st.getIsland();
|
island = st.getIsland();
|
||||||
this.user = user;
|
this.user = user2;
|
||||||
changeOccurred = false;
|
changeOccurred = false;
|
||||||
// Permission prefix
|
// Permission prefix
|
||||||
String prefix = plugin.getIWM().getPermissionPrefix(Util.getWorld(user.getWorld()));
|
String prefix = plugin.getIWM().getPermissionPrefix(Util.getWorld(user.getWorld()));
|
||||||
@ -85,63 +86,83 @@ public class CycleClick implements PanelItem.ClickHandler {
|
|||||||
// Rank
|
// Rank
|
||||||
int currentRank = island.getFlag(flag);
|
int currentRank = island.getFlag(flag);
|
||||||
if (click.equals(ClickType.LEFT)) {
|
if (click.equals(ClickType.LEFT)) {
|
||||||
if (currentRank >= maxRank) {
|
leftClick(flag, rm, currentRank);
|
||||||
island.setFlag(flag, minRank);
|
|
||||||
} else {
|
|
||||||
island.setFlag(flag, rm.getRankUpValue(currentRank));
|
|
||||||
}
|
|
||||||
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_OFF, 1F, 1F);
|
|
||||||
// Fire event
|
|
||||||
Bukkit.getPluginManager().callEvent(new FlagProtectionChangeEvent(island, user.getUniqueId(), flag, island.getFlag(flag)));
|
|
||||||
|
|
||||||
// Subflag support
|
|
||||||
if (flag.hasSubflags()) {
|
|
||||||
// Fire events for all subflags as well
|
|
||||||
flag.getSubflags().forEach(subflag -> Bukkit.getPluginManager().callEvent(new FlagProtectionChangeEvent(island, user.getUniqueId(), subflag, island.getFlag(subflag))));
|
|
||||||
}
|
|
||||||
} else if (click.equals(ClickType.RIGHT)) {
|
} else if (click.equals(ClickType.RIGHT)) {
|
||||||
if (currentRank <= minRank) {
|
rightClick(flag, rm, currentRank);
|
||||||
island.setFlag(flag, maxRank);
|
|
||||||
} else {
|
|
||||||
island.setFlag(flag, rm.getRankDownValue(currentRank));
|
|
||||||
}
|
|
||||||
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
|
|
||||||
// Fire event
|
|
||||||
Bukkit.getPluginManager().callEvent(new FlagProtectionChangeEvent(island, user.getUniqueId(), flag, island.getFlag(flag)));
|
|
||||||
|
|
||||||
// Subflag support
|
} else if (click.equals(ClickType.SHIFT_LEFT) && user2.isOp()) {
|
||||||
if (flag.hasSubflags()) {
|
leftShiftClick(flag);
|
||||||
// Fire events for all subflags as well
|
|
||||||
flag.getSubflags().forEach(subflag -> Bukkit.getPluginManager().callEvent(new FlagProtectionChangeEvent(island, user.getUniqueId(), subflag, island.getFlag(subflag))));
|
|
||||||
}
|
|
||||||
} else 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 {
|
|
||||||
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 {
|
||||||
if (island == null) {
|
reportError();
|
||||||
// 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void reportError() {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void leftClick(Flag flag, RanksManager rm, int currentRank) {
|
||||||
|
if (currentRank >= maxRank) {
|
||||||
|
island.setFlag(flag, minRank);
|
||||||
|
} else {
|
||||||
|
island.setFlag(flag, rm.getRankUpValue(currentRank));
|
||||||
|
}
|
||||||
|
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_OFF, 1F, 1F);
|
||||||
|
// Fire event
|
||||||
|
Bukkit.getPluginManager().callEvent(new FlagProtectionChangeEvent(island, user.getUniqueId(), flag, island.getFlag(flag)));
|
||||||
|
|
||||||
|
// Subflag support
|
||||||
|
if (flag.hasSubflags()) {
|
||||||
|
// Fire events for all subflags as well
|
||||||
|
flag.getSubflags().forEach(subflag -> Bukkit.getPluginManager().callEvent(new FlagProtectionChangeEvent(island, user.getUniqueId(), subflag, island.getFlag(subflag))));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void rightClick(Flag flag, RanksManager rm, int currentRank) {
|
||||||
|
if (currentRank <= minRank) {
|
||||||
|
island.setFlag(flag, maxRank);
|
||||||
|
} else {
|
||||||
|
island.setFlag(flag, rm.getRankDownValue(currentRank));
|
||||||
|
}
|
||||||
|
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
|
||||||
|
// Fire event
|
||||||
|
Bukkit.getPluginManager().callEvent(new FlagProtectionChangeEvent(island, user.getUniqueId(), flag, island.getFlag(flag)));
|
||||||
|
|
||||||
|
// Subflag support
|
||||||
|
if (flag.hasSubflags()) {
|
||||||
|
// Fire events for all subflags as well
|
||||||
|
flag.getSubflags().forEach(subflag -> Bukkit.getPluginManager().callEvent(new FlagProtectionChangeEvent(island, user.getUniqueId(), subflag, island.getFlag(subflag))));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void leftShiftClick(Flag flag) {
|
||||||
|
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 {
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param minRank the minRank to set
|
* @param minRank the minRank to set
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user