minecraft-source/src/net/minecraft/world/item/WrittenBookItem.java

128 lines
4.7 KiB
Java

package net.minecraft.world.item;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.StringTag;
import net.minecraft.world.entity.Entity;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.LecternBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.InteractionResult;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.TranslatableComponent;
import java.util.List;
import net.minecraft.world.level.Level;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.util.StringUtil;
import net.minecraft.network.chat.Component;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
public class WrittenBookItem extends Item {
public WrittenBookItem(final Properties a) {
super(a);
}
public static boolean makeSureTagIsValid(@Nullable final CompoundTag jt) {
if (!WritableBookItem.makeSureTagIsValid(jt)) {
return false;
}
if (!jt.contains("title", 8)) {
return false;
}
final String string2 = jt.getString("title");
return string2.length() <= 32 && jt.contains("author", 8);
}
public static int getGeneration(final ItemStack bek) {
return bek.getTag().getInt("generation");
}
public static int getPageCount(final ItemStack bek) {
final CompoundTag jt2 = bek.getTag();
return (jt2 != null) ? jt2.getList("pages", 8).size() : 0;
}
@Override
public Component getName(final ItemStack bek) {
if (bek.hasTag()) {
final CompoundTag jt3 = bek.getTag();
final String string4 = jt3.getString("title");
if (!StringUtil.isNullOrEmpty(string4)) {
return new TextComponent(string4);
}
}
return super.getName(bek);
}
@Override
public void appendHoverText(final ItemStack bek, @Nullable final Level bjt, final List<Component> list, final TooltipFlag bft) {
if (bek.hasTag()) {
final CompoundTag jt6 = bek.getTag();
final String string7 = jt6.getString("author");
if (!StringUtil.isNullOrEmpty(string7)) {
list.add(new TranslatableComponent("book.byAuthor", new Object[] { string7 }).withStyle(ChatFormatting.GRAY));
}
list.add(new TranslatableComponent("book.generation." + jt6.getInt("generation"), new Object[0]).withStyle(ChatFormatting.GRAY));
}
}
@Override
public InteractionResult useOn(final UseOnContext bfw) {
final Level bjt3 = bfw.getLevel();
final BlockPos fk4 = bfw.getClickedPos();
final BlockState byg5 = bjt3.getBlockState(fk4);
if (byg5.getBlock() == Blocks.LECTERN) {
return LecternBlock.tryPlaceBook(bjt3, fk4, byg5, bfw.getItemInHand()) ? InteractionResult.SUCCESS : InteractionResult.PASS;
}
return InteractionResult.PASS;
}
@Override
public InteractionResultHolder<ItemStack> use(final Level bjt, final Player ayg, final InteractionHand ajh) {
final ItemStack bek5 = ayg.getItemInHand(ajh);
ayg.openItemGui(bek5, ajh);
ayg.awardStat(Stats.ITEM_USED.get(this));
return InteractionResultHolder.<ItemStack>success(bek5);
}
public static boolean resolveBookComponents(final ItemStack bek, @Nullable final CommandSourceStack cq, @Nullable final Player ayg) {
final CompoundTag jt4 = bek.getTag();
if (jt4 == null || jt4.getBoolean("resolved")) {
return false;
}
jt4.putBoolean("resolved", true);
if (!makeSureTagIsValid(jt4)) {
return false;
}
final ListTag jz5 = jt4.getList("pages", 8);
for (int integer6 = 0; integer6 < jz5.size(); ++integer6) {
final String string7 = jz5.getString(integer6);
Component lf8;
try {
lf8 = Component.Serializer.fromJsonLenient(string7);
lf8 = ComponentUtils.updateForEntity(cq, lf8, ayg, 0);
}
catch (Exception exception9) {
lf8 = new TextComponent(string7);
}
jz5.set(integer6, StringTag.valueOf(Component.Serializer.toJson(lf8)));
}
jt4.put("pages", jz5);
return true;
}
@Override
public boolean isFoil(final ItemStack bek) {
return true;
}
}