mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Answer the GRAVITY, LOG and LEAVES traits, block tags and attachment to a face from the platform layer, with Bukkit, modded and stub implementations
This commit is contained in:
1 parent
818dbef261
commit
563c2cd276
15 files changed
+344
-35
No files matched your search
@@ -20,6 +20,7 @@ package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import art.arcane.iris.spi.PlatformFace;
|
||||
import com.volmit.block.BlockFace;
|
||||
import com.volmit.block.BlockRegistry;
|
||||
import com.volmit.block.BlockState;
|
||||
@@ -28,10 +29,8 @@ import com.volmit.resource.ResourceId;
|
||||
|
||||
/**
|
||||
* IrisApi's view of the bound platform's blocks. States convert through their keys, as {@link PlatformBlockStates}
|
||||
* does; the platform decides which blocks exist, so a key it does not know fails. Traits answer from what the
|
||||
* platform's block states can tell. Three traits ({@code GRAVITY}, {@code LOG}, {@code LEAVES}), tags and
|
||||
* attachment to a face are not offered by the platform layer yet, and asking for them throws
|
||||
* {@link UnsupportedOperationException} rather than guessing.
|
||||
* does; the platform decides which blocks exist, so a key it does not know fails. Traits, tags and attachment to
|
||||
* a face are the platform's own answers: its block tags, its falling blocks and its face support.
|
||||
*/
|
||||
final class IrisBlockRegistry implements BlockRegistry {
|
||||
@Override
|
||||
@@ -55,18 +54,20 @@ final class IrisBlockRegistry implements BlockRegistry {
|
||||
case BLOCK_ENTITY -> platform.hasTileEntity();
|
||||
case CUSTOM -> platform.isCustom();
|
||||
case NEEDS_UPDATE -> platform.isUpdatable();
|
||||
case GRAVITY, LOG, LEAVES -> throw unsupported("The " + trait + " trait");
|
||||
case GRAVITY -> platform.hasGravity();
|
||||
case LOG -> platform.isLog();
|
||||
case LEAVES -> platform.isLeaves();
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inTag(BlockState state, ResourceId tag) {
|
||||
throw unsupported("Block tags");
|
||||
return IrisPlatforms.get().registries().isInTag(platformState(state.key()), tag.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAttach(BlockState state, BlockState support, BlockFace face) {
|
||||
throw unsupported("Attachment to a face");
|
||||
return platformState(state.key()).canAttach(platformState(support.key()), PlatformFace.valueOf(face.name()));
|
||||
}
|
||||
|
||||
private static PlatformBlockState platformState(String key) {
|
||||
@@ -76,8 +77,4 @@ final class IrisBlockRegistry implements BlockRegistry {
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
private static UnsupportedOperationException unsupported(String what) {
|
||||
return new UnsupportedOperationException(what + " cannot be answered yet: Iris's platform layer does not offer it");
|
||||
}
|
||||
}
|
||||
@@ -20,9 +20,12 @@ package art.arcane.iris.platform.bukkit;
|
||||
|
||||
import art.arcane.iris.core.nms.INMS;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import art.arcane.iris.spi.PlatformFace;
|
||||
import art.arcane.iris.util.common.data.IrisCustomData;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Tag;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.BlockSupport;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -52,7 +55,9 @@ public final class BukkitBlockState implements PlatformBlockState {
|
||||
private volatile Boolean lit;
|
||||
private volatile Boolean updatable;
|
||||
private volatile Boolean foliage;
|
||||
private volatile Boolean treeBlock;
|
||||
private volatile Boolean log;
|
||||
private volatile Boolean leaves;
|
||||
private volatile Boolean gravity;
|
||||
private volatile Boolean foliagePlantable;
|
||||
private volatile Boolean decorant;
|
||||
private volatile Boolean storage;
|
||||
@@ -263,15 +268,51 @@ public final class BukkitBlockState implements PlatformBlockState {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTreeBlock() {
|
||||
Boolean cached = treeBlock;
|
||||
public boolean isLog() {
|
||||
Boolean cached = log;
|
||||
if (cached == null) {
|
||||
cached = Tag.LOGS.isTagged(data.getMaterial()) || Tag.LEAVES.isTagged(data.getMaterial());
|
||||
treeBlock = cached;
|
||||
cached = Tag.LOGS.isTagged(data.getMaterial());
|
||||
log = cached;
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLeaves() {
|
||||
Boolean cached = leaves;
|
||||
if (cached == null) {
|
||||
cached = Tag.LEAVES.isTagged(data.getMaterial());
|
||||
leaves = cached;
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasGravity() {
|
||||
Boolean cached = gravity;
|
||||
if (cached == null) {
|
||||
cached = data.getMaterial().hasGravity();
|
||||
gravity = cached;
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAttach(PlatformBlockState support, PlatformFace face) {
|
||||
return ((BlockData) support.nativeHandle()).isFaceSturdy(blockFaceOf(face), BlockSupport.FULL);
|
||||
}
|
||||
|
||||
private static BlockFace blockFaceOf(PlatformFace face) {
|
||||
return switch (face) {
|
||||
case UP -> BlockFace.UP;
|
||||
case DOWN -> BlockFace.DOWN;
|
||||
case NORTH -> BlockFace.NORTH;
|
||||
case SOUTH -> BlockFace.SOUTH;
|
||||
case EAST -> BlockFace.EAST;
|
||||
case WEST -> BlockFace.WEST;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFoliagePlantable() {
|
||||
Boolean cached = foliagePlantable;
|
||||
|
||||
@@ -35,9 +35,11 @@ import art.arcane.iris.spi.PlatformRegistries;
|
||||
import art.arcane.iris.util.common.reflect.KeyedType;
|
||||
import art.arcane.volmlib.util.json.JSONArray;
|
||||
import art.arcane.volmlib.util.json.JSONObject;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.Tag;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
@@ -76,6 +78,16 @@ public final class BukkitRegistries implements PlatformRegistries {
|
||||
return data == null ? null : BukkitBlockState.of(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInTag(PlatformBlockState block, String tag) {
|
||||
NamespacedKey tagKey = NamespacedKey.fromString(tag);
|
||||
if (tagKey == null) {
|
||||
return false;
|
||||
}
|
||||
Tag<Material> blockTag = Bukkit.getTag(Tag.REGISTRY_BLOCKS, tagKey, Material.class);
|
||||
return blockTag != null && blockTag.isTagged(((BlockData) block.nativeHandle()).getMaterial());
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformBlockState air() {
|
||||
return BukkitBlockState.of(BukkitBlockResolution.getAir());
|
||||
|
||||
@@ -3,6 +3,7 @@ package art.arcane.iris.core.modules;
|
||||
import art.arcane.iris.spi.IrisPlatform;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import art.arcane.iris.spi.PlatformFace;
|
||||
import art.arcane.iris.spi.PlatformRegistries;
|
||||
import com.volmit.block.BlockFace;
|
||||
import com.volmit.block.BlockState;
|
||||
@@ -19,21 +20,38 @@ import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** IrisApi's blocks are the bound platform's, through their keys; what the platform cannot answer is said so. */
|
||||
/** IrisApi's blocks are the bound platform's, through their keys, and so are its traits, tags and face support. */
|
||||
public class IrisBlockRegistryTest {
|
||||
private final IrisBlockRegistry blocks = new IrisBlockRegistry();
|
||||
private PlatformRegistries registries;
|
||||
private PlatformBlockState stone;
|
||||
private PlatformBlockState sand;
|
||||
|
||||
@Before
|
||||
public void bindPlatform() {
|
||||
PlatformBlockState stone = mock(PlatformBlockState.class);
|
||||
stone = mock(PlatformBlockState.class);
|
||||
when(stone.key()).thenReturn("stone");
|
||||
when(stone.isSolid()).thenReturn(true);
|
||||
when(stone.isOre()).thenReturn(false);
|
||||
PlatformRegistries registries = mock(PlatformRegistries.class);
|
||||
sand = mock(PlatformBlockState.class);
|
||||
when(sand.key()).thenReturn("minecraft:sand");
|
||||
when(sand.hasGravity()).thenReturn(true);
|
||||
PlatformBlockState log = mock(PlatformBlockState.class);
|
||||
when(log.key()).thenReturn("minecraft:oak_log");
|
||||
when(log.isLog()).thenReturn(true);
|
||||
PlatformBlockState leaves = mock(PlatformBlockState.class);
|
||||
when(leaves.key()).thenReturn("minecraft:oak_leaves");
|
||||
when(leaves.isLeaves()).thenReturn(true);
|
||||
registries = mock(PlatformRegistries.class);
|
||||
when(registries.blockOrNull(eq("minecraft:stone"), anyBoolean())).thenReturn(stone);
|
||||
when(registries.blockOrNull(eq("minecraft:sand"), anyBoolean())).thenReturn(sand);
|
||||
when(registries.blockOrNull(eq("minecraft:oak_log"), anyBoolean())).thenReturn(log);
|
||||
when(registries.blockOrNull(eq("minecraft:oak_leaves"), anyBoolean())).thenReturn(leaves);
|
||||
when(registries.blockOrNull(eq("minecraft:nothing"), anyBoolean())).thenReturn(null);
|
||||
when(registries.isInTag(log, "minecraft:logs")).thenReturn(true);
|
||||
IrisPlatform platform = mock(IrisPlatform.class);
|
||||
when(platform.registries()).thenReturn(registries);
|
||||
IrisPlatforms.unbind();
|
||||
@@ -53,6 +71,8 @@ public class IrisBlockRegistryTest {
|
||||
@Test
|
||||
public void aBlockThePlatformDoesNotKnowIsRejected() {
|
||||
assertThrows(IllegalArgumentException.class, () -> blocks.resolve("minecraft:nothing"));
|
||||
assertThrows(IllegalArgumentException.class, () -> blocks.hasTrait(BlockState.parse("minecraft:nothing"), BlockTrait.LOG));
|
||||
assertThrows(IllegalArgumentException.class, () -> blocks.inTag(BlockState.parse("minecraft:nothing"), ResourceId.parse("minecraft:logs")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -64,11 +84,37 @@ public class IrisBlockRegistryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whatThePlatformLayerDoesNotOfferFailsInsteadOfGuessing() {
|
||||
BlockState stone = BlockState.parse("minecraft:stone");
|
||||
public void gravityLogAndLeavesComeFromThePlatformToo() {
|
||||
BlockState sand = BlockState.parse("minecraft:sand");
|
||||
BlockState log = BlockState.parse("minecraft:oak_log");
|
||||
BlockState leaves = BlockState.parse("minecraft:oak_leaves");
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> blocks.hasTrait(stone, BlockTrait.GRAVITY));
|
||||
assertThrows(UnsupportedOperationException.class, () -> blocks.inTag(stone, ResourceId.parse("minecraft:logs")));
|
||||
assertThrows(UnsupportedOperationException.class, () -> blocks.canAttach(stone, stone, BlockFace.UP));
|
||||
assertTrue(blocks.hasTrait(sand, BlockTrait.GRAVITY));
|
||||
assertFalse(blocks.hasTrait(sand, BlockTrait.LOG));
|
||||
assertTrue(blocks.hasTrait(log, BlockTrait.LOG));
|
||||
assertFalse(blocks.hasTrait(log, BlockTrait.GRAVITY));
|
||||
assertTrue(blocks.hasTrait(leaves, BlockTrait.LEAVES));
|
||||
assertFalse(blocks.hasTrait(leaves, BlockTrait.LOG));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tagsAreTheRegistrysMembership() {
|
||||
assertTrue(blocks.inTag(BlockState.parse("minecraft:oak_log"), ResourceId.parse("minecraft:logs")));
|
||||
assertFalse(blocks.inTag(BlockState.parse("minecraft:stone"), ResourceId.parse("minecraft:logs")));
|
||||
assertFalse("a tag nobody knows has no members", blocks.inTag(BlockState.parse("minecraft:oak_log"), ResourceId.parse("pack:nothing")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void everyFaceAsksThePlatformAboutThatFace() {
|
||||
BlockState torch = BlockState.parse("minecraft:stone");
|
||||
BlockState support = BlockState.parse("minecraft:sand");
|
||||
for (BlockFace face : BlockFace.values()) {
|
||||
when(stone.canAttach(sand, PlatformFace.valueOf(face.name()))).thenReturn(face == BlockFace.NORTH);
|
||||
}
|
||||
|
||||
for (BlockFace face : BlockFace.values()) {
|
||||
assertEquals(face.name(), face == BlockFace.NORTH, blocks.canAttach(torch, support, face));
|
||||
verify(stone).canAttach(sand, PlatformFace.valueOf(face.name()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -228,6 +228,11 @@ public class ContentKeyValidatorTest {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInTag(PlatformBlockState block, String tag) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformBlockState deepSlateOre(PlatformBlockState block, PlatformBlockState ore) {
|
||||
return null;
|
||||
|
||||
@@ -476,6 +476,11 @@ public class SchemaBuilderParityTest {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInTag(PlatformBlockState block, String tag) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformBlockState deepSlateOre(PlatformBlockState block, PlatformBlockState ore) {
|
||||
return null;
|
||||
|
||||
@@ -4,6 +4,7 @@ import art.arcane.iris.engine.object.prefab.IrisObject;
|
||||
import art.arcane.iris.spi.IrisPlatform;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import art.arcane.iris.spi.PlatformFace;
|
||||
import art.arcane.iris.spi.PlatformRegistries;
|
||||
import art.arcane.iris.util.common.math.IrisBlockVector;
|
||||
import org.junit.AfterClass;
|
||||
@@ -198,7 +199,22 @@ public class TreePlausibilizerTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTreeBlock() {
|
||||
public boolean isLog() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLeaves() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasGravity() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAttach(PlatformBlockState support, PlatformFace face) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user