Skip to content

Commit

Permalink
Merge pull request #10 from qyl27/dev/edit-hand
Browse files Browse the repository at this point in the history
Merge dev/edit hand
  • Loading branch information
qyl27 authored Apr 11, 2023
2 parents 46a1385 + 464485a commit fe70d8e
Show file tree
Hide file tree
Showing 21 changed files with 320 additions and 45 deletions.
43 changes: 38 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,51 @@
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 有所帮助。

## Origin(原帖地址):
## Usage(食用方法)

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

### Commands(命令)

- `/nbtedit me`
Edit player themselves.
编辑玩家自身。

- `/nbtedit hand`
Edit ItemStack in player's main hand.
编辑玩家主手上的物品。

- `/nbtedit <x> <y> <z>`
Edit BlockEntity at <x> <y> <z>.
编辑位于 <x> <y> <z> 的方块实体。

- `/nbtedit <entity selector>`
Edit Entity with <entity selector>.
编辑由 <entity selector> 选择的实体。

### Permissions(权限)
`nbtedit.use`:
The permission to use NBTEdit. Server operators have this permission by default.
使用 NBTEdit 的权限。默认服务器OP拥有。

## Origin(原帖地址)
http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/1286750-in-game-nbtedit-edit-mob-spawners-attributes-in

