Skip to content

Commit

Permalink
feat: anvil input
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Oct 30, 2024
1 parent fb553f5 commit bb49924
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.network.ClientConnection;
import net.minecraft.network.NetworkThreadUtils;
import net.minecraft.network.packet.c2s.play.CloseHandledScreenC2SPacket;
import net.minecraft.network.packet.c2s.play.RenameItemC2SPacket;
import net.minecraft.network.packet.s2c.play.OpenScreenS2CPacket;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
Expand All @@ -17,6 +18,9 @@
import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerPlayNetworkHandler.class)
public abstract class ServerPlayNetworkHandlerMixin extends ServerCommonNetworkHandler {
Expand Down Expand Up @@ -48,4 +52,13 @@ private void handleScreenClose(CloseHandledScreenC2SPacket packet, Operation<Voi

original.call(packet);
}

@Inject(method = "onRenameItem", at = @At("TAIL"))
private void handleAnvilInput(RenameItemC2SPacket packet, CallbackInfo ci) {
if (this.player.currentScreenHandler instanceof SlateScreenHandler handler) {
String input = packet.getName();
handler.onAnvilInput(input);
handler.syncState();
}
}
}
7 changes: 7 additions & 0 deletions src/main/kotlin/net/mcbrawls/slate/Slate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ open class Slate {
}
}

/**
* Called when the client input changes.
*/
open fun onAnvilInput(player: ServerPlayerEntity, input: String) {
callbackHandler.collectInputCallbacks().onInput(this, player, input)
}

/**
* Opens a slate for the given player.
* @return whether the slate was opened successfully
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import net.mcbrawls.slate.Slate
*/
open class SlateCallbackHandler {
val callbacks: MutableList<SlateCallback> = mutableListOf()
val inputCallbacks: MutableList<SlateInputCallback> = mutableListOf()

/**
* Adds a callback invoked when the slate is opened.
Expand All @@ -29,6 +30,13 @@ open class SlateCallbackHandler {
callbacks.add(callback)
}

/**
* Adds a callback invoked when the slate input changes, namely for anvil screen handler types.
*/
fun onInput(callback: SlateInputCallback) {
inputCallbacks.add(callback)
}

/**
* Combines all callbacks for the given type into one callable object.
*/
Expand All @@ -39,4 +47,13 @@ open class SlateCallbackHandler {
.forEach { callback -> callback.invoke(slate, player) }
}
}

/**
* Combines all callbacks for the given type into one callable object.
*/
fun collectInputCallbacks(): SlateInputCallback {
return SlateInputCallback { slate, player, input ->
inputCallbacks.forEach { callback -> callback.onInput(slate, player, input) }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.mcbrawls.slate.callback

import net.mcbrawls.slate.Slate
import net.minecraft.server.network.ServerPlayerEntity

fun interface SlateInputCallback {
fun onInput(slate: Slate, player: ServerPlayerEntity, input: String)
}
34 changes: 20 additions & 14 deletions src/main/kotlin/net/mcbrawls/slate/screen/SlateScreenHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ class SlateScreenHandler(
}
}

override fun syncState() {
super.syncState()

// manually update offhand
clearOffhandSlotClient()
fun onAnvilInput(input: String) {
if (player is ServerPlayerEntity) {
slate.onAnvilInput(player, input)
}
}

override fun sendContentUpdates() {
Expand All @@ -66,10 +65,6 @@ class SlateScreenHandler(
}
}

override fun quickMove(player: PlayerEntity, slot: Int): ItemStack {
return ItemStack.EMPTY
}

override fun internalOnSlotClick(slotIndex: Int, button: Int, actionType: SlotActionType, player: PlayerEntity) {
val isOffhandSwap = actionType == SlotActionType.SWAP && button == PlayerInventory.OFF_HAND_SLOT
if (isOffhandSwap) {
Expand All @@ -85,12 +80,11 @@ class SlateScreenHandler(
}
}

override fun canInsertIntoSlot(slot: Slot): Boolean {
return false
}
override fun syncState() {
super.syncState()

override fun canUse(player: PlayerEntity): Boolean {
return true
// manually update offhand
clearOffhandSlotClient()
}

/**
Expand All @@ -110,4 +104,16 @@ class SlateScreenHandler(
val inventoryPacket = InventoryS2CPacket(playerScreenHandler.syncId, playerScreenHandler.nextRevision(), playerScreenHandler.stacks, playerScreenHandler.cursorStack)
player.networkHandler.sendPacket(inventoryPacket)
}

override fun quickMove(player: PlayerEntity, slot: Int): ItemStack {
return ItemStack.EMPTY
}

override fun canInsertIntoSlot(slot: Slot): Boolean {
return false
}

override fun canUse(player: PlayerEntity): Boolean {
return true
}
}
6 changes: 6 additions & 0 deletions src/test/kotlin/net/mcbrawls/slate/test/SlateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ class SlateTest : ModInitializer {
// bounds tests
tiles.setInventory(0, 0, tile(ItemStack(Items.ORANGE_WOOL)))
tiles[tiles.lastIndex] = tile(ItemStack(Items.RED_WOOL))

callbacks {
onInput { _, _, input ->
println(input)
}
}
}

subslate.open(context.player)
Expand Down

0 comments on commit bb49924

Please sign in to comment.