Skip to content

Commit

Permalink
Merge pull request #2550 from BentoBoxWorld/custom_server
Browse files Browse the repository at this point in the history
Use custom server to handle new enums
  • Loading branch information
tastybento authored Nov 8, 2024
2 parents cd40d27 + 04ebe61 commit c77da07
Show file tree
Hide file tree
Showing 35 changed files with 249 additions and 161 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<distributionManagement>
<repository>
<id>codemc-releases</id>
<url>https://repo.codemc.org/repository/bentoboxworld</url>
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
</repository>
</distributionManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@


import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.bukkit.Registry;
import org.bukkit.block.Biome;

import com.google.gson.TypeAdapter;
Expand All @@ -32,11 +32,12 @@ public final class BiomeTypeAdapter extends TypeAdapter<Biome>
*/
final Map<String, Biome> biomeMap;

@SuppressWarnings("deprecation")
public BiomeTypeAdapter() {
this.biomeMap = new HashMap<>();

// Put in current values.
Arrays.stream(Biome.values()).forEach(biome -> this.biomeMap.put(biome.name(), biome));
Registry.BIOME.forEach(biome -> this.biomeMap.put(biome.name(), biome));

// Put in renamed biomes values.
this.biomeMap.put("TALL_BIRCH_FOREST", Biome.OLD_GROWTH_BIRCH_FOREST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
import java.util.concurrent.CompletableFuture;

import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;
Expand Down Expand Up @@ -216,10 +221,24 @@ private void deserializeValue(Method method, T instance, PropertyDescriptor prop
try {
// Floats need special handling because the database returns them as doubles
Type setType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0];
BentoBox.getInstance().logDebug("name = " + setType.getTypeName());
plugin.logError("Default setting value will be used: "
+ propertyDescriptor.getReadMethod().invoke(instance));
plugin.logError(method.getName());
plugin.logError(propertyDescriptor.getReadMethod().getName());
plugin.logError(instance.toString());
if (setType.getTypeName().equals("float")) {
double d = (double) setTo;
float f = (float)d;
method.invoke(instance, f);
} else if (setType.getTypeName().equals("org.bukkit.Sound")) {
Sound s = Registry.SOUNDS
.get(NamespacedKey.fromString(((String) setTo).toLowerCase(Locale.ENGLISH)));
method.invoke(instance, s);
} else if (setType.getTypeName().equals("org.bukkit.block.Biome")) {
Biome b = Registry.BIOME
.get(NamespacedKey.fromString(((String) setTo).toLowerCase(Locale.ENGLISH)));
method.invoke(instance, b);
} else {
method.invoke(instance, setTo);
}
Expand Down Expand Up @@ -368,8 +387,9 @@ public CompletableFuture<Boolean> saveObject(T instance) throws IllegalAccessExc
// Get the read method
Method method = propertyDescriptor.getReadMethod();
// Invoke the read method to get the value. We have no idea what type of value it is.
BentoBox.getInstance().logDebug("Method is " + method.getName());
Object value = method.invoke(instance);

BentoBox.getInstance().logDebug("Value is " + value);
String storageLocation = field.getName();

// Check if there is an annotation on the field
Expand All @@ -395,6 +415,7 @@ public CompletableFuture<Boolean> saveObject(T instance) throws IllegalAccessExc
}

if (!checkAdapter(field, config, storageLocation, value)) {
BentoBox.getInstance().logDebug("No adapter");
// Set the filename if it has not be set already
if (filename.isEmpty() && method.getName().equals("getUniqueId")) {
// Save the name for when the file is saved
Expand All @@ -407,6 +428,7 @@ public CompletableFuture<Boolean> saveObject(T instance) throws IllegalAccessExc
serializeSet((Set<Object>)value, config, storageLocation);
} else {
// For all other data that doesn't need special serialization
BentoBox.getInstance().logDebug("For all other data that doesn't need special serializationr");
config.set(storageLocation, serialize(value));
}
}
Expand Down Expand Up @@ -561,6 +583,7 @@ private void setComment(@NonNull String comment, @NonNull YamlConfiguration conf
private Object serialize(@Nullable Object object) {
// Null is a value object and is serialized as the string "null"
if (object == null) {
BentoBox.getInstance().logDebug("Object is null");
return "null";
}
// UUID has it's own serialization, that is not picked up automatically
Expand All @@ -575,6 +598,11 @@ private Object serialize(@Nullable Object object) {
if (object instanceof Location l) {
return Util.getStringLocation(l);
}
// Keyed interfaces that are replacing enums
if (object instanceof Keyed k) {
BentoBox.getInstance().logDebug("Object is keyed");
return k.getKey().getKey();
}
// Enums
if (object instanceof Enum<?> e) {
//Custom enums are a child of the Enum class. Just get the names of each one.
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/world/bentobox/bentobox/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,6 @@ public static WorldRegenerator getRegenerator() {
throw new IllegalStateException("Class " + clazz.getName() + " does not implement WorldRegenerator");
}
} catch (Exception e) {
e.printStackTrace();
plugin.logWarning("No Regenerator found for " + bukkitVersion + ", falling back to Bukkit API.");
handler = new world.bentobox.bentobox.nms.fallback.WorldRegeneratorImpl();
}
Expand Down Expand Up @@ -773,7 +772,6 @@ public static PasteHandler getPasteHandler() {
throw new IllegalStateException("Class " + clazz.getName() + " does not implement PasteHandler");
}
} catch (Exception e) {
e.printStackTrace();
plugin.logWarning("No PasteHandler found for " + bukkitVersion + ", falling back to Bukkit API.");
handler = new world.bentobox.bentobox.nms.fallback.PasteHandlerImpl();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.Block;
Expand All @@ -34,6 +35,7 @@
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
Expand All @@ -58,6 +60,7 @@
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.mocks.ServerMocks;
import world.bentobox.bentobox.util.Util;

/**
Expand Down Expand Up @@ -103,9 +106,13 @@ public abstract class AbstractCommonSetup {
protected FlagsManager fm;
@Mock
protected Spigot spigot;
protected Server server;


@Before
public void setUp() throws Exception {

server = ServerMocks.newServer();
// Bukkit
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
// Set up plugin
Expand Down Expand Up @@ -221,6 +228,7 @@ public void setUp() throws Exception {
*/
@After
public void tearDown() throws Exception {
ServerMocks.unsetBukkitServer();
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.DatabaseSetup;
import world.bentobox.bentobox.managers.RanksManager;
Expand Down Expand Up @@ -126,9 +125,8 @@ public void setUp() throws Exception {
}

@After
public void tearDown() throws IOException {
User.clearUsers();
Mockito.framework().clearInlineMocks();
public void tearDown() throws Exception {
super.tearDown();
deleteAll(new File("database"));
deleteAll(new File("database_backup"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
Expand Down Expand Up @@ -128,12 +127,9 @@ public void setUp() throws Exception {

}

/**
*/
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
public void tearDown() throws Exception {
super.tearDown();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.bukkit.Bukkit;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand All @@ -43,6 +43,7 @@
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.HooksManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.mocks.ServerMocks;

/**
* @author tastybento
Expand All @@ -68,8 +69,11 @@ public class AdminBlueprintLoadCommandTest {
private Map<String, Blueprint> map;
private File blueprintsFolder;

/**
*/
@BeforeClass
public static void beforeClass() {
ServerMocks.newServer();
}

@Before
public void setUp() throws Exception {
// Set up plugin
Expand Down Expand Up @@ -125,8 +129,6 @@ public void setUp() throws Exception {
abcc = new AdminBlueprintLoadCommand(ac);
}

/**
*/
@After
public void tearDown() throws Exception {
User.clearUsers();
Expand Down Expand Up @@ -181,7 +183,6 @@ public void testExecuteUserStringListOfStringNoLoad() {
* Test method for {@link world.bentobox.bentobox.api.commands.admin.blueprints.AdminBlueprintLoadCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
@Ignore("Enums")
public void testExecuteUserStringListOfStringSuccessCaps() {
assertTrue(abcc.execute(user, "", List.of("island")));
verify(user).sendMessage("general.success");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.bukkit.util.Vector;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand All @@ -45,6 +45,7 @@
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.HooksManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.mocks.ServerMocks;

/**
* @author tastybento
Expand All @@ -68,8 +69,11 @@ public class AdminBlueprintSaveCommandTest {
private BlueprintsManager bm;
private Blueprint bp = new Blueprint();

/**
*/
@BeforeClass
public static void beforeClass() {
ServerMocks.newServer();
}

@Before
public void setUp() throws Exception {
// Set up plugin
Expand Down Expand Up @@ -200,7 +204,6 @@ public void testCanExecute() {
* Test method for {@link world.bentobox.bentobox.api.commands.admin.blueprints.AdminBlueprintSaveCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
@Ignore("Enums")
public void testExecuteUserStringListOfString() {
testCanExecute();
assertTrue(absc.execute(user, "", List.of("island")));
Expand All @@ -212,7 +215,6 @@ public void testExecuteUserStringListOfString() {
* Test method for {@link world.bentobox.bentobox.api.commands.admin.blueprints.AdminBlueprintSaveCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
@Ignore("Enums")
public void testExecuteUserStringListOfStringFileExists() {
testCanExecute();
assertTrue(absc.execute(user, "", List.of("island")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ public void setUp() throws Exception {
}

@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
public void tearDown() throws Exception {
super.tearDown();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ public void setUp() throws Exception {
}

@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
public void tearDown() throws Exception {
super.tearDown();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ public void setUp() throws Exception {
}

@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
public void tearDown() throws Exception {
super.tearDown();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.framework;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -152,9 +151,8 @@ public void setUp() throws Exception {
}

@After
public void tearDown() {
User.clearUsers();
framework().clearInlineMocks();
public void tearDown() throws Exception {
super.tearDown();
}

/**
Expand Down
Loading

0 comments on commit c77da07

Please sign in to comment.