diff --git a/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedEngineEffects.java b/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedEngineEffects.java index dd101010d..2b3a58977 100644 --- a/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedEngineEffects.java +++ b/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedEngineEffects.java @@ -55,6 +55,7 @@ public final class ModdedEngineEffects extends EngineAssignedComponent implement private static final double PLAYER_REFRESH_CHANCE = 0.02D; private static final Set UNKNOWN_POTIONS = ConcurrentHashMap.newKeySet(); private static final Set UNKNOWN_SOUNDS = ConcurrentHashMap.newKeySet(); + private static final Set UNKNOWN_SOUND_CATEGORIES = ConcurrentHashMap.newKeySet(); private static final Set UNKNOWN_PARTICLES = ConcurrentHashMap.newKeySet(); private static final Set UNSUPPORTED_PARTICLES = ConcurrentHashMap.newKeySet(); @@ -252,6 +253,14 @@ public final class ModdedEngineEffects extends EngineAssignedComponent implement warnUnknown(UNKNOWN_SOUNDS, "sound", key); return; } + String categoryKey = effect.getSoundCategoryKey(); + NativePlayerEffects.SoundCategory category = NativePlayerEffects.soundCategory(categoryKey); + if (category == null) { + if (UNKNOWN_SOUND_CATEGORIES.add(categoryKey)) { + IrisLogging.warn("Unknown sound category: \"" + categoryKey + "\"."); + } + return; + } int distance = effect.getSoundDistance(); double x = player.x() + RNG.r.i(-distance, distance); @@ -260,7 +269,7 @@ public final class ModdedEngineEffects extends EngineAssignedComponent implement float volume = (float) effect.getVolume(); float pitch = (float) RNG.r.d(effect.getMinPitch(), effect.getMaxPitch()); NativePlayerEffects.sound(player, sound, new NativePlayerEffects.SoundEmission( - x, y, z, volume, pitch, ThreadLocalRandom.current().nextLong())); + category, x, y, z, volume, pitch, ThreadLocalRandom.current().nextLong())); } private void applyParticles(NativeWorld level, NativeProtocolPlayer player, IrisEffect effect) { diff --git a/adapters/modded-common/src/test/java/art/arcane/iris/modded/ModdedDecoratorHooksTest.java b/adapters/modded-common/src/test/java/art/arcane/iris/modded/ModdedDecoratorHooksTest.java index c9bb86add..0cfbb8e9d 100644 --- a/adapters/modded-common/src/test/java/art/arcane/iris/modded/ModdedDecoratorHooksTest.java +++ b/adapters/modded-common/src/test/java/art/arcane/iris/modded/ModdedDecoratorHooksTest.java @@ -1,14 +1,32 @@ package art.arcane.iris.modded; +import art.arcane.iris.generation.mantle.EngineMantle; +import art.arcane.volmlib.nativelib.minecraft26_2.modded.ModdedBlockState; import art.arcane.volmlib.nativelib.minecraft26_2.modded.NativeBlockFaces; import art.arcane.volmlib.nativelib.terrain.NativeBlockState; import art.arcane.volmlib.util.hunk.Hunk; +import art.arcane.volmlib.util.mantle.runtime.Mantle; +import art.arcane.volmlib.util.matter.Matter; +import net.minecraft.SharedConstants; +import net.minecraft.core.Direction; +import net.minecraft.server.Bootstrap; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.properties.BlockStateProperties; +import net.minecraft.world.level.block.state.properties.BooleanProperty; +import org.junit.BeforeClass; import org.junit.Test; import org.mockito.MockedStatic; +import java.util.EnumSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; @@ -17,6 +35,12 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class ModdedDecoratorHooksTest { + @BeforeClass + public static void bootstrap() { + SharedConstants.tryDetectVersion(); + Bootstrap.bootStrap(); + } + @Test public void faceFixingExcludesTheUpperBoundaryWhileKeepingTheLastRowAccessible() { NativeBlockState state = mock(NativeBlockState.class); @@ -41,4 +65,107 @@ public class ModdedDecoratorHooksTest { verify(hunk, never()).get(1, 4, 2); verify(hunk).get(1, 3, 2); } + + @Test + public void stackedVineCurtainHangsFromTheWallItsTopStrandFaces() { + Hunk hunk = Hunk.newArrayHunk(16, 8, 16); + hunk.set(8, 6, 8, stone()); + hunk.set(8, 5, 7, stone()); + hunk.set(9, 4, 8, stone()); + NativeBlockState vine = state(Blocks.VINE, Direction.NORTH); + + for (int y = 5; y >= 3; y--) { + hunk.set(8, y, 8, fix(vine, hunk, y)); + } + + assertEquals(EnumSet.of(Direction.NORTH, Direction.UP), faces(hunk.get(8, 5, 8))); + assertEquals(EnumSet.of(Direction.NORTH), faces(hunk.get(8, 4, 8))); + assertEquals(EnumSet.of(Direction.NORTH), faces(hunk.get(8, 3, 8))); + } + + @Test + public void unsupportedAuthoredSideFaceOnALoneVineIsDroppedAndReattached() { + NativeBlockState vine = state(Blocks.VINE, Direction.NORTH); + Hunk walled = Hunk.newArrayHunk(16, 8, 16); + walled.set(9, 3, 8, stone()); + + assertEquals(EnumSet.of(Direction.EAST), faces(fix(vine, walled, 3))); + assertEquals(EnumSet.of(Direction.UP), faces(fix(vine, Hunk.newArrayHunk(16, 8, 16), 3))); + } + + @Test + public void glowLichenSideFaceIsNotHeldByTheLichenAboveIt() { + NativeBlockState lichen = state(Blocks.GLOW_LICHEN, Direction.NORTH); + Hunk hunk = Hunk.newArrayHunk(16, 8, 16); + hunk.set(8, 2, 8, stone()); + hunk.set(8, 4, 8, state(Blocks.GLOW_LICHEN, Direction.NORTH)); + + assertEquals(EnumSet.of(Direction.DOWN), faces(fix(lichen, hunk, 3))); + } + + @Test + public void supportedAuthoredSideFaceKeepsItsDirection() { + NativeBlockState vine = state(Blocks.VINE, Direction.NORTH); + Hunk hunk = Hunk.newArrayHunk(16, 8, 16); + hunk.set(8, 3, 7, stone()); + hunk.set(9, 3, 8, stone()); + + assertEquals(EnumSet.of(Direction.NORTH), faces(fix(vine, hunk, 3))); + } + + @Test + public void vineWithoutAuthoredSideFaceReattachesToEverySturdyNeighbour() { + NativeBlockState vine = state(Blocks.VINE, Direction.UP); + Hunk hunk = Hunk.newArrayHunk(16, 8, 16); + hunk.set(7, 3, 8, stone()); + hunk.set(8, 3, 9, stone()); + hunk.set(8, 4, 8, stone()); + + assertEquals(EnumSet.of(Direction.SOUTH, Direction.WEST, Direction.UP), faces(fix(vine, hunk, 3))); + } + + @SuppressWarnings("unchecked") + private static NativeBlockState fix(NativeBlockState state, Hunk hunk, int y) { + EngineMantle mantle = mock(EngineMantle.class); + Mantle storage = mock(Mantle.class); + NativeBlockState air = ModdedBlockState.of(Blocks.AIR.defaultBlockState(), null); + when(mantle.getMantle()).thenReturn(storage); + when(storage.get(anyInt(), anyInt(), anyInt(), eq(NativeBlockState.class))).thenReturn(air); + return new ModdedDecoratorHooks().fixFaces(state, hunk, 8, 8, 8, y, 8, mantle); + } + + private static NativeBlockState stone() { + return ModdedBlockState.of(Blocks.STONE.defaultBlockState(), null); + } + + private static NativeBlockState state(Block block, Direction... faces) { + BlockState state = block.defaultBlockState(); + for (Direction face : faces) { + state = state.setValue(property(face), Boolean.TRUE); + } + return ModdedBlockState.of(state, null); + } + + private static Set faces(NativeBlockState state) { + BlockState handle = (BlockState) state.nativeHandle(); + EnumSet faces = EnumSet.noneOf(Direction.class); + for (Direction face : Direction.values()) { + BooleanProperty property = property(face); + if (handle.hasProperty(property) && handle.getValue(property)) { + faces.add(face); + } + } + return faces; + } + + private static BooleanProperty property(Direction face) { + return switch (face) { + case NORTH -> BlockStateProperties.NORTH; + case EAST -> BlockStateProperties.EAST; + case SOUTH -> BlockStateProperties.SOUTH; + case WEST -> BlockStateProperties.WEST; + case UP -> BlockStateProperties.UP; + case DOWN -> BlockStateProperties.DOWN; + }; + } } diff --git a/adapters/modded-common/src/test/java/art/arcane/volmlib/nativelib/minecraft26_2/modded/NativePlayerEffectsTest.java b/adapters/modded-common/src/test/java/art/arcane/volmlib/nativelib/minecraft26_2/modded/NativePlayerEffectsTest.java new file mode 100644 index 000000000..31f35745b --- /dev/null +++ b/adapters/modded-common/src/test/java/art/arcane/volmlib/nativelib/minecraft26_2/modded/NativePlayerEffectsTest.java @@ -0,0 +1,69 @@ +package art.arcane.volmlib.nativelib.minecraft26_2.modded; + +import art.arcane.iris.world.entity.IrisEffect; +import net.minecraft.SharedConstants; +import net.minecraft.network.protocol.game.ClientboundSoundPacket; +import net.minecraft.server.Bootstrap; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.network.ServerGamePacketListenerImpl; +import net.minecraft.sounds.SoundSource; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class NativePlayerEffectsTest { + private static final String SOUND = "minecraft:block.amethyst_block.chime"; + + @BeforeClass + public static void bootstrap() { + SharedConstants.tryDetectVersion(); + Bootstrap.bootStrap(); + } + + @Test + public void everyVanillaSoundSourceIsReachableByItsName() { + for (SoundSource source : SoundSource.values()) { + assertSame(source.getName(), source, sentSource(NativePlayerEffects.soundCategory(source.getName()))); + } + } + + @Test + public void effectCategoryNamesResolveIgnoringCase() { + assertSame(SoundSource.AMBIENT, sentSource(category(" Ambient "))); + assertSame(SoundSource.RECORDS, sentSource(category("RECORD"))); + assertSame(SoundSource.MASTER, sentSource(category(null))); + } + + @Test + public void unknownCategoryNamesDoNotResolve() { + assertNull(category("ambiant")); + assertNull(category("records")); + assertNull(category("players")); + } + + private static NativePlayerEffects.SoundCategory category(String authored) { + return NativePlayerEffects.soundCategory(new IrisEffect().setSoundCategory(authored).getSoundCategoryKey()); + } + + private static SoundSource sentSource(NativePlayerEffects.SoundCategory category) { + assertNotNull(category); + NativePlayerEffects.Sound sound = NativePlayerEffects.sound(SOUND); + assertNotNull(sound); + ServerPlayer handle = mock(ServerPlayer.class); + ServerGamePacketListenerImpl connection = mock(ServerGamePacketListenerImpl.class); + handle.connection = connection; + + NativePlayerEffects.sound(NativeProtocolPlayer.fromHandle(handle), sound, + new NativePlayerEffects.SoundEmission(category, 1.0D, 64.0D, -3.0D, 1.0F, 1.0F, 7L)); + + ArgumentCaptor packet = ArgumentCaptor.forClass(ClientboundSoundPacket.class); + verify(connection).send(packet.capture()); + return packet.getValue().getSource(); + } +} diff --git a/core/src/main/java/art/arcane/iris/generation/decoration/DecoratorCore.java b/core/src/main/java/art/arcane/iris/generation/decoration/DecoratorCore.java index 905dbb137..9859db55b 100644 --- a/core/src/main/java/art/arcane/iris/generation/decoration/DecoratorCore.java +++ b/core/src/main/java/art/arcane/iris/generation/decoration/DecoratorCore.java @@ -37,6 +37,7 @@ final class DecoratorCore { private static final long SEED_OFFSET = 29356788L; private static final long PART_FACTOR = 10439677L; + private static final String VINE = "minecraft:vine"; private static final String WEEPING_VINES = "minecraft:weeping_vines"; private static final String WEEPING_VINES_PLANT = "minecraft:weeping_vines_plant"; private static final String TWISTING_VINES = "minecraft:twisting_vines"; @@ -454,22 +455,20 @@ final class DecoratorCore { if (fixer != null) { return fixer.fixFaces(b, hunk, rX, rZ, x, y, z, mantle); } - BlockData rawB = (BlockData) b.nativeHandle(); - BlockData cloned = rawB.clone(); + BlockData cloned = ((BlockData) b.nativeHandle()).clone(); MultipleFacing data = (MultipleFacing) cloned; - if (data.getFaces().stream().anyMatch(f -> f != BlockFace.UP && f != BlockFace.DOWN)) { - // The pack chose which side a hanging strand faces: keep it (the vine above holds it up), and tie the - // block under a solid ceiling to it, so ceiling decorators hang curtains instead of flat ceiling sheets. - if (data.getAllowedFaces().contains(BlockFace.UP) && sturdy(BlockFace.UP, hunk, rX, rZ, x, y, z, mantle)) { - data.setFace(BlockFace.UP, true); - } + if (keepSupportedSides(data, VINE.equals(IrisProceduralBlocks.materialKey(b)), hunk, rX, rZ, x, y, z, mantle)) { return BukkitBlockState.of(cloned); } - data.getFaces().forEach(f -> data.setFace(f, false)); + for (BlockFace f : Faces.CARTESIAN) { + if (data.hasFace(f)) { + data.setFace(f, false); + } + } boolean found = false; - for (BlockFace f : BlockFace.values()) { - if (f.isCartesian() && sturdy(f, hunk, rX, rZ, x, y, z, mantle) && data.getAllowedFaces().contains(f)) { + for (BlockFace f : Faces.CARTESIAN) { + if (data.getAllowedFaces().contains(f) && sturdy(f, hunk, rX, rZ, x, y, z, mantle)) { found = true; data.setFace(f, true); } @@ -483,20 +482,58 @@ final class DecoratorCore { return BukkitBlockState.of(cloned); } - /** Whether the neighbour on that face (mantle first, then this chunk's hunk) offers a full sturdy face to attach to. */ + private static boolean keepSupportedSides(MultipleFacing data, boolean vine, Hunk hunk, + int rX, int rZ, int x, int y, int z, EngineMantle mantle) { + boolean side = false; + for (BlockFace f : Faces.SIDES) { + if (!data.hasFace(f)) { + continue; + } + if (sturdy(f, hunk, rX, rZ, x, y, z, mantle) || vine && vineAboveCarries(f, hunk, rX, rZ, x, y, z, mantle)) { + side = true; + } else { + data.setFace(f, false); + } + } + if (!side) { + return false; + } + if (data.getAllowedFaces().contains(BlockFace.UP)) { + data.setFace(BlockFace.UP, sturdy(BlockFace.UP, hunk, rX, rZ, x, y, z, mantle)); + } + if (data.hasFace(BlockFace.DOWN) && !sturdy(BlockFace.DOWN, hunk, rX, rZ, x, y, z, mantle)) { + data.setFace(BlockFace.DOWN, false); + } + return true; + } + private static boolean sturdy(BlockFace f, Hunk hunk, int rX, int rZ, int x, int y, int z, EngineMantle mantle) { int yy = y + f.getModY(); - NativeBlockState rs = mantle == null ? null : mantle.getMantle().get(x + f.getModX(), yy, z + f.getModZ(), NativeBlockState.class); - BlockData r = rs == null ? (BlockData) EngineMantle.AIR.get().nativeHandle() : (BlockData) rs.nativeHandle(); - if (r.isFaceSturdy(f.getOppositeFace(), BlockSupport.FULL)) { + BlockFace attached = f.getOppositeFace(); + if (mantle != null && sturdy(mantle.getMantle().get(x + f.getModX(), yy, z + f.getModZ(), NativeBlockState.class), attached)) { return true; } int xx = rX + f.getModX(); int zz = rZ + f.getModZ(); - if (xx < 0 || xx > 15 || zz < 0 || zz > 15 || yy < 0 || yy >= hunk.getHeight()) { - return false; + return xx >= 0 && xx <= 15 && zz >= 0 && zz <= 15 && yy >= 0 && yy < hunk.getHeight() + && sturdy(hunk.get(xx, yy, zz), attached); + } + + private static boolean sturdy(NativeBlockState state, BlockFace attached) { + return state != null && ((BlockData) state.nativeHandle()).isFaceSturdy(attached, BlockSupport.FULL); + } + + private static boolean vineAboveCarries(BlockFace f, Hunk hunk, int rX, int rZ, + int x, int y, int z, EngineMantle mantle) { + if (mantle != null && carries(mantle.getMantle().get(x, y + 1, z, NativeBlockState.class), f)) { + return true; } - return ((BlockData) hunk.get(xx, yy, zz).nativeHandle()).isFaceSturdy(f.getOppositeFace(), BlockSupport.FULL); + return y + 1 < hunk.getHeight() && carries(hunk.get(rX, y + 1, rZ), f); + } + + private static boolean carries(NativeBlockState state, BlockFace f) { + return state != null && VINE.equals(IrisProceduralBlocks.materialKey(state)) + && state.nativeHandle() instanceof MultipleFacing facing && facing.hasFace(f); } static boolean canGoOn(NativeBlockState decorator, NativeBlockState surface) { @@ -586,4 +623,10 @@ final class DecoratorCore { default -> state; }; } + + private static final class Faces { + private static final BlockFace[] SIDES = {BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST}; + private static final BlockFace[] CARTESIAN = {BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST, + BlockFace.UP, BlockFace.DOWN}; + } } diff --git a/core/src/main/java/art/arcane/iris/world/entity/IrisEffect.java b/core/src/main/java/art/arcane/iris/world/entity/IrisEffect.java index d41d027cf..508d343f9 100644 --- a/core/src/main/java/art/arcane/iris/world/entity/IrisEffect.java +++ b/core/src/main/java/art/arcane/iris/world/entity/IrisEffect.java @@ -51,6 +51,10 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.util.Vector; +import java.util.Locale; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + @Snippet("effect") @Accessors(chain = true) @NoArgsConstructor @@ -58,6 +62,9 @@ import org.bukkit.util.Vector; @Description("An iris effect") @Data public class IrisEffect { + private static final String MASTER_SOUND_CATEGORY = "master"; + private static final Set UNKNOWN_SOUND_CATEGORIES = ConcurrentHashMap.newKeySet(); + private final transient AtomicCache pt = new AtomicCache<>(); private final transient AtomicCache latch = new AtomicCache<>(); private final transient AtomicCache particleEffectResolved = new AtomicCache<>(); @@ -119,8 +126,8 @@ public class IrisEffect { @Description("The sound volume.") private double volume = 1.5D; @DependsOn({"sound"}) - @Description("The sound category the sound plays in, which picks the client volume slider that controls it (for example MUSIC, AMBIENT or HOSTILE).") - private SoundCategory soundCategory = SoundCategory.MASTER; + @Description("The sound category the sound plays in, which picks the client volume slider that controls it: master, music, record, weather, block, hostile, neutral, player, ambient, voice or ui.") + private String soundCategory = MASTER_SOUND_CATEGORY; @DependsOn({"particleEffect"}) @MinNumber(0) @MaxNumber(512) @@ -185,6 +192,10 @@ public class IrisEffect { return sound; } + public String getSoundCategoryKey() { + return soundCategory == null || soundCategory.isBlank() ? MASTER_SOUND_CATEGORY : soundCategory.trim().toLowerCase(Locale.ROOT); + } + public Particle getParticleEffect() { if (particleEffect == null) { return null; @@ -199,6 +210,32 @@ public class IrisEffect { return soundResolved.aquire(() -> resolveKeyed(Sound.class, sound)); } + static SoundCategory bukkitSoundCategory(String key) { + return switch (key) { + case "master" -> SoundCategory.MASTER; + case "music" -> SoundCategory.MUSIC; + case "record" -> SoundCategory.RECORDS; + case "weather" -> SoundCategory.WEATHER; + case "block" -> SoundCategory.BLOCKS; + case "hostile" -> SoundCategory.HOSTILE; + case "neutral" -> SoundCategory.NEUTRAL; + case "player" -> SoundCategory.PLAYERS; + case "ambient" -> SoundCategory.AMBIENT; + case "voice" -> SoundCategory.VOICE; + case "ui" -> SoundCategory.UI; + default -> null; + }; + } + + private SoundCategory resolveSoundCategory() { + String key = getSoundCategoryKey(); + SoundCategory category = bukkitSoundCategory(key); + if (category == null && UNKNOWN_SOUND_CATEGORIES.add(key)) { + IrisLogging.warn("Unknown sound category: \"" + key + "\"."); + } + return category; + } + private static T resolveKeyed(Class type, String key) { NamespacedKey namespacedKey = NamespacedKey.fromString(key); return namespacedKey == null ? null : RegistryUtil.lookup(type).get(namespacedKey); @@ -242,9 +279,10 @@ public class IrisEffect { BukkitFx.run(p, () -> { Sound soundType = getSound(); Particle particleType = getParticleEffect(); - if (soundType != null) { + SoundCategory category = soundType == null ? null : resolveSoundCategory(); + if (category != null) { Location part = p.getLocation().clone().add(RNG.r.i(-soundDistance, soundDistance), RNG.r.i(-soundDistance, soundDistance), RNG.r.i(-soundDistance, soundDistance)); - p.playSound(part, soundType, soundCategory, (float) volume, (float) RNG.r.d(minPitch, maxPitch)); + p.playSound(part, soundType, category, (float) volume, (float) RNG.r.d(minPitch, maxPitch)); } if (particleType != null) { @@ -319,9 +357,10 @@ public class IrisEffect { J.runEntity(p, () -> { Sound soundType = getSound(); Particle particleType = getParticleEffect(); - if (soundType != null) { + SoundCategory category = soundType == null ? null : resolveSoundCategory(); + if (category != null) { Location part = p.getLocation().clone().add(RNG.r.i(-soundDistance, soundDistance), RNG.r.i(-soundDistance, soundDistance), RNG.r.i(-soundDistance, soundDistance)); - p.getWorld().playSound(part, soundType, soundCategory, (float) volume, (float) RNG.r.d(minPitch, maxPitch)); + p.getWorld().playSound(part, soundType, category, (float) volume, (float) RNG.r.d(minPitch, maxPitch)); } if (particleType != null) { diff --git a/core/src/test/java/art/arcane/iris/generation/decoration/DecoratorCoreTest.java b/core/src/test/java/art/arcane/iris/generation/decoration/DecoratorCoreTest.java index 1063af9b3..c1c555711 100644 --- a/core/src/test/java/art/arcane/iris/generation/decoration/DecoratorCoreTest.java +++ b/core/src/test/java/art/arcane/iris/generation/decoration/DecoratorCoreTest.java @@ -11,11 +11,15 @@ import art.arcane.volmlib.util.math.RNG; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockSupport; import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.MultipleFacing; import org.junit.Test; import org.mockito.MockedStatic; +import java.util.EnumSet; import java.util.LinkedHashMap; +import java.util.Locale; import java.util.Map; +import java.util.Set; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -25,15 +29,23 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyDouble; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.when; public class DecoratorCoreTest { + private static final String VINE = "minecraft:vine"; + private static final String GLOW_LICHEN = "minecraft:glow_lichen"; + private static final Set VINE_FACES = EnumSet.of(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, + BlockFace.WEST, BlockFace.UP); + private static final Set LICHEN_FACES = EnumSet.of(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, + BlockFace.WEST, BlockFace.UP, BlockFace.DOWN); @Test public void partSeed_differsByPartOrdinal() { @@ -741,6 +753,121 @@ public class DecoratorCoreTest { } } + @Test + public void stackedVineCurtainHangsFromTheWallItsTopStrandFaces() { + IrisDecorator decorator = mock(IrisDecorator.class); + IrisData data = mock(IrisData.class); + NativeBlockState vine = facingState(VINE, VINE_FACES, EnumSet.of(BlockFace.NORTH)); + when(decorator.getHeight(any(RNG.class), anyDouble(), anyDouble(), eq(data))).thenReturn(3); + when(decorator.getTopThreshold()).thenReturn(1.0); + when(decorator.pickBlockData(any(RNG.class), eq(data), anyDouble(), anyDouble())).thenReturn(vine); + when(decorator.pickBlockDataTop(any(RNG.class), eq(data), anyDouble(), anyDouble())).thenReturn(vine); + Hunk output = Hunk.newArrayHunk(16, 8, 16); + output.set(8, 6, 8, sturdyState()); + output.set(8, 5, 7, sturdyState()); + output.set(9, 4, 8, sturdyState()); + + DecoratorCore.placeStackDown(decorator, 8, 8, 8, 8, 5, 0, output, new RNG(1L), data, 3, + new DecoratorCore.PlaceOpts(), null); + + assertEquals(EnumSet.of(BlockFace.NORTH, BlockFace.UP), faces(output.get(8, 5, 8))); + assertEquals(EnumSet.of(BlockFace.NORTH), faces(output.get(8, 4, 8))); + assertEquals(EnumSet.of(BlockFace.NORTH), faces(output.get(8, 3, 8))); + } + + @Test + public void unsupportedAuthoredSideFaceOnALoneVineIsDroppedAndReattached() { + NativeBlockState vine = facingState(VINE, VINE_FACES, EnumSet.of(BlockFace.NORTH)); + Hunk walled = Hunk.newArrayHunk(16, 8, 16); + walled.set(9, 3, 8, sturdyState()); + + assertEquals(EnumSet.of(BlockFace.EAST), faces(DecoratorCore.fixFacesForHunk(vine, walled, 8, 8, 8, 3, 8, null))); + assertEquals(EnumSet.of(BlockFace.UP), + faces(DecoratorCore.fixFacesForHunk(vine, Hunk.newArrayHunk(16, 8, 16), 8, 8, 8, 3, 8, null))); + } + + @Test + public void glowLichenSideFaceIsNotHeldByTheLichenAboveIt() { + NativeBlockState lichen = facingState(GLOW_LICHEN, LICHEN_FACES, EnumSet.of(BlockFace.NORTH)); + Hunk output = Hunk.newArrayHunk(16, 8, 16); + output.set(8, 2, 8, sturdyState()); + output.set(8, 4, 8, facingState(GLOW_LICHEN, LICHEN_FACES, EnumSet.of(BlockFace.NORTH))); + + assertEquals(EnumSet.of(BlockFace.DOWN), faces(DecoratorCore.fixFacesForHunk(lichen, output, 8, 8, 8, 3, 8, null))); + } + + @Test + public void supportedAuthoredSideFaceKeepsItsDirection() { + NativeBlockState vine = facingState(VINE, VINE_FACES, EnumSet.of(BlockFace.NORTH)); + Hunk output = Hunk.newArrayHunk(16, 8, 16); + output.set(8, 3, 7, sturdyState()); + output.set(9, 3, 8, sturdyState()); + + assertEquals(EnumSet.of(BlockFace.NORTH), faces(DecoratorCore.fixFacesForHunk(vine, output, 8, 8, 8, 3, 8, null))); + } + + @Test + public void vineWithoutAuthoredSideFaceReattachesToEverySturdyNeighbour() { + NativeBlockState vine = facingState(VINE, VINE_FACES, EnumSet.of(BlockFace.UP)); + Hunk output = Hunk.newArrayHunk(16, 8, 16); + output.set(7, 3, 8, sturdyState()); + output.set(8, 3, 9, sturdyState()); + output.set(8, 4, 8, sturdyState()); + + assertEquals(EnumSet.of(BlockFace.SOUTH, BlockFace.WEST, BlockFace.UP), + faces(DecoratorCore.fixFacesForHunk(vine, output, 8, 8, 8, 3, 8, null))); + } + + private NativeBlockState facingState(String material, Set allowed, Set faces) { + MultipleFacing handle = facing(material, allowed, faces); + String key = handle.getAsString(); + NativeBlockState state = mock(NativeBlockState.class); + when(state.isVineBlock()).thenReturn(true); + when(state.materialKey()).thenReturn(material); + when(state.key()).thenReturn(key); + when(state.nativeHandle()).thenReturn(handle); + return state; + } + + private static MultipleFacing facing(String material, Set allowed, Set faces) { + EnumSet current = EnumSet.noneOf(BlockFace.class); + current.addAll(faces); + MultipleFacing data = mock(MultipleFacing.class); + when(data.getAllowedFaces()).thenReturn(allowed); + when(data.getFaces()).thenAnswer(invocation -> EnumSet.copyOf(current)); + when(data.hasFace(any(BlockFace.class))).thenAnswer(invocation -> current.contains(invocation.getArgument(0))); + doAnswer(invocation -> { + BlockFace face = invocation.getArgument(0); + if (!allowed.contains(face)) { + throw new IllegalArgumentException(face + " is not allowed on " + material); + } + if (invocation.getArgument(1)) { + current.add(face); + } else { + current.remove(face); + } + return null; + }).when(data).setFace(any(BlockFace.class), anyBoolean()); + when(data.clone()).thenAnswer(invocation -> facing(material, allowed, current)); + when(data.getAsString()).thenAnswer(invocation -> { + StringBuilder key = new StringBuilder(material).append('['); + for (BlockFace face : allowed) { + if (key.charAt(key.length() - 1) != '[') { + key.append(','); + } + key.append(face.name().toLowerCase(Locale.ROOT)).append('=').append(current.contains(face)); + } + return key.append(']').toString(); + }); + return data; + } + + private static Set faces(NativeBlockState state) { + EnumSet faces = EnumSet.noneOf(BlockFace.class); + faces.addAll(((MultipleFacing) state.nativeHandle()).getFaces()); + return faces; + } + private NativeBlockState airState() { NativeBlockState air = mock(NativeBlockState.class); when(air.isAir()).thenReturn(true); diff --git a/core/src/test/java/art/arcane/iris/pack/schema/SchemaBuilderIdentityTest.java b/core/src/test/java/art/arcane/iris/pack/schema/SchemaBuilderIdentityTest.java index a4d0b8579..1ab80416b 100644 --- a/core/src/test/java/art/arcane/iris/pack/schema/SchemaBuilderIdentityTest.java +++ b/core/src/test/java/art/arcane/iris/pack/schema/SchemaBuilderIdentityTest.java @@ -84,7 +84,7 @@ public class SchemaBuilderIdentityTest { @ClassRule public static final PlatformLeakGuard PLATFORM_GUARD = PlatformLeakGuard.clean(); - private static final String EXPECTED_SCHEMA_DIGEST = "2224e73a1e9d76231d4df8bb1992451340e5f533b3fbafae9f4e1437747e666c"; + private static final String EXPECTED_SCHEMA_DIGEST = "f7890d23da41a0411eb92828d59c68d89ecc9ca266b357a9fd5d92107a9da4a5"; private static final List> SCHEMA_ROOTS = List.of( IrisDimension.class, diff --git a/core/src/test/java/art/arcane/iris/world/entity/IrisEffectSoundCategoryTest.java b/core/src/test/java/art/arcane/iris/world/entity/IrisEffectSoundCategoryTest.java new file mode 100644 index 000000000..6270b4967 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/world/entity/IrisEffectSoundCategoryTest.java @@ -0,0 +1,58 @@ +package art.arcane.iris.world.entity; + +import org.bukkit.SoundCategory; +import org.junit.Test; + +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + +public class IrisEffectSoundCategoryTest { + private static final Map VANILLA_CATEGORIES = Map.ofEntries( + Map.entry("master", SoundCategory.MASTER), + Map.entry("music", SoundCategory.MUSIC), + Map.entry("record", SoundCategory.RECORDS), + Map.entry("weather", SoundCategory.WEATHER), + Map.entry("block", SoundCategory.BLOCKS), + Map.entry("hostile", SoundCategory.HOSTILE), + Map.entry("neutral", SoundCategory.NEUTRAL), + Map.entry("player", SoundCategory.PLAYERS), + Map.entry("ambient", SoundCategory.AMBIENT), + Map.entry("voice", SoundCategory.VOICE), + Map.entry("ui", SoundCategory.UI)); + + @Test + public void soundCategoryIsStoredAsAPlainStringField() throws NoSuchFieldException { + assertEquals(String.class, IrisEffect.class.getDeclaredField("soundCategory").getType()); + } + + @Test + public void soundCategoryDefaultsToMaster() { + assertEquals("master", new IrisEffect().getSoundCategoryKey()); + assertEquals("master", new IrisEffect().setSoundCategory(null).getSoundCategoryKey()); + assertEquals("master", new IrisEffect().setSoundCategory(" ").getSoundCategoryKey()); + } + + @Test + public void soundCategoryKeyIgnoresCaseAndSurroundingSpace() { + assertEquals("ambient", new IrisEffect().setSoundCategory(" Ambient ").getSoundCategoryKey()); + assertEquals("music", new IrisEffect().setSoundCategory("MUSIC").getSoundCategoryKey()); + } + + @Test + public void everyVanillaCategoryNameResolvesToItsBukkitCategory() { + for (Map.Entry entry : VANILLA_CATEGORIES.entrySet()) { + assertSame(entry.getKey(), entry.getValue(), IrisEffect.bukkitSoundCategory(entry.getKey())); + } + assertEquals(SoundCategory.values().length, VANILLA_CATEGORIES.size()); + } + + @Test + public void unknownCategoryNamesDoNotResolve() { + assertNull(IrisEffect.bukkitSoundCategory("ambiant")); + assertNull(IrisEffect.bukkitSoundCategory("records")); + assertNull(IrisEffect.bukkitSoundCategory("players")); + } +}