Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Player NBT Support #16

Open
wants to merge 2 commits into
base: 1.16.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/main/java/malte0811/nbtedit/command/CommandNbtEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public static void register(CommandDispatcher<CommandSource> disp) {
.then(Commands.argument("hand", new HandArgument())
.executes(data -> editHand(data.getSource(), data.getArgument("hand", Hand.class))))
)
.then(Commands.literal("player")
.then(Commands.literal("me")
.executes(data -> editPlayer(data.getSource())))
.then(Commands.argument("target", EntityArgument.player())
.executes(data -> editPlayer(data.getSource(), EntityArgument.getPlayer(data, "target")))))
);
}

Expand All @@ -80,6 +85,16 @@ public static void registerSerializers() {
new ArgumentSerializer<>(HandArgument::new));
}

private static int editPlayer(CommandSource source) throws CommandSyntaxException {
return editPlayer(source, source.asPlayer());
}

private static int editPlayer(CommandSource source, ServerPlayerEntity player) throws CommandSyntaxException {
ServerPlayerEntity playerSource = source.asPlayer();
openEditWindow(playerSource, new EditPosKey(playerSource.getUniqueID(), player.getUniqueID()));
return 0;
}

private static int editHand(CommandSource source, Hand hand) throws CommandSyntaxException {
ServerPlayerEntity player = source.asPlayer();
openEditWindow(player, new EditPosKey(player.getUniqueID(), hand));
Expand All @@ -97,14 +112,14 @@ private static int editHand(CommandSource source, Hand hand) throws CommandSynta
);

