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
44 changes: 44 additions & 0 deletions src/main/java/world/bentobox/limits/DisplayNames.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package world.bentobox.limits;

import java.util.Locale;

import org.bukkit.NamespacedKey;
import org.bukkit.entity.EntityType;

import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;

/**
* Resolves the display name of a limited material or entity for a user.
*
* <p>Locale files may provide manual translations under
* {@code island.limits.materials.<material>} and {@code island.limits.entities.<entity>}
* (lowercase Bukkit names). Any key without a translation falls back to the automatic
* prettified English name, so translations are entirely optional.
*/
public final class DisplayNames {

private DisplayNames() {
}

/**
* @param user the user whose locale is used
* @param key the material key, e.g. {@code minecraft:hopper}
* @return translated material name, or the prettified key if no translation exists
*/
public static String material(User user, NamespacedKey key) {
String translated = user.getTranslationOrNothing("island.limits.materials." + key.getKey());
return translated == null || translated.isEmpty() ? Util.prettifyText(key.getKey()) : translated;
}

/**
* @param user the user whose locale is used
* @param type the entity type
* @return translated entity name, or the prettified type name if no translation exists
*/
public static String entity(User user, EntityType type) {
String translated = user
.getTranslationOrNothing("island.limits.entities." + type.toString().toLowerCase(Locale.ROOT));
return translated == null || translated.isEmpty() ? Util.prettifyText(type.toString()) : translated;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import world.bentobox.bentobox.api.panels.Tab;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;

Check warning on line 28 in src/main/java/world/bentobox/limits/commands/player/LimitTab.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import 'world.bentobox.bentobox.util.Util'.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_Limits&issues=AZ9MvhH1TGeptNLGQnUL&open=AZ9MvhH1TGeptNLGQnUL&pullRequest=286
import world.bentobox.limits.DisplayNames;
import world.bentobox.limits.EntityGroup;
import world.bentobox.limits.Limits;
import world.bentobox.limits.objects.IslandBlockCount;
Expand Down Expand Up @@ -112,7 +113,7 @@
PanelItemBuilder pib = new PanelItemBuilder();
pib.name(user.getTranslation("island.limits.panel.entity-group-name-syntax", TextVariables.NAME,
g.getName()));
String description = "(" + prettyNames(g) + ")\n";
String description = "(" + prettyNames(user, g) + ")\n";
pib.icon(g.getIcon());
int count = ibc == null ? 0 : sumGroupCount(ibc, g);
String color = count >= limit ? user.getTranslation(MAX_COLOR_KEY)
Expand Down Expand Up @@ -144,7 +145,7 @@
map.forEach((k, v) -> {
PanelItemBuilder pib = new PanelItemBuilder();
pib.name(user.getTranslation("island.limits.panel.entity-name-syntax", TextVariables.NAME,
Util.prettifyText(k.toString())));
DisplayNames.entity(user, k)));
Material m;
try {
if (E2M.containsKey(k)) {
Expand Down Expand Up @@ -172,7 +173,7 @@
for (Entry<NamespacedKey, Integer> en : matLimits.entrySet()) {
PanelItemBuilder pib = new PanelItemBuilder();
pib.name(user.getTranslation("island.limits.panel.block-name-syntax", TextVariables.NAME,
Util.prettifyText(en.getKey().getKey())));
DisplayNames.material(user, en.getKey())));
Material mat = Registry.MATERIAL.get(B2M.getOrDefault(en.getKey(), en.getKey()));
pib.icon(Objects.requireNonNullElse(mat, Material.PAPER));

Expand Down Expand Up @@ -222,11 +223,11 @@
return "";
}

private String prettyNames(EntityGroup v) {
private String prettyNames(User user, EntityGroup v) {
StringBuilder sb = new StringBuilder();
List<EntityType> l = new ArrayList<>(v.getTypes());
for (int i = 0; i < l.size(); i++) {
sb.append(Util.prettifyText(l.get(i).toString()));
sb.append(DisplayNames.entity(user, l.get(i)));
if (i + 1 < l.size()) sb.append(", ");
if ((i + 1) % 5 == 0) sb.append("\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;

Check warning on line 54 in src/main/java/world/bentobox/limits/listeners/BlockLimitsListener.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import 'world.bentobox.bentobox.util.Util'.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_Limits&issues=AZ9MvhFVTGeptNLGQnUK&open=AZ9MvhFVTGeptNLGQnUK&pullRequest=286
import world.bentobox.limits.DisplayNames;
import world.bentobox.limits.Limits;
import world.bentobox.limits.Settings;
import world.bentobox.limits.objects.IslandBlockCount;
Expand Down Expand Up @@ -307,7 +308,7 @@
if (limit > -1) {
if (addon.getSettings().isShowLimitMessages()) {
user.notify("block-limits.hit-limit",
"[material]", Util.prettifyText(m.toString()),
"[material]", DisplayNames.material(user, m.getKey()),
TextVariables.NUMBER, String.valueOf(limit));
}
e.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
import world.bentobox.limits.EntityGroup;
import world.bentobox.limits.DisplayNames;
import world.bentobox.limits.Limits;
import world.bentobox.limits.Settings;
import world.bentobox.limits.objects.IslandBlockCount;
Expand Down Expand Up @@ -151,15 +152,16 @@ public void onBlock(HangingPlaceEvent hangingPlaceEvent) {
if (!addon.getSettings().isShowLimitMessages()) {
return;
}
User u = User.getInstance(player);
if (res.getTypelimit() != null) {
User.getInstance(player).notify("block-limits.hit-limit", "[material]",
Util.prettifyText(hangingPlaceEvent.getEntity().getType().toString()),
u.notify("block-limits.hit-limit", "[material]",
DisplayNames.entity(u, hangingPlaceEvent.getEntity().getType()),
TextVariables.NUMBER, String.valueOf(res.getTypelimit().getValue()));
} else {
User.getInstance(player).notify("block-limits.hit-limit", "[material]",
u.notify("block-limits.hit-limit", "[material]",
res.getGrouplimit().getKey().getName() + " ("
+ res.getGrouplimit().getKey().getTypes().stream()
.map(x -> Util.prettifyText(x.toString()))
.map(x -> DisplayNames.entity(u, x))
.collect(Collectors.joining(", "))
+ ")",
TextVariables.NUMBER, String.valueOf(res.getGrouplimit().getValue()));
Expand Down Expand Up @@ -235,15 +237,16 @@ private void notifyEntityLimit(Player player, EntityType type, AtLimitResult res
if (!addon.getSettings().isShowLimitMessages()) {
return;
}
User u = User.getInstance(player);
if (res.getTypelimit() != null) {
User.getInstance(player).notify(ENTITY_LIMIT_HIT, ENTITY_PLACEHOLDER,
Util.prettifyText(type.toString()), TextVariables.NUMBER,
u.notify(ENTITY_LIMIT_HIT, ENTITY_PLACEHOLDER,
DisplayNames.entity(u, type), TextVariables.NUMBER,
String.valueOf(res.getTypelimit().getValue()));
} else {
User.getInstance(player).notify(ENTITY_LIMIT_HIT, ENTITY_PLACEHOLDER,
u.notify(ENTITY_LIMIT_HIT, ENTITY_PLACEHOLDER,
res.getGrouplimit().getKey().getName() + " ("
+ res.getGrouplimit().getKey().getTypes().stream()
.map(x -> Util.prettifyText(x.toString()))
.map(x -> DisplayNames.entity(u, x))
.collect(Collectors.joining(", "))
+ ")",
TextVariables.NUMBER, String.valueOf(res.getGrouplimit().getValue()));
Expand Down Expand Up @@ -568,15 +571,16 @@ private void tellPlayers(Location location, Entity entity, SpawnReason spawnReas
for (Entity ent : w.getNearbyEntities(location, 5, 5, 5)) {
if (ent instanceof Player p) {
p.updateInventory();
User u = User.getInstance(p);
if (res.getTypelimit() != null) {
User.getInstance(p).notify(ENTITY_LIMIT_HIT, ENTITY_PLACEHOLDER,
Util.prettifyText(entity.getType().toString()),
u.notify(ENTITY_LIMIT_HIT, ENTITY_PLACEHOLDER,
DisplayNames.entity(u, entity.getType()),
TextVariables.NUMBER, String.valueOf(res.getTypelimit().getValue()));
} else {
User.getInstance(p).notify(ENTITY_LIMIT_HIT, ENTITY_PLACEHOLDER,
u.notify(ENTITY_LIMIT_HIT, ENTITY_PLACEHOLDER,
res.getGrouplimit().getKey().getName() + " ("
+ res.getGrouplimit().getKey().getTypes().stream()
.map(x -> Util.prettifyText(x.toString()))
.map(x -> DisplayNames.entity(u, x))
.collect(Collectors.joining(", "))
+ ")",
TextVariables.NUMBER, String.valueOf(res.getGrouplimit().getValue()));
Expand Down
145 changes: 76 additions & 69 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
@@ -1,69 +1,76 @@
###########################################################################################
# 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: "<red>[material] limited to [number]!"
entity-limits:
hit-limit: "<red>[entity] spawning limited to [number]!"
limits:
panel-title: "Island limits"

admin:
limits:
main:
parameters: "<player>"
description: "show the island limits for player"
calc:
parameters: "<player>"
description: "recalculate the island limits for player"
finished: "<green> Island recalc finished successfully!"
offset:
unknown: "<red> Unknown material or entity [name]."
description: "allows to manage limits offsets for materials and entities"
set:
parameters: "<player> <material|entity> <number>"
description: "sets new offset for material or entity limit"
success: "<green> Limit offset for [name] is set to [number]."
same: "<red> Limit offset for [name] is already [number]."
add:
parameters: "<player> <material|entity> <number>"
description: "adds offset for material or entity limit"
success: "<green> Limit offset for [name] is increased till [number]."
remove:
parameters: "<player> <material|entity> <number>"
description: "reduces offset for material or entity limit"
success: "<green> Limit offset for [name] is reduced till [number]."
reset:
parameters: "<player> <material|entity>"
description: "removes offset for material or entity"
success: "<green> Limit offset for [name] is set to 0."
view:
parameters: "<player> <material|entity>"
description: "displays offset for material or entity"
message: "<green> [name] offset is set to [number]."
island:
limits:
description: "show your island limits"
max-color: "<red>"
regular-color: "<green>"
block-limit-syntax: "[number]/[limit]"
no-limits: "<red> No limits set in this world"
panel:
title-syntax: '[title] [env]'
entity-group-name-syntax: '[name]'
entity-name-syntax: '[name]'
block-name-syntax: '[name]'
env-overworld: "Overworld"
env-nether: "Nether"
env-end: "End"
errors:
no-owner: "<red> That island has no owner"
not-on-island: "<red> This location does not have limits set."
recount:
description: "recounts limits for your island"
now-recounting: "<aqua> Now recounting. This could take a while, please wait..."
in-progress: "<red> Island recound is in progress. Please wait..."
time-out: "<red> Time out when recounting. Is the island really big?"

###########################################################################################
# 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: "<red>[material] limited to [number]!"
entity-limits:
hit-limit: "<red>[entity] spawning limited to [number]!"
limits:
panel-title: "Island limits"

admin:
limits:
main:
parameters: "<player>"
description: "show the island limits for player"
calc:
parameters: "<player>"
description: "recalculate the island limits for player"
finished: "<green> Island recalc finished successfully!"
offset:
unknown: "<red> Unknown material or entity [name]."
description: "allows to manage limits offsets for materials and entities"
set:
parameters: "<player> <material|entity> <number>"
description: "sets new offset for material or entity limit"
success: "<green> Limit offset for [name] is set to [number]."
same: "<red> Limit offset for [name] is already [number]."
add:
parameters: "<player> <material|entity> <number>"
description: "adds offset for material or entity limit"
success: "<green> Limit offset for [name] is increased till [number]."
remove:
parameters: "<player> <material|entity> <number>"
description: "reduces offset for material or entity limit"
success: "<green> Limit offset for [name] is reduced till [number]."
reset:
parameters: "<player> <material|entity>"
description: "removes offset for material or entity"
success: "<green> Limit offset for [name] is set to 0."
view:
parameters: "<player> <material|entity>"
description: "displays offset for material or entity"
message: "<green> [name] offset is set to [number]."
island:
limits:
description: "show your island limits"
# Optional manual name translations, used in the limits GUI and hit-limit messages.
# Keys are lowercase Bukkit Material / EntityType names. Anything not listed falls
# back to the automatic English name, so these sections can be left empty.
#materials:
# hopper: "Hopper"
#entities:
# enderman: "Enderman"
max-color: "<red>"
regular-color: "<green>"
block-limit-syntax: "[number]/[limit]"
no-limits: "<red> No limits set in this world"
panel:
title-syntax: '[title] [env]'
entity-group-name-syntax: '[name]'
entity-name-syntax: '[name]'
block-name-syntax: '[name]'
env-overworld: "Overworld"
env-nether: "Nether"
env-end: "End"
errors:
no-owner: "<red> That island has no owner"
not-on-island: "<red> This location does not have limits set."
recount:
description: "recounts limits for your island"
now-recounting: "<aqua> Now recounting. This could take a while, please wait..."
in-progress: "<red> Island recound is in progress. Please wait..."
time-out: "<red> Time out when recounting. Is the island really big?"

50 changes: 50 additions & 0 deletions src/test/java/world/bentobox/limits/DisplayNamesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package world.bentobox.limits;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import world.bentobox.bentobox.api.user.User;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class DisplayNamesTest {

@Mock
private User user;

@Test
void testMaterialTranslated() {
when(user.getTranslationOrNothing(anyString())).thenReturn("");
when(user.getTranslationOrNothing("island.limits.materials.hopper")).thenReturn("Trichter");
assertEquals("Trichter", DisplayNames.material(user, Material.HOPPER.getKey()));
}

@Test
void testMaterialFallsBackToPrettyName() {
when(user.getTranslationOrNothing(anyString())).thenReturn("");
assertEquals("Hopper", DisplayNames.material(user, Material.HOPPER.getKey()));
}

@Test
void testEntityTranslated() {
when(user.getTranslationOrNothing(anyString())).thenReturn("");
when(user.getTranslationOrNothing("island.limits.entities.enderman")).thenReturn("Enderman (DE)");
assertEquals("Enderman (DE)", DisplayNames.entity(user, EntityType.ENDERMAN));
}

@Test
void testEntityFallsBackToPrettyName() {
when(user.getTranslationOrNothing(anyString())).thenReturn("");
assertEquals("Enderman", DisplayNames.entity(user, EntityType.ENDERMAN));
}
}
Loading