Skip to content

Instantly share code, notes, and snippets.

@fantalatone
Last active December 6, 2022 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fantalatone/65116264037b83955a99c8c014d328ef to your computer and use it in GitHub Desktop.
Save fantalatone/65116264037b83955a99c8c014d328ef to your computer and use it in GitHub Desktop.
/* 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