mirror of
https://github.com/BentoBoxWorld/Level.git
synced 2024-11-27 12:38:21 +01:00
commit
cefb70a714
72
pom.xml
72
pom.xml
@ -41,13 +41,9 @@
|
|||||||
</issueManagement>
|
</issueManagement>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
<snapshotRepository>
|
|
||||||
<id>codemc-snapshots</id>
|
|
||||||
<url>https://repo.codemc.org/repository/maven-snapshots</url>
|
|
||||||
</snapshotRepository>
|
|
||||||
<repository>
|
<repository>
|
||||||
<id>codemc-releases</id>
|
<id>bentoboxworld</id>
|
||||||
<url>https://repo.codemc.org/repository/maven-releases</url>
|
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|
||||||
@ -58,8 +54,8 @@
|
|||||||
<!-- Non-minecraft related dependencies -->
|
<!-- Non-minecraft related dependencies -->
|
||||||
<powermock.version>2.0.9</powermock.version>
|
<powermock.version>2.0.9</powermock.version>
|
||||||
<!-- More visible way how to change dependency versions -->
|
<!-- More visible way how to change dependency versions -->
|
||||||
<spigot.version>1.20.4-R0.1-SNAPSHOT</spigot.version>
|
<spigot.version>1.21.3-R0.1-SNAPSHOT</spigot.version>
|
||||||
<bentobox.version>2.5.1-SNAPSHOT</bentobox.version>
|
<bentobox.version>2.7.1-SNAPSHOT</bentobox.version>
|
||||||
<!-- Warps addon version -->
|
<!-- Warps addon version -->
|
||||||
<warps.version>1.12.0</warps.version>
|
<warps.version>1.12.0</warps.version>
|
||||||
<!-- Visit addon version -->
|
<!-- Visit addon version -->
|
||||||
@ -71,7 +67,7 @@
|
|||||||
<!-- Do not change unless you want different name for local builds. -->
|
<!-- Do not change unless you want different name for local builds. -->
|
||||||
<build.number>-LOCAL</build.number>
|
<build.number>-LOCAL</build.number>
|
||||||
<!-- This allows to change between versions. -->
|
<!-- This allows to change between versions. -->
|
||||||
<build.version>2.16.1</build.version>
|
<build.version>2.17.0</build.version>
|
||||||
<sonar.projectKey>BentoBoxWorld_Level</sonar.projectKey>
|
<sonar.projectKey>BentoBoxWorld_Level</sonar.projectKey>
|
||||||
<sonar.organization>bentobox-world</sonar.organization>
|
<sonar.organization>bentobox-world</sonar.organization>
|
||||||
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
|
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
|
||||||
@ -127,6 +123,26 @@
|
|||||||
</pluginRepositories>
|
</pluginRepositories>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spigot-repo</id>
|
||||||
|
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>codemc</id>
|
||||||
|
<url>https://repo.codemc.org/repository/maven-snapshots/</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>codemc-repo</id>
|
||||||
|
<url>https://repo.codemc.org/repository/maven-public/</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>bentoboxworld</id>
|
||||||
|
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>jitpack.io</id>
|
||||||
|
<url>https://jitpack.io</url>
|
||||||
|
</repository>
|
||||||
<!--Wild Stacker repo -->
|
<!--Wild Stacker repo -->
|
||||||
<repository>
|
<repository>
|
||||||
<id>bg-repo</id>
|
<id>bg-repo</id>
|
||||||
@ -142,22 +158,6 @@
|
|||||||
<id>songoda-plugins</id>
|
<id>songoda-plugins</id>
|
||||||
<url>https://repo.songoda.com/repository/minecraft-plugins/</url>
|
<url>https://repo.songoda.com/repository/minecraft-plugins/</url>
|
||||||
</repository>
|
</repository>
|
||||||
<repository>
|
|
||||||
<id>spigot-repo</id>
|
|
||||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>codemc-repo</id>
|
|
||||||
<url>https://repo.codemc.org/repository/maven-public/</url>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>codemc-public</id>
|
|
||||||
<url>https://repo.codemc.org/repository/maven-public/</url>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>jitpack.io</id>
|
|
||||||
<url>https://jitpack.io</url>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -235,7 +235,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>dev.rosewood</groupId>
|
<groupId>dev.rosewood</groupId>
|
||||||
<artifactId>rosestacker</artifactId>
|
<artifactId>rosestacker</artifactId>
|
||||||
<version>1.3.0</version>
|
<version>1.5.27</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Ultimate Stacker dependency -->
|
<!-- Ultimate Stacker dependency -->
|
||||||
@ -274,17 +274,17 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-clean-plugin</artifactId>
|
<artifactId>maven-clean-plugin</artifactId>
|
||||||
<version>3.1.0</version>
|
<version>3.4.0</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-resources-plugin</artifactId>
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
<version>3.1.0</version>
|
<version>3.3.1</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.8.0</version>
|
<version>3.13.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<release>${java.version}</release>
|
<release>${java.version}</release>
|
||||||
</configuration>
|
</configuration>
|
||||||
@ -292,7 +292,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>3.0.0-M5</version>
|
<version>3.5.2</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<argLine>
|
<argLine>
|
||||||
${argLine}
|
${argLine}
|
||||||
@ -329,12 +329,12 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
<version>3.1.0</version>
|
<version>3.4.2</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
<version>3.0.1</version>
|
<version>3.11.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<doclint>none</doclint> <!-- Turnoff all checks -->
|
<doclint>none</doclint> <!-- Turnoff all checks -->
|
||||||
<failOnError>false</failOnError>
|
<failOnError>false</failOnError>
|
||||||
@ -353,7 +353,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-source-plugin</artifactId>
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
<version>3.0.1</version>
|
<version>3.3.1</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>attach-sources</id>
|
<id>attach-sources</id>
|
||||||
@ -366,17 +366,17 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-install-plugin</artifactId>
|
<artifactId>maven-install-plugin</artifactId>
|
||||||
<version>2.5.2</version>
|
<version>3.1.3</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-deploy-plugin</artifactId>
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
<version>2.8.2</version>
|
<version>3.1.3</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-shade-plugin</artifactId>
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
<version>3.3.1-SNAPSHOT</version>
|
<version>3.6.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<minimizeJar>true</minimizeJar>
|
<minimizeJar>true</minimizeJar>
|
||||||
<artifactSet>
|
<artifactSet>
|
||||||
|
@ -2,6 +2,7 @@ package world.bentobox.level;
|
|||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -111,7 +112,8 @@ public class PlaceholderManager {
|
|||||||
rank = Math.max(1, Math.min(rank, Level.TEN));
|
rank = Math.max(1, Math.min(rank, Level.TEN));
|
||||||
if (weighted) {
|
if (weighted) {
|
||||||
return addon.getManager().getWeightedTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L)
|
return addon.getManager().getWeightedTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L)
|
||||||
.findFirst().map(Island::getOwner).map(addon.getPlayers()::getName).orElse("");
|
.findFirst().map(Island::getOwner).filter(Objects::nonNull).map(addon.getPlayers()::getName)
|
||||||
|
.orElse("");
|
||||||
}
|
}
|
||||||
@Nullable
|
@Nullable
|
||||||
UUID owner = addon.getManager().getTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L)
|
UUID owner = addon.getManager().getTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L)
|
||||||
|
@ -62,7 +62,12 @@ public class IslandLevelCalculator {
|
|||||||
Material.LIME_SHULKER_BOX, Material.MAGENTA_SHULKER_BOX, Material.ORANGE_SHULKER_BOX,
|
Material.LIME_SHULKER_BOX, Material.MAGENTA_SHULKER_BOX, Material.ORANGE_SHULKER_BOX,
|
||||||
Material.PINK_SHULKER_BOX, Material.PURPLE_SHULKER_BOX, Material.RED_SHULKER_BOX, Material.RED_SHULKER_BOX,
|
Material.PINK_SHULKER_BOX, Material.PURPLE_SHULKER_BOX, Material.RED_SHULKER_BOX, Material.RED_SHULKER_BOX,
|
||||||
Material.WHITE_SHULKER_BOX, Material.YELLOW_SHULKER_BOX, Material.COMPOSTER, Material.BARREL,
|
Material.WHITE_SHULKER_BOX, Material.YELLOW_SHULKER_BOX, Material.COMPOSTER, Material.BARREL,
|
||||||
Material.DISPENSER, Material.DROPPER, Material.SMOKER, Material.BLAST_FURNACE);
|
Material.DISPENSER, Material.DROPPER, Material.SMOKER, Material.BLAST_FURNACE, Material.BUNDLE,
|
||||||
|
Material.RED_BUNDLE, Material.BLACK_BUNDLE, Material.BLUE_BUNDLE, Material.BROWN_BUNDLE,
|
||||||
|
Material.CYAN_BUNDLE, Material.GRAY_BUNDLE, Material.GREEN_BUNDLE, Material.LIGHT_BLUE_BUNDLE,
|
||||||
|
Material.LIGHT_GRAY_BUNDLE, Material.LIME_BUNDLE, Material.MAGENTA_BUNDLE, Material.ORANGE_BUNDLE,
|
||||||
|
Material.PINK_BUNDLE, Material.PURPLE_BUNDLE, Material.RED_BUNDLE, Material.WHITE_BUNDLE,
|
||||||
|
Material.YELLOW_BUNDLE);
|
||||||
private static final int CHUNKS_TO_SCAN = 100;
|
private static final int CHUNKS_TO_SCAN = 100;
|
||||||
private final Level addon;
|
private final Level addon;
|
||||||
private final Queue<Pair<Integer, Integer>> chunksToCheck;
|
private final Queue<Pair<Integer, Integer>> chunksToCheck;
|
||||||
@ -469,6 +474,7 @@ public class IslandLevelCalculator {
|
|||||||
|
|
||||||
// Scan chests
|
// Scan chests
|
||||||
if (addon.getSettings().isIncludeChests() && CHESTS.contains(m)) {
|
if (addon.getSettings().isIncludeChests() && CHESTS.contains(m)) {
|
||||||
|
|
||||||
chestBlocks.add(cp.chunk);
|
chestBlocks.add(cp.chunk);
|
||||||
}
|
}
|
||||||
// Add the value of the block's material
|
// Add the value of the block's material
|
||||||
|
@ -13,6 +13,7 @@ import java.util.Objects;
|
|||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.Registry;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
@ -96,7 +97,7 @@ public class BlockConfig {
|
|||||||
ConfigurationSection blocks = Objects.requireNonNull(blockValues2.getConfigurationSection("blocks"));
|
ConfigurationSection blocks = Objects.requireNonNull(blockValues2.getConfigurationSection("blocks"));
|
||||||
Map<Material, Integer> bv = new EnumMap<>(Material.class);
|
Map<Material, Integer> bv = new EnumMap<>(Material.class);
|
||||||
// Update blockvalues to latest settings
|
// Update blockvalues to latest settings
|
||||||
Arrays.stream(Material.values()).filter(Material::isBlock)
|
Registry.MATERIAL.stream().filter(Material::isBlock)
|
||||||
.filter(m -> !m.name().startsWith("LEGACY_"))
|
.filter(m -> !m.name().startsWith("LEGACY_"))
|
||||||
.filter(m -> !m.isAir())
|
.filter(m -> !m.isAir())
|
||||||
.filter(m -> !m.equals(Material.WATER))
|
.filter(m -> !m.equals(Material.WATER))
|
||||||
|
@ -58,6 +58,7 @@ public class ValuePanel
|
|||||||
this.activeFilter = Filter.NAME_ASC;
|
this.activeFilter = Filter.NAME_ASC;
|
||||||
this.materialRecordList = Arrays.stream(Material.values()).
|
this.materialRecordList = Arrays.stream(Material.values()).
|
||||||
filter(Material::isBlock).
|
filter(Material::isBlock).
|
||||||
|
filter(Material::isItem). // Remove things like PITCHER_CROP
|
||||||
filter(m -> !m.name().startsWith("LEGACY_")).
|
filter(m -> !m.name().startsWith("LEGACY_")).
|
||||||
filter(this.addon.getBlockConfig()::isNotHiddenBlock).
|
filter(this.addon.getBlockConfig()::isNotHiddenBlock).
|
||||||
map(material ->
|
map(material ->
|
||||||
@ -584,6 +585,7 @@ public class ValuePanel
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
int index = this.pageIndex * slot.amountMap().getOrDefault(BLOCK, 1) + slot.slot();
|
int index = this.pageIndex * slot.amountMap().getOrDefault(BLOCK, 1) + slot.slot();
|
||||||
|
|
||||||
if (index >= this.elementList.size())
|
if (index >= this.elementList.size())
|
||||||
|
@ -2,7 +2,7 @@ name: Level
|
|||||||
main: world.bentobox.level.Level
|
main: world.bentobox.level.Level
|
||||||
version: ${version}${build.number}
|
version: ${version}${build.number}
|
||||||
icon: DIAMOND
|
icon: DIAMOND
|
||||||
api-version: 2.5.1
|
api-version: 2.7.1
|
||||||
|
|
||||||
authors: tastybento
|
authors: tastybento
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
name: BentoBox-Level
|
name: BentoBox-Level
|
||||||
main: world.bentobox.level.LevelPladdon
|
main: world.bentobox.level.LevelPladdon
|
||||||
version: ${project.version}${build.number}
|
version: ${project.version}${build.number}
|
||||||
api-version: "1.19"
|
api-version: "1.21"
|
||||||
|
|
||||||
authors: [tastybento]
|
authors: [tastybento]
|
||||||
contributors: ["The BentoBoxWorld Community"]
|
contributors: ["The BentoBoxWorld Community"]
|
||||||
|
@ -66,6 +66,7 @@ import world.bentobox.level.config.BlockConfig;
|
|||||||
import world.bentobox.level.config.ConfigSettings;
|
import world.bentobox.level.config.ConfigSettings;
|
||||||
import world.bentobox.level.listeners.IslandActivitiesListeners;
|
import world.bentobox.level.listeners.IslandActivitiesListeners;
|
||||||
import world.bentobox.level.listeners.JoinLeaveListener;
|
import world.bentobox.level.listeners.JoinLeaveListener;
|
||||||
|
import world.bentobox.level.mocks.ServerMocks;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author tastybento
|
* @author tastybento
|
||||||
@ -147,6 +148,7 @@ public class LevelTest {
|
|||||||
*/
|
*/
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
|
Server server = ServerMocks.newServer();
|
||||||
// Set up plugin
|
// Set up plugin
|
||||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||||
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||||
@ -186,7 +188,6 @@ public class LevelTest {
|
|||||||
|
|
||||||
// Server
|
// Server
|
||||||
PowerMockito.mockStatic(Bukkit.class);
|
PowerMockito.mockStatic(Bukkit.class);
|
||||||
Server server = mock(Server.class);
|
|
||||||
when(Bukkit.getServer()).thenReturn(server);
|
when(Bukkit.getServer()).thenReturn(server);
|
||||||
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||||
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class));
|
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class));
|
||||||
@ -252,6 +253,7 @@ public class LevelTest {
|
|||||||
*/
|
*/
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
|
ServerMocks.unsetBukkitServer();
|
||||||
deleteAll(new File("database"));
|
deleteAll(new File("database"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
118
src/test/java/world/bentobox/level/mocks/ServerMocks.java
Normal file
118
src/test/java/world/bentobox/level/mocks/ServerMocks.java
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
package world.bentobox.level.mocks;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.notNull;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Keyed;
|
||||||
|
import org.bukkit.NamespacedKey;
|
||||||
|
import org.bukkit.Registry;
|
||||||
|
import org.bukkit.Server;
|
||||||
|
import org.bukkit.Tag;
|
||||||
|
import org.bukkit.UnsafeValues;
|
||||||
|
import org.eclipse.jdt.annotation.NonNull;
|
||||||
|
|
||||||
|
public final class ServerMocks {
|
||||||
|
|
||||||
|
public static @NonNull Server newServer() {
|
||||||
|
Server mock = mock(Server.class);
|
||||||
|
|
||||||
|
Logger noOp = mock(Logger.class);
|
||||||
|
when(mock.getLogger()).thenReturn(noOp);
|
||||||
|
when(mock.isPrimaryThread()).thenReturn(true);
|
||||||
|
|
||||||
|
// Unsafe
|
||||||
|
UnsafeValues unsafe = mock(UnsafeValues.class);
|
||||||
|
when(mock.getUnsafe()).thenReturn(unsafe);
|
||||||
|
|
||||||
|
// Server must be available before tags can be mocked.
|
||||||
|
Bukkit.setServer(mock);
|
||||||
|
|
||||||
|
// Bukkit has a lot of static constants referencing registry values. To initialize those, the
|
||||||
|
// registries must be able to be fetched before the classes are touched.
|
||||||
|
Map<Class<? extends Keyed>, Object> registers = new HashMap<>();
|
||||||
|
|
||||||
|
doAnswer(invocationGetRegistry -> registers.computeIfAbsent(invocationGetRegistry.getArgument(0), clazz -> {
|
||||||
|
Registry<?> registry = mock(Registry.class);
|
||||||
|
Map<NamespacedKey, Keyed> cache = new HashMap<>();
|
||||||
|
doAnswer(invocationGetEntry -> {
|
||||||
|
NamespacedKey key = invocationGetEntry.getArgument(0);
|
||||||
|
// Some classes (like BlockType and ItemType) have extra generics that will be
|
||||||
|
// erased during runtime calls. To ensure accurate typing, grab the constant's field.
|
||||||
|
// This approach also allows us to return null for unsupported keys.
|
||||||
|
Class<? extends Keyed> constantClazz;
|
||||||
|
try {
|
||||||
|
//noinspection unchecked
|
||||||
|
constantClazz = (Class<? extends Keyed>) clazz
|
||||||
|
.getField(key.getKey().toUpperCase(Locale.ROOT).replace('.', '_')).getType();
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cache.computeIfAbsent(key, key1 -> {
|
||||||
|
Keyed keyed = mock(constantClazz);
|
||||||
|
doReturn(key).when(keyed).getKey();
|
||||||
|
return keyed;
|
||||||
|
});
|
||||||
|
}).when(registry).get(notNull());
|
||||||
|
return registry;
|
||||||
|
})).when(mock).getRegistry(notNull());
|
||||||
|
|
||||||
|
// Tags are dependent on registries, but use a different method.
|
||||||
|
// This will set up blank tags for each constant; all that needs to be done to render them
|
||||||
|
// functional is to re-mock Tag#getValues.
|
||||||
|
doAnswer(invocationGetTag -> {
|
||||||
|
Tag<?> tag = mock(Tag.class);
|
||||||
|
doReturn(invocationGetTag.getArgument(1)).when(tag).getKey();
|
||||||
|
doReturn(Set.of()).when(tag).getValues();
|
||||||
|
doAnswer(invocationIsTagged -> {
|
||||||
|
Keyed keyed = invocationIsTagged.getArgument(0);
|
||||||
|
Class<?> type = invocationGetTag.getArgument(2);
|
||||||
|
if (!type.isAssignableFrom(keyed.getClass())) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// Since these are mocks, the exact instance might not be equal. Consider equal keys equal.
|
||||||
|
return tag.getValues().contains(keyed)
|
||||||
|
|| tag.getValues().stream().anyMatch(value -> value.getKey().equals(keyed.getKey()));
|
||||||
|
}).when(tag).isTagged(notNull());
|
||||||
|
return tag;
|
||||||
|
}).when(mock).getTag(notNull(), notNull(), notNull());
|
||||||
|
|
||||||
|
// Once the server is all set up, touch BlockType and ItemType to initialize.
|
||||||
|
// This prevents issues when trying to access dependent methods from a Material constant.
|
||||||
|
try {
|
||||||
|
Class.forName("org.bukkit.inventory.ItemType");
|
||||||
|
Class.forName("org.bukkit.block.BlockType");
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void unsetBukkitServer() {
|
||||||
|
try {
|
||||||
|
Field server = Bukkit.class.getDeclaredField("server");
|
||||||
|
server.setAccessible(true);
|
||||||
|
server.set(null, null);
|
||||||
|
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ServerMocks() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user