Paginates geo limited mob settings.

This commit is contained in:
tastybento 2020-08-20 19:33:42 -07:00
parent e8d5e66514
commit c2375e7f50
3 changed files with 69 additions and 84 deletions

View File

@ -1,23 +1,14 @@
package world.bentobox.bentobox.listeners.flags.clicklisteners;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.EntityType;
import org.bukkit.event.inventory.ClickType;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.panels.builders.TabbedPanelBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.clicklisteners.GeoMobLimitTab.EntityLimitTabType;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.util.Util;
@ -28,15 +19,6 @@ import world.bentobox.bentobox.util.Util;
*/
public class GeoLimitClickListener implements ClickHandler {
/**
* A list of all living entity types, minus some
*/
private final List<EntityType> livingEntityTypes = Arrays.stream(EntityType.values())
.filter(EntityType::isAlive)
.filter(t -> !(t.equals(EntityType.PLAYER) || t.equals(EntityType.GIANT) || t.equals(EntityType.ARMOR_STAND)))
.sorted(Comparator.comparing(EntityType::name))
.collect(Collectors.toList());
@Override
public boolean onClick(Panel panel, User user, ClickType clickType, int slot) {
// Get the world
@ -52,51 +34,22 @@ public class GeoLimitClickListener implements ClickHandler {
return true;
}
String panelName = user.getTranslation("protection.flags.GEO_LIMIT_MOBS.name");
if (panel.getName().equals(panelName)) {
// This is a click on the geo limit panel
// Slot relates to the enum
EntityType c = livingEntityTypes.get(slot);
if (iwm.getGeoLimitSettings(user.getWorld()).contains(c.name())) {
iwm.getGeoLimitSettings(user.getWorld()).remove(c.name());
} else {
iwm.getGeoLimitSettings(user.getWorld()).add(c.name());
}
// Apply change to panel
panel.getInventory().setItem(slot, getPanelItem(c, user).getItem());
// Save settings
iwm.getAddon(Util.getWorld(user.getWorld())).ifPresent(GameModeAddon::saveWorldSettings);
} else {
// Open the Sub Settings panel
openPanel(user, panelName);
}
// Open the Sub Settings panel
openPanel(user);
return true;
}
private void openPanel(User user, String panelName) {
private void openPanel(User user) {
// Close the current panel
user.closeInventory();
// Open a new panel
PanelBuilder pb = new PanelBuilder();
pb.user(user).name(panelName);
// Make panel items
livingEntityTypes.forEach(c -> pb.item(getPanelItem(c, user)));
pb.build();
new TabbedPanelBuilder()
.user(user)
.world(user.getWorld())
.tab(1, new GeoMobLimitTab(user, EntityLimitTabType.GEO_LIMIT))
.startingSlot(1)
.size(54)
.build().openPanel();
}
private PanelItem getPanelItem(EntityType c, User user) {
PanelItemBuilder pib = new PanelItemBuilder();
pib.name(Util.prettifyText(c.toString()));
pib.clickHandler(this);
if (BentoBox.getInstance().getIWM().getGeoLimitSettings(user.getWorld()).contains(c.name())) {
pib.icon(Material.GREEN_SHULKER_BOX);
pib.description(user.getTranslation("protection.panel.flag-item.setting-active"));
} else {
pib.icon(Material.RED_SHULKER_BOX);
pib.description(user.getTranslation("protection.panel.flag-item.setting-disabled"));
}
return pib.build();
}
}

View File

@ -29,18 +29,7 @@ import world.bentobox.bentobox.util.Util;
* @author tastybento
*
*/
public class MobLimitTab implements Tab, ClickHandler {
private BentoBox plugin = BentoBox.getInstance();
private User user;
/**
* @param user
*/
public MobLimitTab(User user) {
super();
this.user = user;
}
public class GeoMobLimitTab implements Tab, ClickHandler {
/**
* A list of all living entity types, minus some
@ -51,18 +40,45 @@ public class MobLimitTab implements Tab, ClickHandler {
.sorted(Comparator.comparing(EntityType::name))
.collect(Collectors.toList()));
public enum EntityLimitTabType {
GEO_LIMIT,
MOB_LIMIT
}
private final BentoBox plugin = BentoBox.getInstance();
private final User user;
private final EntityLimitTabType type;
/**
* @param user
*/
public GeoMobLimitTab(User user, EntityLimitTabType type) {
super();
this.user = user;
this.type = type;
}
@Override
public boolean onClick(Panel panel, User user, ClickType clickType, int slot) {
// This is a click on the mob limit panel
// Case panel to Tabbed Panel to get the active page
TabbedPanel tp = (TabbedPanel)panel;
// Conver the slot and active page to an index
// Convert the slot and active page to an index
int index = tp.getActivePage() * 36 + slot - 9;
EntityType c = LIVING_ENTITY_TYPES.get(index);
if (plugin.getIWM().getMobLimitSettings(user.getWorld()).contains(c.name())) {
plugin.getIWM().getMobLimitSettings(user.getWorld()).remove(c.name());
if (type == EntityLimitTabType.MOB_LIMIT) {
if (plugin.getIWM().getMobLimitSettings(user.getWorld()).contains(c.name())) {
plugin.getIWM().getMobLimitSettings(user.getWorld()).remove(c.name());
} else {
plugin.getIWM().getMobLimitSettings(user.getWorld()).add(c.name());
}
} else {
plugin.getIWM().getMobLimitSettings(user.getWorld()).add(c.name());
if (plugin.getIWM().getGeoLimitSettings(user.getWorld()).contains(c.name())) {
plugin.getIWM().getGeoLimitSettings(user.getWorld()).remove(c.name());
} else {
plugin.getIWM().getGeoLimitSettings(user.getWorld()).add(c.name());
}
}
// Apply change to panel
panel.getInventory().setItem(slot, getPanelItem(c, user).getItem());
@ -73,13 +89,18 @@ public class MobLimitTab implements Tab, ClickHandler {
@Override
public PanelItem getIcon() {
return new PanelItemBuilder().icon(Material.IRON_BOOTS).name(user.getTranslation("protection.flags.LIMIT_MOBS.name")).build();
if (type == EntityLimitTabType.MOB_LIMIT) {
return new PanelItemBuilder().icon(Material.IRON_BOOTS).name(user.getTranslation("protection.flags.LIMIT_MOBS.name")).build();
} else {
return new PanelItemBuilder().icon(Material.CHAINMAIL_CHESTPLATE).name(user.getTranslation("protection.flags.GEO_LIMIT_MOBS.name")).build();
}
}
@Override
public String getName() {
return user.getTranslation("protection.flags.LIMIT_MOBS.name");
return type == EntityLimitTabType.MOB_LIMIT ? user.getTranslation("protection.flags.LIMIT_MOBS.name")
: user.getTranslation("protection.flags.GEO_LIMIT_MOBS.name");
}
@Override
@ -97,12 +118,22 @@ public class MobLimitTab implements Tab, ClickHandler {
PanelItemBuilder pib = new PanelItemBuilder();
pib.name(Util.prettifyText(c.toString()));
pib.clickHandler(this);
if (!BentoBox.getInstance().getIWM().getMobLimitSettings(user.getWorld()).contains(c.name())) {
pib.icon(Material.GREEN_SHULKER_BOX);
pib.description(user.getTranslation("protection.flags.LIMIT_MOBS.can"));
if (type == EntityLimitTabType.MOB_LIMIT) {
if (!BentoBox.getInstance().getIWM().getMobLimitSettings(user.getWorld()).contains(c.name())) {
pib.icon(Material.GREEN_SHULKER_BOX);
pib.description(user.getTranslation("protection.flags.LIMIT_MOBS.can"));
} else {
pib.icon(Material.RED_SHULKER_BOX);
pib.description(user.getTranslation("protection.flags.LIMIT_MOBS.cannot"));
}
} else {
pib.icon(Material.RED_SHULKER_BOX);
pib.description(user.getTranslation("protection.flags.LIMIT_MOBS.cannot"));
if (BentoBox.getInstance().getIWM().getGeoLimitSettings(user.getWorld()).contains(c.name())) {
pib.icon(Material.GREEN_SHULKER_BOX);
pib.description(user.getTranslation("protection.panel.flag-item.setting-active"));
} else {
pib.icon(Material.RED_SHULKER_BOX);
pib.description(user.getTranslation("protection.panel.flag-item.setting-disabled"));
}
}
return pib.build();
}

View File

@ -8,6 +8,7 @@ import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
import world.bentobox.bentobox.api.panels.builders.TabbedPanelBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.clicklisteners.GeoMobLimitTab.EntityLimitTabType;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.util.Util;
@ -46,7 +47,7 @@ public class MobLimitClickListener implements ClickHandler {
new TabbedPanelBuilder()
.user(user)
.world(user.getWorld())
.tab(1, new MobLimitTab(user))
.tab(1, new GeoMobLimitTab(user, EntityLimitTabType.MOB_LIMIT))
.startingSlot(1)
.size(54)
.build().openPanel();