#[SOLVED] Need help working with packages between server and client

19 messages · Page 1 of 1 (latest)

ocean furnace
#

I don't quite understand how to work with packages. I have buttons in the interface and I don't understand how to correctly link them to an action on the server.

The system itself works (should work) in such a way that when you click on a button, a spell is used.

package freedman.saomod.spell;

import freedman.saomod.component.ModComponents;
import freedman.saomod.component.PlayerDataComponent;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class SpellCasting {

    private static final Identifier CUSTOM_FONT = Identifier.of("saomod", "sao_font");

    public static void castSpell(PlayerEntity player, int slot) {
        PlayerDataComponent data = ModComponents.PLAYER_DATA.get(player);
        String spellId = data.getSpellSlot(slot);

        if (spellId == null || spellId.isEmpty()) {
            player.sendMessage(Text.literal("Нет заклинания в этой ячейке.").setStyle(Style.EMPTY.withFont(CUSTOM_FONT)), true);
            return;
        }

        Spell spell = SpellRegistry.get(spellId);
        if (spell == null) {
            player.sendMessage(Text.literal("Неизвестное заклинание: " + spellId).setStyle(Style.EMPTY.withFont(CUSTOM_FONT)), true);
            return;
        }

        if (!data.knowsSpell(spellId)) {
            player.sendMessage(Text.literal("Вы не знаете это заклинание!").setStyle(Style.EMPTY.withFont(CUSTOM_FONT)), true);
            return;
        }

        if (data.getCurrentMana() >= spell.getManaCost()) {
            data.setCurrentMana(data.getCurrentMana() - spell.getManaCost());
            spell.cast(player);
        } else {
            player.sendMessage(Text.literal("Недостаточно маны!").setStyle(Style.EMPTY.withFont(CUSTOM_FONT)), true);
        }
    }
}

#
package freedman.saomod.spell;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.FireballEntity;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

public class FireballSpell implements Spell {

    @Override
    public String getId() {
        return "saomod:fireball";
    }

    @Override
    public int getManaCost() {
        return 20;
    }

    @Override
    public String getName() {
        return "Огненный шар";
    }

    @Override
    public String getIcon () {
        return " ";
    }

    @Override
    public void cast(PlayerEntity caster) {
        World world = caster.getWorld();
        if (!world.isClient) {
            Vec3d look = caster.getRotationVec(1.0F); // направление взгляда игрока

            FireballEntity fireball = new FireballEntity(
                    world,
                    caster,
                    look, // направление
                    1     // сила взрыва
            );

            fireball.setPosition(caster.getX(), caster.getEyeY(), caster.getZ());
            world.spawnEntity(fireball);
        }
    }
}

#
package freedman.saomod.spell;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class SpellRegistry {
    private static final Map<String, Spell> SPELLS = new HashMap<>();

    public static void register(Spell spell) {
        SPELLS.put(spell.getId(), spell);
    }

    public static Spell get(String id) {
        return SPELLS.get(id);
    }

    public static String getName(String id) {
        Spell spell = SPELLS.get(id);
        return spell.getName();
    }

    public static Collection<Spell> getAll() {
        return SPELLS.values();
    }
}
#
package freedman.saomod.keybinds;

import freedman.saomod.spell.SpellCasting;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;

public class ModKeyBindings {

    public static KeyBinding SPELL_SLOT_1;
    public static KeyBinding SPELL_SLOT_2;
    public static KeyBinding SPELL_SLOT_3;
    public static KeyBinding SPELL_SLOT_4;
    public static KeyBinding SPELL_SLOT_5;

