mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Test loot material lookup, plugin block keys, the module summary log and the plugin content wait
This commit is contained in:
1 parent
bfd2a762c7
commit
65a6cebe9c
6 files changed
+323
-3
No files matched your search
@@ -29,6 +29,7 @@ import art.arcane.volmlib.util.format.Form;
|
||||
import com.volmit.item.ItemAttribute;
|
||||
import com.volmit.item.ItemDrop;
|
||||
import com.volmit.item.ItemEnchantment;
|
||||
import com.volmit.resource.ContentKey;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Material;
|
||||
@@ -96,7 +97,7 @@ public final class BukkitItemStacks {
|
||||
stack.setAmount(drop.amount());
|
||||
return stack;
|
||||
}
|
||||
Material material = Material.matchMaterial(drop.item().toString());
|
||||
Material material = materialOf(drop.item());
|
||||
if (material == null) {
|
||||
IrisLogging.warnOnce("loot:material:" + drop.item(), "Unknown Material: %s", drop.item());
|
||||
return null;
|
||||
@@ -104,6 +105,11 @@ public final class BukkitItemStacks {
|
||||
return new ItemStack(material, drop.amount());
|
||||
}
|
||||
|
||||
/** The vanilla material a platform item key such as {@code minecraft:coal} names; null when none does. */
|
||||
static Material materialOf(ContentKey item) {
|
||||
return Material.matchMaterial(item.toString());
|
||||
}
|
||||
|
||||
private static KMap<String, Object> customNbtOf(ItemDrop drop) {
|
||||
Map<String, Object> customNbt = drop.extras().customNbt();
|
||||
return customNbt.isEmpty() ? null : new KMap<>(customNbt);
|
||||
|
||||
@@ -56,7 +56,7 @@ import java.util.Map;
|
||||
/**
|
||||
* Bukkit adapter resolving namespaced keys against the live Bukkit registries.
|
||||
*/
|
||||
public final class BukkitRegistries implements PlatformRegistries {
|
||||
public class BukkitRegistries implements PlatformRegistries {
|
||||
@Override
|
||||
public PlatformBlockState block(String key) {
|
||||
BlockData data = BukkitBlockResolution.get(key);
|
||||
@@ -126,13 +126,18 @@ public final class BukkitRegistries implements PlatformRegistries {
|
||||
|
||||
@Override
|
||||
public List<String> blockKeys() {
|
||||
List<String> keys = new ArrayList<>(vanillaBlockKeys());
|
||||
keys.addAll(externalKeys(DataType.BLOCK));
|
||||
return keys;
|
||||
}
|
||||
|
||||
List<String> vanillaBlockKeys() {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (Material material : Registry.MATERIAL) {
|
||||
if (material.isBlock()) {
|
||||
keys.add(material.getKey().toString());
|
||||
}
|
||||
}
|
||||
keys.addAll(externalKeys(DataType.BLOCK));
|
||||
return keys;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.spi.IrisPlatform;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.LogLevel;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/** Iris loads one pack many times while starting; its module summary belongs in the log once. */
|
||||
public class IrisApiLoggerTest {
|
||||
private static final String SUMMARY = "Pack natureplanet uses 22 modules:";
|
||||
|
||||
private final List<LogLevel> levels = new ArrayList<>();
|
||||
private IrisPlatform previousPlatform;
|
||||
|
||||
@Before
|
||||
public void rememberPlatform() {
|
||||
previousPlatform = IrisPlatforms.isBound() ? IrisPlatforms.get() : null;
|
||||
IrisPlatforms.unbind();
|
||||
}
|
||||
|
||||
@After
|
||||
public void restorePlatform() {
|
||||
IrisPlatforms.unbind();
|
||||
if (previousPlatform != null) {
|
||||
IrisPlatforms.bind(previousPlatform);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aRepeatedMessageIsOnlyADebugLine() {
|
||||
bindCapturingPlatform();
|
||||
System.Logger log = IrisApiLogger.showingEachMessageOnce("test");
|
||||
|
||||
log.log(System.Logger.Level.INFO, SUMMARY);
|
||||
log.log(System.Logger.Level.INFO, SUMMARY);
|
||||
|
||||
assertEquals(List.of(LogLevel.INFO, LogLevel.DEBUG), levels);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aMessageLoggedBeforeThePlatformLogExistsIsShownAgain() {
|
||||
System.Logger log = IrisApiLogger.showingEachMessageOnce("test");
|
||||
log.log(System.Logger.Level.INFO, SUMMARY);
|
||||
|
||||
bindCapturingPlatform();
|
||||
log.log(System.Logger.Level.INFO, SUMMARY);
|
||||
|
||||
assertEquals(List.of(LogLevel.INFO), levels);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void differentMessagesAreEachShown() {
|
||||
bindCapturingPlatform();
|
||||
System.Logger log = IrisApiLogger.showingEachMessageOnce("test");
|
||||
|
||||
log.log(System.Logger.Level.INFO, SUMMARY);
|
||||
log.log(System.Logger.Level.INFO, "Pack natureplanet uses 23 modules:");
|
||||
|
||||
assertEquals(List.of(LogLevel.INFO, LogLevel.INFO), levels);
|
||||
}
|
||||
|
||||
private void bindCapturingPlatform() {
|
||||
IrisPlatform platform = mock(IrisPlatform.class);
|
||||
doAnswer(invocation -> levels.add(invocation.getArgument(0, LogLevel.class)))
|
||||
.when(platform).log(any(LogLevel.class), anyString());
|
||||
IrisPlatforms.bind(platform);
|
||||
}
|
||||
}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
package art.arcane.iris.core.service;
|
||||
|
||||
import art.arcane.iris.core.link.ExternalDataProvider;
|
||||
import art.arcane.iris.core.link.Identifier;
|
||||
import art.arcane.iris.core.link.data.DataType;
|
||||
import art.arcane.iris.spi.IrisPlatform;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.PlatformScheduler;
|
||||
import org.bukkit.event.server.ServerLoadEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Pack validation runs before plugins like CraftEngine register their blocks, so whatever checks a pack against the
|
||||
* block registry waits until the server has started and every provider has loaded its content.
|
||||
*/
|
||||
public class ExternalDataSVCProviderContentTest {
|
||||
private final ExternalDataSVC external = new ExternalDataSVC();
|
||||
private final AtomicInteger runs = new AtomicInteger();
|
||||
private IrisPlatform previousPlatform;
|
||||
|
||||
@Before
|
||||
public void bindPlatformRunningTasksAtOnce() {
|
||||
previousPlatform = IrisPlatforms.isBound() ? IrisPlatforms.get() : null;
|
||||
IrisPlatforms.unbind();
|
||||
PlatformScheduler scheduler = mock(PlatformScheduler.class);
|
||||
doAnswer(invocation -> {
|
||||
invocation.getArgument(0, Runnable.class).run();
|
||||
return null;
|
||||
}).when(scheduler).global(any(Runnable.class));
|
||||
IrisPlatform platform = mock(IrisPlatform.class);
|
||||
when(platform.scheduler()).thenReturn(scheduler);
|
||||
IrisPlatforms.bind(platform);
|
||||
}
|
||||
|
||||
@After
|
||||
public void restorePlatform() {
|
||||
IrisPlatforms.unbind();
|
||||
if (previousPlatform != null) {
|
||||
IrisPlatforms.bind(previousPlatform);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anActionWaitsForTheServerToStart() {
|
||||
external.afterProviderContentLoads(runs::incrementAndGet);
|
||||
assertEquals(0, runs.get());
|
||||
|
||||
external.onServerLoad(serverStarted());
|
||||
assertEquals(1, runs.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anActionWaitsForAProviderStillLoadingItsContent() {
|
||||
LoadingProvider provider = new LoadingProvider();
|
||||
external.registerProvider(provider);
|
||||
external.afterProviderContentLoads(runs::incrementAndGet);
|
||||
|
||||
external.onServerLoad(serverStarted());
|
||||
assertEquals(0, runs.get());
|
||||
|
||||
provider.loading = false;
|
||||
external.onProviderContentLoaded(provider);
|
||||
assertEquals(1, runs.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anActionRunsOnce() {
|
||||
external.afterProviderContentLoads(runs::incrementAndGet);
|
||||
|
||||
external.onServerLoad(serverStarted());
|
||||
external.onServerLoad(serverStarted());
|
||||
|
||||
assertEquals(1, runs.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anActionQueuedOnceContentHasLoadedRunsAtOnce() {
|
||||
external.onServerLoad(serverStarted());
|
||||
|
||||
external.afterProviderContentLoads(runs::incrementAndGet);
|
||||
|
||||
assertEquals(1, runs.get());
|
||||
}
|
||||
|
||||
/** Bukkit events check the server's main thread when built, so a test can only mock one. */
|
||||
private static ServerLoadEvent serverStarted() {
|
||||
return mock(ServerLoadEvent.class);
|
||||
}
|
||||
|
||||
private static final class LoadingProvider extends ExternalDataProvider {
|
||||
private volatile boolean loading = true;
|
||||
|
||||
private LoadingProvider() {
|
||||
super("TestBlocks");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLoadingContent() {
|
||||
return loading;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Collection<@NotNull Identifier> getTypes(@NotNull DataType dataType) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidProvider(@NotNull Identifier id, DataType dataType) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package art.arcane.iris.platform.bukkit;
|
||||
|
||||
import com.volmit.resource.ContentKey;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import org.bukkit.Material;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/** Loot items arrive as namespaced keys, which {@code Material.valueOf} cannot read. */
|
||||
public class BukkitItemStacksMaterialTest {
|
||||
@Test
|
||||
public void aNamespacedVanillaItemKeyNamesItsMaterial() {
|
||||
assertEquals(Material.COAL, BukkitItemStacks.materialOf(registered("minecraft:coal")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anItemKeyNoMaterialHasNamesNothing() {
|
||||
assertNull(BukkitItemStacks.materialOf(registered("minecraft:not_an_item")));
|
||||
}
|
||||
|
||||
private static ContentKey registered(String key) {
|
||||
return new ContentKey.Registered(ResourceId.parse(key));
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package art.arcane.iris.platform.bukkit;
|
||||
|
||||
import art.arcane.iris.core.link.Identifier;
|
||||
import art.arcane.iris.core.link.data.DataType;
|
||||
import art.arcane.iris.core.pack.ContentKeyValidator;
|
||||
import art.arcane.iris.core.service.ExternalDataSVC;
|
||||
import art.arcane.iris.spi.IrisServices;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Blocks of plugins like CraftEngine are real block keys to a pack, so validation must not call them unknown. */
|
||||
public class BukkitRegistriesExternalBlocksTest {
|
||||
private static final String PLUGIN_BLOCK = "etopia:pine_log";
|
||||
private static final String VANILLA_BLOCK = "minecraft:stone";
|
||||
|
||||
@Before
|
||||
public void registerAPluginBlock() {
|
||||
ExternalDataSVC external = mock(ExternalDataSVC.class);
|
||||
when(external.getAllIdentifiers(DataType.BLOCK)).thenReturn(List.of(Identifier.fromString(PLUGIN_BLOCK)));
|
||||
when(external.getAllIdentifiers(DataType.ITEM)).thenReturn(List.of());
|
||||
IrisServices.register(ExternalDataSVC.class, external);
|
||||
}
|
||||
|
||||
@After
|
||||
public void forgetThePluginBlock() {
|
||||
IrisServices.remove(ExternalDataSVC.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void blockKeysHoldVanillaAndPluginBlocks() {
|
||||
List<String> keys = registriesWithoutAServer().blockKeys();
|
||||
|
||||
assertTrue(keys.contains(VANILLA_BLOCK));
|
||||
assertTrue(keys.contains(PLUGIN_BLOCK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validationKnowsAPluginBlock() {
|
||||
assertEquals(List.of(), ContentKeyValidator.validate(registriesWithoutAServer(),
|
||||
List.of(VANILLA_BLOCK, PLUGIN_BLOCK), List.of(), List.of()));
|
||||
}
|
||||
|
||||
/** Bukkit's material registry needs a running server; only the vanilla half of the keys comes from it. */
|
||||
private static BukkitRegistries registriesWithoutAServer() {
|
||||
return new BukkitRegistries() {
|
||||
@Override
|
||||
List<String> vanillaBlockKeys() {
|
||||
return List.of(VANILLA_BLOCK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> itemKeys() {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> entityKeys() {
|
||||
return List.of();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user