Skip to content

Commit 465c9f2

Browse files
Merge pull request #7 from Vonr/master
Added option for page numbers and fixed lang file twice
2 parents 281b3b1 + 8c0f1db commit 465c9f2

File tree

2 files changed

+89
-1
lines changed
  • master/src/main/java/net/earthcomputer/clientcommands/command
  • src/main/resources/assets/clientcommands/lang

2 files changed

+89
-1
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

src/main/resources/assets/clientcommands/lang/en_us.lang

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ commands.cabort.usage=/cabort
6363
commands.cbook.success=Successfully edited book
6464
commands.cbook.noBook=You are not holding a book
6565
commands.cbook.noPlayer=You are not a player
66-
commands.cbook.usage=/cbook <fill|random>
66+
commands.cbook.usage=/cbook <fill|random> <pages>
6767

6868
commands.ccalc.mathError=Math error: %s
6969
commands.ccalc.syntaxError=Syntax error

0 commit comments

Comments
 (0)