Skip to content

Commit

Permalink
Added chest/hopper placeholders, refactored getBlockCounts to be cons…
Browse files Browse the repository at this point in the history
…istent, implemented getBlockCount
  • Loading branch information
basonjar committed Jul 29, 2021
1 parent 4c5fdd6 commit 21986d3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
27 changes: 27 additions & 0 deletions src/main/java/world/bentobox/limits/Limits.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.stream.Collectors;

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

import world.bentobox.bentobox.api.addons.Addon;
Expand Down Expand Up @@ -48,6 +49,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 +127,29 @@ public JoinListener getJoinListener() {
return joinListener;
}

private void registerPlaceholders(GameModeAddon gm) {
if (getPlugin().getPlaceholdersManager() == null) return;

// Hopper count
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_island_hopper_count",
user -> String.valueOf(getBlockLimitListener().getIsland(gm.getIslands().
getIsland(gm.getOverWorld(), user).getUniqueId()).getBlockCount(Material.HOPPER)));
// Hopper limit
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_island_hopper_limit",
user -> String.valueOf(getBlockLimitListener().getIsland(gm.getIslands().
getIsland(gm.getOverWorld(), user).getUniqueId()).getBlockLimit(Material.HOPPER)));
// Chest count
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_island_chest_count",
user -> String.valueOf(getBlockLimitListener().getIsland(gm.getIslands().
getIsland(gm.getOverWorld(), user).getUniqueId()).getBlockCount(Material.CHEST)));
// Chest limit
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_island_chest_limit",
user -> String.valueOf(getBlockLimitListener().getIsland(gm.getIslands().
getIsland(gm.getOverWorld(), user).getUniqueId()).getBlockLimit(Material.CHEST)));
}

}
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 or -1 for unlimited
*/
public Integer getBlockCount(Material m) {
return blockCounts.getOrDefault(m, -1);
}

/**
* 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

0 comments on commit 21986d3

Please sign in to comment.