Skip to content

Commit

Permalink
Merge pull request #141 from BentoBoxWorld/develop
Browse files Browse the repository at this point in the history
Release 1.17.2
  • Loading branch information
tastybento authored Aug 21, 2021
2 parents 4c5fdd6 + be2c648 commit 395709d
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>1.17.1</build.version>
<build.version>1.17.2</build.version>
<sonar.projectKey>BentoBoxWorld_Limits</sonar.projectKey>
<sonar.organization>bentobox-world</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/world/bentobox/limits/Limits.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package world.bentobox.limits;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.bukkit.Material;
import org.bukkit.World;

import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.limits.commands.AdminCommand;
import world.bentobox.limits.commands.PlayerCommand;
import world.bentobox.limits.listeners.BlockLimitsListener;
Expand Down Expand Up @@ -48,6 +53,7 @@ public void onEnable() {
// Register commands
gm.getAdminCommand().ifPresent(a -> new AdminCommand(this, a));
gm.getPlayerCommand().ifPresent(a -> new PlayerCommand(this, a));
registerPlaceholders(gm);
log("Limits will apply to " + gm.getDescription().getName());
}
);
Expand Down Expand Up @@ -125,4 +131,64 @@ public JoinListener getJoinListener() {
return joinListener;
}

private void registerPlaceholders(GameModeAddon gm) {
if (getPlugin().getPlaceholdersManager() == null) return;
Arrays.stream(Material.values())
.filter(m -> m.isBlock())
.forEach(m -> registerCountAndLimitPlaceholders(m, gm));
}

/**
* Registers placeholders for the count and limit of the material
* in the format of %Limits_(gamemode prefix)_island_(lowercase material name)_count%
* and %Limits_(gamemode prefix)_island_(lowercase material name)_limit%
*
* Example: registerCountAndLimitPlaceholders("HOPPER", gm);
* Placeholders:
* "Limits_bskyblock_island_hopper_count"
* "Limits_bskyblock_island_hopper_limit"
*
* @param m
* @param gm
*/
private void registerCountAndLimitPlaceholders(Material m, GameModeAddon gm) {
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_island_" + m.toString().toLowerCase() + "_count",
user -> String.valueOf(getCount(user, m, gm)));
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_island_" + m.toString().toLowerCase() + "_limit",
user -> getLimit(user, m, gm));
}

/**
* @param user - Used to identify the island the user belongs to
* @param m - The material we are trying to count on the island
* @param gm
* @return Number of blocks of the specified material on the given user's island
*/
private int getCount(@Nullable User user, Material m, GameModeAddon gm) {
Island is = gm.getIslands().getIsland(gm.getOverWorld(), user);
if (is == null) {
return 0;
}
return getBlockLimitListener().getIsland(gm.getIslands().getIsland(gm.getOverWorld(), user).getUniqueId()).
getBlockCount(m);
}

