From 99c51263644cbd1dde9262517cfd98117044a264 Mon Sep 17 00:00:00 2001 From: Pyrofab Date: Fri, 9 Apr 2021 22:15:22 +0200 Subject: [PATCH] =?UTF-8?q?Add=20helper=20methods=20to=20expose=20componen?= =?UTF-8?q?ts=20through=20API=C2=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + cardinal-components-block/build.gradle | 1 + .../cca/api/v3/block/BlockComponent.java | 4 + .../block/BlockComponentFactoryRegistry.java | 3 + .../api/v3/block/BlockComponentProvider.java | 4 + .../cca/api/v3/block/BlockComponents.java | 118 ++++++++++++++++++ .../src/main/resources/fabric.mod.json | 1 + changelog.md | 10 ++ gradle.properties | 4 +- .../componenttest/CardinalComponentsTest.java | 14 +++ .../componenttest/TestComponents.java | 18 +-- .../componenttest/VitaCompound.java | 95 ++++++++++++++ .../componenttest/VitalityCondenser.java | 3 +- .../componenttest/VitalityStickItem.java | 19 +++ .../assets/componenttest/lang/en_us.json | 1 + src/testmod/resources/fabric.mod.json | 3 +- 16 files changed, 278 insertions(+), 21 deletions(-) create mode 100644 src/testmod/java/dev/onyxstudios/componenttest/VitaCompound.java diff --git a/build.gradle b/build.gradle index d44c7c64..1bad1f2c 100644 --- a/build.gradle +++ b/build.gradle @@ -141,6 +141,7 @@ dependencies { modImplementation fabricApi.module("fabric-item-api-v1", rootProject.fabric_api_version) modImplementation fabricApi.module("fabric-item-groups-v0", rootProject.fabric_api_version) modImplementation fabricApi.module("fabric-events-interaction-v0", rootProject.fabric_api_version) + modImplementation fabricApi.module("fabric-api-lookup-api-v1", rootProject.fabric_api_version) modRuntime fabricApi.module("fabric-networking-v0", rootProject.rootProject.fabric_api_version) modRuntime fabricApi.module("fabric-resource-loader-v0", rootProject.fabric_api_version) modRuntime fabricApi.module("fabric-mining-levels-v0", rootProject.fabric_api_version) diff --git a/cardinal-components-block/build.gradle b/cardinal-components-block/build.gradle index 6355e14a..0894642d 100644 --- a/cardinal-components-block/build.gradle +++ b/cardinal-components-block/build.gradle @@ -1,4 +1,5 @@ dependencies { api project(":cardinal-components-base") api project(":cardinal-components-util") + modApi fabricApi.module("fabric-api-lookup-api-v1", rootProject.fabric_api_version) } diff --git a/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponent.java b/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponent.java index 3d4030cb..dbad5023 100644 --- a/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponent.java +++ b/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponent.java @@ -25,10 +25,14 @@ import dev.onyxstudios.cca.api.v3.component.ComponentV3; import net.minecraft.block.BlockState; import net.minecraft.nbt.CompoundTag; +import org.jetbrains.annotations.ApiStatus; /** * A helper interface for components that entirely rely on contextual information like {@link BlockState}s. + * @deprecated use {@link net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup} instead */ +@Deprecated +@ApiStatus.ScheduledForRemoval(inVersion = "3.0.0") public interface BlockComponent extends ComponentV3 { @Override default void readFromNbt(CompoundTag tag) { diff --git a/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponentFactoryRegistry.java b/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponentFactoryRegistry.java index a6b65afe..91075e8f 100644 --- a/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponentFactoryRegistry.java +++ b/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponentFactoryRegistry.java @@ -44,7 +44,10 @@ public interface BlockComponentFactoryRegistry { * @throws NullPointerException if any of the arguments is {@code null} * @see BlockComponent * @see BlockComponents + * @deprecated use {@link net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup} instead */ + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "3.0.0") void registerFor(Identifier blockId, ComponentKey key, BlockComponentProvider factory); /** diff --git a/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponentProvider.java b/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponentProvider.java index 024a02e2..e76f6aa9 100644 --- a/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponentProvider.java +++ b/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponentProvider.java @@ -29,6 +29,7 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.world.BlockView; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; /** @@ -55,7 +56,10 @@ * @see BlockComponents * @see BlockComponentFactoryRegistry * @since 2.5.0 + * @deprecated use {@link net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup} instead */ +@Deprecated +@ApiStatus.ScheduledForRemoval(inVersion = "3.0.0") @FunctionalInterface public interface BlockComponentProvider { /** diff --git a/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponents.java b/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponents.java index 7c50928d..06ca99c8 100644 --- a/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponents.java +++ b/cardinal-components-block/src/main/java/dev/onyxstudios/cca/api/v3/block/BlockComponents.java @@ -25,8 +25,10 @@ import dev.onyxstudios.cca.api.v3.component.ComponentKey; import dev.onyxstudios.cca.internal.block.InternalBlockComponentProvider; import nerdhub.cardinal.components.api.component.Component; +import net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.world.BlockView; @@ -34,16 +36,112 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import java.util.NoSuchElementException; +import java.util.function.BiFunction; + /** * This class consists exclusively of static methods that return a {@link Component} by querying some block context. */ @ApiStatus.Experimental public final class BlockComponents { + /** + * Retrieves a context-less {@link BlockApiLookup} for the given {@link ComponentKey}. + * + *

The component must also be exposed to the lookup by registering a provider + * for relevant block entities. + * + * @param key the key denoting the component for which a block lookup will be retrieved + * @param the type of the component/API + * @return a {@link BlockApiLookup} for retrieving instances of {@code C} + * @see #exposeApi(ComponentKey, BlockApiLookup) + * @see #exposeApi(ComponentKey, BlockApiLookup, BiFunction) + */ + @ApiStatus.Experimental + public static BlockApiLookup getApiLookup(ComponentKey key) { + return BlockApiLookup.get(key.getId(), key.getComponentClass(), Void.class); + } + + /** + * Exposes an API for all block entities to which a given component is attached. + * + * @see #exposeApi(ComponentKey, BlockApiLookup, BiFunction) + */ + @ApiStatus.Experimental + public static void exposeApi(ComponentKey key, BlockApiLookup apiLookup) { + apiLookup.registerFallback((world, pos, state, blockEntity, context) -> { + if (blockEntity != null) { + // yes you can cast to + @SuppressWarnings("unchecked") A ret = key.getNullable(blockEntity); + return ret; + } + return null; + }); + } + + /** + * Exposes an API for all block entities to which a given component is attached. + * + *

Usage Example

+ * Let us pretend we have the {@code FLUID_CONTAINER} API, as defined in {@link BlockApiLookup}'s usage example. + * + *
{@code
+     * public interface FluidContainerCompound extends Component {
+     *      ComponentKey KEY = ComponentRegistry.register(new Identifier("mymod:fluid_container_compound"), FluidContainerCompound.class);
+     *
+     *      FluidContainer get(Direction side);
+     * }
+     * }
+ * + *
{@code
+     * @Override
+     * public void onInitialize() {
+     *     BlockComponents.exposeApi(
+     *         FluidContainerCompound.KEY,
+     *         MyApi.FLUID_CONTAINER,
+     *         FluidContainerCompound::get
+     *     );
+     * }
+     * }
+ */ + @ApiStatus.Experimental + public static void exposeApi(ComponentKey key, BlockApiLookup apiLookup, BiFunction mapper) { + apiLookup.registerFallback((world, pos, state, blockEntity, context) -> { + if (blockEntity != null) { + C ret = key.getNullable(blockEntity); + if (ret != null) return mapper.apply(ret, context); + } + return null; + }); + } + + /** + * Exposes an API for block entities of a given type, assuming the given component is attached. + * + *

This method should be preferred to other overloads as it is more performant than the more generic alternatives. + * If the component is not {@linkplain BlockComponentFactoryRegistry#registerFor(Class, ComponentKey, BlockEntityComponentFactory) attached} + * to one of the {@code types}, calling {@link BlockApiLookup#find(World, BlockPos, Object)} + * on the corresponding block will throw a {@link NoSuchElementException}. + * + * @see #exposeApi(ComponentKey, BlockApiLookup, BiFunction) + */ + @ApiStatus.Experimental + public static void exposeApi(ComponentKey key, BlockApiLookup apiLookup, BiFunction mapper, BlockEntityType... types) { + apiLookup.registerForBlockEntities((blockEntity, context) -> mapper.apply(key.get(blockEntity), context), types); + } + /** + * @deprecated use {@link ComponentKey#get(Object) KEY.get(blockEntity)} instead + */ + @Deprecated public static @Nullable C get(ComponentKey key, BlockEntity blockEntity) { return get(key, blockEntity, null); } + /** + * @deprecated use {@link BlockApiLookup} if you need additional context, otherwise call {@link ComponentKey#get(Object) KEY.get(blockEntity)} + */ + @Deprecated + @ApiStatus.ScheduledForRemoval public static @Nullable C get(ComponentKey key, BlockEntity blockEntity, @Nullable Direction side) { World world = blockEntity.getWorld(); @@ -58,18 +156,38 @@ public final class BlockComponents { return key.getNullable(blockEntity); } + /** + * @deprecated use {@link BlockApiLookup} if you need additional context or if you want to query a BE-less block, otherwise call {@link ComponentKey#get(Object) KEY.get(world.getBlockEntity(pos))} + */ + @Deprecated + @ApiStatus.ScheduledForRemoval public static @Nullable C get(ComponentKey key, BlockView world, BlockPos pos) { return get(key, world, pos, null); } + /** + * @deprecated use {@link BlockApiLookup} if you need additional context or if you want to query a BE-less block, otherwise call {@link ComponentKey#get(Object) KEY.get(world.getBlockEntity(pos))} + */ + @Deprecated + @ApiStatus.ScheduledForRemoval public static @Nullable C get(ComponentKey key, BlockState blockState, BlockView world, BlockPos pos) { return get(key, blockState, world, pos, null); } + /** + * @deprecated use {@link BlockApiLookup} if you need additional context or if you want to query a BE-less block, otherwise call {@link ComponentKey#get(Object) KEY.get(world.getBlockEntity(pos))} + */ + @Deprecated + @ApiStatus.ScheduledForRemoval public static @Nullable C get(ComponentKey key, BlockView world, BlockPos pos, @Nullable Direction side) { return get(key, world.getBlockState(pos), world, pos, side); } + /** + * @deprecated use {@link BlockApiLookup} if you need additional context or if you want to query a BE-less block, otherwise call {@link ComponentKey#get(Object) KEY.get(world.getBlockEntity(pos))} + */ + @Deprecated + @ApiStatus.ScheduledForRemoval public static @Nullable C get(ComponentKey key, BlockState blockState, BlockView world, BlockPos pos, @Nullable Direction side) { @Nullable C res = getFromBlock(key, world, pos, side, blockState); diff --git a/cardinal-components-block/src/main/resources/fabric.mod.json b/cardinal-components-block/src/main/resources/fabric.mod.json index c91e39b2..4f7fa392 100644 --- a/cardinal-components-block/src/main/resources/fabric.mod.json +++ b/cardinal-components-block/src/main/resources/fabric.mod.json @@ -19,6 +19,7 @@ }, "depends": { "fabric-api-base": "*", + "fabric-api-lookup-api-v1": "*", "cardinal-components-base": "<3.0.0-" }, "authors": [ diff --git a/changelog.md b/changelog.md index ae2c77cc..41a94a17 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,13 @@ +------------------------------------------------------ +Version 2.8.0 +------------------------------------------------------ +Additions +- Added helper methods in `BlockComponents` to expose block components through Fabric API API-API API + +Changes +- Methods and classes in `cardinal-components-block` which purpose was to access components on regular blocks have been + scheduled for removal as they are now superseded by APIĀ² + ------------------------------------------------------ Version 2.7.13 ------------------------------------------------------ diff --git a/gradle.properties b/gradle.properties index bd75f86f..69775556 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,10 +7,10 @@ yarn_mappings=5 loader_version=0.10.6+build.214 #Fabric api -fabric_api_version=0.25.1+build.416-1.16 +fabric_api_version=0.32.5+1.16 #Publishing -mod_version = 2.7.13 +mod_version = 2.8.0 curseforge_id = 318449 curseforge_versions = 1.16.2; 1.16.3; 1.16.4; 1.16.5 changelog_url = https://github.com/OnyxStudios/Cardinal-Components-API/blob/master/changelog.md diff --git a/src/testmod/java/dev/onyxstudios/componenttest/CardinalComponentsTest.java b/src/testmod/java/dev/onyxstudios/componenttest/CardinalComponentsTest.java index 442bbfdb..3860b7ac 100644 --- a/src/testmod/java/dev/onyxstudios/componenttest/CardinalComponentsTest.java +++ b/src/testmod/java/dev/onyxstudios/componenttest/CardinalComponentsTest.java @@ -23,6 +23,7 @@ package dev.onyxstudios.componenttest; import com.google.common.reflect.TypeToken; +import dev.onyxstudios.cca.api.v3.block.BlockComponents; import dev.onyxstudios.cca.api.v3.component.ComponentContainer; import dev.onyxstudios.cca.api.v3.component.ComponentRegistryV3; import dev.onyxstudios.cca.api.v3.util.ComponentContainerMetafactory; @@ -35,11 +36,13 @@ import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.fabric.api.event.player.UseItemCallback; +import net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry; import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder; import net.minecraft.block.Blocks; import net.minecraft.block.Material; +import net.minecraft.block.entity.BlockEntityType; import net.minecraft.entity.EntityType; import net.minecraft.entity.SpawnGroup; import net.minecraft.entity.mob.ZombieEntity; @@ -49,11 +52,13 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; import net.minecraft.util.TypedActionResult; +import net.minecraft.util.math.Direction; import net.minecraft.util.registry.Registry; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import javax.annotation.Nullable; +import java.util.Objects; import java.util.UUID; import java.util.function.BiFunction; @@ -76,6 +81,8 @@ public class CardinalComponentsTest { public static final EntityType VITALITY_ZOMBIE = Registry.register(Registry.ENTITY_TYPE, "componenttest:vita_zombie", FabricEntityTypeBuilder.create(SpawnGroup.MONSTER, VitalityZombieEntity::new).dimensions(EntityType.ZOMBIE.getDimensions()).build()); + public static final BlockApiLookup VITA_API_LOOKUP = BlockApiLookup.get(CardinalComponentsTest.id("sided_vita"), Vita.class, Direction.class); + public static Identifier id(String path) { return new Identifier("componenttest", path); } @@ -143,6 +150,13 @@ public static void init() { LOGGER.info("{} vitality: {}", stack, TestComponents.ALT_VITA.get(stack).getVitality()); // init components return TypedActionResult.pass(stack); }); + + VITA_API_LOOKUP.registerForBlocks( + (world, pos, state, blockEntity, context) -> TestComponents.VITA.get(Objects.requireNonNull(world.getExistingChunk(pos.getX() >> 4, pos.getZ() >> 4))), + VITALITY_CONDENSER + ); + BlockComponents.exposeApi(TestComponents.VITA, VITA_API_LOOKUP, (vita, side) -> side == Direction.UP ? vita : null, BlockEntityType.END_PORTAL); + BlockComponents.exposeApi(VitaCompound.KEY, VITA_API_LOOKUP, VitaCompound::get, BlockEntityType.END_GATEWAY); } public interface TestContainerFactory { diff --git a/src/testmod/java/dev/onyxstudios/componenttest/TestComponents.java b/src/testmod/java/dev/onyxstudios/componenttest/TestComponents.java index 37e7e7b6..d0e4ab5a 100644 --- a/src/testmod/java/dev/onyxstudios/componenttest/TestComponents.java +++ b/src/testmod/java/dev/onyxstudios/componenttest/TestComponents.java @@ -44,17 +44,14 @@ import dev.onyxstudios.componenttest.vita.*; import nerdhub.cardinal.components.api.ComponentRegistry; import nerdhub.cardinal.components.api.ComponentType; +import net.minecraft.block.entity.EndGatewayBlockEntity; import net.minecraft.block.entity.EndPortalBlockEntity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.mob.HostileEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Items; import net.minecraft.util.Identifier; -import net.minecraft.util.math.Direction; -import net.minecraft.world.CollisionView; -import net.minecraft.world.chunk.Chunk; -import java.util.Objects; import java.util.UUID; import java.util.function.BiFunction; @@ -97,19 +94,8 @@ public void registerChunkComponentFactories(ChunkComponentFactoryRegistry regist @Override public void registerBlockComponentFactories(BlockComponentFactoryRegistry registry) { - registry.registerFor(CardinalComponentsTest.id("vita_condenser"), VITA, - (state, world, pos, side) -> { - if (world instanceof CollisionView) - return VITA.get(Objects.requireNonNull(((CollisionView) world).getExistingChunk(pos.getX() >> 4, pos.getZ() >> 4))); - if (world instanceof Chunk) return VITA.get(world); - return null; - }); + registry.registerFor(EndGatewayBlockEntity.class, VitaCompound.KEY, VitaCompound::new); registry.registerFor(EndPortalBlockEntity.class, VITA, SyncedVita::new); - registry.registerFor( - new Identifier("end_gateway"), - VITA, - (state, world, pos, side) -> side != Direction.UP ? VITA.maybeGet(world.getBlockEntity(pos)).orElse(null) : null - ); } @Override diff --git a/src/testmod/java/dev/onyxstudios/componenttest/VitaCompound.java b/src/testmod/java/dev/onyxstudios/componenttest/VitaCompound.java new file mode 100644 index 00000000..d0acfa1f --- /dev/null +++ b/src/testmod/java/dev/onyxstudios/componenttest/VitaCompound.java @@ -0,0 +1,95 @@ +/* + * Cardinal-Components-API + * Copyright (C) 2019-2021 OnyxStudios + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE + * OR OTHER DEALINGS IN THE SOFTWARE. + */ +package dev.onyxstudios.componenttest; + +import dev.onyxstudios.cca.api.v3.component.ComponentKey; +import dev.onyxstudios.cca.api.v3.component.ComponentRegistryV3; +import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent; +import dev.onyxstudios.componenttest.vita.SyncedVita; +import dev.onyxstudios.componenttest.vita.Vita; +import net.fabricmc.fabric.api.util.NbtType; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Util; +import net.minecraft.util.math.Direction; + +import java.util.EnumMap; +import java.util.Map; + +public class VitaCompound implements AutoSyncedComponent { + public static final ComponentKey KEY = ComponentRegistryV3.INSTANCE.getOrCreate(CardinalComponentsTest.id("vita_compound"), VitaCompound.class); + + private final Map storage = new EnumMap<>(Direction.class); + + public VitaCompound(BlockEntity owner) { + for (Direction side : Direction.values()) { + storage.put(side, new SyncedVita(owner)); + } + } + + @Override + public void readFromNbt(CompoundTag tag) { + storage.forEach((side, vita) -> tag.put(side.name(), Util.make(new CompoundTag(), vita::writeToNbt))); + } + + @Override + public void writeToNbt(CompoundTag tag) { + for (Map.Entry entry : this.storage.entrySet()) { + if (tag.contains(entry.getKey().name(), NbtType.COMPOUND)) { + entry.getValue().readFromNbt(tag.getCompound(entry.getKey().name())); + } + } + } + + @Override + public boolean shouldSyncWith(ServerPlayerEntity player) { + for (SyncedVita value : this.storage.values()) { + if (value.shouldSyncWith(player)) { + return true; + } + } + return false; + } + + @Override + public void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient) { + for (SyncedVita value : this.storage.values()) { + if (value.shouldSyncWith(recipient)) { + value.writeSyncPacket(buf, recipient); + } + } + } + + @Override + public void applySyncPacket(PacketByteBuf buf) { + for (SyncedVita value : this.storage.values()) { + value.applySyncPacket(buf); + } + } + + public Vita get(Direction side) { + return storage.get(side); + } +} diff --git a/src/testmod/java/dev/onyxstudios/componenttest/VitalityCondenser.java b/src/testmod/java/dev/onyxstudios/componenttest/VitalityCondenser.java index 66a07ce3..9f2e55f0 100644 --- a/src/testmod/java/dev/onyxstudios/componenttest/VitalityCondenser.java +++ b/src/testmod/java/dev/onyxstudios/componenttest/VitalityCondenser.java @@ -22,7 +22,6 @@ */ package dev.onyxstudios.componenttest; -import dev.onyxstudios.cca.api.v3.block.BlockComponents; import dev.onyxstudios.componenttest.vita.Vita; import net.minecraft.block.Block; import net.minecraft.block.BlockState; @@ -58,7 +57,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt // only on client side, to confirm that sync works if (world.isClient) { player.sendMessage(new TranslatableText("componenttest:action.chunk_vitality", - Objects.requireNonNull(BlockComponents.get(TestComponents.VITA, world, pos)).getVitality()), true); + Objects.requireNonNull(CardinalComponentsTest.VITA_API_LOOKUP.find(world, pos, state, null, hitInfo.getSide())).getVitality()), true); } return ActionResult.SUCCESS; } diff --git a/src/testmod/java/dev/onyxstudios/componenttest/VitalityStickItem.java b/src/testmod/java/dev/onyxstudios/componenttest/VitalityStickItem.java index 8c079f64..d2ced3c3 100644 --- a/src/testmod/java/dev/onyxstudios/componenttest/VitalityStickItem.java +++ b/src/testmod/java/dev/onyxstudios/componenttest/VitalityStickItem.java @@ -34,10 +34,12 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemUsageContext; import net.minecraft.scoreboard.AbstractTeam; import net.minecraft.server.world.ServerWorld; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; +import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; import net.minecraft.util.math.BlockPos; @@ -73,6 +75,23 @@ public TypedActionResult use(World world, PlayerEntity player, Hand h return TypedActionResult.pass(stack); } + @Override + public ActionResult useOnBlock(ItemUsageContext context) { + // only on client side, to confirm that sync works + if (context.getWorld().isClient && context.getPlayer() != null) { + Vita vita = CardinalComponentsTest.VITA_API_LOOKUP.find( + context.getWorld(), + context.getBlockPos(), + context.getSide() + ); + if (vita != null) { + context.getPlayer().sendMessage(new TranslatableText("componenttest:action.block_vitality", + vita.getVitality()), true); + } + } + return ActionResult.SUCCESS; + } + @Override public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity holder) { // The entity may not have the component, but the stack always does. diff --git a/src/testmod/resources/assets/componenttest/lang/en_us.json b/src/testmod/resources/assets/componenttest/lang/en_us.json index 1dd687a6..e41fb56c 100644 --- a/src/testmod/resources/assets/componenttest/lang/en_us.json +++ b/src/testmod/resources/assets/componenttest/lang/en_us.json @@ -4,5 +4,6 @@ "componenttest:tooltip.self_vitality": "Your vitality: %d", "componenttest:title.world_vitality": "Current vitality: %d + %d", "componenttest:action.chunk_vitality": "Chunk vitality: %d", + "componenttest:action.block_vitality": "Block vitality: %d", "item.componenttest.vita_stick": "Vita Stick" } diff --git a/src/testmod/resources/fabric.mod.json b/src/testmod/resources/fabric.mod.json index 34794957..27333355 100644 --- a/src/testmod/resources/fabric.mod.json +++ b/src/testmod/resources/fabric.mod.json @@ -21,7 +21,8 @@ }, "custom": { "cardinal-components": [ - "componenttest:vita" + "componenttest:vita", + "componenttest:vita_compound" ] }, "depends": {