Skip to content

Commit

Permalink
Fix BE components ticking only on subclasses, closes #127
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Jul 3, 2022
1 parent dd222b3 commit f9e722e
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package dev.onyxstudios.componenttest.content.vita;
package dev.onyxstudios.cca.test.base;

import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import dev.onyxstudios.cca.test.base.BaseVita;
import dev.onyxstudios.cca.test.base.Vita;

public class SyncedVita extends BaseVita implements AutoSyncedComponent {
private final Object owner;
Expand Down
1 change: 1 addition & 0 deletions cardinal-components-block/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ dependencies {
// (which we add to various classes through interface injection)
annotationProcessor api(project(path: ":cardinal-components-base", configuration: "namedElements"))
modApi fabricApi.module("fabric-api-lookup-api-v1", rootProject.fabric_api_version)
testmodImplementation project(":cardinal-components-base").sourceSets.testmod.output
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

public final class StaticBlockComponentPlugin extends LazyDispatcher implements BlockComponentFactoryRegistry {
Expand Down Expand Up @@ -98,8 +104,6 @@ public ComponentContainer.Factory<BlockEntity> buildDedicatedFactory(Class<? ext
type = type.getSuperclass().asSubclass(BlockEntity.class);
for (var e : this.beComponentFactories.getOrDefault(type, Collections.emptyMap()).entrySet()) {
compiled.putIfAbsent(e.getKey(), e.getValue());
if (ClientTickingComponent.class.isAssignableFrom(e.getValue().impl())) this.clientTicking.add(entityClass);
if (ServerTickingComponent.class.isAssignableFrom(e.getValue().impl())) this.serverTicking.add(entityClass);
}
}

Expand All @@ -108,6 +112,8 @@ public ComponentContainer.Factory<BlockEntity> buildDedicatedFactory(Class<? ext

for (var entry : compiled.entrySet()) {
addToBuilder(builder, entry);
if (ClientTickingComponent.class.isAssignableFrom(entry.getValue().impl())) this.clientTicking.add(entityClass);
if (ServerTickingComponent.class.isAssignableFrom(entry.getValue().impl())) this.serverTicking.add(entityClass);
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2022 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.cca.test.block;

import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import dev.onyxstudios.cca.api.v3.component.tick.ServerTickingComponent;
import dev.onyxstudios.cca.test.base.BaseVita;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.world.World;

public class BlockVita extends BaseVita implements AutoSyncedComponent, ServerTickingComponent {
private final BlockEntity owner;

public BlockVita(BlockEntity owner) {
this.owner = owner;
}

@Override
public void setVitality(int value) {
super.setVitality(value);
this.owner.syncComponent(KEY);
}

@Override
public void serverTick() {
World world = this.owner.getWorld();
if (world != null && world.getTime() % 3 == 0) {
this.setVitality(this.getVitality() + 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2022 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.cca.test.block;

import dev.onyxstudios.cca.api.v3.block.BlockComponentFactoryRegistry;
import dev.onyxstudios.cca.api.v3.block.BlockComponentInitializer;
import dev.onyxstudios.cca.test.base.TickingTestComponent;
import net.fabricmc.api.ModInitializer;
import net.minecraft.block.entity.EndGatewayBlockEntity;
import net.minecraft.block.entity.EndPortalBlockEntity;

public class CcaBlockTestMod implements ModInitializer, BlockComponentInitializer {
public static final String MOD_ID = "cca-block-test";

@Override
public void registerBlockComponentFactories(BlockComponentFactoryRegistry registry) {
registry.registerFor(EndGatewayBlockEntity.class, VitaCompound.KEY, VitaCompound::new);
registry.registerFor(EndPortalBlockEntity.class, TickingTestComponent.KEY, be -> new TickingTestComponent());
}

@Override
public void onInitialize() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2022 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.cca.test.block;

import dev.onyxstudios.cca.test.base.TickingTestComponent;
import dev.onyxstudios.cca.test.base.Vita;
import io.github.ladysnake.elmendorf.GameTestUtil;
import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.test.GameTest;
import net.minecraft.test.TestContext;
import net.minecraft.util.math.BlockPos;

import java.util.Objects;

public class CcaBlockTestSuite implements FabricGameTest {
@GameTest(templateName = EMPTY_STRUCTURE)
public void beSerialize(TestContext ctx) {
BlockEntity be = Objects.requireNonNull(
BlockEntityType.END_GATEWAY.instantiate(
ctx.getAbsolutePos(BlockPos.ORIGIN),
Blocks.END_GATEWAY.getDefaultState()
)
);
be.getComponent(Vita.KEY).setVitality(42);
NbtCompound nbt = be.createNbt();
BlockEntity be1 = Objects.requireNonNull(
BlockEntityType.END_GATEWAY.instantiate(
ctx.getAbsolutePos(BlockPos.ORIGIN), Blocks.END_GATEWAY.getDefaultState()
)
);
GameTestUtil.assertTrue("New BlockEntity should have values zeroed", be1.getComponent(Vita.KEY).getVitality() == 0);
be1.readNbt(nbt);
GameTestUtil.assertTrue("BlockEntity component data should survive deserialization", be1.getComponent(Vita.KEY).getVitality() == 42);
ctx.complete();
}

@GameTest(templateName = EMPTY_STRUCTURE)
public void beComponentsTick(TestContext ctx) {
ctx.setBlockState(BlockPos.ORIGIN, Blocks.END_PORTAL);
ctx.waitAndRun(5, () -> {
int ticks = Objects.requireNonNull(ctx.getBlockEntity(BlockPos.ORIGIN)).getComponent(TickingTestComponent.KEY).serverTicks();
GameTestUtil.assertTrue("Component should tick 5 times", ticks == 5);
ctx.complete();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,27 @@
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package dev.onyxstudios.componenttest.content;
package dev.onyxstudios.cca.test.block;

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.content.vita.SyncedVita;
import dev.onyxstudios.cca.test.base.SyncedVita;
import dev.onyxstudios.cca.test.base.Vita;
import net.fabricmc.fabric.api.util.NbtType;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
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<VitaCompound> KEY = ComponentRegistryV3.INSTANCE.getOrCreate(CardinalComponentsTest.id("vita_compound"), VitaCompound.class);
public static final ComponentKey<VitaCompound> KEY = ComponentRegistryV3.INSTANCE.getOrCreate(new Identifier(CcaBlockTestMod.MOD_ID, "vita_compound"), VitaCompound.class);

private final Map<Direction, SyncedVita> storage = new EnumMap<>(Direction.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2022 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.
*/
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
package dev.onyxstudios.cca.test.block;

import dev.onyxstudios.cca.api.v3.util.MethodsReturnNonnullByDefault;

import javax.annotation.ParametersAreNonnullByDefault;
31 changes: 31 additions & 0 deletions cardinal-components-block/src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"schemaVersion": 1,
"environment": "*",
"id": "cca-block-test",
"name": "Cardinal Components API Test Mod",
"description": "Test mod for Cardinal Components API",
"version": "${version}",
"entrypoints": {
"main": [
"dev.onyxstudios.cca.test.block.CcaBlockTestMod"
],
"cardinal-components": [
"dev.onyxstudios.cca.test.block.CcaBlockTestMod"
],
"fabric-gametest": [
"dev.onyxstudios.cca.test.block.CcaBlockTestSuite"
]
},
"depends": {
"fabric-api-base": "*"
},
"authors": [
"Pyrofab"
],
"license": "MIT",
"custom": {
"cardinal-components": [
"cca-block-test:vita_compound"
]
}
}
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
------------------------------------------------------
Version 5.0.1
------------------------------------------------------
**Fixes**
- Fixed components ticking only on subclasses of the block entities they were declared for
(e.g. if a ticking component got attached to ChestBlockEntity, it would only tick on TrappedChestBlockEntity)

------------------------------------------------------
Version 5.0.0
------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fabric_api_version=0.52.4+1.19
elmendorf_version=0.6.0

#Publishing
mod_version = 5.0.0
mod_version = 5.0.1
curseforge_id = 318449
modrinth_id = K01OU20C
curseforge_versions = 1.19
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import dev.onyxstudios.cca.internal.base.GenericContainerBuilder;
import dev.onyxstudios.cca.test.base.BaseVita;
import dev.onyxstudios.cca.test.base.Vita;
import dev.onyxstudios.cca.test.block.VitaCompound;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@
import dev.onyxstudios.cca.api.v3.scoreboard.ScoreboardComponentFactoryRegistry;
import dev.onyxstudios.cca.api.v3.scoreboard.ScoreboardComponentInitializer;
import dev.onyxstudios.cca.test.base.BaseVita;
import dev.onyxstudios.cca.test.base.SyncedVita;
import dev.onyxstudios.cca.test.base.Vita;
import dev.onyxstudios.componenttest.content.vita.ItemVita;
import dev.onyxstudios.componenttest.content.vita.SyncedVita;
import dev.onyxstudios.componenttest.content.vita.TeamVita;
import net.minecraft.block.entity.EndGatewayBlockEntity;
import net.minecraft.block.entity.EndPortalBlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.HostileEntity;
Expand Down Expand Up @@ -70,7 +69,6 @@ public void registerEntityComponentFactories(EntityComponentFactoryRegistry regi

@Override
public void registerBlockComponentFactories(BlockComponentFactoryRegistry registry) {
registry.registerFor(EndGatewayBlockEntity.class, VitaCompound.KEY, VitaCompound::new);
registry.beginRegistration(EndPortalBlockEntity.class, ALT_VITA).after(Vita.KEY).impl(SyncedVita.class).end(SyncedVita::new);
registry.registerFor(EndPortalBlockEntity.class, Vita.KEY, SyncedVita::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package dev.onyxstudios.componenttest.content.vita;

import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import dev.onyxstudios.cca.test.base.SyncedVita;
import dev.onyxstudios.cca.test.base.Vita;
import net.minecraft.scoreboard.Team;
import net.minecraft.server.network.ServerPlayerEntity;
Expand Down
1 change: 0 additions & 1 deletion src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"custom": {
"cardinal-components": [
"componenttest:vita",
"componenttest:vita_compound",
"testmod:test",
"testmod:test_2",
"testmod:test_3"
Expand Down

0 comments on commit f9e722e

Please sign in to comment.