Skip to content

Commit

Permalink
Nbt fixes (#349)
Browse files Browse the repository at this point in the history
* progress towards working nbt

* update item nbt
  • Loading branch information
crazymoose77756 authored May 17, 2024
1 parent 3a6a0d8 commit 7bdd135
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 114 deletions.
96 changes: 55 additions & 41 deletions src/main/java/anticope/rejects/commands/GiveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import anticope.rejects.arguments.EnumStringArgumentType;
import anticope.rejects.utils.GiveUtils;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import meteordevelopment.meteorclient.commands.Command;
import net.minecraft.command.CommandSource;
import net.minecraft.component.ComponentChanges;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.component.type.ProfileComponent;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
Expand All @@ -20,8 +23,7 @@

import java.util.Collection;

import static com.mojang.brigadier.Command.SINGLE_SUCCESS;
import static meteordevelopment.meteorclient.MeteorClient.mc;
import static anticope.rejects.utils.accounts.GetPlayerUUID.getUUID;

public class GiveCommand extends Command {

Expand All @@ -33,86 +35,98 @@ public GiveCommand() {

@Override
public void build(LiteralArgumentBuilder<CommandSource> builder) {
// TODO : finish this
builder.then(literal("egg").executes(ctx -> {
ItemStack inHand = mc.player.getMainHandStack();
ItemStack item = new ItemStack(Items.STRIDER_SPAWN_EGG);
NbtCompound ct = new NbtCompound();

NbtCompound itemNbt = inHand
.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT)
.copyNbt();

if (inHand.getItem() instanceof BlockItem) {
itemNbt.putInt("Time", 1);
itemNbt.putString("id", "minecraft:falling_block");
itemNbt.put("BlockState", new NbtCompound());
itemNbt.getCompound("BlockState").putString("Name", Registries.ITEM.getId(inHand.getItem()).toString());
if (inHand.getComponents().contains(DataComponentTypes.BLOCK_ENTITY_DATA)) {
itemNbt.put("TileEntityData", inHand.get(DataComponentTypes.BLOCK_ENTITY_DATA).copyNbt());
}
NbtCompound t = new NbtCompound();
t.put("EntityTag", ct);
item.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(itemNbt));
ct.putInt("Time", 1);
ct.putString("id", "minecraft:falling_block");
ct.put("BlockState", new NbtCompound());
ct.getCompound("BlockState").putString("Name", Registries.ITEM.getId(inHand.getItem()).toString());

} else {
ct.putString("id", "minecraft:item");
NbtCompound it = new NbtCompound();
it.putString("id", Registries.ITEM.getId(inHand.getItem()).toString());
it.putInt("Count", inHand.getCount());
if (!inHand.getComponents().isEmpty()) {
it.put("tag", inHand.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT).copyNbt());
}
ct.put("Item", it);
NbtCompound itemTag = new NbtCompound();
itemTag.putString("id", Registries.ITEM.getId(inHand.getItem()).toString());
itemTag.putInt("Count", inHand.getCount());

ct.put("Item", itemTag);
}
NbtCompound t = new NbtCompound();
t.put("EntityTag", ct);
item.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(t));
item.set(DataComponentTypes.CUSTOM_NAME, inHand.getName());

var changes = ComponentChanges.builder()
.add(DataComponentTypes.CUSTOM_NAME, inHand.getName())
.add(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(t))
.build();

item.applyChanges(changes);
GiveUtils.giveItem(item);
return SINGLE_SUCCESS;
}));

//TODO: allow for custom cords to place oob
//TODO: allow for custom cords to place oob, though optional args
builder.then(literal("holo").then(argument("message", StringArgumentType.greedyString()).executes(ctx -> {
String message = ctx.getArgument("message", String.class).replace("&", "\247");
ItemStack stack = new ItemStack(Items.ARMOR_STAND);
ItemStack stack = new ItemStack(Items.STRIDER_SPAWN_EGG);
NbtCompound tag = new NbtCompound();
NbtList NbtList = new NbtList();
NbtList.add(NbtDouble.of(mc.player.getX()));
NbtList.add(NbtDouble.of(mc.player.getY()));
NbtList.add(NbtDouble.of(mc.player.getZ()));
NbtList pos = new NbtList();

pos.add(NbtDouble.of(mc.player.getX()));
pos.add(NbtDouble.of(mc.player.getY()));
pos.add(NbtDouble.of(mc.player.getZ()));

tag.putString("id", "minecraft:armor_stand");
tag.put("Pos", pos);
tag.putBoolean("Invisible", true);
tag.putBoolean("Invulnerable", true);
tag.putBoolean("Interpret", true);
tag.putBoolean("NoGravity", true);
tag.putBoolean("CustomNameVisible", true);
tag.putString("CustomName", Text.literal(message).toString());
tag.put("Pos", NbtList);
stack.set(DataComponentTypes.ENTITY_DATA, NbtComponent.of(tag));

var changes = ComponentChanges.builder()
.add(DataComponentTypes.CUSTOM_NAME, Text.literal(message))
.add(DataComponentTypes.ENTITY_DATA, NbtComponent.of(tag))
.build();

stack.applyChanges(changes);
GiveUtils.giveItem(stack);
return SINGLE_SUCCESS;
})));

//TODO, make invisible through potion effect
builder.then(literal("bossbar").then(argument("message", StringArgumentType.greedyString()).executes(ctx -> {
String message = ctx.getArgument("message", String.class).replace("&", "\247");
ItemStack stack = new ItemStack(Items.BAT_SPAWN_EGG);
NbtCompound tag = new NbtCompound();
tag.putString("CustomName", Text.literal(message).toString());
tag.putBoolean("NoAI", true);
tag.putBoolean("Silent", true);
tag.putBoolean("PersistenceRequired", true);
tag.putBoolean("Invisible", true);
tag.put("id", NbtString.of("minecraft:wither"));
stack.set(DataComponentTypes.ENTITY_DATA, NbtComponent.of(tag));

var changes = ComponentChanges.builder()
.add(DataComponentTypes.CUSTOM_NAME, Text.literal(message))
.add(DataComponentTypes.ENTITY_DATA, NbtComponent.of(tag))
.build();
stack.applyChanges(changes);

GiveUtils.giveItem(stack);
return SINGLE_SUCCESS;
})));

// TODO : resolve textures, should be easy now that UUID is resolved
builder.then(literal("head").then(argument("owner", StringArgumentType.greedyString()).executes(ctx -> {
String playerName = ctx.getArgument("owner", String.class);
ItemStack itemStack = new ItemStack(Items.PLAYER_HEAD);
NbtCompound tag = new NbtCompound();
tag.putString("SkullOwner", playerName);
itemStack.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(tag));

var changes = ComponentChanges.builder()
.add(DataComponentTypes.PROFILE, new ProfileComponent(new GameProfile(getUUID(playerName), playerName)))
.build();

itemStack.applyChanges(changes);

GiveUtils.giveItem(itemStack);
return SINGLE_SUCCESS;
})));
Expand Down
Loading

0 comments on commit 7bdd135

Please sign in to comment.