Skip to content

Commit

Permalink
Implement ComponentProvider on WorldProperties, fixes #116
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Feb 6, 2022
1 parent 08100e2 commit 98b6e17
Show file tree
Hide file tree
Showing 18 changed files with 425 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.base;

import dev.onyxstudios.cca.api.v3.component.ComponentKey;
import dev.onyxstudios.cca.api.v3.component.ComponentRegistry;
import dev.onyxstudios.cca.api.v3.component.tick.ClientTickingComponent;
import dev.onyxstudios.cca.api.v3.component.tick.ServerTickingComponent;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;

public class TickingTestComponent implements ServerTickingComponent, ClientTickingComponent {
public static final ComponentKey<TickingTestComponent> KEY = ComponentRegistry.getOrCreate(new Identifier("cca-base-test", "ticking"), TickingTestComponent.class);

private int clientTicks;
private int serverTicks;

@Override
public void readFromNbt(NbtCompound tag) {
this.clientTicks = tag.getInt("clientTicks");
this.serverTicks = tag.getInt("serverTicks");
}

@Override
public void writeToNbt(NbtCompound tag) {
tag.putInt("clientTicks", this.clientTicks);
tag.putInt("serverTicks", this.serverTicks);
}

@Override
public void clientTick() {
this.clientTicks++;
}

@Override
public void serverTick() {
this.serverTicks++;
}

public int clientTicks() {
return clientTicks;
}

public int serverTicks() {
return serverTicks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import dev.onyxstudios.cca.api.v3.component.ComponentV3;
import net.minecraft.util.Identifier;

public interface Vita extends ComponentV3 {
public interface
Vita extends ComponentV3 {
ComponentKey<Vita> KEY = ComponentRegistryV3.INSTANCE.getOrCreate(new Identifier("cca-base-test", "vita"), Vita.class);

static <T> Vita get(T provider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
},
"custom": {
"cardinal-components": [
"cca-base-test:vita"
"cca-base-test:vita",
"cca-base-test:ticking"
]
},
"depends": {
Expand Down
2 changes: 2 additions & 0 deletions cardinal-components-level/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ dependencies {
// (which we add to various classes through interface injection)
annotationProcessor api(project(path: ":cardinal-components-base", configuration: "namedElements"))
api project(path: ":cardinal-components-world", configuration: "namedElements")
testmodImplementation project(":cardinal-components-base").sourceSets.testmod.output
testmodImplementation project(":cardinal-components-world").sourceSets.testmod.output
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.mixin.level.common;

import dev.onyxstudios.cca.api.v3.component.ComponentProvider;
import net.minecraft.world.WorldProperties;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(WorldProperties.class)
public interface MixinWorldProperties extends ComponentProvider {

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"mixins": [
"common.MixinLevelProperties",
"common.MixinMinecraftServer",
"common.MixinUnmodifiableLevelProperties"
"common.MixinUnmodifiableLevelProperties",
"common.MixinWorldProperties"
],
"injectors": {
"defaultRequire": 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.level;

import dev.onyxstudios.cca.api.v3.level.LevelComponentFactoryRegistry;
import dev.onyxstudios.cca.api.v3.level.LevelComponentInitializer;
import dev.onyxstudios.cca.test.base.TickingTestComponent;
import dev.onyxstudios.cca.test.base.Vita;

public class CcaLevelTestMod implements LevelComponentInitializer {
@Override
public void registerLevelComponentFactories(LevelComponentFactoryRegistry registry) {
registry.register(TickingTestComponent.KEY, props -> new TickingTestComponent());
registry.register(Vita.KEY, props -> new LevelVita());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.level;

import dev.onyxstudios.cca.test.base.TickingTestComponent;
import io.github.ladysnake.elmendorf.GameTestUtil;
import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;
import net.minecraft.test.GameTest;
import net.minecraft.test.TestContext;

public class CcaLevelTestSuite implements FabricGameTest {
@GameTest(structureName = EMPTY_STRUCTURE)
public void levelComponentsTick(TestContext ctx) {
int baseTicks = ctx.getWorld().getLevelProperties().getComponent(TickingTestComponent.KEY).serverTicks();
ctx.waitAndRun(5, () -> {
int ticks = ctx.getWorld().getLevelProperties().getComponent(TickingTestComponent.KEY).serverTicks();
GameTestUtil.assertTrue("Component should tick 5 times", ticks - baseTicks == 5);
ctx.complete();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.level;

import dev.onyxstudios.cca.api.v3.level.LevelComponents;
import dev.onyxstudios.cca.test.base.Vita;
import dev.onyxstudios.cca.test.world.AmbientVita;
import net.minecraft.server.MinecraftServer;

public class LevelVita extends AmbientVita {
@Override
public void syncWithAll(MinecraftServer server) {
LevelComponents.sync(Vita.KEY, server);
}
}
23 changes: 23 additions & 0 deletions cardinal-components-level/src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"schemaVersion": 1,
"environment": "*",
"id": "cca-level-test",
"name": "Cardinal Components API Test Mod",
"description": "Test mod for Cardinal Components API",
"version": "${version}",
"entrypoints": {
"cardinal-components": [
"dev.onyxstudios.cca.test.level.CcaLevelTestMod"
],
"fabric-gametest": [
"dev.onyxstudios.cca.test.level.CcaLevelTestSuite"
]
},
"depends": {
"fabric-api-base": "*"
},
"authors": [
"Pyrofab"
],
"license": "MIT"
}
1 change: 1 addition & 0 deletions cardinal-components-world/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ dependencies {
// Need to add the base module to the annotation classpath too, otherwise the mixin obf AP chokes on ComponentProvider
// (which we add to various classes through interface injection)
annotationProcessor api(project(path: ":cardinal-components-base", configuration: "namedElements"))
testmodImplementation project(':cardinal-components-base').sourceSets.testmod.output
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.world;

import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import dev.onyxstudios.cca.api.v3.component.tick.ClientTickingComponent;
import dev.onyxstudios.cca.test.base.BaseVita;
import dev.onyxstudios.cca.test.base.CardinalGameTest;
import dev.onyxstudios.cca.test.base.Vita;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.world.World;

import java.util.Objects;

public abstract class AmbientVita extends BaseVita implements AutoSyncedComponent {

public abstract void syncWithAll(MinecraftServer server);

@Override
public void applySyncPacket(PacketByteBuf buf) {
int vita = buf.readInt();
this.setVitality(vita);
World world = Objects.requireNonNull(MinecraftClient.getInstance().player).world;
// Very bad shortcut to get a dimension's name
Text worldName = new LiteralText(
Objects.requireNonNull(world.getRegistryKey() == World.OVERWORLD ? "Overworld" : "Alien World")
);
Text worldVita = new TranslatableText(
"componenttest:title.world_vitality",
Vita.get(world).getVitality(),
Vita.get(world.getLevelProperties()).getVitality()
);
InGameHud inGameHud = MinecraftClient.getInstance().inGameHud;
inGameHud.setTitleTicks(-1, -1, -1);
inGameHud.setTitle(worldName);
inGameHud.setSubtitle(worldVita);
}

/**
* proper implementation of {@code writeToPacket}, writes a single int instead of a whole tag
*/
@Override
public void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity player) {
buf.writeInt(this.getVitality());
}

public static class WorldVita extends AmbientVita implements ClientTickingComponent {
private final World world;

public WorldVita(World world) {
this.world = world;
}

@Override
public void syncWithAll(MinecraftServer server) {
Vita.KEY.sync(this.world);
}

@Override
public void clientTick() {
if (this.world.getTime() % 2400 == 0) {
CardinalGameTest.LOGGER.info("The world still runs, and is now worth {}", this.vitality);
}
}
}

}
Loading

0 comments on commit 98b6e17

Please sign in to comment.