/**
* @param user - Used to identify the island the user belongs to
* @param m - The material whose limit we are querying
* @param gm
* @return The limit of the specified material on the given user's island
*/
private String getLimit(@Nullable User user, Material m, GameModeAddon gm) {
Island is = gm.getIslands().getIsland(gm.getOverWorld(), user);
if (is == null) {
return "Limit not set";
}
int limit = getBlockLimitListener().getIsland(gm.getIslands().getIsland(gm.getOverWorld(), user).getUniqueId()).
getBlockLimit(m);
return limit == -1 ? "Limit not set" : String.valueOf(limit);
}

}
2 changes: 1 addition & 1 deletion src/main/java/world/bentobox/limits/commands/LimitTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private void addMaterialIcons(IslandBlockCount ibc, Map<Material, Integer> matLi
// Adjust icon
pib.icon(B2M.getOrDefault(en.getKey(), en.getKey()));

int count = ibc == null ? 0 : ibc.getBlockCount().getOrDefault(en.getKey(), 0);
int count = ibc == null ? 0 : ibc.getBlockCounts().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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private void tidyUp() {
if (ibc == null) {
ibc = new IslandBlockCount();
}
ibc.setBlockCount(blockCount.entrySet().stream()
ibc.setBlockCounts(blockCount.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().get())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public BlockLimitsListener(Limits addon) {
handler.loadObjects().forEach(ibc -> {
// Clean up
if (addon.isCoveredGameMode(ibc.getGameMode())) {
ibc.getBlockCount().keySet().removeIf(DO_NOT_COUNT::contains);
ibc.getBlockCounts().keySet().removeIf(DO_NOT_COUNT::contains);
// Store
islandCountMap.put(ibc.getUniqueId(), ibc);
} else {
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/world/bentobox/limits/objects/IslandBlockCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class IslandBlockCount implements DataObject {
private String gameMode = "";

@Expose
private Map<Material, Integer> blockCount = new EnumMap<>(Material.class);
private Map<Material, Integer> blockCounts = new EnumMap<>(Material.class);

private boolean changed;

Expand Down Expand Up @@ -74,24 +74,33 @@ public void setUniqueId(String uniqueId) {
/**
* @return the blockCount
*/
public Map<Material, Integer> getBlockCount() {
return blockCount;
public Map<Material, Integer> getBlockCounts() {
return blockCounts;
}

/**
* @param blockCount the blockCount to set
* @param blockCounts the blockCount to set
*/
public void setBlockCount(Map<Material, Integer> blockCount) {
this.blockCount = blockCount;
public void setBlockCounts(Map<Material, Integer> blockCounts) {
this.blockCounts = blockCounts;
setChanged();
}

/**
* Get the block count for this material for this island
* @param m - material
* @return count
*/
public Integer getBlockCount(Material m) {
return blockCounts.getOrDefault(m, 0);
}

/**
* Add a material to the count
* @param material - material
*/
public void add(Material material) {
blockCount.merge(material, 1, Integer::sum);
blockCounts.merge(material, 1, Integer::sum);
setChanged();
}

Expand All @@ -100,8 +109,8 @@ public void add(Material material) {
* @param material - material
*/
public void remove(Material material) {
blockCount.put(material, blockCount.getOrDefault(material, 0) - 1);
blockCount.values().removeIf(v -> v <= 0);
blockCounts.put(material, blockCounts.getOrDefault(material, 0) - 1);
blockCounts.values().removeIf(v -> v <= 0);
setChanged();
}

Expand All @@ -112,7 +121,7 @@ public void remove(Material material) {
* @return true if count is >= limit
*/
public boolean isAtLimit(Material material, int limit) {
return blockCount.getOrDefault(material, 0) >= limit;
return blockCounts.getOrDefault(material, 0) >= limit;
}

/**
Expand All @@ -122,7 +131,7 @@ public boolean isAtLimit(Material material, int limit) {
*/
public boolean isAtLimit(Material m) {
// Check island limits first
return blockLimits.containsKey(m) && blockCount.getOrDefault(m, 0) >= blockLimits.get(m);
return blockLimits.containsKey(m) && blockCounts.getOrDefault(m, 0) >= blockLimits.get(m);
}

public boolean isBlockLimited(Material m) {
Expand Down
32 changes: 32 additions & 0 deletions src/main/resources/locales/vi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
###########################################################################################
# This is a YML file. Be careful when editing. Check your edits in a YAML checker like #
# the one at http://yaml-online-parser.appspot.com #
###########################################################################################

block-limits:
hit-limit: "&c[material] bị giới hạn trong [number] khối!"
entity-limits:
hit-limit: "&c[entity] bị giới hạn trong [number] thực thể!"
limits:
panel-title: "Giới hạn đảo"


admin:
limits:
main:
parameters: "<người chơi>"
description: "xem giới hạn đảo của người chơi"
calc:
parameters: "<người chơi>"
description: "tính toán lại giới hạn đảo của người chơi"
finished: "&aTính toán đã hoàn thành!"

island:
limits:
description: "xem giới hạn đảo của bạn"
max-color: "&c"
regular-color: "&a"
block-limit-syntax: "[number]/[limit]"
no-limits: "&cKhông có giới hạn ở thế giới này"
recount:
description: "tính toán lại giới hạn đảo của bạn"

0 comments on commit 395709d

Please sign in to comment.