Added recount command for players. Fixed counting for crops.

https://github.com/BentoBoxWorld/Limits/issues/44
This commit is contained in:
tastybento 2019-09-10 19:01:39 -07:00
parent 6078059e20
commit 06da189e26
8 changed files with 136 additions and 22 deletions

View File

@ -45,14 +45,14 @@
<!-- Non-minecraft related dependencies --> <!-- Non-minecraft related dependencies -->
<powermock.version>2.0.2</powermock.version> <powermock.version>2.0.2</powermock.version>
<!-- More visible way how to change dependency versions --> <!-- More visible way how to change dependency versions -->
<spigot.version>1.13.2-R0.1-SNAPSHOT</spigot.version> <spigot.version>1.14.4-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.6.0</bentobox.version> <bentobox.version>1.6.0</bentobox.version>
<!-- Revision variable removes warning about dynamic version --> <!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision> <revision>${build.version}-SNAPSHOT</revision>
<!-- Do not change unless you want different name for local builds. --> <!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number> <build.number>-LOCAL</build.number>
<!-- This allows to change between versions. --> <!-- This allows to change between versions. -->
<build.version>1.6.1</build.version> <build.version>1.7.0</build.version>
</properties> </properties>
<!-- Profiles will allow to automatically change build version. --> <!-- Profiles will allow to automatically change build version. -->

View File

