Skip to content

Commit

Permalink
Add C2S messaging component to test mod
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Apr 19, 2024
1 parent ad6be29 commit 161e611
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2023 OnyxStudios
* Copyright (C) 2019-2024 Ladysnake
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2023 OnyxStudios
* Copyright (C) 2019-2024 Ladysnake
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.Entity;
Expand All @@ -47,6 +48,7 @@
import org.ladysnake.cca.api.v3.entity.TrackingStartCallback;
import org.ladysnake.cca.internal.base.ComponentUpdatePayload;
import org.ladysnake.cca.internal.base.ComponentsInternals;
import org.ladysnake.cca.internal.base.MorePacketCodecs;

import java.util.HashSet;
import java.util.Optional;
Expand All @@ -61,7 +63,7 @@ public final class CardinalComponentsEntity {
*/
public static final CustomPayload.Id<ComponentUpdatePayload<Integer>> PACKET_ID = CustomPayload.id("cardinal-components:entity_sync");
/**
* {@link CustomPayloadC2SPacket} channel for C2S player component messages.
* {@link net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket} channel for C2S player component messages.
*
* <p> Packets emitted on this channel must begin with the {@link ComponentKey#getId() component's type} (as an Identifier).
*
Expand All @@ -74,6 +76,7 @@ public final class CardinalComponentsEntity {
public static void init() {
if (FabricLoader.getInstance().isModLoaded("fabric-networking-api-v1")) {
ComponentUpdatePayload.register(PACKET_ID, PacketCodecs.VAR_INT);
PayloadTypeRegistry.playC2S().register(C2S_SELF_PACKET_ID, ComponentUpdatePayload.codec(C2S_SELF_PACKET_ID, MorePacketCodecs.EMPTY));
PlayerSyncCallback.EVENT.register(player -> syncEntityComponents(player, player));
TrackingStartCallback.EVENT.register(CardinalComponentsEntity::syncEntityComponents);
ServerPlayNetworking.registerGlobalReceiver(CardinalComponentsEntity.C2S_SELF_PACKET_ID, (payload, ctx) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2024 Ladysnake
*
* 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 org.ladysnake.cca.test.entity;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.entity.mob.ShulkerEntity;
import org.ladysnake.cca.api.v3.component.sync.C2SComponentPacketWriter;
import org.ladysnake.cca.test.base.Vita;

public class CcaEntityTestClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (client.targetedEntity instanceof ShulkerEntity) {
((PlayerVita) Vita.get(client.player)).sendC2SMessage(C2SComponentPacketWriter.EMPTY);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;
import org.ladysnake.cca.api.v3.component.sync.AutoSyncedComponent;
import org.ladysnake.cca.api.v3.component.sync.PlayerSyncPredicate;
import org.ladysnake.cca.api.v3.component.tick.ServerTickingComponent;
import org.ladysnake.cca.api.v3.entity.C2SSelfMessagingComponent;
import org.ladysnake.cca.api.v3.entity.RespawnableComponent;
import org.ladysnake.cca.test.base.BaseVita;
import org.ladysnake.cca.test.base.CardinalGameTest;
Expand All @@ -40,7 +42,7 @@
/**
* A Vita component attached to players, and automatically synchronized with their owner
*/
public class PlayerVita extends EntityVita implements AutoSyncedComponent, ServerTickingComponent, RespawnableComponent<BaseVita> {
public class PlayerVita extends EntityVita implements AutoSyncedComponent, ServerTickingComponent, RespawnableComponent<BaseVita>, C2SSelfMessagingComponent {
public static final int INCREASE_VITA = 0b10;
public static final int DECREASE_VITA = 0b100;

Expand Down Expand Up @@ -108,4 +110,9 @@ public void copyForRespawn(@NotNull BaseVita original, boolean lossless, boolean
this.vitality -= 5;
}
}

@Override
public void handleC2SMessage(RegistryByteBuf buf) {
((PlayerEntity) this.owner).sendMessage(Text.of("Sync!"), true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2024 Ladysnake
*
* 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 org.ladysnake.cca.test.entity;

import org.ladysnake.cca.api.v3.util.MethodsReturnNonnullByDefault;

import javax.annotation.ParametersAreNonnullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"main": [
"org.ladysnake.cca.test.entity.CcaEntityTestMod"
],
"client": [
"org.ladysnake.cca.test.entity.CcaEntityTestClient"
],
"cardinal-components": [
"org.ladysnake.cca.test.entity.CcaEntityTestMod"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
* @deprecated level components are a legacy feature
*/
Expand Down

0 comments on commit 161e611

Please sign in to comment.