-
-
Save fantalatone/65116264037b83955a99c8c014d328ef to your computer and use it in GitHub Desktop.
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
/* Entity Class | |
@Override | |
protected InteractionResult mobInteract(Player player, InteractionHand hand) { | |
if (!player.level.isClientSide() && hand.equals(InteractionHand.MAIN_HAND)) { | |
ServerPlayer serverPlayer = (ServerPlayer) player; | |
this.getCapability(NPCTradesProvider.NPC_TRADES).ifPresent(data -> { | |
Networking.sendToPlayer(new OpenScreen(this.getId(), data.getTrades()), serverPlayer); | |
}); | |
} | |
return super.mobInteract(player, hand); | |
} | |
*/ | |
package com.fantalatone.npcmod.core.network.packet; | |
public class OpenScreen { | |
public final int baseID; | |
public final Map<ItemStack, ItemStack> baseTrades; | |
public OpenScreen(int id, Map<ItemStack, ItemStack> trades) { | |
System.out.println(trades); | |
this.baseID = id; | |
this.baseTrades = trades; | |
} | |
public OpenScreen(FriendlyByteBuf buf) { | |
this(buf.readInt(), buf.readMap(FriendlyByteBuf::readItem, FriendlyByteBuf::readItem)); | |
} | |
public void toBytes(FriendlyByteBuf buf) { | |
buf.writeInt(this.baseID); | |
buf.writeMap(this.baseTrades, FriendlyByteBuf::writeItem, (a, b) -> { | |
System.out.println("A " + a); | |
System.out.println("B " + b); | |
}); | |
} | |
public boolean handle(Supplier<NetworkEvent.Context> supplier) { | |
NetworkEvent.Context context = supplier.get(); | |
context.enqueueWork(() -> { | |
Entity entity = Minecraft.getInstance().level.getEntity(baseID); | |
System.out.println("Hi From Packet!"); | |
if (entity instanceof NPCEntity npc) { | |
Minecraft.getInstance().setScreen(new NPCScreen(npc, (HashMap<ItemStack, ItemStack>) this.baseTrades)); | |
} | |
}); | |
context.setPacketHandled(true); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment