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

Nbt fixes #351

Open
wants to merge 3 commits into
base: master
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
151 changes: 100 additions & 51 deletions src/main/java/anticope/rejects/commands/GiveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@

import anticope.rejects.arguments.EnumStringArgumentType;
import anticope.rejects.utils.GiveUtils;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.brigadier.arguments.IntegerArgumentType;
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;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtDouble;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.nbt.*;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;

import java.util.Collection;
import java.util.Optional;

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

public class GiveCommand extends Command {

Expand All @@ -33,86 +37,131 @@ public GiveCommand() {

@Override
public void build(LiteralArgumentBuilder<CommandSource> builder) {
// not transferring custom nbt from source item... yet
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();
NbtCompound data = new NbtCompound();

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));
NbtCompound blockState = new NbtCompound();
blockState.putString("Name", Registries.ITEM.getId(inHand.getItem()).toString());

data.putString("id", "minecraft:falling_block");
data.putInt("Time", 1);
data.put("BlockState", blockState);

} 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 itemData = new NbtCompound();
itemData.putString("id", Registries.ITEM.getId(inHand.getItem()).toString());
itemData.putInt("Count", inHand.getCount());

data.putString("id", "minecraft:item");
data.put("Item", itemData);
}
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.ENTITY_DATA, NbtComponent.of(data))
.build();

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

//TODO: allow for custom cords to place oob
builder.then(literal("holo").then(argument("message", StringArgumentType.greedyString()).executes(ctx -> {
// there is a lot of repeat here, gotta be a better way
builder.then(literal("holo").then(argument("message", StringArgumentType.string()).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;
})));
}).then(argument("x", IntegerArgumentType.integer()).then(argument("y", IntegerArgumentType.integer()).then(argument("z", IntegerArgumentType.integer()).executes(ctx -> {
String message = ctx.getArgument("message", String.class).replace("&", "\247");
ItemStack stack = new ItemStack(Items.STRIDER_SPAWN_EGG);
NbtCompound tag = new NbtCompound();
NbtList pos = new NbtList();

pos.add(NbtDouble.of(Double.parseDouble(ctx.getArgument("x", String.class))));
pos.add(NbtDouble.of(Double.parseDouble(ctx.getArgument("y", String.class))));
pos.add(NbtDouble.of(Double.parseDouble(ctx.getArgument("z", String.class))));

tag.putString("id", "minecraft:armor_stand");
tag.put("Pos", pos);
tag.putBoolean("Invisible", true);
tag.putBoolean("Invulnerable", true);
tag.putBoolean("NoGravity", true);
tag.putBoolean("CustomNameVisible", true);

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;
}))))));

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);
NbtList activeEffects = new NbtList();
NbtCompound tag = new NbtCompound();
tag.putString("CustomName", Text.literal(message).toString());
NbtCompound effect = new NbtCompound();

effect.putInt("duration", 2147483647);
effect.putInt("amplifier", 1);
effect.putString("id", "minecraft:invisibility");
activeEffects.add(effect);

tag.put("active_effects", activeEffects);
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;
})));

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));

PropertyMap properties = new PropertyMap();
properties.put("texture", new Property("textures", getHeadTexture(getUUID(playerName))));

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

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