Skip to content

Commit

Permalink
✨ Fix fatal bug before 5.0.0.
Browse files Browse the repository at this point in the history
Signed-off-by: 秋雨落 <[email protected]>
  • Loading branch information
qyl27 committed Oct 6, 2023
1 parent 5bce701 commit f3a170d
Show file tree
Hide file tree
Showing 46 changed files with 507 additions and 675 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
A Minecraft mod allows you to edit any NBT tags of the game content with a GUI while in-game. Such as TileEntities, Entities. It may help map creators to make custom items or help mod creators to debug.
本模组可以用于在游戏内编辑物品、实体或方块的 NBT ,可能会对地图制作者制作自定义物品或模组开发者 Debug 有所帮助。

**There was a fatal bug before NBTEdit 5.0.0, please update it immediately.**
**所有 NBTEdit 5.0.0 以前的版本都存在严重漏洞,请立即更新。**

## Usage(食用方法)

### Shortcuts(快捷键)
Press `N` (by default) to edit your target BlockEntity, Entity or ItemStack in main hand(if target is missing).
Press `N` (by default) to edit your target BlockEntity, Entity or ItemStack in main hand (if target is missing).
使用 `N` 键(默认情况下)打开编辑界面。编辑的目标为十字准星指向的方块实体或者实体,如果没有指向则编辑主手上的物品。

### Commands(命令)
Expand Down Expand Up @@ -41,6 +44,7 @@ Location(位置): `./config/nbtedit.toml`
| Settings(配置选项) | Description | 说明 |
|------------------------|-----------------------------------------|-------------------------|
| can_edit_other_players | Allow edit other player in multiplayer. | 是否允许对其他玩家使用 NBTEdit 编辑。 |
| debug | Enable debug logs. | 是否启用 Debug 日志。 |

#### Fabric
Location(位置): `./config/nbtedit.json`
Expand All @@ -49,6 +53,8 @@ Location(位置): `./config/nbtedit.json`
|-----------------|-----------------------------------------|-------------------------|
| canEditOthers | Allow edit other player in multiplayer. | 是否允许对其他玩家使用 NBTEdit 编辑。 |
| permissionLevel | Permission level to use NBTEdit. | 使用 NBTEdit 所需的权限等级。 |
| debug | Enable debug logs. | 是否启用 Debug 日志。 |


