added exceptions

This commit is contained in:
Cloudburst
2021-06-03 10:40:23 +02:00
parent a44b819353
commit 650de07978
4 changed files with 31 additions and 25 deletions

View File

@@ -1,6 +1,7 @@
package cloudburst.rejects.commands; package cloudburst.rejects.commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import minegame159.meteorclient.systems.commands.Command; import minegame159.meteorclient.systems.commands.Command;
import minegame159.meteorclient.utils.player.InvUtils; import minegame159.meteorclient.utils.player.InvUtils;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
@@ -9,6 +10,7 @@ import net.minecraft.item.Items;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag; import net.minecraft.nbt.StringTag;
import net.minecraft.network.packet.c2s.play.BookUpdateC2SPacket; import net.minecraft.network.packet.c2s.play.BookUpdateC2SPacket;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import static com.mojang.brigadier.Command.SINGLE_SUCCESS; import static com.mojang.brigadier.Command.SINGLE_SUCCESS;
@@ -17,6 +19,7 @@ import static com.mojang.brigadier.Command.SINGLE_SUCCESS;
public class BookDupeCommand extends Command { public class BookDupeCommand extends Command {
private final ItemStack DUPE_BOOK = new ItemStack(Items.WRITABLE_BOOK, 1); private final ItemStack DUPE_BOOK = new ItemStack(Items.WRITABLE_BOOK, 1);
private final static SimpleCommandExceptionType NO_BOOK_EXCEPTION = new SimpleCommandExceptionType(new LiteralText("No book found"));
public BookDupeCommand() { public BookDupeCommand() {
super("dupe", "Dupes using a held, writable book."); super("dupe", "Dupes using a held, writable book.");
@@ -45,8 +48,9 @@ public class BookDupeCommand extends Command {
@Override @Override
public void build(LiteralArgumentBuilder<CommandSource> builder) { public void build(LiteralArgumentBuilder<CommandSource> builder) {
builder.executes(context -> { builder.executes(context -> {
if (InvUtils.getHand(Items.WRITABLE_BOOK) != Hand.MAIN_HAND) error("No book found, you must be holding a writable book!"); if (InvUtils.getHand(Items.WRITABLE_BOOK) != Hand.MAIN_HAND) throw NO_BOOK_EXCEPTION.create();
else mc.player.networkHandler.sendPacket(new BookUpdateC2SPacket(DUPE_BOOK, true, mc.player.inventory.selectedSlot));
mc.player.networkHandler.sendPacket(new BookUpdateC2SPacket(DUPE_BOOK, true, mc.player.inventory.selectedSlot));
return SINGLE_SUCCESS; return SINGLE_SUCCESS;
}); });

View File

@@ -3,6 +3,8 @@ package cloudburst.rejects.commands;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
import net.minecraft.item.*; import net.minecraft.item.*;
import net.minecraft.nbt.*; import net.minecraft.nbt.*;
@@ -26,6 +28,8 @@ public class GiveCommand extends Command {
super("give", "Gives items in creative", "item", "kit"); super("give", "Gives items in creative", "item", "kit");
} }
private final static SimpleCommandExceptionType NOT_IN_CREATIVE = new SimpleCommandExceptionType(new LiteralText("You must be in creative mode to use this."));
private final static SimpleCommandExceptionType NO_SPACE = new SimpleCommandExceptionType(new LiteralText("No space in hotbar"));
private final Collection<String> PRESETS = Arrays.asList("forceop", "negs", "stacked", "spawners", "bookban", private final Collection<String> PRESETS = Arrays.asList("forceop", "negs", "stacked", "spawners", "bookban",
"test", "eggs"); "test", "eggs");
private final Collection<String> CONTAINERS = Arrays.asList("chest", "shulker", "trapped_chest", "barrel", private final Collection<String> CONTAINERS = Arrays.asList("chest", "shulker", "trapped_chest", "barrel",
@@ -34,6 +38,7 @@ public class GiveCommand extends Command {
@Override @Override
public void build(LiteralArgumentBuilder<CommandSource> builder) { public void build(LiteralArgumentBuilder<CommandSource> builder) {
builder.then(literal("egg").executes(ctx -> { builder.then(literal("egg").executes(ctx -> {
if (!mc.player.abilities.creativeMode) throw NOT_IN_CREATIVE.create();
ItemStack inHand = mc.player.getMainHandStack(); ItemStack inHand = mc.player.getMainHandStack();
ItemStack item = new ItemStack(Items.STRIDER_SPAWN_EGG); ItemStack item = new ItemStack(Items.STRIDER_SPAWN_EGG);
CompoundTag ct = new CompoundTag(); CompoundTag ct = new CompoundTag();
@@ -67,10 +72,7 @@ public class GiveCommand extends Command {
})); }));
builder.then(literal("holo").then(argument("message", StringArgumentType.greedyString()).executes(ctx -> { builder.then(literal("holo").then(argument("message", StringArgumentType.greedyString()).executes(ctx -> {
if (!mc.player.abilities.creativeMode) { if (!mc.player.abilities.creativeMode) throw NOT_IN_CREATIVE.create();
error("Not In Creative Mode!");
return SINGLE_SUCCESS;
}
String message = ctx.getArgument("message", String.class); String message = ctx.getArgument("message", String.class);
message = message.replace("&", "\247"); message = message.replace("&", "\247");
ItemStack stack = new ItemStack(Items.ARMOR_STAND); ItemStack stack = new ItemStack(Items.ARMOR_STAND);
@@ -92,10 +94,7 @@ public class GiveCommand extends Command {
}))); })));
builder.then(literal("firework").executes(ctx -> { builder.then(literal("firework").executes(ctx -> {
if (!mc.player.abilities.creativeMode) { if (!mc.player.abilities.creativeMode) throw NOT_IN_CREATIVE.create();
error("Not In Creative Mode!");
return SINGLE_SUCCESS;
}
ItemStack firework = new ItemStack(Items.FIREWORK_ROCKET); ItemStack firework = new ItemStack(Items.FIREWORK_ROCKET);
CompoundTag baseCompound = new CompoundTag(); CompoundTag baseCompound = new CompoundTag();
CompoundTag tagCompound = new CompoundTag(); CompoundTag tagCompound = new CompoundTag();
@@ -124,10 +123,7 @@ public class GiveCommand extends Command {
})); }));
builder.then(literal("head").then(argument("owner",StringArgumentType.greedyString()).executes(ctx -> { builder.then(literal("head").then(argument("owner",StringArgumentType.greedyString()).executes(ctx -> {
if (!mc.player.abilities.creativeMode) { if (!mc.player.abilities.creativeMode) throw NOT_IN_CREATIVE.create();
error("Not In Creative Mode!");
return SINGLE_SUCCESS;
}
String playerName = ctx.getArgument("owner",String.class); String playerName = ctx.getArgument("owner",String.class);
ItemStack itemStack = new ItemStack(Items.PLAYER_HEAD); ItemStack itemStack = new ItemStack(Items.PLAYER_HEAD);
CompoundTag tag = new CompoundTag(); CompoundTag tag = new CompoundTag();
@@ -139,10 +135,7 @@ public class GiveCommand extends Command {
builder.then(literal("preset").then(argument("name", new EnumStringArgumentType(PRESETS)) builder.then(literal("preset").then(argument("name", new EnumStringArgumentType(PRESETS))
.then(argument("container", new EnumStringArgumentType(CONTAINERS)).executes(context -> { .then(argument("container", new EnumStringArgumentType(CONTAINERS)).executes(context -> {
if (!mc.player.abilities.creativeMode) { if (!mc.player.abilities.creativeMode) throw NOT_IN_CREATIVE.create();
error("Not In Creative Mode!");
return SINGLE_SUCCESS;
}
String name = context.getArgument("name", String.class); String name = context.getArgument("name", String.class);
String container = context.getArgument("container", String.class); String container = context.getArgument("container", String.class);
addItem(createPreset(name, container)); addItem(createPreset(name, container));
@@ -150,13 +143,13 @@ public class GiveCommand extends Command {
})))); }))));
} }
private void addItem(ItemStack item) { private void addItem(ItemStack item) throws CommandSyntaxException {
for(int i = 0; i < 36; i++) { for(int i = 0; i < 36; i++) {
ItemStack stack = mc.player.inventory.getStack(SlotUtils.indexToId(i)); ItemStack stack = mc.player.inventory.getStack(SlotUtils.indexToId(i));
if (!stack.isEmpty()) continue; if (!stack.isEmpty()) continue;
mc.player.networkHandler.sendPacket(new CreativeInventoryActionC2SPacket(SlotUtils.indexToId(i), item)); mc.player.networkHandler.sendPacket(new CreativeInventoryActionC2SPacket(SlotUtils.indexToId(i), item));
return; return;
} }
error("No space in inventory."); throw NO_SPACE.create();
} }
} }

View File

@@ -2,11 +2,14 @@ package cloudburst.rejects.commands;
import com.google.gson.*; import com.google.gson.*;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import minegame159.meteorclient.systems.commands.Command; import minegame159.meteorclient.systems.commands.Command;
import minegame159.meteorclient.systems.commands.arguments.PlayerArgumentType; import minegame159.meteorclient.systems.commands.arguments.PlayerArgumentType;
import minegame159.meteorclient.utils.network.HttpUtils; import minegame159.meteorclient.utils.network.HttpUtils;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.LiteralText;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import org.lwjgl.PointerBuffer; import org.lwjgl.PointerBuffer;
@@ -22,6 +25,8 @@ import static com.mojang.brigadier.Command.SINGLE_SUCCESS;
public class SaveSkinCommand extends Command { public class SaveSkinCommand extends Command {
private final static SimpleCommandExceptionType IO_EXCEPTION = new SimpleCommandExceptionType(new LiteralText("An IOException occurred"));
private final PointerBuffer filters; private final PointerBuffer filters;
private final Gson GSON = new Gson(); private final Gson GSON = new Gson();
@@ -41,13 +46,14 @@ public class SaveSkinCommand extends Command {
builder.then(argument("player", PlayerArgumentType.player()).executes(ctx -> { builder.then(argument("player", PlayerArgumentType.player()).executes(ctx -> {
PlayerEntity playerEntity = ctx.getArgument("player", PlayerEntity.class); PlayerEntity playerEntity = ctx.getArgument("player", PlayerEntity.class);
String path = TinyFileDialogs.tinyfd_saveFileDialog("Save image", null, filters, null); String path = TinyFileDialogs.tinyfd_saveFileDialog("Save image", null, filters, null);
if (path == null) IO_EXCEPTION.create();
if (!path.endsWith(".png")) path += ".png"; if (!path.endsWith(".png")) path += ".png";
saveSkin(playerEntity.getUuidAsString(),path); saveSkin(playerEntity.getUuidAsString(),path);
return SINGLE_SUCCESS; return SINGLE_SUCCESS;
})); }));
} }
private void saveSkin(String uuid, String path) { private void saveSkin(String uuid, String path) throws CommandSyntaxException {
try { try {
//going to explain what happens so I don't forget //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 //request their minecraft profile, all so we can get a base64 encoded string that contains ANOTHER json that then has the skin URL
@@ -82,7 +88,7 @@ public class SaveSkinCommand extends Command {
fos.write(response); fos.write(response);
fos.close(); fos.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw IO_EXCEPTION.create();
} }
} }
} }

View File

@@ -2,9 +2,11 @@ package cloudburst.rejects.commands;
import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import minegame159.meteorclient.systems.commands.Command; import minegame159.meteorclient.systems.commands.Command;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
@@ -22,6 +24,8 @@ public class TerrainExport extends Command {
private final PointerBuffer filters; private final PointerBuffer filters;
private final static SimpleCommandExceptionType IO_EXCEPTION = new SimpleCommandExceptionType(new LiteralText("An IOException occurred"));
public TerrainExport() { public TerrainExport() {
super("terrain-export", "Export an area to the c++ terrain finder format (very popbob command)."); super("terrain-export", "Export an area to the c++ terrain finder format (very popbob command).");
@@ -51,8 +55,7 @@ public class TerrainExport extends Command {
} }
String path = TinyFileDialogs.tinyfd_saveFileDialog("Save data", null, filters, null); String path = TinyFileDialogs.tinyfd_saveFileDialog("Save data", null, filters, null);
if (path == null) if (path == null) throw IO_EXCEPTION.create();
return SINGLE_SUCCESS;
if (!path.endsWith(".txt")) if (!path.endsWith(".txt"))
path += ".txt"; path += ".txt";
try { try {
@@ -60,7 +63,7 @@ public class TerrainExport extends Command {
file.write(stringBuilder.toString().trim()); file.write(stringBuilder.toString().trim());
file.close(); file.close();
} catch (IOException e) { } catch (IOException e) {
error("IOException"); throw IO_EXCEPTION.create();
} }
return SINGLE_SUCCESS; return SINGLE_SUCCESS;