Skip to content

Commit

Permalink
Update to 1.18-pre1
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Nov 15, 2021
1 parent 585360f commit 5e0d2ae
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 65 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ repositories {
}
dependencies {
// Adds a dependency on the base cardinal components module (required by every other module)
// Replace modImplementation with modApi if you expose components in your own API
modImplementation "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-base:<VERSION>"
// Adds a dependency on a specific module
modImplementation "io.github.onyxstudios.Cardinal-Components-API:<MODULE>:<VERSION>"
// Includes Cardinal Components API as a Jar-in-Jar dependency (optional)
include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-base:<VERSION>"
include "io.github.onyxstudios.Cardinal-Components-API:<MODULE>:<VERSION>"
}
```
Expand All @@ -59,7 +63,10 @@ modImplementation "io.github.onyxstudios.Cardinal-Components-API:cardinal-compon

## Basic Usage

To get started, you only need a class implementing `Component`. It is recommended to have it split into an interface and an implementation, so that internals get properly encapsulated and so that the component itself can be used as an API by other mods.
To get started, you only need a class implementing `Component`.
It is recommended to have it split into an interface and an implementation,
so that internals get properly encapsulated and so that the component itself can be used
as an API by other mods.

**Minimal code example:**
```java
Expand All @@ -76,7 +83,9 @@ class RandomIntComponent implements IntComponent {
```
*Note: a component class can be reused for several component types*

If you want your component to be **automatically synchronized with watching clients**, you can also add the [`AutoSyncedComponent`](https://github.com/OnyxStudios/Cardinal-Components-API/blob/master/cardinal-components-base/src/main/java/dev/onyxstudios/cca/api/v3/component/AutoSyncedComponent.java) interface to your implementation:
If you want your component to be **automatically synchronized with watching clients**,
you can also add the [`AutoSyncedComponent`](https://github.com/OnyxStudios/Cardinal-Components-API/blob/master/cardinal-components-base/src/main/java/dev/onyxstudios/cca/api/v3/component/AutoSyncedComponent.java)
interface to your implementation:

```java
class SyncedIntComponent implements IntComponent, AutoSyncedComponent {
Expand Down Expand Up @@ -125,7 +134,11 @@ The next step is to choose an identifier for your component, and to declare it i
```

Components can be provided by objects of various classes, depending on which modules you installed.
The most common providers are [entities](https://github.com/OnyxStudios/Cardinal-Components-API/wiki/Cardinal-Components-Entity), [item stacks](https://github.com/OnyxStudios/Cardinal-Components-API/wiki/Cardinal-Components-Item), [worlds](https://github.com/OnyxStudios/Cardinal-Components-API/wiki/Cardinal-Components-World) and [chunks](https://github.com/OnyxStudios/Cardinal-Components-API/wiki/Cardinal-Components-Chunk), but more are available.
The most common providers are [entities](https://github.com/OnyxStudios/Cardinal-Components-API/wiki/Cardinal-Components-Entity),
[item stacks](https://github.com/OnyxStudios/Cardinal-Components-API/wiki/Cardinal-Components-Item),
[worlds](https://github.com/OnyxStudios/Cardinal-Components-API/wiki/Cardinal-Components-World)
and [chunks](https://github.com/OnyxStudios/Cardinal-Components-API/wiki/Cardinal-Components-Chunk),
but more are available.
To interact with them, you need to **register a component key**, using `ComponentRegistryV3#getOrCreate`;
the resulting `ComponentKey` instance has the query methods you need. You will also need to **attach your
component** to some providers (here, to players and worlds):
Expand Down Expand Up @@ -153,10 +166,7 @@ Do not forget to declare your component initializer as an entrypoint in your `fa
```json
{
"entrypoints": {
"cardinal-components-entity": [
"a.b.c.MyComponents"
],
"cardinal-components-world": [
"cardinal-components": [
"a.b.c.MyComponents"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,23 @@ public final Class<C> getComponentClass() {
*/
@Contract(pure = true)
public <V> @Nullable C getNullable(V provider) {
return this.getInternal((ComponentProvider) provider);
return this.getInternal(((ComponentProvider) provider).getComponentContainer());
}

/**
* @param provider a component provider
* @param <V> the class of the component provider
* @return the nonnull value of the held component of this type
* @throws NullPointerException if {@code provider} is null
* @throws NoSuchElementException if the provider does not provide this type of component
* @throws ClassCastException if <code>provider</code> does not implement {@link ComponentProvider}
* @see #maybeGet(Object)
*/
public final <V> C get(V provider) {
C component = this.getInternal((ComponentProvider) provider);
C component = this.getInternal(((ComponentProvider) provider).getComponentContainer());

assert component == null || this.getComponentClass().isInstance(component);

if (component == null) {
try {
throw new NoSuchElementException(provider + " provides no component of type " + this.getId());
Expand All @@ -101,8 +104,8 @@ public final <V> C get(V provider) {
* @see #get(Object)
*/
public final <V> Optional<C> maybeGet(@Nullable V provider) {
if (provider instanceof ComponentProvider) {
return Optional.ofNullable(this.getInternal((ComponentProvider) provider));
if (provider instanceof ComponentProvider p) {
return Optional.ofNullable(this.getInternal(p.getComponentContainer()));
}
return Optional.empty();
}
Expand Down Expand Up @@ -185,6 +188,17 @@ public void syncWith(ServerPlayerEntity player, ComponentProvider provider) {
}
}

@ApiStatus.Experimental
public void syncWith(ServerPlayerEntity player, ComponentProvider provider, ComponentPacketWriter writer, PlayerSyncPredicate predicate) {
if (predicate.shouldSyncWith(player)) {
Packet<?> packet = provider.toComponentPacket(this, writer, player);

if (packet != null) {
player.networkHandler.sendPacket(packet);
}
}
}

@Override
public final String toString() {
return this.getClass().getSimpleName() + "[\"" + this.id + "\"]";
Expand Down Expand Up @@ -220,24 +234,8 @@ protected ComponentKey(Identifier id, Class<C> componentClass) {
@ApiStatus.Internal
public abstract @Nullable C getInternal(ComponentContainer container);

private @Nullable C getInternal(ComponentProvider provider) {
return this.getInternal(provider.getComponentContainer());
}

@ApiStatus.Internal
public C getFromContainer(ComponentContainer container) {
return Objects.requireNonNull(this.getInternal(container));
}


@ApiStatus.Internal
public void syncWith(ServerPlayerEntity player, ComponentProvider provider, ComponentPacketWriter writer, PlayerSyncPredicate predicate) {
if (predicate.shouldSyncWith(player)) {
Packet<?> packet = provider.toComponentPacket(this, writer, player);

if (packet != null) {
player.networkHandler.sendPacket(packet);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,6 @@ protected StaticComponentPluginBase(String likelyInitTrigger, Class<T> providerC
this.containerFactoryBuilder = ComponentContainer.Factory.builder(providerClass);
}

/**
* Defines an implementation of {@code I} which creates component containers of
* the given implementation type, using an argument of the given {@code factoryArg} type.
*
* <p>The generated class has a single constructor, taking {@code eventCount} parameters of type {@link Event}.
* @param implNameSuffix a unique suffix for the generated class
* @param containerFactoryType the factory interface that is to be implemented by the returned class
* @param containerImpl the type of containers that is to be instantiated by the generated factory
*/
public static <I> Class<? extends I> spinContainerFactory(String implNameSuffix, Class<? super I> containerFactoryType, Class<? extends ComponentContainer> containerImpl, Class<?>... actualFactoryParams) throws IOException {
return spinContainerFactory(implNameSuffix, containerFactoryType, containerImpl, List.of(actualFactoryParams));
}

/**
* Defines an implementation of {@code I} which creates component containers of
* the given implementation type, using an argument of the given {@code factoryArg} type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package dev.onyxstudios.cca.mixin.block.common;

import dev.onyxstudios.cca.api.v3.block.BlockEntitySyncCallback;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.CommandBlockBlockEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -32,8 +32,8 @@

@Mixin(ServerPlayerEntity.class)
public abstract class MixinServerPlayerEntity {
@Inject(method = "sendBlockEntityUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/entity/BlockEntity;toUpdatePacket()Lnet/minecraft/network/Packet;"))
private void syncBlockEntity(BlockEntity blockEntity, CallbackInfo ci) {
BlockEntitySyncCallback.EVENT.invoker().onBlockEntitySync((ServerPlayerEntity)(Object) this, blockEntity);
@Inject(method = "openCommandBlockScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/BlockEntityUpdateS2CPacket;create(Lnet/minecraft/block/entity/BlockEntity;Ljava/util/function/Function;)Lnet/minecraft/network/packet/s2c/play/BlockEntityUpdateS2CPacket;"))
private void syncBlockEntity(CommandBlockBlockEntity commandBlock, CallbackInfo ci) {
BlockEntitySyncCallback.EVENT.invoker().onBlockEntitySync((ServerPlayerEntity)(Object) this, commandBlock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@
import dev.onyxstudios.cca.api.v3.component.ComponentContainer;
import dev.onyxstudios.cca.api.v3.component.ComponentProvider;
import dev.onyxstudios.cca.internal.chunk.StaticChunkComponentPlugin;
import net.minecraft.block.Block;
import net.minecraft.fluid.Fluid;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.HeightLimitView;
import net.minecraft.world.TickScheduler;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ChunkSection;
import net.minecraft.world.chunk.UpgradeData;
import net.minecraft.world.gen.chunk.Blender;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -47,7 +45,7 @@ public class MixinChunk implements ComponentProvider {
private ComponentContainer components;

@Inject(method = "<init>", at = @At("RETURN"))
private void initComponents(ChunkPos $$0, UpgradeData $$1, HeightLimitView $$2, Registry<Biome> $$3, long $$4, ChunkSection[] $$5, TickScheduler<Block> $$6, TickScheduler<Fluid> $$7, CallbackInfo ci) {
private void initComponents(ChunkPos pos, UpgradeData upgradeData, HeightLimitView heightLimitView, Registry<Biome> biome, long inhabitedTime, ChunkSection[] sectionArrayInitializer, Blender blendingData, CallbackInfo ci) {
this.components = StaticChunkComponentPlugin.createContainer((Chunk) (Object) this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,43 @@
*/
package dev.onyxstudios.cca.mixin.chunk.common;

import dev.onyxstudios.cca.api.v3.component.ComponentContainer;
import dev.onyxstudios.cca.api.v3.component.ComponentKey;
import dev.onyxstudios.cca.api.v3.component.ComponentProvider;
import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import dev.onyxstudios.cca.api.v3.component.sync.ComponentPacketWriter;
import dev.onyxstudios.cca.internal.chunk.ComponentsChunkNetworking;
import dev.onyxstudios.cca.internal.chunk.StaticChunkComponentPlugin;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.minecraft.block.Block;
import net.minecraft.fluid.Fluid;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.HeightLimitView;
import net.minecraft.world.TickScheduler;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ChunkSection;
import net.minecraft.world.chunk.ProtoChunk;
import net.minecraft.world.chunk.UpgradeData;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.gen.chunk.Blender;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Iterator;
import java.util.function.Consumer;

@Mixin(WorldChunk.class)
public abstract class MixinWorldChunk extends Chunk implements ComponentProvider {
public MixinWorldChunk(ChunkPos $$0, UpgradeData $$1, HeightLimitView $$2, Registry<Biome> $$3, long $$4, @Nullable ChunkSection[] $$5, TickScheduler<Block> $$6, TickScheduler<Fluid> $$7) {
super($$0, $$1, $$2, $$3, $$4, $$5, $$6, $$7);
public MixinWorldChunk(ChunkPos pos, UpgradeData upgradeData, HeightLimitView heightLimitView, Registry<Biome> biome, long inhabitedTime, @Nullable ChunkSection[] sectionArrayInitializer, @Nullable Blender blendingData) {
super(pos, upgradeData, heightLimitView, biome, inhabitedTime, sectionArrayInitializer, blendingData);
}

@Shadow
Expand Down
5 changes: 2 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
------------------------------------------------------
Version 4.0.0
------------------------------------------------------
### Alpha 1
Updated to 21w37a
Updated to 1.18

**Removed**

- Removed serializable item components
- Item components must now save all their data in the stack NBT
- Consider switching to API Lookup API where possible
- Removed the `cardinal-components-util` module
- According to Github no one ever used it, but if you did and you see no currently available alternative, please open an issue
- According to Github no one ever used it, but if you did, and you see no currently available alternative, please open an issue

------------------------------------------------------
Version 3.1.1
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
org.gradle.jvmargs = -Xmx3G

#see https://modmuss50.me/fabric.html
minecraft_version=21w37a
yarn_mappings=13
loader_version=0.11.7
minecraft_version=1.18-pre1
yarn_mappings=8
loader_version=0.12.5

#Fabric api
fabric_api_version=0.40.3+1.18
fabric_api_version=0.42.2+1.18

#Publishing
mod_version = 4.0.0-alpha.1+21w37a
mod_version = 4.0.0
curseforge_id = 318449
curseforge_versions = 1.18-Snapshot
changelog_url = https://github.com/OnyxStudios/Cardinal-Components-API/blob
Expand Down

0 comments on commit 5e0d2ae

Please sign in to comment.