## Origin(原帖地址)
http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/1286750-in-game-nbtedit-edit-mob-spawners-attributes-in
Expand All @@ -61,7 +67,7 @@ http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/1286750-
- I was kicked when I tried to save my edit(在尝试保存时被服务器踢出):
If it shows `Payload may not be larger than 32767 bytes`, please use [Packet Fixer](https://www.curseforge.com/minecraft/mc-mods/packet-fixer) by [TonimatasDEV](https://github.com/TonimatasDEV) to fix it.
如果客户端显示 `Payload may not be larger than 32767 bytes`,请使用 [TonimatasDEV](https://github.com/TonimatasDEV)[Packet Fixer](https://www.curseforge.com/minecraft/mc-mods/packet-fixer) 修复。


## Bug report(Bug反馈/催更)
Please go to the issues page.
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,7 @@ allprojects {
filesMatching(resourcesToReplace) {
expand replaceTokens
}

duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
}
8 changes: 8 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ publishing {
repositories {
}
}

sourceSets {
main {
resources {
srcDir file('src/generated/resources')
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// 1.20.1 2023-10-01T21:16:50.0633302 Languages: zh_cn
b1886b14eefc7f775e9036becb02204f0de4034b assets/nbtedit/lang/zh_cn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// 1.20.1 2023-10-01T21:16:50.0663481 Languages: en_us
51fa5556855fd781a16c10497a93dc0bcc2cf4aa assets/nbtedit/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"message.editing.item_stack": "Editing ItemStack named %1$s.",
"message.no_permission": "You have no permission to use NBTEdit.",
"message.not_a_player": "Only players can use this command.",
"message.not_loaded": "Block pos was not loaded.",
"message.nothing_to_edit": "There is no any target for editing.",
"message.saved.successful": "Saved successfully!",
"message.saving.failed.block_entity_not_exists": "Save failed. the BlockEntity is no longer exists.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"gui.narration.button.special_character": "插入特殊符号",
"gui.narration.sub_window.value_editor": "数值编辑子窗口",
"gui.title.nbtedit_block_entity": "正在编辑位于 %1$s %2$s %3$s 的方块实体",
"gui.title.nbtedit_entity": "正在编辑实体 ID:%1$s",
"gui.title.nbtedit_entity": "正在编辑实体 ID: %1$s",
"gui.title.nbtedit_item_stack": "正在编辑名为 %1$s 的物品",
"key.category.nbtedit": "游戏内 NBT 修改器 (重制版)",
"key.nbtedit.shortcut": "修改所指向内容的 NBT",
Expand All @@ -22,11 +22,12 @@
"message.editing.item_stack": "正在编辑名为 %1$s 的物品。",
"message.no_permission": "你没有权限使用 NBTEdit!",
"message.not_a_player": "只有在游戏中的玩家可以使用这个命令!",
"message.not_loaded": "方块还未加载!",
"message.nothing_to_edit": "没有任何目标可供编辑。",
"message.saved.successful": "保存成功!",
"message.saving.failed.block_entity_not_exists": "保存失败。目标方块实体已经不存在了!",
"message.saving.failed.entity_not_exists": "保存失败。目标实体不存在了!",
"message.saving.failed.invalid_nbt": "保存失败。无效的 NBT!",
"message.target_is_not_block_entity": "目标没有方块实体可供编辑",
"message.target_is_not_block_entity": "没有目标方块实体可供编辑",
"message.unknown_entity_id": "无效的实体 ID!"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

public interface INBTEditConfig {
boolean canEditOthers();

boolean isDebug();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;

public interface INBTEditNetworking {
void serverRayTraceRequest(ServerPlayer player);
Expand All @@ -14,7 +15,7 @@ public interface INBTEditNetworking {
void clientOpenGuiRequest(ItemStack stack);

void serverOpenClientGui(ServerPlayer player, Entity entity);
void serverOpenClientGui(ServerPlayer player, BlockPos pos);
void serverOpenClientGui(ServerPlayer player, BlockPos pos, BlockEntity blockEntity);
void serverOpenClientGui(ServerPlayer player);
void serverOpenClientGui(ServerPlayer player, ItemStack stack);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import cx.rain.mc.nbtedit.NBTEdit;
import cx.rain.mc.nbtedit.networking.NBTEditEditingHelper;
import cx.rain.mc.nbtedit.utility.Constants;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;

import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;
Expand Down Expand Up @@ -53,7 +52,7 @@ private static int onEntity(final CommandContext<CommandSourceStack> context) th

NBTEdit.getInstance().getLogger().info("Player " + player.getName().getString() +
" issued command /nbtedit with an entity.");
NBTEdit.getInstance().getNetworking().serverOpenClientGui(player, entity);
NBTEditEditingHelper.editEntity(player, entity.getUUID());
return 1;
}

Expand All @@ -68,7 +67,7 @@ private static int onBlockEntity(final CommandContext<CommandSourceStack> contex
NBTEdit.getInstance().getLogger().info("Player " + player.getName().getString() +
" issued command /nbtedit with an block at XYZ: " +
pos.getX() + " " + pos.getY() + " " + pos.getZ() + ".");
NBTEdit.getInstance().getNetworking().serverOpenClientGui(player, pos);
NBTEditEditingHelper.editBlockEntity(player, pos);
return 1;
}

Expand All @@ -81,7 +80,7 @@ private static int onEntityMe(final CommandContext<CommandSourceStack> context)

NBTEdit.getInstance().getLogger().info("Player " + player.getName().getString() +
" issued command /nbtedit to edit itself.");
NBTEdit.getInstance().getNetworking().serverOpenClientGui(player);
NBTEditEditingHelper.editEntity(player, player.getUUID());
return 1;
}

Expand All @@ -95,7 +94,7 @@ private static int onItemHand(final CommandContext<CommandSourceStack> context)
" issued command /nbtedit to edit hand.");

var stack = player.getMainHandItem();
NBTEdit.getInstance().getNetworking().serverOpenClientGui(player, stack);
NBTEditEditingHelper.editItemStack(player, stack);
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package cx.rain.mc.nbtedit.gui.component;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import cx.rain.mc.nbtedit.NBTEdit;
import cx.rain.mc.nbtedit.gui.NBTEditGui;
import cx.rain.mc.nbtedit.nbt.NBTTree;
import cx.rain.mc.nbtedit.nbt.NBTHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarratedElementType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cx.rain.mc.nbtedit.gui.component.button;

import com.mojang.blaze3d.systems.RenderSystem;
import cx.rain.mc.nbtedit.NBTEdit;
import cx.rain.mc.nbtedit.gui.NBTEditGui;
import cx.rain.mc.nbtedit.nbt.NBTHelper;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cx.rain.mc.nbtedit.networking;

import cx.rain.mc.nbtedit.NBTEdit;
import cx.rain.mc.nbtedit.utility.Constants;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

import java.util.UUID;

public class NBTEditEditingHelper {
public static void editBlockEntity(ServerPlayer player, BlockPos pos) {
if (!NBTEditNetworkingHelper.checkPermission(player)) {
return;
}

if (!NBTEditNetworkingHelper.checkPosLoaded(player, pos)) {
return;
}

NBTEdit.getInstance().getLogger().info("Player " + player.getName().getString() + " requested BlockEntity at " +
pos.getX() + " " + pos.getY() + " " + pos.getZ() + ".");

var blockEntity = player.serverLevel().getBlockEntity(pos);
if (blockEntity == null) {
player.createCommandSourceStack().sendFailure(Component.translatable(Constants.MESSAGE_TARGET_IS_NOT_BLOCK_ENTITY)
.withStyle(ChatFormatting.RED));
return;
}

player.sendSystemMessage(Component.translatable(Constants.MESSAGE_EDITING_BLOCK_ENTITY,
pos.getX(), pos.getY(), pos.getZ()).withStyle(ChatFormatting.GREEN));
NBTEdit.getInstance().getNetworking().serverOpenClientGui(player, pos, blockEntity);
}

public static void editEntity(ServerPlayer player, UUID entityUuid) {
if (!NBTEditNetworkingHelper.checkPermission(player)) {
return;
}

var entity = player.serverLevel().getEntity(entityUuid);

assert entity != null; // XXX: qyl27: will it work?

if (entity instanceof Player
&& entity != player
&& !NBTEdit.getInstance().getConfig().canEditOthers()) {
NBTEdit.getInstance().getLogger().info("Player " + player.getName().getString() +
" tried to use /nbtedit on a player. But config is not allow that.");
player.createCommandSourceStack().sendFailure(Component
.translatable(Constants.MESSAGE_CANNOT_EDIT_OTHER_PLAYER)
.withStyle(ChatFormatting.RED));
return;
}

player.sendSystemMessage(Component.translatable(Constants.MESSAGE_EDITING_ENTITY, entityUuid)
.withStyle(ChatFormatting.GREEN));

if (player == entity) {
NBTEdit.getInstance().getNetworking().serverOpenClientGui(player);
NBTEdit.getInstance().getLogger().info("Player " + player.getName().getString() + " is editing itself.");
} else {
NBTEdit.getInstance().getNetworking().serverOpenClientGui(player, entity);
NBTEdit.getInstance().getLogger().info("Player " + player.getName().getString() +
" is editing entity " + entity.getUUID() + ".");
}
}

public static void editItemStack(ServerPlayer player, ItemStack stack) {
if (!NBTEditNetworkingHelper.checkPermission(player)) {
return;
}

player.sendSystemMessage(Component.translatable(Constants.MESSAGE_EDITING_ITEM_STACK,
stack.getDisplayName().getString()).withStyle(ChatFormatting.GREEN));
NBTEdit.getInstance().getLogger().info("Player " + player.getName().getString() +
" is editing ItemStack named " + stack.getDisplayName().getString() + ".");
NBTEdit.getInstance().getNetworking().serverOpenClientGui(player, stack);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cx.rain.mc.nbtedit.networking;

import cx.rain.mc.nbtedit.NBTEdit;
import cx.rain.mc.nbtedit.utility.Constants;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;

public class NBTEditNetworkingHelper {
public static boolean checkPermission(ServerPlayer player) {
var result = NBTEdit.getInstance().getPermission().hasPermission(player);

if (!result) {
player.sendSystemMessage(Component.translatable(Constants.MESSAGE_NO_PERMISSION)
.withStyle(ChatFormatting.RED));
}

return result;
}

public static boolean checkPosLoaded(ServerPlayer player, BlockPos pos) {
var result = player.serverLevel().isLoaded(pos);

if (!result) {
player.sendSystemMessage(Component.translatable(Constants.MESSAGE_NOT_LOADED)
.withStyle(ChatFormatting.RED));
}

return result;
}

public static boolean isDebug() {
return NBTEdit.getInstance().getConfig().isDebug();
}

public static boolean canEditOthers() {
return NBTEdit.getInstance().getConfig().canEditOthers();
}
}
Loading

0 comments on commit f3a170d

Please sign in to comment.