diff --git a/src/main/java/anticope/rejects/commands/GiveCommand.java b/src/main/java/anticope/rejects/commands/GiveCommand.java index c14fe40..91931da 100644 --- a/src/main/java/anticope/rejects/commands/GiveCommand.java +++ b/src/main/java/anticope/rejects/commands/GiveCommand.java @@ -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; @@ -19,9 +22,9 @@ import net.minecraft.text.Text; import java.util.Collection; +import java.util.UUID; -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 { @@ -33,86 +36,98 @@ public GiveCommand() { @Override public void build(LiteralArgumentBuilder 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; }))); diff --git a/src/main/java/anticope/rejects/utils/accounts/GetPlayerUUID.java b/src/main/java/anticope/rejects/utils/accounts/GetPlayerUUID.java new file mode 100644 index 0000000..030485c --- /dev/null +++ b/src/main/java/anticope/rejects/utils/accounts/GetPlayerUUID.java @@ -0,0 +1,48 @@ +package anticope.rejects.utils.accounts; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.UUID; +import java.util.stream.Collectors; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +public class GetPlayerUUID { + + public static UUID getUUID(String playerName) { + // Thanks Bento + try { + Gson gsonReader = new Gson(); + JsonObject jsonObject = gsonReader.fromJson( + getURLContent("https://api.mojang.com/users/profiles/minecraft/" + playerName), + JsonObject.class); + + String userIdString = jsonObject.get("id").toString().replace("\"", "") + .replaceFirst("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5"); + + return UUID.fromString(userIdString); + } catch (Exception ignored) { + return UUID.randomUUID(); + } + } + + private static String getURLContent(String requestedUrl) { + String returnValue; + + try { + URL url = new URL(requestedUrl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); + returnValue = br.lines().collect(Collectors.joining()); + br.close(); + } catch (Exception e) { + returnValue = ""; + } + + return returnValue; + } + +} \ No newline at end of file