-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ComponentProvider on WorldProperties, fixes #116
- Loading branch information
Showing
18 changed files
with
425 additions
and
29 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...-components-base/src/testmod/java/dev/onyxstudios/cca/test/base/TickingTestComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ents-level/src/main/java/dev/onyxstudios/cca/mixin/level/common/MixinWorldProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...nal-components-level/src/testmod/java/dev/onyxstudios/cca/test/level/CcaLevelTestMod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...l-components-level/src/testmod/java/dev/onyxstudios/cca/test/level/CcaLevelTestSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
cardinal-components-level/src/testmod/java/dev/onyxstudios/cca/test/level/LevelVita.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
cardinal-components-level/src/testmod/resources/fabric.mod.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
cardinal-components-world/src/testmod/java/dev/onyxstudios/cca/test/world/AmbientVita.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.