Skip to content

Commit

Permalink
adding LinkItem keybind and handling its packet
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyagara committed Oct 1, 2024
1 parent 9cb1db6 commit 5871f5d
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 10 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ Focusing only on server side for now.
- Send events like advancements, join/leave, death, from the server to Discord.
- Sync advancements completion, all players share the same advancement progress.
- Discord commands to retrieve information about the server.
- Link items in the chat.
- Link items in the chat, via command or using `Left Shift + Alt` hovering over an item.

## TODO

- Enable/disable modules, allow option to disable the relay but not the syncing.
- Maybe instead of appending to the save file, maybe rewrite it.
- Maybe use a small database library for storage as it might be useful for other ideas.
- Build system needs some work, shadowing is probably not done right, add sources to the artifacts.
- Prevent toast from showing for synced players, at the moment the player that was synced will get a toast notification after completing the already completed advancement.
Expand Down
39 changes: 39 additions & 0 deletions common/src/main/java/com/cooptweaks/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.cooptweaks;

import com.cooptweaks.keybinds.misc.Link;
import dev.architectury.event.EventResult;
import dev.architectury.event.events.client.ClientScreenInputEvent;
import dev.architectury.platform.Platform;
import dev.architectury.registry.client.keymappings.KeyMappingRegistry;
import net.fabricmc.api.EnvType;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;

public class Client {
public static final KeyBinding LINK_ITEM_KEY = new KeyBinding(
"key.cooptweaks.link_item",
InputUtil.Type.KEYSYM,
InputUtil.GLFW_KEY_LEFT_ALT,
"category.cooptweaks.misc"
);

public static void init() {
if (Platform.getEnv() != EnvType.CLIENT) {
return;
}

KeyMappingRegistry.register(LINK_ITEM_KEY);

ClientScreenInputEvent.KEY_RELEASED_POST.register((client, screen, key, scanCode, modifiers) -> {
if (screen instanceof HandledScreen<?>) {
if (LINK_ITEM_KEY.matchesKey(key, scanCode) && Screen.hasShiftDown()) {
Link.sendPacket(client);
}
}

return EventResult.pass();
});
}
}
9 changes: 7 additions & 2 deletions common/src/main/java/com/cooptweaks/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.cooptweaks;

import com.cooptweaks.commands.advancements.Progress;
import com.cooptweaks.commands.misc.Link;
import com.cooptweaks.commands.misc.LinkCommand;
import com.cooptweaks.keybinds.misc.Link;
import com.cooptweaks.packets.LinkPacket;
import dev.architectury.event.EventResult;
import dev.architectury.event.events.common.*;
import dev.architectury.networking.NetworkManager;
import net.minecraft.util.math.BlockPos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -87,7 +90,9 @@ public static void init() {
new Progress().register(dispatcher, registryAccess, environment);

// Misc
new Link().register(dispatcher, registryAccess, environment);
new LinkCommand().register(dispatcher, registryAccess, environment);
});

NetworkManager.registerReceiver(NetworkManager.Side.C2S, LinkPacket.PAYLOAD_ID, LinkPacket.CODEC, Link::handlePacket);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.cooptweaks.commands.misc;

import com.cooptweaks.commands.ServerCommand;
import com.cooptweaks.keybinds.misc.Link;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.item.ItemStack;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;

public class Link implements ServerCommand {
public class LinkCommand implements ServerCommand {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
dispatcher
Expand All @@ -34,10 +34,7 @@ public int execute(CommandContext<ServerCommandSource> context) {
return 1;
}

MutableText text = Text.empty();
text.append(player.getDisplayName());
text.append(Text.literal(" linked "));
text.append(item.toHoverableText());
Text text = Link.getHoverableText(item.toHoverableText(), player.getDisplayName());

source.getServer().getPlayerManager().broadcast(text, false);
return 0;
Expand Down
52 changes: 52 additions & 0 deletions common/src/main/java/com/cooptweaks/keybinds/misc/Link.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.cooptweaks.keybinds.misc;

import com.cooptweaks.mixins.client.accessor.HandledScreenAccessor;
import com.cooptweaks.packets.LinkPacket;
import dev.architectury.networking.NetworkManager;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;

public class Link {
public static void sendPacket(MinecraftClient client) {
if (client.player == null) {
return;
}

if (client.currentScreen == null) {
return;
}

Slot slot = ((HandledScreenAccessor) client.currentScreen).getFocusedSlot();

if (slot != null && slot.hasStack()) {
NetworkManager.sendToServer(new LinkPacket(slot.getStack()));
}
}

public static void handlePacket(LinkPacket link, NetworkManager.PacketContext context) {
context.queue(() -> {
PlayerEntity player = context.getPlayer();
MinecraftServer server = player.getServer();
if (server == null) {
return;
}

ItemStack stack = link.value();

server.getPlayerManager().broadcast(getHoverableText(stack.toHoverableText(), player.getDisplayName()), false);
});
}

public static Text getHoverableText(Text stack, Text player) {
MutableText text = Text.empty();
text.append(player);
text.append(Text.literal(" linked "));
text.append(stack);
return text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.cooptweaks.mixins.client.accessor;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.screen.slot.Slot;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Environment(EnvType.CLIENT)
@Mixin(HandledScreen.class)
public interface HandledScreenAccessor {
@Accessor("focusedSlot")
Slot getFocusedSlot();
}
19 changes: 19 additions & 0 deletions common/src/main/java/com/cooptweaks/packets/LinkPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.cooptweaks.packets;

import com.cooptweaks.Main;
import net.minecraft.item.ItemStack;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;

public record LinkPacket(ItemStack value) implements CustomPayload {
private static final Identifier ID = Identifier.of(Main.MOD_ID, "link_item_packet");
public static CustomPayload.Id<LinkPacket> PAYLOAD_ID = new Id<>(ID);
public static final PacketCodec<RegistryByteBuf, LinkPacket> CODEC = ItemStack.PACKET_CODEC.xmap(LinkPacket::new, LinkPacket::value).cast();

@Override
public Id<LinkPacket> getId() {
return PAYLOAD_ID;
}
}
4 changes: 4 additions & 0 deletions common/src/main/resources/assets/cooptweaks/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"category.cooptweaks.misc": "Miscellaneous",
"key.cooptweaks.link_item": "Link Item"
}
1 change: 1 addition & 0 deletions common/src/main/resources/cooptweaks.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"compatibilityLevel": "JAVA_21",
"minVersion": "0.8",
"client": [
"client.accessor.HandledScreenAccessor"
],
"mixins": [
"PlayerAdvancementTrackerMixin"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.cooptweaks.fabric.client;

import com.cooptweaks.Client;
import net.fabricmc.api.ClientModInitializer;

public final class FabricClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
Client.init();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.cooptweaks.neoforge;

import com.cooptweaks.Client;
import com.cooptweaks.Main;
import net.neoforged.fml.common.Mod;

@Mod(Main.MOD_ID)
public final class NeoForgeMain {
public NeoForgeMain() {
Client.init();
Main.init();
}
}

0 comments on commit 5871f5d

Please sign in to comment.