Added cycling click to protection flags.

This commit is contained in:
tastybento 2018-06-02 11:27:51 -07:00
parent 320c9a9f43
commit e7649f19d4
5 changed files with 54 additions and 35 deletions

View File

@ -343,7 +343,10 @@ protection:
description-layout: |+ description-layout: |+
&a[description] &a[description]
&7Allowed for: &f[rank] &7Allowed for:
allowed_rank: "&3- &a"
blocked_rank: "&3- &c"
minimal_rank: "&3- &2"
help-item: help-item:
name: "&aNeed some help?" name: "&aNeed some help?"

View File

@ -124,21 +124,29 @@ public class Flag implements Comparable<Flag> {
* @return - PanelItem for this flag * @return - PanelItem for this flag
*/ */
public PanelItem toPanelItem(BSkyBlock plugin, User user) { public PanelItem toPanelItem(BSkyBlock plugin, User user) {
// Get the island this user is on or their own // Start the flag conversion
Island island = plugin.getIslands().getIslandAt(user.getLocation()).orElse(plugin.getIslands().getIsland(user.getWorld(), user.getUniqueId())); PanelItemBuilder pib = new PanelItemBuilder()
String rank = RanksManager.OWNER_RANK_REF;
if (island != null) {
// TODO: Get the world settings - the player has no island and is not in an island location
rank = plugin.getRanksManager().getRank(island.getFlag(this));
}
return new PanelItemBuilder()
.icon(new ItemStack(icon)) .icon(new ItemStack(icon))
.name(user.getTranslation("protection.panel.flag-item.name-layout", "[name]", user.getTranslation("protection.flags." + id + ".name"))) .name(user.getTranslation("protection.panel.flag-item.name-layout", "[name]", user.getTranslation("protection.flags." + id + ".name")))
.description(user.getTranslation("protection.panel.flag-item.description-layout", .clickHandler(clickHandler);
"[description]", user.getTranslation("protection.flags." + id + ".description"), pib.description(user.getTranslation("protection.panel.flag-item.description-layout", "[description]", user.getTranslation("protection.flags." + id + ".description")));
"[rank]", user.getTranslation(rank)))
.clickHandler(clickHandler) // Get the island this user is on or their own
.build(); Island island = plugin.getIslands().getIslandAt(user.getLocation()).orElse(plugin.getIslands().getIsland(user.getWorld(), user.getUniqueId()));
if (island != null) {
// TODO: Get the world settings - the player has no island and is not in an island location
// Dynamic rank list
plugin.getRanksManager().getRanks().forEach((reference, score) -> {
if (score > RanksManager.BANNED_RANK && score < island.getFlag(this)) {
pib.description(user.getTranslation("protection.panel.flag-item.blocked_rank") + user.getTranslation(reference));
} else if (score <= RanksManager.OWNER_RANK && score > island.getFlag(this)) {
pib.description(user.getTranslation("protection.panel.flag-item.allowed_rank") + user.getTranslation(reference));
} else if (score == island.getFlag(this)) {
pib.description(user.getTranslation("protection.panel.flag-item.minimal_rank") + user.getTranslation(reference));
}
});
}
return pib.build();
} }

View File

@ -5,7 +5,7 @@ import org.bukkit.event.Listener;
import us.tastybento.bskyblock.api.flags.Flag.Type; import us.tastybento.bskyblock.api.flags.Flag.Type;
import us.tastybento.bskyblock.api.panels.PanelItem; import us.tastybento.bskyblock.api.panels.PanelItem;
import us.tastybento.bskyblock.listeners.flags.UpDownClick; import us.tastybento.bskyblock.listeners.flags.CycleClick;
import us.tastybento.bskyblock.managers.RanksManager; import us.tastybento.bskyblock.managers.RanksManager;
public class FlagBuilder { public class FlagBuilder {
@ -21,7 +21,7 @@ public class FlagBuilder {
public FlagBuilder id(String string) { public FlagBuilder id(String string) {
id = string; id = string;
// Set the default click operation to UpDownClick // Set the default click operation to UpDownClick
onClick = new UpDownClick(id); onClick = new CycleClick(id);
return this; return this;
} }
@ -81,7 +81,7 @@ public class FlagBuilder {
/** /**
* Adds a listener for clicks on this flag when it is a panel item. Default is * Adds a listener for clicks on this flag when it is a panel item. Default is
* {@link us.tastybento.bskyblock.listeners.flags.UpDownClick} * {@link us.tastybento.bskyblock.listeners.flags.CycleClick}
* @param onClickListener - the listener for clicks. Must use the ClickOn interface * @param onClickListener - the listener for clicks. Must use the ClickOn interface
* @return FlagBuilder * @return FlagBuilder
*/ */

View File

@ -16,7 +16,7 @@ import us.tastybento.bskyblock.managers.RanksManager;
* @author tastybento * @author tastybento
* *
*/ */
public class UpDownClick implements PanelItem.ClickHandler { public class CycleClick implements PanelItem.ClickHandler {
private BSkyBlock plugin = BSkyBlock.getInstance(); private BSkyBlock plugin = BSkyBlock.getInstance();
private final String id; private final String id;
@ -25,7 +25,7 @@ public class UpDownClick implements PanelItem.ClickHandler {
/** /**
* @param id - the flag id that will be adjusted by this click * @param id - the flag id that will be adjusted by this click
*/ */
public UpDownClick(String id) { public CycleClick(String id) {
this.id = id; this.id = id;
} }
@ -40,13 +40,12 @@ public class UpDownClick implements PanelItem.ClickHandler {
Flag flag = plugin.getFlagsManager().getFlagByID(id); Flag flag = plugin.getFlagsManager().getFlagByID(id);
int currentRank = island.getFlag(flag); int currentRank = island.getFlag(flag);
if (click.equals(ClickType.LEFT)) { if (click.equals(ClickType.LEFT)) {
int nextRank = rm.getRankUpValue(currentRank); if (currentRank == RanksManager.OWNER_RANK) {
island.setFlag(flag, nextRank); island.setFlag(flag, RanksManager.VISITOR_RANK);
} else {
island.setFlag(flag, rm.getRankUpValue(currentRank));
}
user.getWorld().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F); user.getWorld().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
} else if (click.equals(ClickType.RIGHT)) {
int nextRank = rm.getRankDownValue(currentRank);
island.setFlag(flag, nextRank);
user.getWorld().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_OFF, 1F, 1F);
} }
// Apply change to panel // Apply change to panel
panel.getInventory().setItem(slot, flag.toPanelItem(plugin, user).getItem()); panel.getInventory().setItem(slot, flag.toPanelItem(plugin, user).getItem());

View File

@ -44,7 +44,7 @@ import us.tastybento.bskyblock.managers.RanksManager;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class }) @PrepareForTest({Bukkit.class, BSkyBlock.class, User.class })
public class UpDownClickTest { public class CycleClickTest {
private static final Integer PROTECTION_RANGE = 200; private static final Integer PROTECTION_RANGE = 200;
private static final Integer X = 600; private static final Integer X = 600;
@ -183,32 +183,41 @@ public class UpDownClickTest {
@Test @Test
public void testUpDownClick() { public void testUpDownClick() {
UpDownClick udc = new UpDownClick("LOCK"); CycleClick udc = new CycleClick("LOCK");
assertNotNull(udc); assertNotNull(udc);
} }
@Test @Test
public void testOnLeftClick() { public void testOnLeftClick() {
UpDownClick udc = new UpDownClick("LOCK"); final int SLOT = 5;
CycleClick udc = new CycleClick("LOCK");
// Rank starts at member
// Click left // Click left
assertTrue(udc.onClick(panel, user, ClickType.LEFT, 5)); assertTrue(udc.onClick(panel, user, ClickType.LEFT, SLOT));
Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.OWNER_RANK)); Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.OWNER_RANK));
Mockito.verify(flag).toPanelItem(Mockito.any(), Mockito.any()); Mockito.verify(flag).toPanelItem(Mockito.any(), Mockito.any());
Mockito.verify(inv).setItem(Mockito.eq(5), Mockito.any()); Mockito.verify(inv).setItem(Mockito.eq(SLOT), Mockito.any());
// Check rollover
// Clicking when Owner should go to Visitor
when(island.getFlag(Mockito.any())).thenReturn(RanksManager.OWNER_RANK);
assertTrue(udc.onClick(panel, user, ClickType.LEFT, SLOT));
Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.VISITOR_RANK));
Mockito.verify(flag, Mockito.times(2)).toPanelItem(Mockito.any(), Mockito.any());
Mockito.verify(inv, Mockito.times(2)).setItem(Mockito.eq(SLOT), Mockito.any());
} }
@Test @Test
public void testOnRightClick() { public void testOnRightClick() {
UpDownClick udc = new UpDownClick("LOCK"); CycleClick udc = new CycleClick("LOCK");
// Right click // Right click - should do nothing
assertTrue(udc.onClick(panel, user, ClickType.RIGHT, 0)); assertTrue(udc.onClick(panel, user, ClickType.RIGHT, 0));
Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.VISITOR_RANK)); Mockito.verify(island, Mockito.never()).setFlag(Mockito.eq(flag), Mockito.anyInt());
} }
@Test @Test
public void testAllClicks() { public void testAllClicks() {
// Test all possible click types // Test all possible click types
UpDownClick udc = new UpDownClick("LOCK"); CycleClick udc = new CycleClick("LOCK");
Arrays.asList(ClickType.values()).forEach(c -> assertTrue(udc.onClick(panel, user, c, 0))); Arrays.asList(ClickType.values()).forEach(c -> assertTrue(udc.onClick(panel, user, c, 0)));
} }