Skip to content

Commit

Permalink
Added syncId to packet
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpgr committed Feb 4, 2024
1 parent e7e39a3 commit d0a668b
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 37 deletions.
Binary file removed assets/ct2_wip.jpg
Binary file not shown.
Binary file removed gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 0 additions & 7 deletions gradle/wrapper/gradle-wrapper.properties

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.johnpgr.craftingtableiifabric

import net.fabricmc.api.ModInitializer
import net.johnpgr.craftingtableiifabric.api.network.ModMessages
import net.johnpgr.craftingtableiifabric.network.ModMessages
import net.johnpgr.craftingtableiifabric.blocks.ModBlocks
import net.johnpgr.craftingtableiifabric.utils.CreativeTab
import net.minecraft.util.Identifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.johnpgr.craftingtableiifabric

import net.fabricmc.api.ClientModInitializer
import net.johnpgr.craftingtableiifabric.api.network.ModMessages
import net.johnpgr.craftingtableiifabric.network.ModMessages
import net.johnpgr.craftingtableiifabric.blocks.ModBlocks

object CraftingTableIIFabricClient : ClientModInitializer {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package net.johnpgr.craftingtableiifabric.blocks.craftingtableii

import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs
import net.johnpgr.craftingtableiifabric.api.network.ModMessages
import net.johnpgr.craftingtableiifabric.api.network.packet.CraftingPacket
import net.johnpgr.craftingtableiifabric.network.ModMessages
import net.johnpgr.craftingtableiifabric.network.packet.CraftingPacket
import net.johnpgr.craftingtableiifabric.blocks.ModBlocks
import net.johnpgr.craftingtableiifabric.utils.RecipeManager
import net.johnpgr.craftingtableiifabric.utils.PlayerRecipeManager
import net.minecraft.client.network.ClientPlayerEntity
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.inventory.CraftingInventory
import net.minecraft.inventory.CraftingResultInventory
import net.minecraft.inventory.Inventory
import net.minecraft.inventory.RecipeInputInventory
import net.minecraft.item.ItemStack
import net.minecraft.network.packet.c2s.play.CraftRequestC2SPacket
import net.minecraft.recipe.Recipe
import net.minecraft.recipe.RecipeMatcher
import net.minecraft.recipe.book.RecipeBookCategory
Expand All @@ -24,22 +23,22 @@ import net.minecraft.screen.slot.SlotActionType

class CraftingTableIIScreenHandler(
syncId: Int,
private val player: PlayerEntity,
val player: PlayerEntity,
val entity: CraftingTableIIBlockEntity,
) : AbstractRecipeScreenHandler<RecipeInputInventory>(
ModBlocks.getContainerInfo(ModBlocks.CRAFTING_TABLE_II)?.handlerType,
syncId,
) {
private val result = CraftingResultInventory()
val input = CraftingInventory(this, 3, 3)
val result = CraftingResultInventory()

companion object {
const val INVENTORY_COLS = 8
const val INVENTORY_ROWS = 5
const val INVENTORY_SIZE = INVENTORY_ROWS * INVENTORY_COLS
}

private var recipeHandler: RecipeManager? = null
private var playerRecipeManager: PlayerRecipeManager? = null

private val inventory =
object : Inventory {
Expand Down Expand Up @@ -151,8 +150,8 @@ class CraftingTableIIScreenHandler(
if (player.world.isClient) {
val playerInventory = player.inventory
val recipeBook = (player as ClientPlayerEntity).recipeBook
this.recipeHandler =
RecipeManager(playerInventory, recipeBook, this)
this.playerRecipeManager =
PlayerRecipeManager(playerInventory, recipeBook, this)
this.updateRecipes()
}
}
Expand All @@ -171,10 +170,10 @@ class CraftingTableIIScreenHandler(
if (slot.inventory == this.inventory) {
val item = this.inventory.getStack(slotIndex - 10)
val quickCraft = actionType == SlotActionType.QUICK_MOVE
val recipe = this.recipeHandler?.getRecipe(item) ?: return
val recipe = this.playerRecipeManager?.getRecipe(item) ?: return
val buf = PacketByteBufs.create() ?: return

CraftingPacket(recipe.id, quickCraft).write(buf)
CraftingPacket(recipe.id, this.syncId, quickCraft).write(buf)

ClientPlayNetworking.send(
ModMessages.CTII_CRAFT_RECIPE,
Expand All @@ -186,7 +185,7 @@ class CraftingTableIIScreenHandler(

fun updateRecipes() {
this.inventory.clear()
this.recipeHandler?.getCraftableItemStacks()?.forEach { stack ->
this.playerRecipeManager?.getCraftableItemStacks()?.forEach { stack ->
addRecipeItem(stack)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.johnpgr.craftingtableiifabric.api.network
package net.johnpgr.craftingtableiifabric.network

import net.minecraft.util.Identifier

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.johnpgr.craftingtableiifabric.network

enum class MessageType {
C2S,
S2C
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.johnpgr.craftingtableiifabric.api.network
package net.johnpgr.craftingtableiifabric.network

import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
import net.johnpgr.craftingtableiifabric.CraftingTableIIFabric
import net.johnpgr.craftingtableiifabric.api.network.packet.CraftingTableIIHandlerC2S
import net.johnpgr.craftingtableiifabric.network.packet.CraftingPacketC2SHandler
import net.minecraft.util.Identifier

object ModMessages {
Expand Down Expand Up @@ -59,6 +59,6 @@ object ModMessages {
val CTII_CRAFT_RECIPE = register(
CraftingTableIIFabric.id("ctii_craft_recipe_packet"),
MessageType.C2S,
CraftingTableIIHandlerC2S
CraftingPacketC2SHandler
)
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package net.johnpgr.craftingtableiifabric.api.network.packet
package net.johnpgr.craftingtableiifabric.network.packet

import net.minecraft.network.PacketByteBuf
import net.minecraft.util.Identifier

class CraftingPacket(
val recipe: Identifier,
val syncId: Int,
val quickCraft: Boolean
) {
companion object {
fun read(
buf: PacketByteBuf,
): CraftingPacket {
val recipe = buf.readIdentifier()
val syncId = buf.readInt()
val quickCraft = buf.readBoolean()

return CraftingPacket(recipe, quickCraft)
return CraftingPacket(recipe,syncId, quickCraft)
}
}

fun write(buf: PacketByteBuf) {
buf.writeIdentifier(this.recipe)
buf.writeInt(this.syncId)
buf.writeBoolean(this.quickCraft)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:Suppress("UNCHECKED_CAST")

package net.johnpgr.craftingtableiifabric.api.network.packet
package net.johnpgr.craftingtableiifabric.network.packet

import net.fabricmc.fabric.api.networking.v1.PacketSender
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
Expand All @@ -13,7 +13,7 @@ import net.minecraft.server.MinecraftServer
import net.minecraft.server.network.ServerPlayNetworkHandler
import net.minecraft.server.network.ServerPlayerEntity

object CraftingTableIIHandlerC2S : ServerPlayNetworking.PlayChannelHandler {
object CraftingPacketC2SHandler : ServerPlayNetworking.PlayChannelHandler {
override fun receive(
server: MinecraftServer,
player: ServerPlayerEntity,
Expand All @@ -23,9 +23,12 @@ object CraftingTableIIHandlerC2S : ServerPlayNetworking.PlayChannelHandler {
) {
val packet = CraftingPacket.read(buf)

if (player.currentScreenHandler is CraftingTableIIScreenHandler) {
if (player.currentScreenHandler.syncId == packet.syncId
&& player.currentScreenHandler is CraftingTableIIScreenHandler
) {
val craftingScreenHandler =
player.currentScreenHandler as CraftingTableIIScreenHandler

server.recipeManager.get(packet.recipe)
.ifPresent { recipe ->
craftingScreenHandler.fillInputSlots(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import net.minecraft.item.ItemStack
import net.minecraft.recipe.Recipe
import net.minecraft.recipe.RecipeMatcher

class RecipeManager(
class PlayerRecipeManager(
private val playerInventory: PlayerInventory,
private val playerRecipeBook: ClientRecipeBook,
private val craftingScreenHandler: CraftingTableIIScreenHandler
Expand Down
Binary file not shown.

0 comments on commit d0a668b

Please sign in to comment.