public static int editRaytrace(CommandSource src) throws CommandSyntaxException {
PlayerEntity player = src.asPlayer();
ServerPlayerEntity player = src.asPlayer();
RayTraceResult mop = Utils.rayTrace(player);
if (mop != null && mop.getType() == Type.BLOCK) {
BlockPos bPos = ((BlockRayTraceResult) mop).getPos();
return editPos(src, bPos);
} else if (mop != null && mop.getType() == Type.ENTITY) {
Entity e = ((EntityRayTraceResult) mop).getEntity();
return editEntity(src, e);
return e instanceof ServerPlayerEntity ? editPlayer(src, (ServerPlayerEntity) e) : editEntity(src, e);
} else {
throw new CommandSyntaxException(NO_OBJECT_TYPE, NO_OBJECT_MSG);
}
Expand Down
36 changes: 33 additions & 3 deletions src/main/java/malte0811/nbtedit/gui/NBTFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import malte0811.nbtedit.nbt.*;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.*;
import net.minecraftforge.common.util.Constants;
import net.minecraft.util.Hand;
import net.minecraftforge.common.util.Constants.NBT;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
Expand All @@ -29,7 +29,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class NBTFrame extends JFrame {
Expand All @@ -46,9 +45,10 @@ public class NBTFrame extends JFrame {
private JMenuBar bar;
//TODO choosing from GUI
private INBTEditingProvider provider = NBTEdit.proxy;//new VanillaNBTProvider();//
private static final String defaultTitle = "NBTEdit";

public NBTFrame(EditPosKey pos) {
super("NBTEdit");
super(defaultTitle);
editPos = pos;
add(panel);
initGUI();
Expand All @@ -59,6 +59,7 @@ public NBTFrame(EditPosKey pos) {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new CloseListener());
setSize(500, 500);
setTitle(buildTitle());
setVisible(true);
}

Expand All @@ -70,6 +71,35 @@ public void pullNbt() {
});
}

private String buildTitle() {
if (editPos == null)
return defaultTitle;
StringBuilder builder = new StringBuilder(defaultTitle);
builder.append(" - ");
switch (editPos.type){
case PLAYER:
builder.append("Player ").append(editPos.entity);
break;
case HAND:
builder.append("Hand Item (")
.append(editPos.hand == Hand.MAIN_HAND ? "Main" : "Off")
.append(")");
break;
case ENTITY:
builder.append("Entity ").append(editPos.entity);
break;
case TILEENTITY:
builder.append("TileEntity at ")
.append(editPos.tilePos.getX())
.append("/")
.append(editPos.tilePos.getY())
.append("/")
.append(editPos.tilePos.getZ());
break;
}
return builder.toString();
}

private void initGUI() {
tree = new JTree(new DefaultMutableTreeNode(new ImmutablePair<>("nbtroot", new CompoundNBT())));
tree.setCellRenderer(new NBTTreeCellRenderer());
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/malte0811/nbtedit/nbt/EditPosKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public EditPosKey(@Nonnull UUID player, RegistryKey<World> dim) {
hand = Hand.MAIN_HAND;
}

public EditPosKey(@Nonnull UUID player, @Nonnull UUID target) {
this.player = player; //command sender
entity = target; //target player
this.dim = World.OVERWORLD;
tilePos = BlockPos.ZERO;
type = ObjectType.PLAYER;
hand = Hand.MAIN_HAND;
}

public EditPosKey(@Nonnull UUID p, RegistryKey<World> dim, @Nonnull BlockPos pos) {
player = p;
tilePos = pos;
Expand All @@ -77,8 +86,10 @@ public static EditPosKey fromBytes(PacketBuffer pBuf) {
byte typeId = pBuf.readByte();
ObjectType type = ObjectType.VALUES[typeId];
switch (type) {
case ENTITY:
case PLAYER:
UUID p = pBuf.readUniqueId();
return new EditPosKey(user, p);
case ENTITY:
UUID e = pBuf.readUniqueId();
return new EditPosKey(user, dimension, e);
case TILEENTITY:
Expand All @@ -96,6 +107,7 @@ public void toBytes(PacketBuffer pBuf) {
pBuf.writeString(dim.getLocation().toString());
pBuf.writeByte(type.ordinal());
switch (type) {
case PLAYER:
case ENTITY:
//entity
pBuf.writeUniqueId(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void requestNBT(EditPosKey k, @Nonnull Consumer<CompoundNBT> out) {
Minecraft.getInstance().player.sendChatMessage("/data get block " + k.tilePos.getX() + " " + k.tilePos.getY() + " "
+ k.tilePos.getZ());
break;
case PLAYER:
case ENTITY:
Minecraft.getInstance().player.sendChatMessage("/data get entity " + k.entity);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void onMessage(Supplier<NetworkEvent.Context> ctxSupplier) {
if (player.hasPermissionLevel(2)) {
Utils.setNBTAtPos(pos, value, player.server);
} else {
NBTEdit.logger.error("Player " + player.getName() +
NBTEdit.logger.error("Player " + player.getName().getUnformattedComponentText() +
" tried to push NBT data to the server but isn't permitted to do so!");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void onMessage(Supplier<NetworkEvent.Context> ctx) {
CompoundNBT val = Utils.getNBTForPos(pos, server);
NBTEdit.packetHandler.reply(new MessageNBTSync(pos, val), ctx.get());
} else {
NBTEdit.logger.error("Player " + player.getName() +
NBTEdit.logger.error("Player " + player.getName().getUnformattedComponentText() +
" tried to request NBT data from the server but isn't permitted to do so!");
}
});
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/malte0811/nbtedit/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import malte0811.nbtedit.network.MessageBlockUpdate;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
Expand All @@ -18,9 +19,7 @@
import net.minecraftforge.fml.network.PacketDistributor;
import net.minecraftforge.fml.server.ServerLifecycleHooks;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;
import java.util.Optional;


Expand Down Expand Up @@ -83,12 +82,25 @@ public static RayTraceResult rayTrace(Entity entity) {
}

public static CompoundNBT getNBTForPos(EditPosKey k, MinecraftServer server) {

ServerWorld world = server.getWorld(k.dim);
if (world == null) {
return null;
}
switch (k.type) {
case PLAYER:
Entity ent = world.getEntityByUuid(k.entity);
if (ent != null && !ent.removed) {
String entityString = EntityType.getKey(ent.getType()).toString(); //minecraft:player
if (entityString != null) {
CompoundNBT ret = new CompoundNBT();
//manually write data (like writeUnlessRemoved(...)), because serialization check
//returns false for players -> so no nbt data is returned when treating as entity
ret.putString("id", entityString);
ent.writeWithoutTypeId(ret);
return ret;
}
}
break;
case ENTITY:
Entity e = world.getEntityByUuid(k.entity);
if (e != null) {
Expand Down Expand Up @@ -119,6 +131,7 @@ public static void setNBTAtPos(EditPosKey k, CompoundNBT newNbt, MinecraftServer
ServerWorld world = server.getWorld(k.dim);
if (world != null) {
switch (k.type) {
case PLAYER:
case ENTITY:
Entity e = world.getEntityByUuid(k.entity);
if (e != null) {
Expand Down