@ -29,6 +29,8 @@ public class LimitPanel {
// This maps the entity types to the icon that should be shown in the panel // 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 // If the icon is null, then the entity type is not covered by the addon
public final static Map<EntityType, Material> E2M = new HashMap<>(); public final static Map<EntityType, Material> E2M = new HashMap<>();
// This is a map of blocks to Material
public final static Map<Material, Material> B2M = new HashMap<>();
static { static {
E2M.put(EntityType.PIG_ZOMBIE, Material.ZOMBIE_PIGMAN_SPAWN_EGG); E2M.put(EntityType.PIG_ZOMBIE, Material.ZOMBIE_PIGMAN_SPAWN_EGG);
E2M.put(EntityType.MUSHROOM_COW, Material.MOOSHROOM_SPAWN_EGG); E2M.put(EntityType.MUSHROOM_COW, Material.MOOSHROOM_SPAWN_EGG);
@ -73,7 +75,13 @@ public class LimitPanel {
E2M.put(EntityType.ENDER_CRYSTAL, null); E2M.put(EntityType.ENDER_CRYSTAL, null);
E2M.put(EntityType.ENDER_PEARL, null); E2M.put(EntityType.ENDER_PEARL, null);
E2M.put(EntityType.ENDER_DRAGON, null); E2M.put(EntityType.ENDER_DRAGON, null);
// Block to Material icons
B2M.put(Material.SWEET_BERRY_BUSH, Material.SWEET_BERRIES);
B2M.put(Material.POTATOES, Material.POTATO);
B2M.put(Material.CARROTS, Material.CARROT);
B2M.put(Material.BEETROOTS, Material.BEETROOT);
B2M.put(Material.BAMBOO_SAPLING, Material.BAMBOO);
B2M.put(Material.REDSTONE_WIRE, Material.REDSTONE);
} }
/** /**
@ -104,12 +112,9 @@ public class LimitPanel {
for (Entry<Material, Integer> en : matLimits.entrySet()) { for (Entry<Material, Integer> en : matLimits.entrySet()) {
PanelItemBuilder pib = new PanelItemBuilder(); PanelItemBuilder pib = new PanelItemBuilder();
pib.name(Util.prettifyText(en.getKey().toString())); pib.name(Util.prettifyText(en.getKey().toString()));
if (en.getKey() == Material.REDSTONE_WIRE) { // Adjust icon
pib.icon(Material.REDSTONE); pib.icon(B2M.getOrDefault(en.getKey(), en.getKey()));
}
else {
pib.icon(en.getKey());
}
int count = ibc == null ? 0 : ibc.getBlockCount().getOrDefault(en.getKey(), 0); 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"); String color = count >= en.getValue() ? user.getTranslation("island.limits.max-color") : user.getTranslation("island.limits.regular-color");
pib.description(color pib.description(color
@ -128,6 +133,7 @@ public class LimitPanel {
} else if (k.isAlive()) { } else if (k.isAlive()) {
m = Material.valueOf(k.toString() + "_SPAWN_EGG"); m = Material.valueOf(k.toString() + "_SPAWN_EGG");
} else { } else {
// Regular material
m = Material.valueOf(k.toString()); m = Material.valueOf(k.toString());
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -22,6 +22,7 @@ public class PlayerCommand extends CompositeCommand {
public PlayerCommand(Limits addon, CompositeCommand parent) { public PlayerCommand(Limits addon, CompositeCommand parent) {
super(parent, "limits"); super(parent, "limits");
this.addon = addon; this.addon = addon;
new RecountCommand(addon, this);
} }
/* (non-Javadoc) /* (non-Javadoc)

View File

@ -0,0 +1,61 @@
package bentobox.addon.limits.commands;
import java.util.List;
import bentobox.addon.limits.Limits;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
/**
*
* @author tastybento
*/
public class RecountCommand extends CompositeCommand {
private final Limits addon;
/**
* Player command to do a recount. Has a cooldown
*
* @param addon - addon
*/
public RecountCommand(Limits addon, CompositeCommand parent) {
super(parent, "recount");
this.addon = addon;
}
/* (non-Javadoc)
* @see world.bentobox.bentobox.api.commands.BentoBoxCommand#setup()
*/
@Override
public void setup() {
this.setPermission("limits.player.recount");
this.setOnlyPlayer(true);
this.setParametersHelp("island.limits.recount.parameters");
this.setDescription("island.limits.recount.description");
}
/* (non-Javadoc)
* @see world.bentobox.bentobox.api.commands.BentoBoxCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)
*/
@Override
public boolean canExecute(User user, String label, List<String> args) {
if (!args.isEmpty()) {
showHelp(this, user);
return false;
}
if (addon.getIslands().getIsland(getWorld(), user) == null) {
user.sendMessage("general.errors.no-island");
return false;
}
return !checkCooldown(user);
}
@Override
public boolean execute(User user, String label, List<String> args) {
// Set cooldown
setCooldown(user.getUniqueId(), addon.getConfig().getInt("cooldown", 120));
new LimitsCalc(getWorld(), getPlugin(), user.getUniqueId(), addon, user);
return true;
}
}

View File

@ -23,7 +23,6 @@ import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.BlockFadeEvent; import org.bukkit.event.block.BlockFadeEvent;
import org.bukkit.event.block.BlockFormEvent; import org.bukkit.event.block.BlockFormEvent;
import org.bukkit.event.block.BlockFromToEvent; import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockGrowEvent;
import org.bukkit.event.block.BlockMultiPlaceEvent; import org.bukkit.event.block.BlockMultiPlaceEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockSpreadEvent; import org.bukkit.event.block.BlockSpreadEvent;
@ -50,7 +49,7 @@ public class BlockLimitsListener implements Listener {
* Blocks that are not counted * Blocks that are not counted
*/ */
private static final List<Material> DO_NOT_COUNT = Arrays.asList(Material.LAVA, Material.WATER, Material.AIR, Material.FIRE, Material.END_PORTAL, Material.NETHER_PORTAL); private static final List<Material> DO_NOT_COUNT = Arrays.asList(Material.LAVA, Material.WATER, Material.AIR, Material.FIRE, Material.END_PORTAL, Material.NETHER_PORTAL);
private static final List<Material> STACKABLE = Arrays.asList(Material.SUGAR_CANE, Material.BAMBOO);
/** /**
* Save every 10 blocks of change * Save every 10 blocks of change
*/ */
@ -147,7 +146,23 @@ public class BlockLimitsListener implements Listener {
} }
void handleBreak(Cancellable e, Player player, Block b) { void handleBreak(Cancellable e, Player player, Block b) {
notify(e, User.getInstance(player), process(b, false), b.getType()); Material mat = b.getType();
// Special handling for crops that can break in different ways
if (mat.equals(Material.WHEAT_SEEDS)) {
mat = Material.WHEAT;
} else if (mat.equals(Material.BEETROOT_SEEDS)) {
mat = Material.BEETROOT;
}
// Check for stackable plants
if (STACKABLE.contains(b.getType())) {
// Check for blocks above
Block block = b;
while(block.getRelative(BlockFace.UP).getType().equals(mat) && block.getY() < b.getWorld().getMaxHeight()) {
block = block.getRelative(BlockFace.UP);
process(block, false, mat);
}
}
notify(e, User.getInstance(player), process(b, false, mat), mat);
// Player breaks a block and there was a redstone dust/repeater/... above // Player breaks a block and there was a redstone dust/repeater/... above
if (b.getRelative(BlockFace.UP).getType() == Material.REDSTONE_WIRE || b.getRelative(BlockFace.UP).getType() == Material.REPEATER || b.getRelative(BlockFace.UP).getType() == Material.COMPARATOR || b.getRelative(BlockFace.UP).getType() == Material.REDSTONE_TORCH) { if (b.getRelative(BlockFace.UP).getType() == Material.REDSTONE_WIRE || b.getRelative(BlockFace.UP).getType() == Material.REPEATER || b.getRelative(BlockFace.UP).getType() == Material.COMPARATOR || b.getRelative(BlockFace.UP).getType() == Material.REDSTONE_TORCH) {
process(b.getRelative(BlockFace.UP), false); process(b.getRelative(BlockFace.UP), false);
@ -201,18 +216,22 @@ public class BlockLimitsListener implements Listener {
process(e.getBlock(), true); process(e.getBlock(), true);
} }
/*
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onBlock(BlockGrowEvent e) { public void onBlock(BlockGrowEvent e) {
Bukkit.getLogger().info(e.getEventName());
process(e.getBlock(), true); process(e.getBlock(), true);
} }
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onBlock(BlockSpreadEvent e) { public void onBlock(BlockSpreadEvent e) {
Bukkit.getLogger().info(e.getEventName());
process(e.getBlock(), true); process(e.getBlock(), true);
} }
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onBlock(EntityBlockFormEvent e) { public void onBlock(EntityBlockFormEvent e) {
Bukkit.getLogger().info(e.getEventName());
process(e.getBlock(), true); process(e.getBlock(), true);
} }
@ -244,11 +263,24 @@ public class BlockLimitsListener implements Listener {
return process(b, add, b.getType()); return process(b, add, b.getType());
} }
// It wouldn't make sense to count REDSTONE_WALL_TORCH and REDSTONE_TORCH as separed limits. // Return equivalents.
public Material fixMaterial(Material b) { public Material fixMaterial(Material b) {
if (b == Material.REDSTONE_WALL_TORCH) { switch (b) {
case REDSTONE_WALL_TORCH:
return Material.REDSTONE_TORCH; return Material.REDSTONE_TORCH;
} else { case WALL_TORCH:
return Material.TORCH;
case ZOMBIE_WALL_HEAD:
return Material.ZOMBIE_HEAD;
case CREEPER_WALL_HEAD:
return Material.CREEPER_HEAD;
case PLAYER_WALL_HEAD:
return Material.PLAYER_HEAD;
case DRAGON_WALL_HEAD:
return Material.DRAGON_HEAD;
case BAMBOO_SAPLING:
return Material.BAMBOO;
default:
return b; return b;
} }
} }
@ -286,13 +318,14 @@ public class BlockLimitsListener implements Listener {
} else { } else {
if (islandCountMap.containsKey(id)) { if (islandCountMap.containsKey(id)) {
// Check for changes // Check for changes
if (!fixMaterial(changeTo).equals(fixMaterial(b.getType())) && fixMaterial(changeTo).isBlock() && !DO_NOT_COUNT.contains(fixMaterial(changeTo))) { Material fixed = fixMaterial(changeTo);
if (!fixed.equals(fixMaterial(b.getType())) && fixed.isBlock() && !DO_NOT_COUNT.contains(fixed)) {
// Check limit // Check limit
int limit = checkLimit(b.getWorld(), fixMaterial(changeTo), id); int limit = checkLimit(b.getWorld(), fixed, id);
if (limit > -1) { if (limit > -1) {
return limit; return limit;
} }
islandCountMap.get(id).add(fixMaterial(changeTo)); islandCountMap.get(id).add(fixed);
} }
islandCountMap.get(id).remove(fixMaterial(b.getType())); islandCountMap.get(id).remove(fixMaterial(b.getType()));
saveMap.merge(id, 1, Integer::sum); saveMap.merge(id, 1, Integer::sum);

View File

@ -4,24 +4,33 @@ version: ${version}${build.number}
authors: tastybento authors: tastybento
softdepend: AcidIsland, BSkyBlock, CaveBlock, SkyGrid softdepend: AcidIsland, BSkyBlock, CaveBlock
permissions: permissions:
acidisland.limits.player.limits: acidisland.limits.player.limits:
description: Player can use limits command description: Player can use limits command
default: true default: true
acidisland.limits.player.recount:
description: Player can use recount command
default: true
acidisland.limits.admin.limits: acidisland.limits.admin.limits:
description: Player can use admin limits command description: Player can use admin limits command
default: op default: op
bskyblock.limits.player.limits: bskyblock.limits.player.limits:
description: Player can use limits command description: Player can use limits command
default: true default: true
bskyblock.limits.player.recount:
description: Player can use recount command
default: true
bskyblock.limits.admin.limits: bskyblock.limits.admin.limits:
description: Player can use admin limits command description: Player can use admin limits command
default: op default: op
caveblock.limits.player.limits: caveblock.limits.player.limits:
description: Player can use limits command description: Player can use limits command
default: true default: true
caveblock.limits.player.recount:
description: Player can use recount command
default: true
caveblock.limits.admin.limits: caveblock.limits.admin.limits:
description: Player can use admin limits command description: Player can use admin limits command
default: op default: op

View File

@ -10,6 +10,9 @@ gamemodes:
# example: bskyblock.island.limit.hopper.10 # example: bskyblock.island.limit.hopper.10
# permission activates when player logs in. # permission activates when player logs in.
# #
# Cooldown for player recount command in seconds
cooldown: 120
# General block limiting # General block limiting
# Use this section to limit how many blocks can be added to an island. # Use this section to limit how many blocks can be added to an island.
# 0 means the item will be blocked from placement completely. # 0 means the item will be blocked from placement completely.

View File

@ -22,11 +22,12 @@ admin:
finished: "&aIsland recalc finished sucessfully!" finished: "&aIsland recalc finished sucessfully!"
island: island:
limits: limits:
parameters: ""
description: "show your island limits" description: "show your island limits"
max-color: "&c" max-color: "&c"
regular-color: "&a" regular-color: "&a"
block-limit-syntax: "[number]/[limit]" block-limit-syntax: "[number]/[limit]"
no-limits: "&cNo limits set in this world" no-limits: "&cNo limits set in this world"
recount:
description: "recounts limits for your island"