-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding LinkItem keybind and handling its packet
- Loading branch information
Showing
11 changed files
with
145 additions
and
10 deletions.
There are no files selected for viewing
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
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(); | ||
}); | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
common/src/main/java/com/cooptweaks/keybinds/misc/Link.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,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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
common/src/main/java/com/cooptweaks/mixins/client/accessor/HandledScreenAccessor.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,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
19
common/src/main/java/com/cooptweaks/packets/LinkPacket.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,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; | ||
} | ||
} |
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,4 @@ | ||
{ | ||
"category.cooptweaks.misc": "Miscellaneous", | ||
"key.cooptweaks.link_item": "Link Item" | ||
} |
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
2 changes: 2 additions & 0 deletions
2
fabric/src/main/java/com/cooptweaks/fabric/client/FabricClient.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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
neoforge/src/main/java/com/cooptweaks/neoforge/NeoForgeMain.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 |
---|---|---|
@@ -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(); | ||
} | ||
} |