    public static void register() {
        SPELL_SLOT_1 = KeyBindingHelper.registerKeyBinding(new KeyBinding(
                "key.saomod.spell1",
                InputUtil.Type.KEYSYM,
                GLFW.GLFW_KEY_Z,
                "category.saomod.spells"
        ));

        SPELL_SLOT_2 = KeyBindingHelper.registerKeyBinding(new KeyBinding(
                "key.saomod.spell2",
                InputUtil.Type.KEYSYM,
                GLFW.GLFW_KEY_X,
                "category.saomod.spells"
        ));

        SPELL_SLOT_3 = KeyBindingHelper.registerKeyBinding(new KeyBinding(
                "key.saomod.spell3",
                InputUtil.Type.KEYSYM,
                GLFW.GLFW_KEY_C,
                "category.saomod.spells"
        ));

        SPELL_SLOT_4 = KeyBindingHelper.registerKeyBinding(new KeyBinding(
                "key.saomod.spell4",
                InputUtil.Type.KEYSYM,
                GLFW.GLFW_KEY_V,
                "category.saomod.spells"
        ));

        SPELL_SLOT_5 = KeyBindingHelper.registerKeyBinding(new KeyBinding(
                "key.saomod.spell5",
                InputUtil.Type.KEYSYM,
                GLFW.GLFW_KEY_B,
                "category.saomod.spells"
        ));

    }
}
#
import freedman.saomod.keybinds.ModKeyBindings;
import freedman.saomod.network.ModPackets;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.network.PacketByteBuf;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;


public class KeyBindHandler {

    public static void register() {
        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            if (client.player == null) return;

            if (ModKeyBindings.SPELL_SLOT_1.wasPressed()) sendCastPacket(0);
            if (ModKeyBindings.SPELL_SLOT_2.wasPressed()) sendCastPacket(1);
            if (ModKeyBindings.SPELL_SLOT_3.wasPressed()) sendCastPacket(2);
            if (ModKeyBindings.SPELL_SLOT_4.wasPressed()) sendCastPacket(3);
            if (ModKeyBindings.SPELL_SLOT_5.wasPressed()) sendCastPacket(4);
        });
    }

    private static void sendCastPacket(int slot) {
        PacketByteBuf buf = PacketByteBufs.create();
        buf.writeInt(slot); // отправляем номер слота
        ClientPlayNetworking.send(ModPackets.CAST_SPELL, buf);
    }
}

#
package freedman.saomod.network;

import net.minecraft.util.Identifier;

public class ModPackets {
    public static final Identifier CAST_SPELL = Identifier.of("saomod", "cast_spell");
}
#
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.server.network.ServerPlayerEntity;
import freedman.saomod.spell.SpellCasting;
import freedman.saomod.network.ModPackets;

public class ServerPacketHandler {

    public static void register() {
        ServerPlayNetworking.registerGlobalReceiver(ModPackets.CAST_SPELL, (server, player, handler, buf, responseSender) -> {
            int slot = buf.readInt();

            server.execute(() -> {
                SpellCasting.castSpell(player, slot);
            });
        });
    }
}


#

I tried using a neural network, but it writes complete nonsense.

#

I try this

package freedman.saomod.keybinds;

import freedman.saomod.keybinds.ModKeyBindings;
import freedman.saomod.spell.SpellCasting;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;

public class KeyBindHandler {

    public static void register() {
        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            if (client.player == null) return;

            if (ModKeyBindings.SPELL_SLOT_1.wasPressed()) SpellCasting.castSpell(client.player, 0);
            if (ModKeyBindings.SPELL_SLOT_2.wasPressed()) SpellCasting.castSpell(client.player, 0);
            if (ModKeyBindings.SPELL_SLOT_3.wasPressed()) SpellCasting.castSpell(client.player, 0);
            if (ModKeyBindings.SPELL_SLOT_4.wasPressed()) SpellCasting.castSpell(client.player, 0);
            if (ModKeyBindings.SPELL_SLOT_5.wasPressed()) SpellCasting.castSpell(client.player, 0);
        });
    }

}


left valley
#

you mean a packet?

ocean furnace
#

yea

#
ServerPlayNetworking.registerGlobalReceiver(KeyBindHandler.CastSpellSlotPayload.ID, (payload, context) -> {
            int slot = payload.slotId();

            ServerPlayNetworking.registerGlobalReceiver(KeyBindHandler.CastSpellPlayerPayload.ID, (payload2, context2) -> {
                PlayerEntity entity = context2.player().getWorld().getPlayerByUuid(payload2.entityId());


                SpellCasting.castSpell(entity, slot);

            });

        });

#

I copy from Fabric doc

#

Idk can it worked

ocean furnace
#

I found the LibNetworkStack library on Wikipedia, but I'm not entirely sure if it's right for me.

ocean furnace
#

I think I figured it out, according to the official documentation.