mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-06-27 15:24:53 +02:00
Refactor to reduce complexity
This commit is contained in:
parent
9ccdcceaba
commit
f1eaa0fcde
|
@ -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.FlagSettingChangeEvent;
|
import world.bentobox.bentobox.api.events.flags.FlagSettingChangeEvent;
|
||||||
|
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.ClickHandler;
|
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
|
||||||
|
@ -60,18 +61,7 @@ public class IslandToggleClick implements ClickHandler {
|
||||||
{
|
{
|
||||||
if (click.equals(ClickType.SHIFT_LEFT) && user.isOp())
|
if (click.equals(ClickType.SHIFT_LEFT) && user.isOp())
|
||||||
{
|
{
|
||||||
if (!plugin.getIWM().getHiddenFlags(user.getWorld()).contains(flag.getID()))
|
shiftLeftClick(user, flag);
|
||||||
{
|
|
||||||
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
|
||||||
{
|
{
|
||||||
|
@ -82,40 +72,66 @@ public class IslandToggleClick implements ClickHandler {
|
||||||
user.notify("protection.panel.flag-item.setting-cooldown");
|
user.notify("protection.panel.flag-item.setting-cooldown");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Toggle flag
|
toggleFlag(user, flag, island);
|
||||||
island.toggleFlag(flag);
|
|
||||||
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
|
|
||||||
// Set cooldown
|
|
||||||
island.setCooldown(flag);
|
|
||||||
// Fire event
|
|
||||||
Bukkit.getPluginManager().callEvent(new FlagSettingChangeEvent(island,
|
|
||||||
user.getUniqueId(),
|
|
||||||
flag,
|
|
||||||
island.isAllowed(flag)));
|
|
||||||
|
|
||||||
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))));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (island == null) {
|
reportError(user, island);
|
||||||
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 toggleFlag(User user, Flag flag, Island island) {
|
||||||
|
// Toggle flag
|
||||||
|
island.toggleFlag(flag);
|
||||||
|
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
|
||||||
|
// Set cooldown
|
||||||
|
island.setCooldown(flag);
|
||||||
|
// Fire event
|
||||||
|
Bukkit.getPluginManager().callEvent(new FlagSettingChangeEvent(island,
|
||||||
|
user.getUniqueId(),
|
||||||
|
flag,
|
||||||
|
island.isAllowed(flag)));
|
||||||
|
|
||||||
|
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))));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reportError(User user, Island island) {
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void shiftLeftClick(User user, 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);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user