Skip to content

Commit

Permalink
Adds a check that will run on login and other times to set the box size
Browse files Browse the repository at this point in the history
Runs through the advancements that island has and corrects the size of
the box. This enables admins to adjust the value of advancements after
the fact and players will still benefit (or be nerfed) from the changes.
  • Loading branch information
tastybento committed Apr 18, 2021
1 parent 76bfbb1 commit 19356d6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
55 changes: 37 additions & 18 deletions src/main/java/world/bentobox/boxed/AdvancementsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

import org.bukkit.advancement.Advancement;
Expand Down Expand Up @@ -143,6 +144,20 @@ public boolean hasAdvancement(Island island, String advancement) {
return getIsland(island).getAdvancements().contains(advancement);
}

/**
* Check and correct the island's protection size based on accumulated advancements
* @param island - island to check
* @return value of island size change. Negative values means the island range shrank.
*/
public int checkIslandSize(Island island) {
int shouldSize = getIsland(island).getAdvancements().stream().mapToInt(this::getScore).sum();
int diff = shouldSize - island.getProtectionRange();
if (diff != 0) {
this.setProtectionSize(island, shouldSize, null);
}
return diff;
}

/**
* Add advancement to island and adjusts the island's protection size accordingly
* @param p - player who just advanced
Expand All @@ -154,14 +169,7 @@ public int addAdvancement(Player p, Advancement advancement) {
// Wrong world
return 0;
}
String adv = "advancements." + advancement.getKey().toString();
// Check score of advancement
int score = 0;
if (!advConfig.contains(adv) && adv.endsWith("/root")) {
score = advConfig.getInt("settings.default-root-increase");
} else {
score = advConfig.getInt(adv, this.unknownAdvChange);
}
int score = getScore(advancement.getKey().toString());
if (score == 0) {
return 0;
}
Expand All @@ -172,20 +180,31 @@ public int addAdvancement(Player p, Advancement advancement) {
&& addAdvancement(island, advancement.getKey().toString())) {
int oldSize = island.getProtectionRange();
int newSize = Math.max(1, oldSize + score);
island.setProtectionRange(newSize);
// Call Protection Range Change event. Does not support canceling.
IslandEvent.builder()
.island(island)
.location(island.getCenter())
.reason(IslandEvent.Reason.RANGE_CHANGE)
.involvedPlayer(p.getUniqueId())
.admin(true)
.protectionRange(newSize, island.getProtectionRange())
.build();
setProtectionSize(island, newSize, p.getUniqueId());
return score;
}
return 0;

}

private void setProtectionSize(@NonNull Island island, int newSize, @Nullable UUID uuid) {
island.setProtectionRange(newSize);
// Call Protection Range Change event. Does not support canceling.
IslandEvent.builder()
.island(island)
.location(island.getCenter())
.reason(IslandEvent.Reason.RANGE_CHANGE)
.involvedPlayer(uuid)
.admin(true)
.protectionRange(newSize, island.getProtectionRange())
.build();

}

private int getScore(String string) {
String adv = "advancements." + string;
// Check score of advancement
return !advConfig.contains(adv) && adv.endsWith("/root") ? advConfig.getInt("settings.default-root-increase") : advConfig.getInt(adv, this.unknownAdvChange);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ public void syncAdvancements(User user) {
Island island = addon.getIslands().getIsland(addon.getOverWorld(), user);
if (island != null) {
grantAdv(user, addon.getAdvManager().getIsland(island).getAdvancements());
int diff = addon.getAdvManager().checkIslandSize(island);
if (diff > 0) {
user.sendMessage("boxed.size-changed", TextVariables.NUMBER, String.valueOf(diff));
user.getPlayer().playSound(user.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1F, 2F);
} else if (diff < 0) {
user.sendMessage("boxed.size-decreased", TextVariables.NUMBER, String.valueOf(Math.abs(diff)));
user.getPlayer().playSound(user.getLocation(), Sound.ENTITY_VILLAGER_DEATH, 1F, 2F);
}
}
}

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 @@ -4,6 +4,7 @@
boxed:
completed: '&a [name] completed!'
size-changed: '&a Box size increased by [number]!'
size-decreased: '&c Box size decreased by [number]!'
user-completed: '&a [name] completed [description]!'
adv-disallowed: "&c You can only complete advancements in your own box. Revoking [description]."
general:
Expand Down

0 comments on commit 19356d6

Please sign in to comment.