Skip to content

Commit

Permalink
Yes packets ready.
Browse files Browse the repository at this point in the history
  • Loading branch information
Waterpicker committed Oct 16, 2023
1 parent 97ebb68 commit 499f80a
Show file tree
Hide file tree
Showing 25 changed files with 99 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void registerClientBound() {
this.createClientBound(DialogueGraphRegistrySyncPacket.ID, DialogueGraphRegistrySyncPacket.class, DialogueGraphRegistrySyncPacket::decode, DataRegistrySyncPacketHandler::new);
this.createClientBound(ShopRegistrySyncPacket.ID, ShopRegistrySyncPacket.class, ShopRegistrySyncPacket::decode, DataRegistrySyncPacketHandler::new);
this.createClientBound(ShopPresetRegistrySyncPacket.ID, ShopPresetRegistrySyncPacket.class, ShopPresetRegistrySyncPacket::decode, DataRegistrySyncPacketHandler::new);
this.createClientBound(S2COpenShopPacket.ID, S2COpenShopPacket.class, S2COpenShopPacket::new, S2COpenShopHandler::new);
this.createClientBound(S2COpenShopPacket.ID, S2COpenShopPacket.class, S2COpenShopPacket::decode, S2COpenShopHandler::new);
this.createClientBound(S2CSyncPlayerMoneyPacket.ID, S2CSyncPlayerMoneyPacket.class, S2CSyncPlayerMoneyPacket::new, S2CSyncPlayerMoneyHandler::new);
this.createClientBound(S2COpenNpcCustomizationScreenPacket.ID, S2COpenNpcCustomizationScreenPacket.class, S2COpenNpcCustomizationScreenPacket::new, S2COpenNpcCustomizationScreenHandler::new);
this.createClientBound(S2CUpdateNpcDisplayDataPacket.ID, S2CUpdateNpcDisplayDataPacket.class, S2CUpdateNpcDisplayDataPacket::new, S2CUpdateNpcDisplayDataHandler::new);
Expand All @@ -68,7 +68,7 @@ public void registerServerBound() {
this.createServerBound(C2SSaveDatapackEntryPacket.ID, C2SSaveDatapackEntryPacket.class, C2SSaveDatapackEntryPacket::decode, new C2SSaveDatapackEntryHandler());
this.createServerBound(C2SUpdateStatueInfoPacket.ID, C2SUpdateStatueInfoPacket.class, C2SUpdateStatueInfoPacket::decode, new C2SUpdateStatueInfoHandler());
this.createServerBound(C2SCloseShopPacket.ID, C2SCloseShopPacket.class, C2SCloseShopPacket::new, new C2SCloseShopHandler());
this.createServerBound(C2SShopItemPacket.ID, C2SShopItemPacket.class, C2SShopItemPacket::new, new C2SShopItemHandler());
this.createServerBound(C2SShopItemPacket.ID, C2SShopItemPacket.class, C2SShopItemPacket::decode, new C2SShopItemHandler());
this.createServerBound(C2SUpdateNpcDialoguePacket.ID, C2SUpdateNpcDialoguePacket.class, C2SUpdateNpcDialoguePacket::new, new C2SUpdateNpcDialogueHandler());
this.createServerBound(C2SUpdateNpcDisplayDataPacket.ID, C2SUpdateNpcDisplayDataPacket.class, C2SUpdateNpcDisplayDataPacket::new, new C2SUpdateNpcDisplayDataHandler());
this.createServerBound(C2SUpdateNpcShopPacket.ID, C2SUpdateNpcShopPacket.class, C2SUpdateNpcShopPacket::new, new C2SUpdateNpcShopHandler());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,54 @@
package generations.gg.generations.core.generationscore.network.packets;

import generations.gg.generations.core.generationscore.network.ServerNetworkPacketHandler;
import generations.gg.generations.core.generationscore.tags.GenerationsItemTags;
import generations.gg.generations.core.generationscore.world.item.MailItem;
import net.minecraft.nbt.StringTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Inventory;

import java.util.Optional;

public class C2SEditMailHandler implements ServerNetworkPacketHandler<C2SEditMailPacket> {
public void handle(C2SEditMailPacket packet, MinecraftServer server, ServerPlayer player) {
server.execute(() -> C2SEditMailPacket.handleEditMail(
server.execute(() -> handleEditMail(
player,
packet.slot,
packet.contents,
packet.title
packet.slot(),
packet.contents(),
packet.title()
));
}

public static void handleEditMail(ServerPlayer sender, int slot, String contents, Optional<String> title) {
if (Inventory.isHotbarSlot(slot) || slot == 40) {
title.ifPresentOrElse(s -> sealMail(sender, slot, contents, s), () -> updateMailContents(
sender,
slot,
contents
));
}
}

private static void updateMailContents(ServerPlayer sender, int slot, String contents) {
var itemStack = sender.getInventory().getItem(slot);
if (itemStack.is(GenerationsItemTags.POKEMAIL)) {
itemStack.addTagElement("contents", StringTag.valueOf(contents));
}
}

private static void sealMail(ServerPlayer sender, int slot, String contents, String title) {
var itemStack = sender.getInventory().getItem(slot);
if (itemStack.is(GenerationsItemTags.POKEMAIL)) {
var itemStack1 = MailItem.getSealed(itemStack.getItem());
var compoundTag = itemStack.getTag();
if (compoundTag != null) {
itemStack1.setTag(compoundTag);
}
itemStack1.addTagElement("author", StringTag.valueOf(sender.getName().getString()));
itemStack1.addTagElement("contents", StringTag.valueOf(contents));
sender.getInventory().setItem(slot, itemStack1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,10 @@ public void encode(FriendlyByteBuf buf) {
buf.writeUtf(contents, 8192);
buf.writeOptional(title, (byteBuf, s) -> buf.writeUtf(s, 128));
}
public static ResourceLocation ID = GenerationsCore.id("edit_mail");

public static C2SEditMailPacket decode(FriendlyByteBuf buffer) {
return new C2SEditMailPacket(buffer.readVarInt(), buffer.readUtf(8192), buffer.readOptional(arg -> arg.readUtf(128)));
}

public static void handleEditMail(ServerPlayer sender, int slot, String contents, Optional<String> title) {
if (Inventory.isHotbarSlot(slot) || slot == 40) {
title.ifPresentOrElse(s -> sealMail(sender, slot, contents, s), () -> updateMailContents(
sender,
slot,
contents
));
}
}

private static void updateMailContents(ServerPlayer sender, int slot, String contents) {
var itemStack = sender.getInventory().getItem(slot);
if (itemStack.is(GenerationsItemTags.POKEMAIL)) {
itemStack.addTagElement("contents", StringTag.valueOf(contents));
}
}

private static void sealMail(ServerPlayer sender, int slot, String contents, String title) {
var itemStack = sender.getInventory().getItem(slot);
if (itemStack.is(GenerationsItemTags.POKEMAIL)) {
var itemStack1 = MailItem.getSealed(itemStack.getItem());
var compoundTag = itemStack.getTag();
if (compoundTag != null) {
itemStack1.setTag(compoundTag);
}
itemStack1.addTagElement("author", StringTag.valueOf(sender.getName().getString()));
itemStack1.addTagElement("contents", StringTag.valueOf(contents));
sender.getInventory().setItem(slot, itemStack1);
}
}
public static ResourceLocation ID = GenerationsCore.id("edit_mail");
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class C2SToggleCookingPotHandler implements ServerNetworkPacketHandler<C2SToggleCookingPotPacket> {
@Override
public void handle(@NotNull C2SToggleCookingPotPacket packet, @NotNull MinecraftServer minecraftServer, @NotNull ServerPlayer player) {
if (player.level().getBlockEntity(packet.pos) instanceof CookingPotBlockEntity te)
if (player.level().getBlockEntity(packet.pos()) instanceof CookingPotBlockEntity te)
te.setCooking(!te.isCooking());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void handle(C2SRespondDialoguePacket packet, MinecraftServer server, Serv
var node = dialoguePlayer != null ? dialoguePlayer.currentNode : null;

if (dialoguePlayer != null && node instanceof ResponseTakingNode responseTakingNode) {
responseTakingNode.clientResponse(packet.stringResponse);
responseTakingNode.clientResponse(packet.stringResponse());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class C2SSaveDatapackEntryHandler implements ServerNetworkPacketHandler<C
@Override
public void handle(C2SSaveDatapackEntryPacket packet, MinecraftServer server, ServerPlayer player) {
if (player.hasPermissions(4)) { // Operators only can change configs. Too dangerous. TODO: look into forge's PermissionsAPI class?
var namespace = packet.location.getNamespace().equals("minecraft") ? "" : ("/" + packet.location.getNamespace());
DatapackSaver.savePokemodData(server, path -> path.resolve("generated" + namespace + "/" + packet.location.getPath()), packet.data);
var namespace = packet.location().getNamespace().equals("minecraft") ? "" : ("/" + packet.location().getNamespace());
DatapackSaver.savePokemodData(server, path -> path.resolve("generated" + namespace + "/" + packet.location().getPath()), packet.data());
} else {
GenerationsCore.LOGGER.warn("{} tried saving config data without permission. This player is probably cheating or this is a bug.", player);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class S2CChooseDialogueHandler implements ClientNetworkPacketHandler<S2CC
@Override
public void handle(S2CChooseDialoguePacket packet) {
if (Minecraft.getInstance().screen instanceof DialogueScreen dialogueScreen) {
dialogueScreen.activeInfo = new DialogueScreen.ChooseActiveInfo(packet.text, packet.options);
dialogueScreen.activeInfo = new DialogueScreen.ChooseActiveInfo(packet.text(), packet.options());
dialogueScreen.init(
Minecraft.getInstance(),
dialogueScreen.width,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void handle(S2COpenDialogueEditorScreenPacket packet) {
Minecraft.getInstance().setScreen(
new ConfigureDialogueScreen(
Dialogues.instance().getOrElse(
packet.location, new DialogueGraph(
packet.location(), new DialogueGraph(
new SayNode(
List.of("Hi @p!"), null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class S2COpenDialogueMenuHandler implements ClientNetworkPacketHandler<S2

@Override
public void handle(@NotNull S2COpenDialogueMenuPacket packet) {
Minecraft.getInstance().setScreen(new DialogueScreen(packet.closable));
Minecraft.getInstance().setScreen(new DialogueScreen(packet.closable()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class S2CSayDialogueHandler implements ClientNetworkPacketHandler<S2CSayD
@Override
public void handle(S2CSayDialoguePacket packet) {
if (Minecraft.getInstance().screen instanceof DialogueScreen dialogueScreen)
dialogueScreen.activeInfo = new DialogueScreen.SayActiveInfo(packet.text, packet.useNextArrow);
dialogueScreen.activeInfo = new DialogueScreen.SayActiveInfo(packet.text(), packet.useNextArrow());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
public class C2SSetNpcPresetHandler implements ServerNetworkPacketHandler<C2SSetNpcPresetPacket> {
@Override
public void handle(C2SSetNpcPresetPacket packet, MinecraftServer server, ServerPlayer player) {
PlayerNpcEntity npcEntity = (PlayerNpcEntity) player.level().getEntity(packet.entityId);
PlayerNpcEntity npcEntity = (PlayerNpcEntity) player.level().getEntity(packet.entityId());
if (npcEntity != null && player.hasPermissions(4)) {
npcEntity.loadPreset(packet.preset);
npcEntity.loadPreset(packet.preset());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
public class C2SUpdateNpcDialogueHandler implements ServerNetworkPacketHandler<C2SUpdateNpcDialoguePacket> {
@Override
public void handle(C2SUpdateNpcDialoguePacket packet, MinecraftServer server, ServerPlayer player) {
PlayerNpcEntity npcEntity = (PlayerNpcEntity) player.level().getEntity(packet.entityId);
PlayerNpcEntity npcEntity = (PlayerNpcEntity) player.level().getEntity(packet.entityId());
if (npcEntity != null) {
npcEntity.setDialogue(packet.dialogue);
npcEntity.setDialogue(packet.dialogue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,17 @@
import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.Nullable;

public class C2SUpdateNpcDialoguePacket implements GenerationsNetworkPacket<C2SUpdateNpcDialoguePacket> {
public record C2SUpdateNpcDialoguePacket(int entityId, @Nullable ResourceLocation dialogue) implements GenerationsNetworkPacket<C2SUpdateNpcDialoguePacket> {
public static final ResourceLocation ID = GenerationsCore.id("update_npc_dialogue");

private final int entityId;
@Nullable
private final ResourceLocation dialogue;

public C2SUpdateNpcDialoguePacket(int entityId, @Nullable ResourceLocation dialogue) {
this.entityId = entityId;
this.dialogue = dialogue;
}

public C2SUpdateNpcDialoguePacket(FriendlyByteBuf buf) {
this.entityId = buf.readInt();
this.dialogue = buf.readBoolean() ? null : buf.readResourceLocation();
this(buf.readInt(), buf.readNullable(FriendlyByteBuf::readResourceLocation));
}

@Override
public void encode(FriendlyByteBuf buf) {
buf.writeInt(this.entityId);
boolean isDialogueNull = this.dialogue == null;
if (!isDialogueNull)
buf.writeResourceLocation(this.dialogue);
buf.writeNullable(this.dialogue, FriendlyByteBuf::writeResourceLocation);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
public class C2SUpdateNpcDisplayDataHandler implements ServerNetworkPacketHandler<C2SUpdateNpcDisplayDataPacket> {
@Override
public void handle(C2SUpdateNpcDisplayDataPacket packet, MinecraftServer server, ServerPlayer player) {
PlayerNpcEntity npcEntity = (PlayerNpcEntity) player.level().getEntity(packet.entityId);
PlayerNpcEntity npcEntity = (PlayerNpcEntity) player.level().getEntity(packet.entityId());
if (npcEntity != null) {
npcEntity.setDisplayData(packet.npcDisplayData);
GenerationsNetwork.INSTANCE.sendToAllTracking(new S2CUpdateNpcDisplayDataPacket(packet.entityId), npcEntity);
npcEntity.setDisplayData(packet.npcDisplayData());
GenerationsNetwork.INSTANCE.sendToAllTracking(new S2CUpdateNpcDisplayDataPacket(packet.entityId()), npcEntity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,11 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;

public class C2SUpdateNpcDisplayDataPacket implements GenerationsNetworkPacket<C2SUpdateNpcDisplayDataPacket> {
public record C2SUpdateNpcDisplayDataPacket(int entityId, NpcDisplayData npcDisplayData) implements GenerationsNetworkPacket<C2SUpdateNpcDisplayDataPacket> {
public static final ResourceLocation ID = GenerationsCore.id("update_npc_display_data");

private final int entityId;
private final NpcDisplayData npcDisplayData;

public C2SUpdateNpcDisplayDataPacket(int entityId, NpcDisplayData npcDisplayData) {
this.entityId = entityId;
this.npcDisplayData = npcDisplayData;
}

public C2SUpdateNpcDisplayDataPacket(FriendlyByteBuf buf) {
this.entityId = buf.readInt();
this.npcDisplayData = new NpcDisplayData(buf);
this(buf.readInt(), new NpcDisplayData(buf));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
public class C2SUpdateNpcShopHandler implements ServerNetworkPacketHandler<C2SUpdateNpcShopPacket> {
@Override
public void handle(C2SUpdateNpcShopPacket packet, MinecraftServer server, ServerPlayer player) {
PlayerNpcEntity npcEntity = (PlayerNpcEntity) player.level().getEntity(packet.entityId);
PlayerNpcEntity npcEntity = (PlayerNpcEntity) player.level().getEntity(packet.entityId());

if (npcEntity != null) {
if (packet.shop == null) {
if (packet.shop() == null) {
npcEntity.setOffers(null);
} else {
npcEntity.loadOffers(packet.shop);
npcEntity.loadOffers(packet.shop());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,11 @@
import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.Nullable;

public class C2SUpdateNpcShopPacket implements GenerationsNetworkPacket<C2SUpdateNpcShopPacket> {
public record C2SUpdateNpcShopPacket(int entityId, @Nullable ResourceLocation shop) implements GenerationsNetworkPacket<C2SUpdateNpcShopPacket> {
public static final ResourceLocation ID = GenerationsCore.id("update_npc_shop");

private final int entityId;
@Nullable
private final ResourceLocation shop;

public C2SUpdateNpcShopPacket(int entityId, @Nullable ResourceLocation shop) {
this.entityId = entityId;
this.shop = shop;
}

public C2SUpdateNpcShopPacket(FriendlyByteBuf buf) {
this.entityId = buf.readInt();
this.shop = buf.readNullable(FriendlyByteBuf::readResourceLocation);
this(buf.readInt(), buf.readNullable(FriendlyByteBuf::readResourceLocation));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class S2COpenNpcCustomizationScreenHandler implements ClientNetworkPacket
@Override
public void handle(S2COpenNpcCustomizationScreenPacket packet) {
EnvExecutor.runInEnv(Env.CLIENT, () -> () -> {
var npcEntity = (PlayerNpcEntity) Minecraft.getInstance().level.getEntity(packet.entityId);
var npcEntity = (PlayerNpcEntity) Minecraft.getInstance().level.getEntity(packet.entityId());
Minecraft.getInstance().setScreen(new CustomizeNpcScreen(npcEntity));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;

public class S2COpenNpcCustomizationScreenPacket implements GenerationsNetworkPacket<S2COpenNpcCustomizationScreenPacket> {
public record S2COpenNpcCustomizationScreenPacket(int entityId) implements GenerationsNetworkPacket<S2COpenNpcCustomizationScreenPacket> {
public static final ResourceLocation ID = GenerationsCore.id("open_npc_customization");

private final int entityId;

public S2COpenNpcCustomizationScreenPacket(int entityId) {
this.entityId = entityId;
}

public S2COpenNpcCustomizationScreenPacket(FriendlyByteBuf buf) {
this.entityId = buf.readInt();
this(buf.readInt());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void handle(S2CUpdateNpcDisplayDataPacket packet) {
if (Minecraft.getInstance().level == null)
return;

Entity entity = Minecraft.getInstance().level.getEntity(packet.entityId);
Entity entity = Minecraft.getInstance().level.getEntity(packet.entityId());
if (entity instanceof PlayerNpcEntity npcEntity)
npcEntity.updateDisplayData();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;

public class S2CUpdateNpcDisplayDataPacket implements GenerationsNetworkPacket<S2CUpdateNpcDisplayDataPacket> {
public record S2CUpdateNpcDisplayDataPacket(int entityId) implements GenerationsNetworkPacket<S2CUpdateNpcDisplayDataPacket> {
public static final ResourceLocation ID = GenerationsCore.id("update_npc_display_data");

private final int entityId;

public S2CUpdateNpcDisplayDataPacket(int entityId) {
this.entityId = entityId;
}

public S2CUpdateNpcDisplayDataPacket(FriendlyByteBuf buf) {
this.entityId = buf.readInt();
this(buf.readInt());
}

@Override
Expand Down
Loading

0 comments on commit 499f80a

Please sign in to comment.