2020-08-21 04:03:33 +02:00
|
|
|
package world.bentobox.bentobox.listeners.flags.clicklisteners;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import org.bukkit.Material;
|
2021-02-17 00:56:34 +01:00
|
|
|
import org.bukkit.World;
|
2020-08-21 04:03:33 +02:00
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
import org.bukkit.event.inventory.ClickType;
|
2020-09-13 01:44:00 +02:00
|
|
|
import org.eclipse.jdt.annotation.NonNull;
|
2020-08-21 04:03:33 +02:00
|
|
|
import org.eclipse.jdt.annotation.Nullable;
|
|
|
|
|
|
|
|
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.Tab;
|
|
|
|
import world.bentobox.bentobox.api.panels.TabbedPanel;
|
2020-12-27 19:35:49 +01:00
|
|
|
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
|
2020-08-21 04:03:33 +02:00
|
|
|
import world.bentobox.bentobox.api.user.User;
|
2021-03-13 05:04:34 +01:00
|
|
|
import world.bentobox.bentobox.hooks.LangUtilsHook;
|
2020-08-21 04:03:33 +02:00
|
|
|
import world.bentobox.bentobox.util.Util;
|
|
|
|
|
|
|
|
/**
|
2020-09-13 01:44:00 +02:00
|
|
|
* Provides a tab GUI for viewing geo-limited mobs
|
2020-08-21 04:03:33 +02:00
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
2020-08-21 04:33:42 +02:00
|
|
|
public class GeoMobLimitTab implements Tab, ClickHandler {
|
2020-08-21 04:03:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of all living entity types, minus some
|
|
|
|
*/
|
|
|
|
private static final List<EntityType> LIVING_ENTITY_TYPES = Collections.unmodifiableList(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()));
|
|
|
|
|
2020-08-21 04:33:42 +02:00
|
|
|
public enum EntityLimitTabType {
|
|
|
|
GEO_LIMIT,
|
|
|
|
MOB_LIMIT
|
|
|
|
}
|
|
|
|
|
|
|
|
private final BentoBox plugin = BentoBox.getInstance();
|
|
|
|
private final User user;
|
|
|
|
private final EntityLimitTabType type;
|
2021-02-17 00:56:34 +01:00
|
|
|
private final World world;
|
2020-08-21 04:33:42 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-13 01:44:00 +02:00
|
|
|
* @param user - user viewing the tab
|
|
|
|
* @param type - type of tab to show - Geo limit or Mob limit
|
2021-02-17 00:56:34 +01:00
|
|
|
* @param world - world where this tab is being used
|
2020-08-21 04:33:42 +02:00
|
|
|
*/
|
2021-02-17 00:56:34 +01:00
|
|
|
public GeoMobLimitTab(@NonNull User user, @NonNull EntityLimitTabType type, World world) {
|
2020-08-21 04:33:42 +02:00
|
|
|
super();
|
|
|
|
this.user = user;
|
|
|
|
this.type = type;
|
2021-02-17 00:56:34 +01:00
|
|
|
this.world = world;
|
2020-08-21 04:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-21 04:03:33 +02:00
|
|
|
@Override
|
|
|
|
public boolean onClick(Panel panel, User user, ClickType clickType, int slot) {
|
|
|
|
// Case panel to Tabbed Panel to get the active page
|
|
|
|
TabbedPanel tp = (TabbedPanel)panel;
|
2020-08-21 04:33:42 +02:00
|
|
|
// Convert the slot and active page to an index
|
2020-08-21 04:03:33 +02:00
|
|
|
int index = tp.getActivePage() * 36 + slot - 9;
|
|
|
|
EntityType c = LIVING_ENTITY_TYPES.get(index);
|
2020-08-21 04:33:42 +02:00
|
|
|
if (type == EntityLimitTabType.MOB_LIMIT) {
|
2021-02-17 00:56:34 +01:00
|
|
|
if (plugin.getIWM().getMobLimitSettings(world).contains(c.name())) {
|
|
|
|
plugin.getIWM().getMobLimitSettings(world).remove(c.name());
|
2020-08-21 04:33:42 +02:00
|
|
|
} else {
|
2021-02-17 00:56:34 +01:00
|
|
|
plugin.getIWM().getMobLimitSettings(world).add(c.name());
|
2020-08-21 04:33:42 +02:00
|
|
|
}
|
2020-08-21 04:03:33 +02:00
|
|
|
} else {
|
2021-02-17 00:56:34 +01:00
|
|
|
if (plugin.getIWM().getGeoLimitSettings(world).contains(c.name())) {
|
|
|
|
plugin.getIWM().getGeoLimitSettings(world).remove(c.name());
|
2020-08-21 04:33:42 +02:00
|
|
|
} else {
|
2021-02-17 00:56:34 +01:00
|
|
|
plugin.getIWM().getGeoLimitSettings(world).add(c.name());
|
2020-08-21 04:33:42 +02:00
|
|
|
}
|
2020-08-21 04:03:33 +02:00
|
|
|
}
|
|
|
|
// Apply change to panel
|
|
|
|
panel.getInventory().setItem(slot, getPanelItem(c, user).getItem());
|
|
|
|
// Save settings
|
2021-02-17 00:56:34 +01:00
|
|
|
plugin.getIWM().getAddon(Util.getWorld(world)).ifPresent(GameModeAddon::saveWorldSettings);
|
2020-08-21 04:03:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public PanelItem getIcon() {
|
2020-08-21 04:33:42 +02:00
|
|
|
if (type == EntityLimitTabType.MOB_LIMIT) {
|
2020-09-13 01:44:00 +02:00
|
|
|
return new PanelItemBuilder().icon(Material.IRON_BOOTS).name(getName()).build();
|
2020-08-21 04:33:42 +02:00
|
|
|
} else {
|
2020-09-13 01:44:00 +02:00
|
|
|
return new PanelItemBuilder().icon(Material.CHAINMAIL_CHESTPLATE).name(getName()).build();
|
2020-08-21 04:03:33 +02:00
|
|
|
|
2020-08-21 04:33:42 +02:00
|
|
|
}
|
2020-08-21 04:03:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
2020-08-21 04:33:42 +02:00
|
|
|
return type == EntityLimitTabType.MOB_LIMIT ? user.getTranslation("protection.flags.LIMIT_MOBS.name")
|
|
|
|
: user.getTranslation("protection.flags.GEO_LIMIT_MOBS.name");
|
2020-08-21 04:03:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<@Nullable PanelItem> getPanelItems() {
|
|
|
|
// Make panel items
|
|
|
|
return LIVING_ENTITY_TYPES.stream().map(c -> getPanelItem(c, user)).collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getPermission() {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
private PanelItem getPanelItem(EntityType c, User user) {
|
|
|
|
PanelItemBuilder pib = new PanelItemBuilder();
|
2021-03-13 05:04:34 +01:00
|
|
|
pib.name(LangUtilsHook.getEntityName(c, user));
|
2020-08-21 04:03:33 +02:00
|
|
|
pib.clickHandler(this);
|
2020-08-21 04:33:42 +02:00
|
|
|
if (type == EntityLimitTabType.MOB_LIMIT) {
|
2021-02-17 00:56:34 +01:00
|
|
|
if (!BentoBox.getInstance().getIWM().getMobLimitSettings(world).contains(c.name())) {
|
2020-08-21 04:33:42 +02:00
|
|
|
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"));
|
|
|
|
}
|
2020-08-21 04:03:33 +02:00
|
|
|
} else {
|
2021-02-17 00:56:34 +01:00
|
|
|
if (BentoBox.getInstance().getIWM().getGeoLimitSettings(world).contains(c.name())) {
|
2020-08-21 04:33:42 +02:00
|
|
|
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"));
|
|
|
|
}
|
2020-08-21 04:03:33 +02:00
|
|
|
}
|
|
|
|
return pib.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|