Skip to content

Commit b1db4c1

Browse files
authored
Merge pull request #99 from BentoBoxWorld/MC_1_21_3
Mc 1 21 3
2 parents b3a81c6 + 90121c4 commit b1db4c1

8 files changed

Lines changed: 151 additions & 28 deletions

File tree

pom.xml

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,27 @@
4141
</issueManagement>
4242

4343
<distributionManagement>
44-
<snapshotRepository>
45-
<id>codemc-snapshots</id>
46-
<url>https://repo.codemc.org/repository/maven-snapshots</url>
47-
</snapshotRepository>
4844
<repository>
49-
<id>codemc-releases</id>
50-
<url>https://repo.codemc.org/repository/maven-releases</url>
45+
<id>bentoboxworld</id>
46+
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
5147
</repository>
5248
</distributionManagement>
5349

5450
<properties>
5551
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5652
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
57-
<java.version>17</java.version>
53+
<java.version>21</java.version>
5854
<!-- Non-minecraft related dependencies -->
5955
<powermock.version>2.0.9</powermock.version>
6056
<!-- More visible way how to change dependency versions -->
61-
<spigot.version>1.21.1-R0.1-SNAPSHOT</spigot.version>
62-
<bentobox.version>2.5.1-SNAPSHOT</bentobox.version>
57+
<spigot.version>1.21.3-R0.1-SNAPSHOT</spigot.version>
58+
<bentobox.version>2.7.1-SNAPSHOT</bentobox.version>
6359
<!-- Revision variable removes warning about dynamic version -->
6460
<revision>${build.version}-SNAPSHOT</revision>
6561
<!-- Do not change unless you want different name for local builds. -->
6662
<build.number>-LOCAL</build.number>
6763
<!-- This allows to change between versions. -->
68-
<build.version>2.7.3</build.version>
64+
<build.version>2.8.0</build.version>
6965

7066
<sonar.projectKey>BentoBoxWorld_Boxed</sonar.projectKey>
7167
<sonar.organization>bentobox-world</sonar.organization>
@@ -119,6 +115,10 @@
119115
<id>spigot-repo</id>
120116
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
121117
</repository>
118+
<repository>
119+
<id>bentoboxworld</id>
120+
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
121+
</repository>
122122
<repository>
123123
<id>codemc</id>
124124
<url>https://repo.codemc.org/repository/maven-snapshots/</url>
@@ -185,12 +185,6 @@
185185
<version>${spigot.version}</version>
186186
<scope>provided</scope>
187187
</dependency>
188-
<dependency>
189-
<groupId>org.spigotmc.....</groupId>
190-
<artifactId>spigot</artifactId>
191-
<version>1.21-R0.1-SNAPSHOT</version>
192-
<scope>provided</scope>
193-
</dependency>
194188
<dependency>
195189
<groupId>org.spigotmc......</groupId>
196190
<artifactId>spigot</artifactId>

src/main/java/world/bentobox/boxed/generators/biomes/AbstractCopyBiomeProvider.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package world.bentobox.boxed.generators.biomes;
22

3-
import java.util.Arrays;
43
import java.util.List;
54

5+
import org.bukkit.Registry;
66
import org.bukkit.World.Environment;
77
import org.bukkit.block.Biome;
88
import org.bukkit.generator.BiomeProvider;
99
import org.bukkit.generator.WorldInfo;
1010
import org.bukkit.util.Vector;
1111
import org.eclipse.jdt.annotation.Nullable;
1212

13-
import com.google.common.base.Enums;
14-
1513
import world.bentobox.bentobox.BentoBox;
1614
import world.bentobox.boxed.Boxed;
1715
import world.bentobox.boxed.generators.chunks.AbstractBoxedChunkGenerator;
@@ -56,7 +54,7 @@ public Biome getBiome(WorldInfo worldInfo, int x, int y, int z) {
5654
@Override
5755
public List<Biome> getBiomes(WorldInfo worldInfo) {
5856
// Return all of them for now!
59-
return Arrays.stream(Biome.values()).filter(b -> !b.equals(Biome.CUSTOM)).filter(b -> !b.equals(Enums.getIfPresent(Biome.class, "CHERRY_GROVE").orNull())).toList();
57+
return Registry.BIOME.stream().filter(b -> !b.equals(Biome.CUSTOM)).toList();
6058
}
6159

6260
}

