Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@
return;
}
int amount = Math.min(requested, currentHand.getAmount());

// Apply blockconfig donation limit
String donationId = customId != null ? customId : material.name();
Object blockObj = customId != null ? (Object) customId : material;
Object displayKey = customId != null ? customId : material;
Integer limit = addon.getBlockConfig().getLimit(blockObj);
if (limit != null) {
int currentDonated = addon.getManager().getDonatedBlocks(island).getOrDefault(donationId, 0);
int remaining = Math.max(0, limit - currentDonated);
if (remaining == 0) {
user.sendMessage("island.donate.limit-reached",
MATERIAL_PLACEHOLDER, Utils.prettifyObject(displayKey, user));
return;
}
amount = Math.min(amount, remaining);
}

long points = (long) amount * blockValue;

if (amount >= currentHand.getAmount()) {
Expand All @@ -159,11 +176,9 @@
currentHand.setAmount(currentHand.getAmount() - amount);
}

String donationId = customId != null ? customId : material.name();
addon.getManager().donateBlocks(island, user.getUniqueId(), donationId, amount, points);
addon.getManager().recalculateAfterDonation(island);

Object displayKey = customId != null ? customId : material;
user.sendMessage("island.donate.hand.success",
TextVariables.NUMBER, String.valueOf(amount),
MATERIAL_PLACEHOLDER, Utils.prettifyObject(displayKey, user),
Expand Down Expand Up @@ -211,26 +226,47 @@
return true;
}

private void performInvDonation(User user, Island island) {

Check failure on line 229 in src/main/java/world/bentobox/level/commands/IslandDonateCommand.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_Level&issues=AZ4u77jWBO_6kV5uEUCw&open=AZ4u77jWBO_6kV5uEUCw&pullRequest=439
PlayerInventory pInv = user.getPlayer().getInventory();
ItemStack[] contents = pInv.getStorageContents();
Map<String, Integer> donated = new HashMap<>();
long totalPoints = 0L;

for (int i = 0; i < contents.length; i++) {

Check warning on line 235 in src/main/java/world/bentobox/level/commands/IslandDonateCommand.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Reduce the total number of break and continue statements in this loop to use at most one.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_Level&issues=AZ4u77jWBO_6kV5uEUCv&open=AZ4u77jWBO_6kV5uEUCv&pullRequest=439
ItemStack item = contents[i];
Integer value = donationValue(item);
if (value == null) {
continue;
}
int amount = item.getAmount();
long points = (long) value * amount;
String customId = addon.getCustomBlockId(item);
String donationId = customId != null ? customId : item.getType().name();
Object blockObj = customId != null ? (Object) customId : item.getType();

// Apply blockconfig donation limit: only take up to the remaining capacity
int amount = item.getAmount();
Integer limit = addon.getBlockConfig().getLimit(blockObj);
if (limit != null) {
// getDonatedBlocks returns the live cached map, already updated by earlier donateBlocks calls
int alreadyDonated = addon.getManager().getDonatedBlocks(island).getOrDefault(donationId, 0);
int remaining = Math.max(0, limit - alreadyDonated);
if (remaining == 0) {
// Limit already reached for this material — leave item in inventory
continue;
}
amount = Math.min(amount, remaining);
}

// Remove accepted amount from inventory slot
if (amount >= item.getAmount()) {
contents[i] = null;
} else {
item.setAmount(item.getAmount() - amount);
}

long points = (long) value * amount;
donated.merge(donationId, amount, Integer::sum);
totalPoints += points;
addon.getManager().donateBlocks(island, user.getUniqueId(), donationId, amount, points);
contents[i] = null;
}
pInv.setStorageContents(contents);

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ island:
gui-title: "<black><bold>Donate Blocks"
gui-info: "<gold>Donate blocks to your island|<gray>Currently donated: <gold>[points]<gray> points|<red>Warning: donated items are|<red>destroyed and cannot be returned!"
preview: "<yellow>Points to add: <gold>[points]|<red>These items will be destroyed!"
limit-reached: "<red>The donation limit for [material] has been reached."
hand:
keyword: "hand"
success: "<green>Donated [number] x [material] for <aqua>[points]<green> permanent points!"
Expand Down
Loading