Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mc 1 21 3 #99

Merged
merged 3 commits into from
Nov 9, 2024
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
26 changes: 10 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,27 @@
</issueManagement>

<distributionManagement>
<snapshotRepository>
<id>codemc-snapshots</id>
<url>https://repo.codemc.org/repository/maven-snapshots</url>
</snapshotRepository>
<repository>
<id>codemc-releases</id>
<url>https://repo.codemc.org/repository/maven-releases</url>
<id>bentoboxworld</id>
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
</repository>
</distributionManagement>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<java.version>21</java.version>
<!-- Non-minecraft related dependencies -->
<powermock.version>2.0.9</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.21.1-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.5.1-SNAPSHOT</bentobox.version>
<spigot.version>1.21.3-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.7.1-SNAPSHOT</bentobox.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>2.7.3</build.version>
<build.version>2.8.0</build.version>

<sonar.projectKey>BentoBoxWorld_Boxed</sonar.projectKey>
<sonar.organization>bentobox-world</sonar.organization>
Expand Down Expand Up @@ -119,6 +115,10 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
</repository>
<repository>
<id>bentoboxworld</id>
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
</repository>
<repository>
<id>codemc</id>
<url>https://repo.codemc.org/repository/maven-snapshots/</url>
Expand Down Expand Up @@ -185,12 +185,6 @@
<version>${spigot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc.....</groupId>
<artifactId>spigot</artifactId>
<version>1.21-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc......</groupId>
<artifactId>spigot</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package world.bentobox.boxed.generators.biomes;

import java.util.Arrays;
import java.util.List;

import org.bukkit.Registry;
import org.bukkit.World.Environment;
import org.bukkit.block.Biome;
import org.bukkit.generator.BiomeProvider;
import org.bukkit.generator.WorldInfo;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.Nullable;

import com.google.common.base.Enums;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.boxed.Boxed;
import world.bentobox.boxed.generators.chunks.AbstractBoxedChunkGenerator;
Expand Down Expand Up @@ -56,7 +54,7 @@ public Biome getBiome(WorldInfo worldInfo, int x, int y, int z) {
@Override
public List<Biome> getBiomes(WorldInfo worldInfo) {
// Return all of them for now!
return Arrays.stream(Biome.values()).filter(b -> !b.equals(Biome.CUSTOM)).filter(b -> !b.equals(Enums.getIfPresent(Biome.class, "CHERRY_GROVE").orNull())).toList();
return Registry.BIOME.stream().filter(b -> !b.equals(Biome.CUSTOM)).toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.SortedMap;
import java.util.TreeMap;

import org.bukkit.Registry;
import org.bukkit.World.Environment;
import org.bukkit.block.Biome;
import org.bukkit.block.BlockFace;
Expand All @@ -21,8 +22,6 @@
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;

import com.google.common.base.Enums;

import world.bentobox.boxed.Boxed;

/**
Expand Down Expand Up @@ -1129,7 +1128,7 @@ Biome getBeachBiome(int t) {
}

public static double convertToY(double x) {
x = Math.max(-1, Math.min(1, x)); // Clamp value
x = Math.clamp(x, -1, 1);
if (x >= -1 && x < -0.5) {
return 2 * x + 1;
} else if (x >= -0.5 && x < 0) {
Expand Down Expand Up @@ -1196,7 +1195,7 @@ private Biome getMappedBiome(WorldInfo worldInfo, int x, int y, int z, BiomePara
@Override
public List<Biome> getBiomes(WorldInfo worldInfo) {
// Return all of them for now!
return Arrays.stream(Biome.values()).filter(b -> !b.equals(Biome.CUSTOM)).toList();
return Registry.BIOME.stream().filter(b -> !b.equals(Biome.CUSTOM)).toList();
}

/**
Expand All @@ -1215,7 +1214,7 @@ private SortedMap<Double, Biome> loadQuad(YamlConfiguration config, String secto
if (split.length == 2) {
try {
double d = Double.parseDouble(split[0]);
Biome biome = Enums.getIfPresent(Biome.class, split[1].toUpperCase(Locale.ENGLISH)).orNull();
Biome biome = Biome.valueOf(split[1].toUpperCase(Locale.ENGLISH));
if (biome == null) {
addon.logError(split[1].toUpperCase(Locale.ENGLISH) + " is an unknown biome on this server.");
result.put(d, Biome.CUSTOM);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package world.bentobox.boxed.nms.v1_21_3_R0_1_SNAPSHOT;

public class GetMetaData extends world.bentobox.boxed.nms.v1_21_R0_1_SNAPSHOT.GetMetaData {
// Identical to 1.21
}
2 changes: 1 addition & 1 deletion src/main/resources/addon.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Boxed
main: world.bentobox.boxed.Boxed
version: ${version}${build.number}
api-version: 2.5.1
api-version: 2.7.1
metrics: true
icon: "COMPOSTER"
repository: "BentoBoxWorld/Boxed"
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: BentoBox-Boxed
main: world.bentobox.boxed.BoxedPladdon
version: ${project.version}${build.number}
api-version: "1.19"
api-version: "1.21"

authors: [tastybento]
contributors: ["The BentoBoxWorld Community"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -55,6 +56,7 @@
import world.bentobox.bentobox.util.Util;
import world.bentobox.boxed.Boxed;
import world.bentobox.boxed.Settings;
import world.bentobox.boxed.mocks.ServerMocks;

/**
* @author tastybento
Expand Down Expand Up @@ -102,7 +104,7 @@ public class EnderPearlListenerTest {
*/
@Before
public void setUp() throws Exception {

ServerMocks.newServer();
// Set up plugin
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Expand Down Expand Up @@ -170,6 +172,13 @@ public void setUp() throws Exception {
epl = new EnderPearlListener(addon);
}

@After
public void tearDown() {
ServerMocks.unsetBukkitServer();
User.clearUsers();
Mockito.framework().clearInlineMocks();
}

/**
* Test method for {@link world.bentobox.boxed.listeners.EnderPearlListener#EnderPearlListener(world.bentobox.boxed.Boxed)}.
*/
Expand Down
118 changes: 118 additions & 0 deletions src/test/java/world/bentobox/boxed/mocks/ServerMocks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package world.bentobox.boxed.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