src/main/java/world/bentobox/boxed/generators/biomes/AbstractSeedBiomeProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.SortedMap;
1212
import java.util.TreeMap;
1313

14+
import org.bukkit.Registry;
1415
import org.bukkit.World.Environment;
1516
import org.bukkit.block.Biome;
1617
import org.bukkit.block.BlockFace;
@@ -21,8 +22,6 @@
2122
import org.bukkit.util.Vector;
2223
import org.eclipse.jdt.annotation.NonNull;
2324

24-
import com.google.common.base.Enums;
25-
2625
import world.bentobox.boxed.Boxed;
2726

2827
/**
@@ -1129,7 +1128,7 @@ Biome getBeachBiome(int t) {
11291128
}
11301129

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

12021201
/**
@@ -1215,7 +1214,7 @@ private SortedMap<Double, Biome> loadQuad(YamlConfiguration config, String secto
12151214
if (split.length == 2) {
12161215
try {
12171216
double d = Double.parseDouble(split[0]);
1218-
Biome biome = Enums.getIfPresent(Biome.class, split[1].toUpperCase(Locale.ENGLISH)).orNull();
1217+
Biome biome = Biome.valueOf(split[1].toUpperCase(Locale.ENGLISH));
12191218
if (biome == null) {
12201219
addon.logError(split[1].toUpperCase(Locale.ENGLISH) + " is an unknown biome on this server.");
12211220
result.put(d, Biome.CUSTOM);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package world.bentobox.boxed.nms.v1_21_3_R0_1_SNAPSHOT;
2+
3+
public class GetMetaData extends world.bentobox.boxed.nms.v1_21_R0_1_SNAPSHOT.GetMetaData {
4+
// Identical to 1.21
5+
}

src/main/resources/addon.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Boxed
22
main: world.bentobox.boxed.Boxed
33
version: ${version}${build.number}
4-
api-version: 2.5.1
4+
api-version: 2.7.1
55
metrics: true
66
icon: "COMPOSTER"
77
repository: "BentoBoxWorld/Boxed"

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: BentoBox-Boxed
22
main: world.bentobox.boxed.BoxedPladdon
33
version: ${project.version}${build.number}
4-
api-version: "1.19"
4+
api-version: "1.21"
55

66
authors: [tastybento]
77
contributors: ["The BentoBoxWorld Community"]

src/test/java/world/bentobox/boxed/listeners/EnderPearlListenerTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
3636
import org.bukkit.util.BoundingBox;
3737
import org.bukkit.util.Vector;
38+
import org.junit.After;
3839
import org.junit.Before;
3940
import org.junit.Test;
4041
import org.junit.runner.RunWith;
@@ -55,6 +56,7 @@
5556
import world.bentobox.bentobox.util.Util;
5657
import world.bentobox.boxed.Boxed;
5758
import world.bentobox.boxed.Settings;
59+
import world.bentobox.boxed.mocks.ServerMocks;
5860

5961
/**
6062
* @author tastybento
@@ -102,7 +104,7 @@ public class EnderPearlListenerTest {
102104
*/
103105
@Before
104106
public void setUp() throws Exception {
105-
107+
ServerMocks.newServer();
106108
// Set up plugin
107109
plugin = mock(BentoBox.class);
108110
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
@@ -170,6 +172,13 @@ public void setUp() throws Exception {
170172
epl = new EnderPearlListener(addon);
171173
}
172174

175+
@After
176+
public void tearDown() {
177+
ServerMocks.unsetBukkitServer();
178+
User.clearUsers();
179+
Mockito.framework().clearInlineMocks();
180+
}
181+
173182
/**
174183
* Test method for {@link world.bentobox.boxed.listeners.EnderPearlListener#EnderPearlListener(world.bentobox.boxed.Boxed)}.
175184
*/
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package world.bentobox.boxed.mocks;
2+
3+
import static org.mockito.ArgumentMatchers.notNull;
4+
import static org.mockito.Mockito.doAnswer;
5+
import static org.mockito.Mockito.doReturn;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.when;
8+
9+
import java.lang.reflect.Field;
10+
import java.util.HashMap;
11+
import java.util.Locale;
12+
import java.util.Map;
13+
import java.util.Set;
14+
import java.util.logging.Logger;
15+
16+
import org.bukkit.Bukkit;
17+
import org.bukkit.Keyed;
18+
import org.bukkit.NamespacedKey;
19+
import org.bukkit.Registry;
20+
import org.bukkit.Server;
21+
import org.bukkit.Tag;
22+
import org.bukkit.UnsafeValues;
23+
import org.eclipse.jdt.annotation.NonNull;
24+
25+
public final class ServerMocks {
26+
27+
public static @NonNull Server newServer() {
28+
Server mock = mock(Server.class);
29+
30+
Logger noOp = mock(Logger.class);
31+
when(mock.getLogger()).thenReturn(noOp);
32+
when(mock.isPrimaryThread()).thenReturn(true);
33+
34+
// Unsafe
35+
UnsafeValues unsafe = mock(UnsafeValues.class);
36+
when(mock.getUnsafe()).thenReturn(unsafe);
37+
38+
// Server must be available before tags can be mocked.
39+
Bukkit.setServer(mock);
40+
41+
// Bukkit has a lot of static constants referencing registry values. To initialize those, the
42+
// registries must be able to be fetched before the classes are touched.
43+
Map<Class<? extends Keyed>, Object> registers = new HashMap<>();
44+
45+
doAnswer(invocationGetRegistry -> registers.computeIfAbsent(invocationGetRegistry.getArgument(0), clazz -> {
46+
Registry<?> registry = mock(Registry.class);
47+
Map<NamespacedKey, Keyed> cache = new HashMap<>();
48+
doAnswer(invocationGetEntry -> {
49+
NamespacedKey key = invocationGetEntry.getArgument(0);
50+
// Some classes (like BlockType and ItemType) have extra generics that will be
51+
// erased during runtime calls. To ensure accurate typing, grab the constant's field.
52+
// This approach also allows us to return null for unsupported keys.
53+
Class<? extends Keyed> constantClazz;
54+
try {
55+
//noinspection unchecked
56+
constantClazz = (Class<? extends Keyed>) clazz
57+
.getField(key.getKey().toUpperCase(Locale.ROOT).replace('.', '_')).getType();
58+
} catch (ClassCastException e) {
59+
throw new RuntimeException(e);
60+
} catch (NoSuchFieldException e) {
61+
return null;
62+
}
63+
64+
return cache.computeIfAbsent(key, key1 -> {
65+
Keyed keyed = mock(constantClazz);
66+
doReturn(key).when(keyed).getKey();
67+
return keyed;
68+
});
69+
}).when(registry).get(notNull());
70+
return registry;
71+
})).when(mock).getRegistry(notNull());
72+
73+
// Tags are dependent on registries, but use a different method.
74+
// This will set up blank tags for each constant; all that needs to be done to render them
75+
// functional is to re-mock Tag#getValues.
76+
doAnswer(invocationGetTag -> {
77+
Tag<?> tag = mock(Tag.class);
78+
doReturn(invocationGetTag.getArgument(1)).when(tag).getKey();
79+
doReturn(Set.of()).when(tag).getValues();
80+
doAnswer(invocationIsTagged -> {
81+
Keyed keyed = invocationIsTagged.getArgument(0);
82+
Class<?> type = invocationGetTag.getArgument(2);
83+
if (!type.isAssignableFrom(keyed.getClass())) {
84+
return null;
85+
}
86+
// Since these are mocks, the exact instance might not be equal. Consider equal keys equal.
87+
return tag.getValues().contains(keyed)
88+
|| tag.getValues().stream().anyMatch(value -> value.getKey().equals(keyed.getKey()));
89+
}).when(tag).isTagged(notNull());
90+
return tag;
91+
}).when(mock).getTag(notNull(), notNull(), notNull());
92+
93+
// Once the server is all set up, touch BlockType and ItemType to initialize.
94+
// This prevents issues when trying to access dependent methods from a Material constant.
95+
try {
96+
Class.forName("org.bukkit.inventory.ItemType");
97+
Class.forName("org.bukkit.block.BlockType");
98+
} catch (ClassNotFoundException e) {
99+
throw new RuntimeException(e);
100+
}
101+
102+
return mock;
103+
}
104+
105+
public static void unsetBukkitServer() {
106+
try {
107+
Field server = Bukkit.class.getDeclaredField("server");
108+
server.setAccessible(true);
109+
server.set(null, null);
110+
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
111+
throw new RuntimeException(e);
112+
}
113+
}
114+
115+
private ServerMocks() {
116+
}
117+
118+
}

0 commit comments

Comments
 (0)