mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-12-30 13:07:46 +01:00
Made the SettingsPanel fancier (well, I tried xD)
Had to remove the "MENU" FlagType, which I replaced by the "subPanel" argument in the Flag itself.
This commit is contained in:
parent
4525a04324
commit
b410d49c2b
@ -19,10 +19,19 @@ import us.tastybento.bskyblock.managers.RanksManager;
|
||||
public class Flag implements Comparable<Flag> {
|
||||
|
||||
public enum Type {
|
||||
PROTECTION,
|
||||
SETTING,
|
||||
MENU,
|
||||
WORLD_SETTING
|
||||
PROTECTION(Material.SHIELD),
|
||||
SETTING(Material.COMMAND),
|
||||
WORLD_SETTING(Material.GRASS);
|
||||
|
||||
private Material icon;
|
||||
|
||||
Type(Material icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
public Material getIcon() {
|
||||
return icon;
|
||||
}
|
||||
}
|
||||
|
||||
private final String id;
|
||||
@ -32,8 +41,9 @@ public class Flag implements Comparable<Flag> {
|
||||
private boolean setting;
|
||||
private final int defaultRank;
|
||||
private final PanelItem.ClickHandler clickHandler;
|
||||
private final boolean subPanel;
|
||||
|
||||
Flag(String id, Material icon, Listener listener, boolean defaultSetting, Type type, int defaultRank, PanelItem.ClickHandler clickListener) {
|
||||
Flag(String id, Material icon, Listener listener, boolean defaultSetting, Type type, int defaultRank, PanelItem.ClickHandler clickListener, boolean subPanel) {
|
||||
this.id = id;
|
||||
this.icon = icon;
|
||||
this.listener = listener;
|
||||
@ -41,6 +51,7 @@ public class Flag implements Comparable<Flag> {
|
||||
this.type = type;
|
||||
this.defaultRank = defaultRank;
|
||||
this.clickHandler = clickListener;
|
||||
this.subPanel = subPanel;
|
||||
}
|
||||
|
||||
public String getID() {
|
||||
@ -99,6 +110,13 @@ public class Flag implements Comparable<Flag> {
|
||||
return defaultRank;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return whether the flag uses a subpanel or not
|
||||
*/
|
||||
public boolean hasSubPanel() {
|
||||
return subPanel;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@ -157,7 +175,7 @@ public class Flag implements Comparable<Flag> {
|
||||
.icon(new ItemStack(icon))
|
||||
.name(user.getTranslation("protection.panel.flag-item.name-layout", TextVariables.NAME, user.getTranslation(getNameReference())))
|
||||
.clickHandler(clickHandler);
|
||||
if (getType().equals(Type.MENU)) {
|
||||
if (hasSubPanel()) {
|
||||
pib.description(user.getTranslation("protection.panel.flag-item.menu-layout", TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference())));
|
||||
return pib.build();
|
||||
}
|
||||
@ -208,7 +226,7 @@ public class Flag implements Comparable<Flag> {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Flag [id=" + id + ", icon=" + icon + ", listener=" + listener + ", type=" + type + ", defaultSetting="
|
||||
+ setting + ", defaultRank=" + defaultRank + ", clickHandler=" + clickHandler + "]";
|
||||
+ setting + ", defaultRank=" + defaultRank + ", clickHandler=" + clickHandler + ", subPanel=" + subPanel + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -17,6 +17,7 @@ public class FlagBuilder {
|
||||
private Type type = Type.PROTECTION;
|
||||
private int defaultRank = RanksManager.MEMBER_RANK;
|
||||
private PanelItem.ClickHandler onClick;
|
||||
private boolean subPanel = false;
|
||||
|
||||
public FlagBuilder id(String string) {
|
||||
id = string;
|
||||
@ -43,7 +44,7 @@ public class FlagBuilder {
|
||||
}
|
||||
|
||||
public Flag build() {
|
||||
return new Flag(id, icon, listener, setting, type, defaultRank, onClick);
|
||||
return new Flag(id, icon, listener, setting, type, defaultRank, onClick, subPanel);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,5 +97,15 @@ public class FlagBuilder {
|
||||
this.onClick = onClickListener;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Marks this flag as "using a sub-panel"
|
||||
* @param subPanel - whether the flag will use a sub-panel or not
|
||||
* @return FlagBuilder
|
||||
*/
|
||||
public FlagBuilder subPanel(boolean subPanel) {
|
||||
this.subPanel = subPanel;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package us.tastybento.bskyblock.commands.admin;
|
||||
import java.util.List;
|
||||
|
||||
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
||||
import us.tastybento.bskyblock.api.flags.Flag;
|
||||
import us.tastybento.bskyblock.api.user.User;
|
||||
import us.tastybento.bskyblock.panels.SettingsPanel;
|
||||
import us.tastybento.bskyblock.util.Util;
|
||||
@ -34,7 +35,7 @@ public class AdminWorldCommand extends CompositeCommand {
|
||||
public boolean execute(User user, List<String> args) {
|
||||
// Settings are only shown if you are in the right world
|
||||
if (Util.getWorld(user.getWorld()).equals(getWorld())) {
|
||||
SettingsPanel.openWorldSettingsPanel(getPlugin(), user);
|
||||
SettingsPanel.openPanel(getPlugin(), user, Flag.Type.WORLD_SETTING);
|
||||
return true;
|
||||
} else {
|
||||
user.sendMessage("general.errors.wrong-world");
|
||||
|
@ -3,6 +3,7 @@ package us.tastybento.bskyblock.commands.island;
|
||||
import java.util.List;
|
||||
|
||||
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
||||
import us.tastybento.bskyblock.api.flags.Flag;
|
||||
import us.tastybento.bskyblock.api.user.User;
|
||||
import us.tastybento.bskyblock.panels.SettingsPanel;
|
||||
import us.tastybento.bskyblock.util.Util;
|
||||
@ -33,7 +34,7 @@ public class IslandSettingsCommand extends CompositeCommand {
|
||||
public boolean execute(User user, List<String> args) {
|
||||
// Settings are only shown if you are in the right world
|
||||
if (Util.getWorld(user.getWorld()).equals(getWorld())) {
|
||||
SettingsPanel.openPanel(getPlugin(), user);
|
||||
SettingsPanel.openPanel(getPlugin(), user, Flag.Type.PROTECTION); //TODO keep track of history?
|
||||
return true;
|
||||
} else {
|
||||
user.sendMessage("general.errors.wrong-world");
|
||||
|
@ -157,8 +157,8 @@ public class Flags {
|
||||
.onClick(new WorldToggleClickListener("PISTON_PUSH"))
|
||||
.build();
|
||||
static InvincibleVisitorsListener ilv = new InvincibleVisitorsListener();
|
||||
public static final Flag INVINCIBLE_VISITORS = new FlagBuilder().id("INVINCIBLE_VISITORS").icon(Material.DIAMOND_CHESTPLATE).type(Type.MENU)
|
||||
.listener(ilv).onClick(ilv).build();
|
||||
public static final Flag INVINCIBLE_VISITORS = new FlagBuilder().id("INVINCIBLE_VISITORS").icon(Material.DIAMOND_CHESTPLATE).type(Type.WORLD_SETTING)
|
||||
.listener(ilv).onClick(ilv).subPanel(true).build();
|
||||
public static final Flag REMOVE_MOBS = new FlagBuilder().id("REMOVE_MOBS").icon(Material.GLOWSTONE_DUST).type(Type.WORLD_SETTING)
|
||||
.listener(new RemoveMobsListener()).allowedByDefault(true).onClick(new WorldToggleClickListener("REMOVE_MOBS")).build();
|
||||
|
||||
|
@ -1,8 +1,13 @@
|
||||
package us.tastybento.bskyblock.panels;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.material.MaterialData;
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.api.flags.Flag;
|
||||
import us.tastybento.bskyblock.api.panels.PanelItem;
|
||||
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
|
||||
import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder;
|
||||
import us.tastybento.bskyblock.api.user.User;
|
||||
|
||||
/**
|
||||
@ -11,35 +16,49 @@ import us.tastybento.bskyblock.api.user.User;
|
||||
*/
|
||||
public class SettingsPanel {
|
||||
|
||||
private SettingsPanel() {}
|
||||
|
||||
/**
|
||||
* Dynamically creates the panel.
|
||||
* @param plugin - plugin
|
||||
* @param user the User to show the panel to
|
||||
*/
|
||||
public static void openPanel(BSkyBlock plugin, User user) {
|
||||
// Make a panel for settings
|
||||
public static void openPanel(BSkyBlock plugin, User user, Flag.Type flagType) {
|
||||
// Create the panel
|
||||
PanelBuilder panelBuilder = new PanelBuilder()
|
||||
.name(user.getTranslation("protection.panel.title"));
|
||||
.name(user.getTranslation("protection.panel." + flagType.toString() + ".title"))
|
||||
.size(54);
|
||||
|
||||
// Add flags, sorted
|
||||
plugin.getFlagsManager().getFlags().stream().filter(f -> !f.getType().equals(Flag.Type.WORLD_SETTING))
|
||||
.sorted((e1, e2) -> e1.getID().compareTo(e2.getID())).forEach((f -> panelBuilder.item(f.toPanelItem(plugin, user))));
|
||||
// Make the panel
|
||||
setupHeader(user, panelBuilder, flagType);
|
||||
|
||||
plugin.getFlagsManager().getFlags().stream().filter(f -> f.getType().equals(flagType))
|
||||
.sorted((e1, e2) -> e1.getID().compareTo(e2.getID())).forEach((f -> panelBuilder.item(f.toPanelItem(plugin, user))));
|
||||
|
||||
// Show it to the player
|
||||
panelBuilder.build().open(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically creates the world settings panel.
|
||||
* @param plugin - plugin
|
||||
* @param user the User to show the panel to
|
||||
*/
|
||||
public static void openWorldSettingsPanel(BSkyBlock plugin, User user) {
|
||||
// Make a panel for settings
|
||||
PanelBuilder panelBuilder = new PanelBuilder().name(user.getTranslation("protection.panel.world-settings", "[world_name]", plugin.getIWM().getWorldName(user.getWorld())));
|
||||
// Add flags, sorted
|
||||
plugin.getFlagsManager().getFlags().stream().filter(f -> f.getType().equals(Flag.Type.WORLD_SETTING))
|
||||
.sorted((e1, e2) -> e1.getID().compareTo(e2.getID())).forEach((f -> panelBuilder.item(f.toPanelItem(plugin, user))));
|
||||
// Make the panel
|
||||
panelBuilder.build().open(user);
|
||||
|
||||
private static void setupHeader(User user, PanelBuilder panelBuilder, Flag.Type currentFlagType) {
|
||||
int slot = 2;
|
||||
for (Flag.Type flagType : Flag.Type.values()) {
|
||||
PanelItem panelItem = new PanelItemBuilder()
|
||||
.icon(flagType.getIcon())
|
||||
.name(user.getTranslation("protection.panel." + flagType.toString() + ".title"))
|
||||
.description(user.getTranslation("protection.panel." + flagType.toString() + ".description"))
|
||||
.glow(flagType.equals(currentFlagType))
|
||||
.clickHandler((panel, user1, clickType, slot1) -> {
|
||||
if (!flagType.equals(currentFlagType)) {
|
||||
openPanel(BSkyBlock.getInstance(), user, flagType);
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.build();
|
||||
panelBuilder.item(slot, panelItem);
|
||||
slot += 2;
|
||||
}
|
||||
|
||||
while(panelBuilder.nextSlot() < 9) {
|
||||
panelBuilder.item(new PanelItemBuilder().icon(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 15)).build());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,49 +78,49 @@ public class FlagTest {
|
||||
|
||||
@Test
|
||||
public void testHashCode() {
|
||||
Flag flag1 = new Flag(null, null, null, false, null, 0, null);
|
||||
Flag flag2 = new Flag(null, null, null, false, null, 0, null);
|
||||
Flag flag1 = new Flag(null, null, null, false, null, 0, null, false);
|
||||
Flag flag2 = new Flag(null, null, null, false, null, 0, null, false);
|
||||
assertTrue(flag1.hashCode() == flag2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlag() {
|
||||
assertNotNull(new Flag(null, null, null, false, null, 0, null));
|
||||
assertNotNull(new Flag(null, null, null, false, null, 0, null, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetID() {
|
||||
Flag id = new Flag("id", null, null, false, null, 0, null);
|
||||
Flag id = new Flag("id", null, null, false, null, 0, null, false);
|
||||
assertEquals("id", id.getID());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetIcon() {
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, null, 0, null);
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, null, 0, null, false);
|
||||
assertEquals(Material.ACACIA_DOOR, id.getIcon());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetListener() {
|
||||
Listener l = mock(Listener.class);
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, l, false, null, 0, null);
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, l, false, null, 0, null, false);
|
||||
Optional<Listener> ol = Optional.ofNullable(l);
|
||||
assertEquals(ol, id.getListener());
|
||||
id = new Flag("id", Material.ACACIA_DOOR, null, false, null, 0, null);
|
||||
id = new Flag("id", Material.ACACIA_DOOR, null, false, null, 0, null, false);
|
||||
assertEquals(Optional.empty(), id.getListener());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDefaultSetting() {
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, null, 0, null);
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, null, 0, null, false);
|
||||
assertFalse(id.isSetForWorld(mock(World.class)));
|
||||
id = new Flag("id", Material.ACACIA_DOOR, null, true, null, 0, null);
|
||||
id = new Flag("id", Material.ACACIA_DOOR, null, true, null, 0, null, false);
|
||||
assertTrue(id.isSetForWorld(mock(World.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetDefaultSetting() {
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, null, 0, null);
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, null, 0, null, false);
|
||||
assertFalse(id.isSetForWorld(mock(World.class)));
|
||||
id.setDefaultSetting(true);
|
||||
assertTrue(id.isSetForWorld(mock(World.class)));
|
||||
@ -131,15 +131,15 @@ public class FlagTest {
|
||||
|
||||
@Test
|
||||
public void testGetType() {
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null);
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null, false);
|
||||
assertEquals(Flag.Type.PROTECTION,id.getType());
|
||||
id = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.SETTING, 0, null);
|
||||
id = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.SETTING, 0, null, false);
|
||||
assertEquals(Flag.Type.SETTING,id.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDefaultRank() {
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 100, null);
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 100, null, false);
|
||||
assertEquals(100, id.getDefaultRank());
|
||||
}
|
||||
|
||||
@ -147,18 +147,18 @@ public class FlagTest {
|
||||
@Test
|
||||
public void testEqualsObject() {
|
||||
Flag flag1 = null;
|
||||
Flag flag2 = new Flag(null, null, null, false, null, 0, null);
|
||||
Flag flag2 = new Flag(null, null, null, false, null, 0, null, false);
|
||||
|
||||
assertFalse(flag2.equals(null));
|
||||
int i = 45;
|
||||
assertFalse(flag2.equals(i));
|
||||
|
||||
flag1 = new Flag(null, null, null, false, null, 0, null);
|
||||
flag1 = new Flag(null, null, null, false, null, 0, null, false);
|
||||
flag2 = flag1;
|
||||
assertTrue(flag1.equals(flag2));
|
||||
assertTrue(flag2.equals(flag1));
|
||||
|
||||
flag2 = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null);
|
||||
flag2 = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null, false);
|
||||
assertFalse(flag1.equals(flag2));
|
||||
assertFalse(flag2.equals(flag1));
|
||||
|
||||
@ -201,7 +201,7 @@ public class FlagTest {
|
||||
when(rm.getRank(Mockito.eq(RanksManager.VISITOR_RANK))).thenReturn("Visitor");
|
||||
when(rm.getRank(Mockito.eq(RanksManager.OWNER_RANK))).thenReturn("Owner");
|
||||
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null);
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null, false);
|
||||
|
||||
PanelItem pi = id.toPanelItem(plugin, user);
|
||||
|
||||
@ -214,14 +214,14 @@ public class FlagTest {
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null);
|
||||
assertEquals("Flag [id=id, icon=ACACIA_DOOR, listener=null, type=PROTECTION, defaultSetting=false, defaultRank=0, clickHandler=null]", id.toString());
|
||||
Flag id = new Flag("id", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null, false);
|
||||
assertEquals("Flag [id=id, icon=ACACIA_DOOR, listener=null, type=PROTECTION, defaultSetting=false, defaultRank=0, clickHandler=null, subPanel=false]", id.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompareTo() {
|
||||
Flag aaa = new Flag("AAA", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null);
|
||||
Flag bbb = new Flag("BBB", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null);
|
||||
Flag aaa = new Flag("AAA", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null, false);
|
||||
Flag bbb = new Flag("BBB", Material.ACACIA_DOOR, null, false, Flag.Type.PROTECTION, 0, null, false);
|
||||
assertTrue(aaa.compareTo(bbb) < bbb.compareTo(aaa));
|
||||
assertTrue(aaa.compareTo(aaa) == 0);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user