Adds multiple home support.
Some checks failed
Java CI with Gradle / build (push) Has been cancelled

This commit is contained in:
2026-09-27 04:15:26 -07:00
parent 013dc6749c
commit a13f512be6
12 changed files with 488 additions and 45 deletions

View File

@@ -0,0 +1,41 @@
/*
This file is part of Metropolis.
Metropolis is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Metropolis is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Metropolis. If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
package com.majinnaibu.bukkitplugins.metropolis;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import java.util.UUID;
import com.sk89q.worldedit.math.BlockVector3;
import org.junit.jupiter.api.Test;
class HomeNumberAllocatorTest {
@Test
void choosesTheFirstUnusedPositiveHomeNumber() {
UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
PlayerHome first =
new PlayerHome(playerId, "PlayerName", 1, BlockVector3.ZERO, BlockVector3.ZERO);
PlayerHome third =
new PlayerHome(playerId, "PlayerName", 3, BlockVector3.ZERO, BlockVector3.ZERO);
assertEquals(1, HomeNumberAllocator.nextHomeNumber(List.of()));
assertEquals(2, HomeNumberAllocator.nextHomeNumber(List.of(first, third)));
}
}

View File

@@ -0,0 +1,130 @@
/*
This file is part of Metropolis.
Metropolis is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Metropolis is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Metropolis. If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
package com.majinnaibu.bukkitplugins.metropolis;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.jupiter.api.Test;
class HomePermissionOverrideTest {
@Test
void selectsTheHighestPriorityAndUsesLastEntryForTies() throws InvalidConfigurationException {
YamlConfiguration config = new YamlConfiguration();
config.loadFromString(
"""
permissionOverrides:
metropolis.plots.vip:
priority: 10
plotMultiplier: 2
maxPlots: 2
metropolis.plots.elite:
priority: 20
plotMultiplier: 3
maxPlots: 5
metropolis.plots.staff:
priority: 20
plotMultiplier: 4
maxPlots: 8
""");
List<String> warnings = new ArrayList<>();
List<HomePermissionOverride> overrides =
HomePermissionOverride.load(
config.getConfigurationSection("permissionOverrides"), warnings::add);
HomePermissionOverride match =
HomePermissionOverride.findMatch(
overrides,
Set.of("metropolis.plots.vip", "metropolis.plots.elite", "metropolis.plots.staff")
::contains);
assertEquals(3, overrides.size());
assertEquals("metropolis.plots.staff", match.getPermission());
assertEquals(4, match.getPlotMultiplier());
assertEquals(8, match.getMaxPlots());
assertEquals(List.of(), warnings);
}
@Test
void preservesDottedPermissionNamesAndSkipsInvalidEntries() throws InvalidConfigurationException {
YamlConfiguration config = new YamlConfiguration();
config.loadFromString(
"""
permissionOverrides:
metropolis.plots.vip:
priority: 3
plotMultiplier: 2
maxPlots: 4
metropolis.plots.invalid:
plotMultiplier: 2
maxPlots: 4
""");
List<String> warnings = new ArrayList<>();
List<HomePermissionOverride> overrides =
HomePermissionOverride.load(
config.getConfigurationSection("permissionOverrides"), warnings::add);
HomePermissionOverride match =
HomePermissionOverride.findMatch(overrides, "metropolis.plots.vip"::equals);
assertEquals(1, overrides.size());
assertEquals("metropolis.plots.vip", overrides.get(0).getPermission());
assertSame(overrides.get(0), match);
assertNull(HomePermissionOverride.findMatch(overrides, ignored -> false));
assertEquals(1, warnings.size());
}
@Test
void resolvesUserOverrideBeforePermissionAndGlobalFallback()
throws InvalidConfigurationException {
YamlConfiguration config = new YamlConfiguration();
config.set("permissionOverrides.metropolis.plots.vip.priority", 10);
config.set("permissionOverrides.metropolis.plots.vip.plotMultiplier", 2);
config.set("permissionOverrides.metropolis.plots.vip.maxPlots", 4);
List<HomePermissionOverride> overrides =
HomePermissionOverride.load(
config.getConfigurationSection("permissionOverrides"), ignored -> {});
UserOverride userOverride = new UserOverride("PlayerName", 3, 6);
assertEquals(
6,
HomePermissionOverride.resolveMaxPlots(
userOverride, overrides, "metropolis.plots.vip"::equals, 1));
assertEquals(
3,
HomePermissionOverride.resolvePlotMultiplier(
userOverride, overrides, "metropolis.plots.vip"::equals, 1));
assertEquals(
4,
HomePermissionOverride.resolveMaxPlots(null, overrides, "metropolis.plots.vip"::equals, 1));
assertEquals(
2,
HomePermissionOverride.resolvePlotMultiplier(
null, overrides, "metropolis.plots.vip"::equals, 1));
assertEquals(1, HomePermissionOverride.resolveMaxPlots(null, overrides, ignored -> false, 1));
assertEquals(
1, HomePermissionOverride.resolvePlotMultiplier(null, overrides, ignored -> false, 1));
}
}

View File

@@ -27,6 +27,7 @@ import static org.mockito.Mockito.when;
import java.util.UUID;
import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin;
import com.majinnaibu.bukkitplugins.metropolis.PlayerHome;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
@@ -40,12 +41,13 @@ class MetropolisHomeAcquireTest {
UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
when(player.getUniqueId()).thenReturn(playerId);
when(plugin.getNumPlots(playerId)).thenReturn(0);
when(plugin.getMaxPlots(playerId)).thenReturn(1);
when(plugin.getMaxPlots(player)).thenReturn(1);
when(plugin.acquireHome(player)).thenReturn(mock(PlayerHome.class));
MetropolisHomeAcquire executor = new MetropolisHomeAcquire(plugin);
assertTrue(executor.onCommand(player, mock(Command.class), "acquire", new String[0]));
verify(plugin).assignPlot(player);
verify(plugin).acquireHome(player);
}
@Test
@@ -55,11 +57,11 @@ class MetropolisHomeAcquireTest {
UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
when(player.getUniqueId()).thenReturn(playerId);
when(plugin.getNumPlots(playerId)).thenReturn(1);
when(plugin.getMaxPlots(playerId)).thenReturn(1);
when(plugin.getMaxPlots(player)).thenReturn(1);
MetropolisHomeAcquire executor = new MetropolisHomeAcquire(plugin);
assertFalse(executor.onCommand(player, mock(Command.class), "acquire", new String[0]));
verify(plugin, never()).assignPlot(player);
verify(plugin, never()).acquireHome(player);
}
}