Skip to content

Commit

Permalink
chore: update to new networking API (stream codecs)
Browse files Browse the repository at this point in the history
Older arch network API is deprecated now
  • Loading branch information
desht committed May 2, 2024
1 parent a1a5da5 commit 6f433a2
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 345 deletions.
7 changes: 4 additions & 3 deletions common/src/main/java/dev/ftb/mods/ftblibrary/FTBLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.architectury.event.events.common.CommandRegistrationEvent;
import dev.architectury.event.events.common.LifecycleEvent;
import dev.architectury.event.events.common.PlayerEvent;
import dev.architectury.networking.NetworkManager;
import dev.architectury.utils.Env;
import dev.architectury.utils.EnvExecutor;
import dev.ftb.mods.ftblibrary.net.FTBLibraryNet;
Expand All @@ -21,7 +22,7 @@ public class FTBLibrary {

public FTBLibrary() {
CommandRegistrationEvent.EVENT.register(FTBLibraryCommands::registerCommands);
FTBLibraryNet.init();
FTBLibraryNet.register();
LifecycleEvent.SERVER_STARTED.register(this::serverStarted);
LifecycleEvent.SERVER_STOPPED.register(this::serverStopped);
PlayerEvent.PLAYER_JOIN.register(this::playerJoined);
Expand All @@ -34,14 +35,14 @@ public static ResourceLocation rl(String path) {
}

private void serverStarted(MinecraftServer server) {
KnownServerRegistries.server = new KnownServerRegistries(server);
KnownServerRegistries.server = KnownServerRegistries.create(server);
}

private void serverStopped(MinecraftServer server) {
KnownServerRegistries.server = null;
}

private void playerJoined(ServerPlayer player) {
new SyncKnownServerRegistriesPacket(KnownServerRegistries.server).sendTo(player);
NetworkManager.sendToPlayer(player, new SyncKnownServerRegistriesPacket(KnownServerRegistries.server));
}
}
195 changes: 100 additions & 95 deletions common/src/main/java/dev/ftb/mods/ftblibrary/FTBLibraryCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.architectury.networking.NetworkManager;
import dev.architectury.platform.Mod;
import dev.architectury.platform.Platform;
import dev.architectury.registry.registries.RegistrarManager;
Expand All @@ -29,8 +30,8 @@
import net.minecraft.world.level.block.entity.TickingBlockEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;


Expand Down Expand Up @@ -94,103 +95,27 @@ public static void registerCommands(CommandDispatcher<CommandSourceStack> dispat
.then(Commands.literal("nbtedit")
.then(Commands.literal("block")
.then(Commands.argument("pos", BlockPosArgument.blockPos())
.executes(context -> editNBT(context, (info, tag) -> {
var pos = BlockPosArgument.getSpawnablePos(context, "pos");
var blockEntity = context.getSource().getLevel().getBlockEntity(pos);

if (blockEntity == null) {
return;
}

info.putString("type", "block");
info.putInt("x", pos.getX());
info.putInt("y", pos.getY());
info.putInt("z", pos.getZ());
tag.merge(blockEntity.saveWithFullMetadata(context.getSource().getLevel().registryAccess()));
tag.remove("x");
tag.remove("y");
tag.remove("z");
info.putString("id", tag.getString("id"));
tag.remove("id");

var key = RegistrarManager.getId(blockEntity.getType(), Registries.BLOCK_ENTITY_TYPE);
info.put("text", InfoAppender.create(context)
.add(Component.literal("Class"), Component.literal(blockEntity.getClass().getName()))
.add(Component.literal("ID"), Component.literal(key == null ? "null" : key.toString()))
.add(Component.literal("Block"), Component.literal(String.valueOf(RegistrarManager.getId(blockEntity.getBlockState().getBlock(), Registries.BLOCK))))
.add(Component.literal("Block Class"), Component.literal(blockEntity.getBlockState().getBlock().getClass().getName()))
.add(Component.literal("Position"), Component.literal("[" + pos.getX() + ", " + pos.getY() + ", " + pos.getZ() + "]"))
.add(Component.literal("Mod"), Component.literal(key == null ? "null" : Platform.getOptionalMod(key.getNamespace()).map(Mod::getName).orElse("Unknown")))
.add(Component.literal("Ticking"), Component.literal(blockEntity instanceof TickingBlockEntity ? "true" : "false"))
.list);

var title = blockEntity instanceof Nameable ? ((Nameable) blockEntity).getDisplayName() : null;

if (title == null) {
title = Component.literal(blockEntity.getClass().getSimpleName());
}

info.putString("title", Component.Serializer.toJson(title, context.getSource().registryAccess()));
}))
.executes(context -> editNBT(context, (info, tag) -> editBlockNBT(context, info, tag)))
)
)
.then(Commands.literal("entity")
.then(Commands.argument("entity", EntityArgument.entity())
.executes(context -> editNBT(context, (info, tag) -> {
var entity = EntityArgument.getEntity(context, "entity");

if (entity instanceof Player) {
return;
}

info.putString("type", "entity");
info.putInt("id", entity.getId());

entity.save(tag);

var key = RegistrarManager.getId(entity.getType(), Registries.ENTITY_TYPE);
info.put("text", InfoAppender.create(context)
.add(Component.literal("Class"), Component.literal(entity.getClass().getName()))
.add(Component.literal("ID"), Component.literal(key == null ? "null" : key.toString()))
.add(Component.literal("Mod"), Component.literal(key == null ? "null" : Platform.getOptionalMod(key.getNamespace()).map(Mod::getName).orElse("Unknown")))
.list());
info.putString("title", Component.Serializer.toJson(entity.getDisplayName(), entity.level().registryAccess()));
}))
.executes(context -> editNBT(context, (info, tag) -> editEntityNBT(context, info, tag)))
)
)
.then(Commands.literal("player")
.then(Commands.argument("player", EntityArgument.player())
.executes(context -> editNBT(context, (info, tag) -> {
var player = EntityArgument.getPlayer(context, "player");

info.putString("type", "player");
info.putUUID("id", player.getUUID());

player.saveWithoutId(tag);
tag.remove("id");

info.put("text", InfoAppender.create(context)
.add(Component.literal("Name"), player.getName())
.add(Component.literal("Display Name"), player.getDisplayName())
.add(Component.literal("UUID"), Component.literal(player.getUUID().toString()))
.list());
info.putString("title", Component.Serializer.toJson(player.getDisplayName(), player.level().registryAccess()));
}))
.executes(context -> editNBT(context, (info, tag) -> editPlayerNBT(context, info, tag)))
)
)
.then(Commands.literal("item")
.executes(context -> editNBT(context, (info, tag) -> {
var player = context.getSource().getPlayerOrException();
info.putString("type", "item");
Tag res = player.getItemInHand(InteractionHand.MAIN_HAND).save(player.level().registryAccess(), tag);
if (res instanceof CompoundTag t) tag.merge(t);
}))
.executes(context -> editNBT(context, (info, tag) -> editItemNBT(context, info, tag)))
)
)
.then(Commands.literal("clientconfig")
.requires(CommandSourceStack::isPlayer)
.executes(context -> {
new EditConfigPacket(true).sendTo(Objects.requireNonNull(context.getSource().getPlayer()));
NetworkManager.sendToPlayer(context.getSource().getPlayerOrException(), new EditConfigPacket(true));
return 1;
})
);
Expand All @@ -211,14 +136,6 @@ public static void registerCommands(CommandDispatcher<CommandSourceStack> dispat
dispatcher.register(command);
}

private static void addInfo(ListTag list, Component key, Component value, HolderLookup.Provider provider) {
list.add(StringTag.valueOf(Component.Serializer.toJson(
key.copy().withStyle(ChatFormatting.BLUE).append(": ").append(value.copy().withStyle(ChatFormatting.GOLD)),
provider)
)
);
}

private static int editNBT(CommandContext<CommandSourceStack> context, NBTEditCallback data) throws CommandSyntaxException {
var player = context.getSource().getPlayerOrException();
var info = new CompoundTag();
Expand All @@ -227,25 +144,113 @@ private static int editNBT(CommandContext<CommandSourceStack> context, NBTEditCa

if (!info.isEmpty()) {
EDITING_NBT.put(player.getUUID(), info);
new EditNBTPacket(info, tag).sendTo(player);
NetworkManager.sendToPlayer(player, new EditNBTPacket(info, tag));
return 1;
}

return 0;
}

private record InfoAppender(ListTag list, HolderLookup.Provider provider) {
static InfoAppender create(CommandContext<CommandSourceStack> context) {
return new InfoAppender(new ListTag(), context.getSource().registryAccess());
private static void editItemNBT(CommandContext<CommandSourceStack> context, CompoundTag info, CompoundTag tag) throws CommandSyntaxException {
var player = context.getSource().getPlayerOrException();
info.putString("type", "item");
Tag res = player.getItemInHand(InteractionHand.MAIN_HAND).save(player.level().registryAccess(), tag);
if (res instanceof CompoundTag t) tag.merge(t);
}

private static void editPlayerNBT(CommandContext<CommandSourceStack> context, CompoundTag info, CompoundTag tag) throws CommandSyntaxException {
var player = EntityArgument.getPlayer(context, "player");

info.putString("type", "player");
info.putUUID("id", player.getUUID());

player.saveWithoutId(tag);
tag.remove("id");

info.put("text", InfoBuilder.create(context)
.add(Component.literal("Name"), player.getName())
.add(Component.literal("Display Name"), player.getDisplayName())
.add(Component.literal("UUID"), Component.literal(player.getUUID().toString()))
.build());
info.putString("title", Component.Serializer.toJson(player.getDisplayName(), player.level().registryAccess()));
}

private static void editEntityNBT(CommandContext<CommandSourceStack> context, CompoundTag info, CompoundTag tag) throws CommandSyntaxException {
var entity = EntityArgument.getEntity(context, "entity");

if (entity instanceof Player) {
return;
}

private InfoAppender add(Component key, Component value) {
info.putString("type", "entity");
info.putInt("id", entity.getId());

entity.save(tag);

var key = RegistrarManager.getId(entity.getType(), Registries.ENTITY_TYPE);
info.put("text", InfoBuilder.create(context)
.add(Component.literal("Class"), Component.literal(entity.getClass().getName()))
.add(Component.literal("ID"), Component.literal(key == null ? "null" : key.toString()))
.add(Component.literal("Mod"), Component.literal(key == null ? "null" : Platform.getOptionalMod(key.getNamespace()).map(Mod::getName).orElse("Unknown")))
.build());
info.putString("title", Component.Serializer.toJson(entity.getDisplayName(), entity.level().registryAccess()));
}

private static void editBlockNBT(CommandContext<CommandSourceStack> context, CompoundTag info, CompoundTag tag) throws CommandSyntaxException {
var pos = BlockPosArgument.getSpawnablePos(context, "pos");
var blockEntity = context.getSource().getLevel().getBlockEntity(pos);

if (blockEntity == null) {
return;
}

info.putString("type", "block");
info.putInt("x", pos.getX());
info.putInt("y", pos.getY());
info.putInt("z", pos.getZ());
tag.merge(blockEntity.saveWithFullMetadata(context.getSource().getLevel().registryAccess()));
tag.remove("x");
tag.remove("y");
tag.remove("z");
info.putString("id", tag.getString("id"));
tag.remove("id");

var key = RegistrarManager.getId(blockEntity.getType(), Registries.BLOCK_ENTITY_TYPE);
info.put("text", InfoBuilder.create(context)
.add(Component.literal("Class"), Component.literal(blockEntity.getClass().getName()))
.add(Component.literal("ID"), Component.literal(key == null ? "null" : key.toString()))
.add(Component.literal("Block"), Component.literal(String.valueOf(RegistrarManager.getId(blockEntity.getBlockState().getBlock(), Registries.BLOCK))))
.add(Component.literal("Block Class"), Component.literal(blockEntity.getBlockState().getBlock().getClass().getName()))
.add(Component.literal("Position"), Component.literal("[" + pos.getX() + ", " + pos.getY() + ", " + pos.getZ() + "]"))
.add(Component.literal("Mod"), Component.literal(key == null ? "null" : Platform.getOptionalMod(key.getNamespace()).map(Mod::getName).orElse("Unknown")))
.add(Component.literal("Ticking"), Component.literal(blockEntity instanceof TickingBlockEntity ? "true" : "false"))
.build());

var title = blockEntity instanceof Nameable ? ((Nameable) blockEntity).getDisplayName() : null;

if (title == null) {
title = Component.literal(blockEntity.getClass().getSimpleName());
}

info.putString("title", Component.Serializer.toJson(title, context.getSource().registryAccess()));
}

private record InfoBuilder(ListTag list, HolderLookup.Provider provider) {
static InfoBuilder create(CommandContext<CommandSourceStack> context) {
return new InfoBuilder(new ListTag(), context.getSource().registryAccess());
}

private InfoBuilder add(Component key, Component value) {
list.add(StringTag.valueOf(Component.Serializer.toJson(
key.copy().withStyle(ChatFormatting.BLUE).append(": ").append(value.copy().withStyle(ChatFormatting.GOLD)),
provider)
)
);
return this;
}

private ListTag build() {
return list;
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package dev.ftb.mods.ftblibrary.nbtedit;

import com.mojang.blaze3d.platform.InputConstants;
import dev.architectury.networking.NetworkManager;
import dev.ftb.mods.ftblibrary.FTBLibrary;
import dev.ftb.mods.ftblibrary.config.*;
import dev.ftb.mods.ftblibrary.config.ui.EditStringConfigOverlay;
import dev.ftb.mods.ftblibrary.icon.*;
import dev.ftb.mods.ftblibrary.net.EditNBTResponsePacket;
import dev.ftb.mods.ftblibrary.ui.*;
import dev.ftb.mods.ftblibrary.ui.input.Key;
import dev.ftb.mods.ftblibrary.ui.input.MouseButton;
import dev.ftb.mods.ftblibrary.ui.misc.AbstractThreePanelScreen;
import dev.ftb.mods.ftblibrary.ui.misc.SimpleToast;
import dev.ftb.mods.ftblibrary.util.NBTUtils;
import dev.ftb.mods.ftblibrary.util.StringUtils;
import dev.ftb.mods.ftblibrary.util.TooltipList;
import dev.ftb.mods.ftblibrary.util.client.PositionedIngredient;
Expand Down Expand Up @@ -80,6 +83,18 @@ public NBTEditorScreen(CompoundTag info, CompoundTag nbt, NBTCallback callback)
setSelected(buttonNBTRoot);
}

public static void openEditor(CompoundTag info, CompoundTag tag) {
new NBTEditorScreen(info, tag, (accepted, responseTag) -> {
if (accepted) {
if (NBTUtils.getSizeInBytes(responseTag, false) >= 30000L) {
FTBLibrary.LOGGER.error("NBT too large to send!");
} else {
NetworkManager.sendToServer(new EditNBTResponsePacket(info, responseTag));
}
}
}).openGui();
}

private String getInfoTitle(CompoundTag info) {
if (info.contains("title")) {
MutableComponent title = Component.Serializer.fromJson(info.getString("title"), Minecraft.getInstance().level.registryAccess());
Expand Down
Loading

0 comments on commit 6f433a2

Please sign in to comment.