## Screenshots(使用截图):
## Screenshots(使用截图)
![使用截图 #1](https://github.com/qyl27/NBTEdit/raw/1.19.4/img/2.png)
![使用截图 #2](https://github.com/qyl27/NBTEdit/raw/1.19.4/img/3.png)

## Bug report(Bug反馈/催更):
Please go to the issues page.
## Bug report(Bug反馈/催更)
Please go to the issues page.
请到 Issues 页面提出。

### Update log:
## Update log(更新日志)
```
NBTEdit 1.19.4-4.2.2:
Add command /nbtedit hand to edit ItemStack in hand.
NBTEdit 1.19.4-4.2.1:
Fix a mistake about scroll bar.
Bump version.
Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apply plugin: 'dev.architectury.loom'
group = 'cx.rain.mc.nbtedit'
archivesBaseName = 'nbtedit'

version = '1.19.4-4.2.1'
version = '1.19.4-4.2.2'
if (System.getenv("JITPACK") == "true") {
version += "-${System.getenv("VERSION")}"
}
Expand Down Expand Up @@ -127,6 +127,8 @@ tasks.withType(JavaCompile).configureEach {

import org.apache.tools.ant.filters.ReplaceTokens
task injectString(type: Copy) {
delete(file("${buildDir}/generated"))

from ('src/main/java') {
include '**/*.java'
filter(ReplaceTokens, tokens: [version: project.version])
Expand Down
Binary file added img/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.19.3 2023-03-04T11:28:48.4945746 Languages: zh_cn
4e143a95fed37073870660433a9357dfb69faf1e assets/nbtedit/lang/zh_cn.json
// 1.19.4 2023-04-12T06:43:16.0395219 Languages: zh_cn
6818a7498365cc7db19ff59c03ddbca95f4bc802 assets/nbtedit/lang/zh_cn.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.19.3 2023-03-04T11:28:48.4975785 Languages: en_us
316b09aae6169a350fba32329ac9188008562161 assets/nbtedit/lang/en_us.json
// 1.19.4 2023-04-12T06:43:16.0465219 Languages: en_us
d1df59968c06f0986ee3f3c5323a8097bb806852 assets/nbtedit/lang/en_us.json
4 changes: 3 additions & 1 deletion src/generated/resources/assets/nbtedit/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"gui.narration.sub_window.value_editor": "Value Editor Sub-Window",
"gui.title.nbtedit_block_entity": "Editing BlockEntity in {0} {1} {2}",
"gui.title.nbtedit_entity": "Editing Entity {0}",
"gui.title.nbtedit_item_stack": "Editing ItemStack named {0}",
"key.category.nbtedit": "In-game NBTEdit (Reborn)",
"key.nbtedit.shortcut": "Modify target Entity or BlockEntity NBT",
"message.cannot_edit_other_player": "Sorry, but you cannot edit other player.",
"message.editing.block_entity": "Editing BlockEntity at {0} {1} {2}",
"message.editing.block_entity": "Editing BlockEntity at {0} {1} {2}.",
"message.editing.entity": "Editing Entity {0}.",
"message.editing.item_stack": "Editing ItemStack named {0}.",
"message.no_permission": "You have no permission to use NBTEdit.",
"message.not_a_player": "Only players can use this command.",
"message.nothing_to_edit": "There is no any target for editing.",
Expand Down
6 changes: 4 additions & 2 deletions src/generated/resources/assets/nbtedit/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"gui.narration.sub_window.value_editor": "数值编辑子窗口",
"gui.title.nbtedit_block_entity": "正在编辑位于 {0} {1} {2} 的方块实体",
"gui.title.nbtedit_entity": "正在编辑实体 ID: {0}",
"gui.title.nbtedit_item_stack": "正在编辑名为 {0} 的物品",
"key.category.nbtedit": "游戏内 NBT 修改器 (重制版)",
"key.nbtedit.shortcut": "修改所指向内容的 NBT",
"message.cannot_edit_other_player": "你不能编辑其他玩家!",
"message.editing.block_entity": "正在编辑位于 %s %s %s 的方块实体。",
"message.editing.entity": "正在编辑实体 %s。",
"message.editing.block_entity": "正在编辑位于 {0} {1} {2} 的方块实体。",
"message.editing.entity": "正在编辑实体 {0}。",
"message.editing.item_stack": "正在编辑名为 {0} 的物品。",
"message.no_permission": "你没有权限使用 NBTEdit!",
"message.not_a_player": "只有在游戏中的玩家可以使用这个命令!",
"message.nothing_to_edit": "没有任何目标可供编辑。",
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/cx/rain/mc/nbtedit/client/NBTEditClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public class NBTEditClient {
public NBTEditClient() {
clipboardSaves = new ClipboardStates(Paths.get(".", "nbtedit", "Clipboard.nbt").toFile());

ClipboardStates clipboard = getClipboardSaves();
clipboard.load();
clipboard.save();
// Fixme: it is not working.
// ClipboardStates clipboard = getClipboardSaves();
// clipboard.load();
// clipboard.save();
}

public ClipboardStates getClipboardSaves() {
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/cx/rain/mc/nbtedit/command/NBTEditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public class NBTEditCommand {
.then(argument("block", BlockPosArgument.blockPos())
.executes(NBTEditCommand::onBlockEntity))
.then(literal("me")
.executes(NBTEditCommand::onEntityMe));
.executes(NBTEditCommand::onEntityMe))
.then(literal("hand")
.executes(NBTEditCommand::onItemHand));

@SubscribeEvent
public static void onRegisterCommands(RegisterCommandsEvent event) {
Expand Down Expand Up @@ -95,6 +97,20 @@ private static int onEntityMe(final CommandContext<CommandSourceStack> context)
return 1;
}

private static int onItemHand(final CommandContext<CommandSourceStack> context) {
if (!ensurePlayer(context)) {
return 0;
}

var player = context.getSource().getPlayer();
NBTEdit.getInstance().getLogger().info("Player " + player.getName().getString() +
" issued command /nbtedit to edit hand.");

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

private static boolean ensurePlayer(final CommandContext<CommandSourceStack> context) {
var source = context.getSource();
if (source.getEntity() instanceof ServerPlayer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ protected void addTranslations() {
add(Constants.MESSAGE_UNKNOWN_ENTITY_ID, "Unknown Entity ID.");

add(Constants.MESSAGE_EDITING_ENTITY, "Editing Entity {0}.");
add(Constants.MESSAGE_EDITING_BLOCK_ENTITY, "Editing BlockEntity at {0} {1} {2}");
add(Constants.MESSAGE_EDITING_BLOCK_ENTITY, "Editing BlockEntity at {0} {1} {2}.");
add(Constants.MESSAGE_EDITING_ITEM_STACK, "Editing ItemStack named {0}.");

add(Constants.MESSAGE_SAVING_SUCCESSFUL, "Saved successfully!");
add(Constants.MESSAGE_SAVING_FAILED_INVALID_NBT, "Save failed. Invalid NBT.");
Expand All @@ -33,6 +34,7 @@ protected void addTranslations() {

add(Constants.GUI_TITLE_NBTEDIT_ENTITY, "Editing Entity {0}");
add(Constants.GUI_TITLE_NBTEDIT_BLOCK_ENTITY, "Editing BlockEntity in {0} {1} {2}");
add(Constants.GUI_TITLE_NBTEDIT_ITEM_STACK, "Editing ItemStack named {0}");
add(Constants.GUI_BUTTON_SAVE, "Save");
add(Constants.GUI_BUTTON_LOAD, "Load");
add(Constants.GUI_BUTTON_QUIT, "Quit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ protected void addTranslations() {
add(Constants.MESSAGE_CANNOT_EDIT_OTHER_PLAYER, "你不能编辑其他玩家!");
add(Constants.MESSAGE_UNKNOWN_ENTITY_ID, "无效的实体 ID!");

add(Constants.MESSAGE_EDITING_ENTITY, "正在编辑实体 %s。");
add(Constants.MESSAGE_EDITING_BLOCK_ENTITY, "正在编辑位于 %s %s %s 的方块实体。");
add(Constants.MESSAGE_EDITING_ENTITY, "正在编辑实体 {0}。");
add(Constants.MESSAGE_EDITING_BLOCK_ENTITY, "正在编辑位于 {0} {1} {2} 的方块实体。");
add(Constants.MESSAGE_EDITING_ITEM_STACK, "正在编辑名为 {0} 的物品。");

add(Constants.MESSAGE_SAVING_SUCCESSFUL, "保存成功!");
add(Constants.MESSAGE_SAVING_FAILED_INVALID_NBT, "保存失败。无效的 NBT!");
Expand All @@ -33,6 +34,7 @@ protected void addTranslations() {

add(Constants.GUI_TITLE_NBTEDIT_ENTITY, "正在编辑实体 ID: {0}");
add(Constants.GUI_TITLE_NBTEDIT_BLOCK_ENTITY, "正在编辑位于 {0} {1} {2} 的方块实体");
add(Constants.GUI_TITLE_NBTEDIT_ITEM_STACK, "正在编辑名为 {0} 的物品");
add(Constants.GUI_BUTTON_SAVE, "保存");
add(Constants.GUI_BUTTON_LOAD, "加载");
add(Constants.GUI_BUTTON_QUIT, "退出");
Expand Down
49 changes: 33 additions & 16 deletions src/main/java/cx/rain/mc/nbtedit/gui/screen/NBTEditScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;

import java.util.UUID;

public class NBTEditScreen extends Screen {
protected final boolean isEntity;

protected UUID entityUuid;
protected int entityId;
protected int entityId = -1;
protected boolean isSelf;

protected BlockPos blockPos;

protected ItemStack itemStack;

protected NBTEditGui gui;

public NBTEditScreen(UUID uuid, int id, CompoundTag tag, boolean self) {
super(Component.translatable(Constants.GUI_TITLE_NBTEDIT_ENTITY, uuid));
minecraft = Minecraft.getInstance();

isEntity = true;
entityUuid = uuid;
entityId = id;
isSelf = self;
Expand All @@ -44,12 +44,20 @@ public NBTEditScreen(BlockPos pos, CompoundTag tag) {
pos.getX(), pos.getY(), pos.getZ()));
minecraft = Minecraft.getInstance();

isEntity = false;
blockPos = pos;

gui = new NBTEditGui(NBTTree.root(tag));
}

public NBTEditScreen(ItemStack itemStack, CompoundTag tag) {
super(Component.translatable(Constants.GUI_TITLE_NBTEDIT_ITEM_STACK, itemStack.getDisplayName().getString()));
minecraft = Minecraft.getInstance();

this.itemStack = itemStack;

gui = new NBTEditGui(NBTTree.root(tag));
}

@Override
protected void init() {
super.init();
Expand All @@ -69,11 +77,6 @@ protected void init() {
.build());
}

// @Override
// public void onClose() {
// super.onClose();
// }

@Override
public void tick() {
if (!getMinecraft().player.isAlive()) {
Expand Down Expand Up @@ -107,29 +110,41 @@ public void render(PoseStack stack, int mouseX, int mouseY, float partialTick) {
// <editor-fold desc="Properties and accessors.">

public boolean isEntity() {
return isEntity;
return entityUuid != null || entityId != -1;
}

public boolean isBlockEntity() {
return !isEntity;
return blockPos != null;
}

public boolean isItemStack() {
return itemStack != null;
}

public Entity getEntity() {
if (!isEntity()) {
throw new UnsupportedOperationException("Cannot get Entity by an BlockEntity!");
throw new UnsupportedOperationException("Cannot get Entity, it is not an Entity!");
}

return getMinecraft().level.getEntity(entityId);
}

public BlockPos getBlockPos() {
if (!isBlockEntity()) {
throw new UnsupportedOperationException("Cannot get block position of an Entity!");
throw new UnsupportedOperationException("Cannot get block position, it is not a BlockEntity!");
}

return blockPos;
}

public ItemStack getItemStack() {
if (!isItemStack()) {
throw new UnsupportedOperationException("Cannot get ItemStack, it is not an ItemStack!");
}

return itemStack;
}

// </editor-fold>

// <editor-fold desc="Button handler.">
Expand All @@ -144,10 +159,12 @@ private void onQuitClicked(Button button) {
}

private void doSave() {
if (isEntity) {
if (isEntity()) {
NBTEdit.getInstance().getNetworkManager().saveEditing(getEntity(), gui.getTree().toCompound(), isSelf);
} else {
} else if (isBlockEntity()) {
NBTEdit.getInstance().getNetworkManager().saveEditing(getBlockPos(), gui.getTree().toCompound());
} else {
NBTEdit.getInstance().getNetworkManager().saveEditing(getItemStack(), gui.getTree().toCompound());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import cx.rain.mc.nbtedit.NBTEdit;
import cx.rain.mc.nbtedit.utility.RayTraceHelper;
import net.minecraft.client.Minecraft;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = NBTEdit.MODID, bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
@OnlyIn(Dist.CLIENT)
public class OnNBTEditShortcut {
@SubscribeEvent
public static void onKeyboardInput(InputEvent.Key event) {
Expand Down
Loading

0 comments on commit fe70d8e

Please sign in to comment.