Skip to content

Commit 840bbe7

Browse files
authored
Merge pull request #283 from BentoBoxWorld/feat/65-limit-message-toggle
Add show-limit-messages option to silence limit notifications
2 parents 7601929 + 98e731d commit 840bbe7

6 files changed

Lines changed: 87 additions & 3 deletions

File tree

src/main/java/world/bentobox/limits/Settings.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum GeneralGroup {
4949
private final List<String> gameModes;
5050
private final boolean logLimitsOnJoin;
5151
private final boolean asyncGolums;
52+
private final boolean showLimitMessages;
5253
private static final List<EntityType> DISALLOWED = Arrays.asList(
5354
EntityType.TNT,
5455
EntityType.EVOKER_FANGS,
@@ -93,6 +94,8 @@ public Settings(Limits addon) {
9394
logLimitsOnJoin = addon.getConfig().getBoolean("log-limits-on-join", true);
9495
// Async Golums
9596
asyncGolums = addon.getConfig().getBoolean("async-golums", true);
97+
// Show or suppress the "hit the limit" player notifications
98+
showLimitMessages = addon.getConfig().getBoolean("show-limit-messages", true);
9699

97100
addon.log("Entity limits:");
98101
envLimits.forEach((env, m) -> m.entrySet().stream()
@@ -256,6 +259,13 @@ public boolean isAsyncGolums() {
256259
return asyncGolums;
257260
}
258261

262+
/**
263+
* @return true if players should be told when they hit a limit; false to limit silently
264+
*/
265+
public boolean isShowLimitMessages() {
266+
return showLimitMessages;
267+
}
268+
259269
public Map<GeneralGroup, Integer> getGeneral() {
260270
return general;
261271
}

src/main/java/world/bentobox/limits/listeners/BlockLimitsListener.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,11 @@ public void onBlock(BlockMultiPlaceEvent e) {
305305

306306
private void notify(Cancellable e, User user, int limit, Material m) {
307307
if (limit > -1) {
308-
user.notify("block-limits.hit-limit",
309-
"[material]", Util.prettifyText(m.toString()),
310-
TextVariables.NUMBER, String.valueOf(limit));
308+
if (addon.getSettings().isShowLimitMessages()) {
309+
user.notify("block-limits.hit-limit",
310+
"[material]", Util.prettifyText(m.toString()),
311+
TextVariables.NUMBER, String.valueOf(limit));
312+
}
311313
e.setCancelled(true);
312314
}
313315
}

src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ public void onBlock(HangingPlaceEvent hangingPlaceEvent) {
148148
AtLimitResult res = atLimit(island, hangingPlaceEvent.getEntity());
149149
if (res.hit()) {
150150
hangingPlaceEvent.setCancelled(true);
151+
if (!addon.getSettings().isShowLimitMessages()) {
152+
return;
153+
}
151154
if (res.getTypelimit() != null) {
152155
User.getInstance(player).notify("block-limits.hit-limit", "[material]",
153156
Util.prettifyText(hangingPlaceEvent.getEntity().getType().toString()),
@@ -229,6 +232,9 @@ private EntityType spawnEggType(ItemStack item) {
229232
}
230233

231234
private void notifyEntityLimit(Player player, EntityType type, AtLimitResult res) {
235+
if (!addon.getSettings().isShowLimitMessages()) {
236+
return;
237+
}
232238
if (res.getTypelimit() != null) {
233239
User.getInstance(player).notify(ENTITY_LIMIT_HIT, ENTITY_PLACEHOLDER,
234240
Util.prettifyText(type.toString()), TextVariables.NUMBER,
@@ -548,6 +554,9 @@ private boolean isWither(Block block) {
548554
}
549555

550556
private void tellPlayers(Location location, Entity entity, SpawnReason spawnReason, AtLimitResult res) {
557+
if (!addon.getSettings().isShowLimitMessages()) {
558+
return;
559+
}
551560
if (spawnReason.equals(SpawnReason.SPAWNER) || spawnReason.equals(SpawnReason.NATURAL)
552561
|| spawnReason.equals(SpawnReason.INFECTION) || spawnReason.equals(SpawnReason.NETHER_PORTAL)
553562
|| spawnReason.equals(SpawnReason.REINFORCEMENTS) || spawnReason.equals(SpawnReason.SLIME_SPLIT)) {

src/main/resources/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ log-limits-on-join: true
3030
# Use async checks for snowmen and iron golums. Set to false if you see problems.
3131
async-golums: true
3232

33+
# Show players a message when they hit a limit. Set to false to enforce limits
34+
# silently - placements and spawns are still blocked, but no message is sent.
35+
show-limit-messages: true
36+
3337
# General block limiting
3438
# Use this section to limit how many blocks can be added to an island.
3539
# 0 means the item will be blocked from placement completely.

src/test/java/world/bentobox/limits/SettingsTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ void testAsyncGolumsDefaultsTrue() {
103103
assertTrue(settings.isAsyncGolums());
104104
}
105105

106+
@Test
107+
void testShowLimitMessagesDefaultsTrue() {
108+
assertTrue(settings.isShowLimitMessages());
109+
}
110+
111+
@Test
112+
void testShowLimitMessagesDisabled() {
113+
config.set("show-limit-messages", false);
114+
Settings s = new Settings(addon);
115+
assertFalse(s.isShowLimitMessages());
116+
}
117+
106118
@Test
107119
void testGetGeneralEmpty() {
108120
// Default config.yml does not have ANIMALS or MOBS entries in entitylimits

src/test/java/world/bentobox/limits/listeners/BlockLimitsListenerTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class BlockLimitsListenerTest {
9191
@Mock
9292
private Limits addon;
9393

94+
@Mock
95+
private world.bentobox.limits.Settings limitsSettings;
96+
9497
@Mock
9598
private World world;
9699

@@ -135,6 +138,8 @@ void setUp() throws Exception {
135138
doAnswer(invocation -> null).when(addon).log(anyString());
136139
doAnswer(invocation -> null).when(addon).logError(anyString());
137140
when(addon.isCoveredGameMode(anyString())).thenReturn(true);
141+
when(addon.getSettings()).thenReturn(limitsSettings);
142+
when(limitsSettings.isShowLimitMessages()).thenReturn(true);
138143
when(addon.inGameModeWorld(any(World.class))).thenReturn(false);
139144
// Override to true for our test world so event handlers don't bail early
140145
when(addon.inGameModeWorld(world)).thenReturn(true);
@@ -403,6 +408,48 @@ void testBlockPlaceAtLimitCancelsEvent() {
403408
assertTrue(event.isCancelled());
404409
}
405410

411+
@Test
412+
void testBlockPlaceAtLimitNotifiesWhenMessagesEnabled() {
413+
IslandBlockCount ibc = new IslandBlockCount("test-island-id", "BSkyBlock");
414+
for (int i = 0; i < 10; i++) {
415+
ibc.add(Environment.NORMAL, Material.HOPPER.getKey());
416+
}
417+
listener.setIsland("test-island-id", ibc);
418+
Notifier notifier = mock(Notifier.class);
419+
when(plugin.getNotifier()).thenReturn(notifier);
420+
421+
Block block = mockBlock(Material.HOPPER, blockLocation);
422+
BlockState replacedState = mock(BlockState.class);
423+
BlockPlaceEvent event = new BlockPlaceEvent(block, replacedState, block, new ItemStack(Material.HOPPER), player, true, EquipmentSlot.HAND);
424+
425+
listener.onBlock(event);
426+
427+
assertTrue(event.isCancelled());
428+
verify(notifier).notify(any(), anyString());
429+
}
430+
431+
@Test
432+
void testBlockPlaceAtLimitSilentWhenMessagesDisabled() {
433+
when(limitsSettings.isShowLimitMessages()).thenReturn(false);
434+
IslandBlockCount ibc = new IslandBlockCount("test-island-id", "BSkyBlock");
435+
for (int i = 0; i < 10; i++) {
436+
ibc.add(Environment.NORMAL, Material.HOPPER.getKey());
437+
}
438+
listener.setIsland("test-island-id", ibc);
439+
Notifier notifier = mock(Notifier.class);
440+
when(plugin.getNotifier()).thenReturn(notifier);
441+
442+
Block block = mockBlock(Material.HOPPER, blockLocation);
443+
BlockState replacedState = mock(BlockState.class);
444+
BlockPlaceEvent event = new BlockPlaceEvent(block, replacedState, block, new ItemStack(Material.HOPPER), player, true, EquipmentSlot.HAND);
445+
446+
listener.onBlock(event);
447+
448+
// Still enforced, just silent
449+
assertTrue(event.isCancelled());
450+
verify(notifier, never()).notify(any(), anyString());
451+
}
452+
406453
@Test
407454
void testBlockPlaceUnlimitedMaterialAllowed() {
408455
Block block = mockBlock(Material.DIRT, blockLocation);

0 commit comments

Comments
 (0)