2019-02-09 19:07:16 +01:00
|
|
|
package bentobox.addon.limits.commands;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2019-10-05 22:05:39 +02:00
|
|
|
import java.util.Optional;
|
2019-02-09 19:07:16 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.World;
|
2019-04-23 00:50:15 +02:00
|
|
|
import org.bukkit.entity.EntityType;
|
2019-02-09 19:07:16 +01:00
|
|
|
|
2019-11-08 20:27:36 +01:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
|
2019-02-09 19:07:16 +01:00
|
|
|
import bentobox.addon.limits.Limits;
|
|
|
|
import bentobox.addon.limits.objects.IslandBlockCount;
|
|
|
|
import world.bentobox.bentobox.api.localization.TextVariables;
|
|
|
|
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
|
|
|
|
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
|
|
|
|
import world.bentobox.bentobox.api.user.User;
|
|
|
|
import world.bentobox.bentobox.database.objects.Island;
|
|
|
|
import world.bentobox.bentobox.util.Util;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a panel of the blocks that are limited and their status
|
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
2019-03-17 22:54:00 +01:00
|
|
|
public class LimitPanel {
|
2019-02-09 19:07:16 +01:00
|
|
|
|
2019-02-09 19:30:02 +01:00
|
|
|
private final Limits addon;
|
2019-06-26 02:07:44 +02:00
|
|
|
// This maps the entity types to the icon that should be shown in the panel
|
|
|
|
// If the icon is null, then the entity type is not covered by the addon
|
2019-11-09 23:44:58 +01:00
|
|
|
private static final Map<EntityType, Material> E2M = ImmutableMap.<EntityType, Material>builder()
|
2019-11-08 20:27:36 +01:00
|
|
|
.put(EntityType.PIG_ZOMBIE, Material.ZOMBIE_PIGMAN_SPAWN_EGG)
|
|
|
|
.put(EntityType.MUSHROOM_COW, Material.MOOSHROOM_SPAWN_EGG)
|
|
|
|
.put(EntityType.SNOWMAN, Material.SNOW_BLOCK)
|
|
|
|
.put(EntityType.IRON_GOLEM, Material.IRON_BLOCK)
|
|
|
|
.put(EntityType.ILLUSIONER, Material.VILLAGER_SPAWN_EGG)
|
|
|
|
.put(EntityType.WITHER, Material.WITHER_SKELETON_SKULL)
|
|
|
|
.put(EntityType.BOAT, Material.OAK_BOAT)
|
|
|
|
.put(EntityType.ARMOR_STAND, Material.ARMOR_STAND)
|
|
|
|
.put(EntityType.ITEM_FRAME, Material.ITEM_FRAME)
|
|
|
|
.put(EntityType.PAINTING, Material.PAINTING)
|
|
|
|
// Minecarts
|
|
|
|
.put(EntityType.MINECART_TNT, Material.TNT_MINECART)
|
|
|
|
.put(EntityType.MINECART_CHEST, Material.CHEST_MINECART)
|
|
|
|
.put(EntityType.MINECART_COMMAND, Material.COMMAND_BLOCK_MINECART)
|
|
|
|
.put(EntityType.MINECART_FURNACE, Material.FURNACE_MINECART)
|
|
|
|
.put(EntityType.MINECART_HOPPER, Material.HOPPER_MINECART)
|
|
|
|
.put(EntityType.MINECART_MOB_SPAWNER, Material.MINECART)
|
|
|
|
.build();
|
2019-09-11 04:01:39 +02:00
|
|
|
// This is a map of blocks to Material
|
2019-11-09 23:44:58 +01:00
|
|
|
private static final Map<Material, Material> B2M;
|
2019-04-23 00:50:15 +02:00
|
|
|
static {
|
2019-11-08 20:27:36 +01:00
|
|
|
ImmutableMap.Builder<Material, Material> builder = ImmutableMap.<Material, Material>builder()
|
|
|
|
.put(Material.POTATOES, Material.POTATO)
|
|
|
|
.put(Material.CARROTS, Material.CARROT)
|
|
|
|
.put(Material.BEETROOTS, Material.BEETROOT)
|
|
|
|
.put(Material.REDSTONE_WIRE, Material.REDSTONE);
|
2019-09-11 04:01:39 +02:00
|
|
|
// Block to Material icons
|
2019-11-08 21:35:47 +01:00
|
|
|
Optional.ofNullable(Material.getMaterial("SWEET_BERRY_BUSH")).ifPresent(material -> builder.put(material, Material.getMaterial("SWEET_BERRIES")));
|
|
|
|
Optional.ofNullable(Material.getMaterial("BAMBOO_SAPLING")).ifPresent(material -> builder.put(material, Material.getMaterial("BAMBOO")));
|
2019-11-08 20:27:36 +01:00
|
|
|
B2M = builder.build();
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
2019-02-09 19:07:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param addon - limit addon
|
|
|
|
*/
|
|
|
|
public LimitPanel(Limits addon) {
|
|
|
|
this.addon = addon;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showLimits(World world, User user, UUID target) {
|
|
|
|
PanelBuilder pb = new PanelBuilder().name(user.getTranslation(world, "limits.panel-title")).user(user);
|
|
|
|
// Get the island for the target
|
|
|
|
Island island = addon.getIslands().getIsland(world, target);
|
|
|
|
if (island == null) {
|
2019-02-09 20:13:20 +01:00
|
|
|
if (user.getUniqueId().equals(target)) {
|
|
|
|
user.sendMessage("general.errors.no-island");
|
|
|
|
} else {
|
|
|
|
user.sendMessage("general.errors.player-has-no-island");
|
|
|
|
}
|
2019-02-09 19:07:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(island.getUniqueId());
|
|
|
|
Map<Material, Integer> matLimits = addon.getBlockLimitListener().getMaterialLimits(world, island.getUniqueId());
|
2019-05-05 06:28:40 +02:00
|
|
|
if (matLimits.isEmpty() && addon.getSettings().getLimits().isEmpty()) {
|
2019-02-09 19:07:16 +01:00
|
|
|
user.sendMessage("island.limits.no-limits");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (Entry<Material, Integer> en : matLimits.entrySet()) {
|
|
|
|
PanelItemBuilder pib = new PanelItemBuilder();
|
|
|
|
pib.name(Util.prettifyText(en.getKey().toString()));
|
2019-09-11 04:01:39 +02:00
|
|
|
// Adjust icon
|
|
|
|
pib.icon(B2M.getOrDefault(en.getKey(), en.getKey()));
|
|
|
|
|
2019-02-09 19:07:16 +01:00
|
|
|
int count = ibc == null ? 0 : ibc.getBlockCount().getOrDefault(en.getKey(), 0);
|
|
|
|
String color = count >= en.getValue() ? user.getTranslation("island.limits.max-color") : user.getTranslation("island.limits.regular-color");
|
|
|
|
pib.description(color
|
|
|
|
+ user.getTranslation("island.limits.block-limit-syntax",
|
|
|
|
TextVariables.NUMBER, String.valueOf(count),
|
|
|
|
"[limit]", String.valueOf(en.getValue())));
|
|
|
|
pb.item(pib.build());
|
|
|
|
}
|
2019-04-23 00:50:15 +02:00
|
|
|
addon.getSettings().getLimits().forEach((k,v) -> {
|
|
|
|
PanelItemBuilder pib = new PanelItemBuilder();
|
|
|
|
pib.name(Util.prettifyText(k.toString()));
|
|
|
|
Material m = Material.BARRIER;
|
|
|
|
try {
|
|
|
|
if (E2M.containsKey(k)) {
|
|
|
|
m = E2M.get(k);
|
|
|
|
} else if (k.isAlive()) {
|
|
|
|
m = Material.valueOf(k.toString() + "_SPAWN_EGG");
|
|
|
|
} else {
|
2019-09-11 04:01:39 +02:00
|
|
|
// Regular material
|
2019-04-23 00:50:15 +02:00
|
|
|
m = Material.valueOf(k.toString());
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
m = Material.BARRIER;
|
|
|
|
}
|
|
|
|
pib.icon(m);
|
|
|
|
long count = getCount(island, k);
|
|
|
|
String color = count >= v ? user.getTranslation("island.limits.max-color") : user.getTranslation("island.limits.regular-color");
|
|
|
|
pib.description(color
|
|
|
|
+ user.getTranslation("island.limits.block-limit-syntax",
|
|
|
|
TextVariables.NUMBER, String.valueOf(count),
|
|
|
|
"[limit]", String.valueOf(v)));
|
|
|
|
pb.item(pib.build());
|
|
|
|
});
|
2019-02-09 19:07:16 +01:00
|
|
|
pb.build();
|
|
|
|
}
|
|
|
|
|
2019-04-23 00:50:15 +02:00
|
|
|
private long getCount(Island island, EntityType ent) {
|
|
|
|
return island.getWorld().getEntities().stream()
|
|
|
|
.filter(e -> e.getType().equals(ent))
|
|
|
|
.filter(e -> island.inIslandSpace(e.getLocation())).count();
|
|
|
|
}
|
2019-02-09 19:07:16 +01:00
|
|
|
}
|