addon-level/src/main/java/world/bentobox/level/panels/DetailsGUITab.java

212 lines
8.1 KiB
Java
Raw Normal View History

2020-07-04 00:27:05 +02:00
/**
*
*/
package world.bentobox.level.panels;
import java.util.ArrayList;
import java.util.Collections;
2020-07-06 01:52:03 +02:00
import java.util.EnumMap;
2020-07-04 00:27:05 +02:00
import java.util.List;
2020-07-04 23:36:37 +02:00
import java.util.Map;
2020-07-04 00:27:05 +02:00
import org.bukkit.Material;
2020-07-04 23:36:37 +02:00
import org.bukkit.Tag;
2020-07-04 00:27:05 +02:00
import org.bukkit.World;
import org.bukkit.event.inventory.ClickType;
import org.eclipse.jdt.annotation.Nullable;
2020-07-04 23:36:37 +02:00
import com.google.common.base.Enums;
2020-07-05 03:13:02 +02:00
import world.bentobox.bentobox.api.localization.TextVariables;
2020-07-04 00:27:05 +02:00
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.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
2020-07-04 23:36:37 +02:00
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
2020-07-04 00:27:05 +02:00
import world.bentobox.level.Level;
import world.bentobox.level.objects.IslandLevels;
2020-07-04 00:27:05 +02:00
/**
* @author tastybento
*
*/
public class DetailsGUITab implements Tab, ClickHandler {
2020-07-04 23:36:37 +02:00
public enum DetailsType {
ABOVE_SEA_LEVEL_BLOCKS,
ALL_BLOCKS,
SPAWNERS,
UNDERWATER_BLOCKS
}
2020-07-05 03:13:02 +02:00
/**
* Converts block materials to item materials
*/
2020-07-04 23:36:37 +02:00
private static final Map<Material, Material> M2I;
static {
2020-07-06 01:52:03 +02:00
Map<Material, Material> m2i = new EnumMap<>(Material.class);
2020-07-04 23:36:37 +02:00
m2i.put(Material.WATER, Material.WATER_BUCKET);
m2i.put(Material.LAVA, Material.LAVA_BUCKET);
m2i.put(Material.AIR, Material.BLACK_STAINED_GLASS_PANE);
m2i.put(Material.VOID_AIR, Material.BLACK_STAINED_GLASS_PANE);
m2i.put(Material.CAVE_AIR, Material.BLACK_STAINED_GLASS_PANE);
m2i.put(Material.WALL_TORCH, Material.TORCH);
m2i.put(Material.REDSTONE_WALL_TORCH, Material.REDSTONE_TORCH);
m2i.put(Material.TALL_SEAGRASS, Material.SEAGRASS);
m2i.put(Material.PISTON_HEAD, Material.PISTON);
m2i.put(Material.MOVING_PISTON, Material.PISTON);
m2i.put(Material.REDSTONE_WIRE, Material.REDSTONE);
m2i.put(Material.NETHER_PORTAL, Material.MAGENTA_STAINED_GLASS_PANE);
m2i.put(Material.END_PORTAL, Material.BLACK_STAINED_GLASS_PANE);
m2i.put(Material.ATTACHED_MELON_STEM, Material.MELON_SEEDS);
m2i.put(Material.ATTACHED_PUMPKIN_STEM, Material.PUMPKIN_SEEDS);
m2i.put(Material.MELON_STEM, Material.MELON_SEEDS);
m2i.put(Material.PUMPKIN_STEM, Material.PUMPKIN_SEEDS);
m2i.put(Material.COCOA, Material.COCOA_BEANS);
m2i.put(Material.TRIPWIRE, Material.STRING);
m2i.put(Material.CARROTS, Material.CARROT);
m2i.put(Material.POTATOES, Material.POTATO);
m2i.put(Material.BEETROOTS, Material.BEETROOT);
m2i.put(Material.END_GATEWAY, Material.BEDROCK);
m2i.put(Material.FROSTED_ICE, Material.ICE);
m2i.put(Material.KELP_PLANT, Material.KELP);
m2i.put(Material.BUBBLE_COLUMN, Material.WATER_BUCKET);
m2i.put(Material.SWEET_BERRY_BUSH, Material.SWEET_BERRIES);
m2i.put(Material.BAMBOO_SAPLING, Material.BAMBOO);
2020-07-05 03:13:02 +02:00
m2i.put(Material.FIRE, Material.FLINT_AND_STEEL);
2020-07-04 23:36:37 +02:00
// 1.16.1
if (Enums.getIfPresent(Material.class, "WEEPING_VINES_PLANT").isPresent()) {
m2i.put(Material.WEEPING_VINES_PLANT, Material.WEEPING_VINES);
m2i.put(Material.TWISTING_VINES_PLANT, Material.TWISTING_VINES);
m2i.put(Material.SOUL_WALL_TORCH, Material.SOUL_TORCH);
}
M2I = Collections.unmodifiableMap(m2i);
}
2020-07-04 00:27:05 +02:00
private final Level addon;
2020-07-04 23:36:37 +02:00
private final @Nullable Island island;
private List<PanelItem> items;
private DetailsType type;
2020-07-04 00:27:05 +02:00
private final User user;
2020-07-04 23:36:37 +02:00
private final World world;
2020-07-04 00:27:05 +02:00
2020-07-04 23:36:37 +02:00
public DetailsGUITab(Level addon, World world, User user, DetailsType type) {
2020-07-04 00:27:05 +02:00
this.addon = addon;
this.world = world;
this.user = user;
2020-07-04 23:36:37 +02:00
this.island = addon.getIslands().getIsland(world, user);
this.type = type;
// Generate report
generateReport(type);
2020-07-04 00:27:05 +02:00
}
2020-07-04 23:36:37 +02:00
private void createItem(Material m, Integer count) {
if (count == null || count <= 0) return;
// Convert walls
m = Enums.getIfPresent(Material.class, m.name().replace("WALL_", "")).or(m);
// Tags
2020-07-05 03:13:02 +02:00
if (Enums.getIfPresent(Material.class, "SOUL_CAMPFIRE").isPresent()) {
if (Tag.FIRE.isTagged(m)) {
items.add(new PanelItemBuilder()
.icon(Material.CAMPFIRE)
.name(Util.prettifyText(m.name()) + " x " + count)
.build());
return;
}
2020-07-04 23:36:37 +02:00
}
if (Tag.FLOWER_POTS.isTagged(m)) {
m = Enums.getIfPresent(Material.class, m.name().replace("POTTED_", "")).or(m);
}
items.add(new PanelItemBuilder()
.icon(M2I.getOrDefault(m, m))
2020-07-05 03:13:02 +02:00
.name(user.getTranslation("island.level-details.syntax", TextVariables.NAME,
Util.prettifyText(m.name()), TextVariables.NUMBER, String.valueOf(count)))
2020-07-04 23:36:37 +02:00
.build());
}
private void generateReport(DetailsType type) {
items = new ArrayList<>();
IslandLevels ld = addon.getManager().getLevelsData(island);
2020-07-04 23:36:37 +02:00
// Get the items from the report
2020-07-06 01:52:03 +02:00
Map<Material, Integer> sumTotal = new EnumMap<>(Material.class);
sumTotal.putAll(ld.getMdCount());
sumTotal.putAll(ld.getUwCount());
2020-07-04 23:36:37 +02:00
switch(type) {
case ABOVE_SEA_LEVEL_BLOCKS:
ld.getMdCount().forEach(this::createItem);
2020-07-04 23:36:37 +02:00
break;
case SPAWNERS:
sumTotal.entrySet().stream().filter(m -> m.getKey().equals(Material.SPAWNER)).forEach(e -> createItem(e.getKey(), e.getValue()));
break;
case UNDERWATER_BLOCKS:
ld.getUwCount().forEach(this::createItem);
2020-07-04 23:36:37 +02:00
break;
default:
sumTotal.forEach(this::createItem);
break;
}
2020-07-05 03:13:02 +02:00
if (type.equals(DetailsType.ALL_BLOCKS) && items.isEmpty()) {
// Nothing here - looks like they need to run level
items.add(new PanelItemBuilder()
.name(user.getTranslation("island.level-details.hint")).icon(Material.WRITTEN_BOOK)
.build());
}
2020-07-04 00:27:05 +02:00
}
@Override
public PanelItem getIcon() {
2020-07-04 23:36:37 +02:00
switch(type) {
case ABOVE_SEA_LEVEL_BLOCKS:
2020-07-05 03:13:02 +02:00
return new PanelItemBuilder().icon(Material.GRASS_BLOCK).name(user.getTranslation("island.level-details.above-sea-level-blocks")).build();
2020-07-04 23:36:37 +02:00
case SPAWNERS:
2020-07-05 03:13:02 +02:00
return new PanelItemBuilder().icon(Material.SPAWNER).name(user.getTranslation("island.level-details.spawners")).build();
2020-07-04 23:36:37 +02:00
case UNDERWATER_BLOCKS:
2020-07-05 03:13:02 +02:00
return new PanelItemBuilder().icon(Material.WATER_BUCKET).name(user.getTranslation("island.level-details.underwater-blocks")).build();
2020-07-04 23:36:37 +02:00
default:
2020-07-05 03:13:02 +02:00
return new PanelItemBuilder().icon(Material.GRASS_BLOCK).name(user.getTranslation("island.level-details.all-blocks")).build();
2020-07-04 23:36:37 +02:00
}
2020-07-04 00:27:05 +02:00
}
@Override
public String getName() {
2020-07-05 03:13:02 +02:00
String name = user.getTranslation("island.level-details.no-island");
2020-07-04 23:36:37 +02:00
if (island.getOwner() != null) {
2020-07-05 03:13:02 +02:00
name = island.getName() != null ? island.getName() :
user.getTranslation("island.level-details.names-island", TextVariables.NAME, addon.getPlayers().getName(island.getOwner()));
2020-07-04 23:36:37 +02:00
}
return name;
2020-07-04 00:27:05 +02:00
}
@Override
public List<@Nullable PanelItem> getPanelItems() {
2020-07-04 23:36:37 +02:00
return items;
2020-07-04 00:27:05 +02:00
}
@Override
public String getPermission() {
2020-07-05 03:13:02 +02:00
String permPrefix = addon.getPlugin().getIWM().getPermissionPrefix(world);
2020-07-04 23:36:37 +02:00
switch(type) {
case ABOVE_SEA_LEVEL_BLOCKS:
2020-07-05 03:13:02 +02:00
return permPrefix + "island.level.details.above-sea-level";
2020-07-04 23:36:37 +02:00
case SPAWNERS:
2020-07-05 03:13:02 +02:00
return permPrefix + "island.level.details.spawners";
2020-07-04 23:36:37 +02:00
case UNDERWATER_BLOCKS:
2020-07-05 03:13:02 +02:00
return permPrefix + "island.level.details.underwater";
2020-07-04 23:36:37 +02:00
default:
2020-07-05 03:13:02 +02:00
return permPrefix + "island.level.details.blocks";
2020-07-04 23:36:37 +02:00
}
}
@Override
public boolean onClick(Panel panel, User user, ClickType clickType, int slot) {
return true;
2020-07-04 00:27:05 +02:00
}
}