|
| 1 | +package net.earthcomputer.clientcommands.command; |
| 2 | + |
| 3 | +import io.netty.buffer.Unpooled; |
| 4 | +import net.minecraft.client.entity.EntityPlayerSP; |
| 5 | +import net.minecraft.client.resources.I18n; |
| 6 | +import net.minecraft.command.CommandException; |
| 7 | +import net.minecraft.command.ICommandSender; |
| 8 | +import net.minecraft.command.WrongUsageException; |
| 9 | +import net.minecraft.init.Items; |
| 10 | +import net.minecraft.item.ItemStack; |
| 11 | +import net.minecraft.nbt.NBTTagList; |
| 12 | +import net.minecraft.nbt.NBTTagString; |
| 13 | +import net.minecraft.network.PacketBuffer; |
| 14 | +import net.minecraft.network.play.client.CPacketCustomPayload; |
| 15 | +import net.minecraft.server.MinecraftServer; |
| 16 | +import net.minecraft.util.math.BlockPos; |
| 17 | +import net.minecraft.util.text.TextComponentString; |
| 18 | +import net.minecraft.util.text.TextComponentTranslation; |
| 19 | +import net.minecraft.util.text.TextFormatting; |
| 20 | + |
| 21 | +import javax.annotation.Nullable; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Random; |
| 26 | +import java.util.function.IntSupplier; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import java.util.stream.IntStream; |
| 29 | + |
| 30 | +public class CommandBook extends ClientCommandBase { |
| 31 | + @Override |
| 32 | + public String getName() { |
| 33 | + return "cbook"; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public String getUsage(ICommandSender sender) { |
| 38 | + return "commands.cbook.usage"; |
| 39 | + } |
| 40 | + |
| 41 | + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { |
| 42 | + if (args.length == 0) |
| 43 | + throw new WrongUsageException(getUsage(sender)); |
| 44 | + |
| 45 | + if (!(sender instanceof EntityPlayerSP)) |
| 46 | + throw new CommandException("commands.cbook.noPlayer"); |
| 47 | + |
| 48 | + int limit = args.length > 1 ? parseInt(args[1], 1, 50) : 50; |
| 49 | + |
| 50 | + EntityPlayerSP player = (EntityPlayerSP) sender; |
| 51 | + ItemStack heldItem = player.getHeldItemMainhand(); |
| 52 | + if (heldItem.getItem() != Items.WRITABLE_BOOK) { |
| 53 | + throw new CommandException("commands.cbook.noBook"); |
| 54 | + } |
| 55 | + |
| 56 | + IntStream characterGenerator; |
| 57 | + |
| 58 | + switch (args[0]) { |
| 59 | + case "fill": |
| 60 | + characterGenerator = IntStream.generate(() -> 0x10ffff); |
| 61 | + break; |
| 62 | + case "random": |
| 63 | + characterGenerator = new Random().ints(0x80, 0x10ffff - 0x800).map(i -> i < 0xd800 ? i : i + 0x800); |
| 64 | + break; |
| 65 | + default: |
| 66 | + throw new CommandException(getUsage(sender)); |
| 67 | + } |
| 68 | + |
| 69 | + String joinedPages = characterGenerator.limit(50 * 210).mapToObj(i -> String.valueOf((char) i)).collect(Collectors.joining()); |
| 70 | + |
| 71 | + NBTTagList pages = new NBTTagList(); |
| 72 | + |
| 73 | + for (int page = 0; page < limit; page++) { |
| 74 | + pages.appendTag(new NBTTagString(joinedPages.substring(page * 210, (page + 1) * 210))); |
| 75 | + } |
| 76 | + |
| 77 | + if (heldItem.hasTagCompound()) { |
| 78 | + heldItem.getTagCompound().setTag("pages", pages); |
| 79 | + } else { |
| 80 | + heldItem.setTagInfo("pages", pages); |
| 81 | + } |
| 82 | + PacketBuffer buf = new PacketBuffer(Unpooled.buffer()); |
| 83 | + buf.writeItemStack(heldItem); |
| 84 | + player.connection.sendPacket(new CPacketCustomPayload("MC|BEdit", buf)); |
| 85 | + |
| 86 | + sender.sendMessage(new TextComponentTranslation("commands.cbook.success")); |
| 87 | + } |
| 88 | +} |
0 commit comments