Skip to content

Commit cc3b337

Browse files
authored
Merge pull request #36 from BentoBoxWorld/MC_1.21.3
Update to MC 1.21.3
2 parents ff681e2 + 7266a79 commit cc3b337

File tree

5 files changed

+133
-17
lines changed

5 files changed

+133
-17
lines changed

pom.xml

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,9 @@
4242
</issueManagement>
4343

4444
<distributionManagement>
45-
<snapshotRepository>
46-
<id>codemc-snapshots</id>
47-
<url>https://repo.codemc.org/repository/maven-snapshots</url>
48-
</snapshotRepository>
4945
<repository>
50-
<id>codemc-releases</id>
51-
<url>https://repo.codemc.org/repository/maven-releases</url>
46+
<id>codemc</id>
47+
<url>https://repo.codemc.org/repository/bentoboxworld</url>
5248
</repository>
5349
</distributionManagement>
5450

@@ -59,8 +55,8 @@
5955
<!-- Non-minecraft related dependencies -->
6056
<mockito.version>5.12.0</mockito.version>
6157
<!-- More visible way how to change dependency versions -->
62-
<spigot.version>1.20.4-R0.1-SNAPSHOT</spigot.version>
63-
<bentobox.version>2.4.1-SNAPSHOT</bentobox.version>
58+
<spigot.version>1.21.3-R0.1-SNAPSHOT</spigot.version>
59+
<bentobox.version>2.7.1-SNAPSHOT</bentobox.version>
6460
<!-- Revision variable removes warning about dynamic version -->
6561
<revision>${build.version}-SNAPSHOT</revision>
6662
<!-- Do not change unless you want different name for local builds. -->
@@ -122,11 +118,7 @@
122118
</repository>
123119
<repository>
124120
<id>codemc</id>
125-
<url>https://repo.codemc.org/repository/maven-snapshots/</url>
126-
</repository>
127-
<repository>
128-
<id>codemc-public</id>
129-
<url>https://repo.codemc.org/repository/maven-public/</url>
121+
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
130122
</repository>
131123
</repositories>
132124

src/main/java/com/wasteofplastic/invswitcher/Store.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ public void getInventory(Player player, World world) {
125125

126126
private void setHeath(InventoryStorage store, Player player, String overworldName) {
127127
// Health
128-
double health = store.getHealth().getOrDefault(overworldName, player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
128+
double health = store.getHealth().getOrDefault(overworldName,
129+
player.getAttribute(Attribute.MAX_HEALTH).getValue());
129130

130-
AttributeInstance attr = player.getAttribute(Attribute.GENERIC_MAX_HEALTH);
131+
AttributeInstance attr = player.getAttribute(Attribute.MAX_HEALTH);
131132
if (attr != null && health > attr.getValue()) {
132133
health = attr.getValue();
133134
}

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: InvSwitcher
22
main: com.wasteofplastic.invswitcher.InvSwitcher
33
version: ${version}${build.number}
4-
api-version: 1.16
4+
api-version: 2.7.1
55

66
authors: tastybento
77

src/test/java/com/wasteofplastic/invswitcher/StoreTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import org.mockito.Mockito;
4242
import org.mockito.junit.MockitoJUnitRunner;
4343

44+
import com.wasteofplastic.invswitcher.mocks.ServerMocks;
45+
4446
import world.bentobox.bentobox.BentoBox;
4547
import world.bentobox.bentobox.Settings;
4648
import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType;
@@ -73,6 +75,8 @@ public class StoreTest {
7375
public void setUp()
7476
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
7577

78+
ServerMocks.newServer();
79+
7680
// BentoBox
7781
BentoBox plugin = mock(BentoBox.class);
7882
// Use reflection to set the private static field "instance" in BentoBox
@@ -124,7 +128,8 @@ public void setUp()
124128
}
125129

126130
@After
127-
public void clear() throws IOException{
131+
public void tearDown() throws IOException {
132+
ServerMocks.unsetBukkitServer();
128133
//remove any database data
129134
File file = new File("database");
130135
Path pathToBeDeleted = file.toPath();
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.wasteofplastic.invswitcher.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)