.save-skin, better .give, interactionMenu, cate...

This commit is contained in:
Cloudburst
2021-04-27 11:43:16 +02:00
parent a9bfe215fa
commit 2a05c8d0ce
23 changed files with 2369 additions and 299 deletions

View File

@@ -0,0 +1,90 @@
package cloudburst.rejects.commands;
import com.google.gson.*;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import minegame159.meteorclient.systems.commands.Command;
import minegame159.meteorclient.systems.commands.arguments.PlayerArgumentType;
import minegame159.meteorclient.utils.network.HttpUtils;
import minegame159.meteorclient.utils.player.ChatUtils;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.GameProfileArgumentType;
import net.minecraft.entity.player.PlayerEntity;
import org.apache.commons.codec.binary.Base64;
import org.lwjgl.BufferUtils;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.util.tinyfd.TinyFileDialogs;
import java.io.*;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import static com.mojang.brigadier.Command.SINGLE_SUCCESS;
public class SaveSkinCommand extends Command {
private final PointerBuffer filters;
private final Gson GSON = new Gson();
public SaveSkinCommand() {
super("save-skin","Download a player's skin by name.", "skin","skinsteal");
filters = BufferUtils.createPointerBuffer(1);
ByteBuffer pngFilter = MemoryUtil.memASCII("*.png");
filters.put(pngFilter);
filters.rewind();
}
@Override
public void build(LiteralArgumentBuilder<CommandSource> builder) {
builder.then(argument("player", PlayerArgumentType.player()).executes(ctx -> {
PlayerEntity playerEntity = ctx.getArgument("player", PlayerEntity.class);
String path = TinyFileDialogs.tinyfd_saveFileDialog("Save image", null, filters, null);
if (!path.endsWith(".png")) path += ".png";
saveSkin(playerEntity.getUuidAsString(),path);
return SINGLE_SUCCESS;
}));
}
private void saveSkin(String uuid, String path) {
try {
//going to explain what happens so I don't forget
//request their minecraft profile, all so we can get a base64 encoded string that contains ANOTHER json that then has the skin URL
String PROFILE_REQUEST_URL = "https://sessionserver.mojang.com/session/minecraft/profile/%s";
JsonObject object = HttpUtils.get(String.format(PROFILE_REQUEST_URL, uuid),JsonObject.class);
//Get the properties array which has what we need
JsonArray array = object.getAsJsonArray("properties");
JsonObject property = array.get(0).getAsJsonObject();
//value is what we grab but it's encoded so we have to decode it
String base64String = property.get("value").getAsString();
byte[] bs = Base64.decodeBase64(base64String);
//Convert the response to json and pull the skin url from there
String secondResponse = new String(bs, StandardCharsets.UTF_8);
JsonObject finalResponseObject = GSON.fromJson(secondResponse, JsonObject.class);
JsonObject texturesObject = finalResponseObject.getAsJsonObject("textures");
JsonObject skinObj = texturesObject.getAsJsonObject("SKIN");
String skinURL = skinObj.get("url").getAsString();
InputStream in = new BufferedInputStream(new URL(skinURL).openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1 != (n = in.read(buf))) {
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file.getPath());
fos.write(response);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}