ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemBooklet.java

142 lines
7.8 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("ItemBooklet.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.items;
2015-08-28 21:17:09 +02:00
2024-03-02 21:23:08 +01:00
import com.mojang.blaze3d.platform.Window;
import de.ellpeck.actuallyadditions.api.booklet.IBookletPage;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.blocks.IHudDisplay;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
2024-03-02 21:23:08 +01:00
import net.minecraft.ChatFormatting;
2024-03-04 20:21:48 +01:00
import net.minecraft.advancements.AdvancementHolder;
import net.minecraft.client.Minecraft;
2024-03-03 01:20:53 +01:00
import net.minecraft.client.gui.GuiGraphics;
2024-03-02 21:23:08 +01:00
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.PlayerAdvancements;
import net.minecraft.server.ServerAdvancementManager;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.HitResult;
2024-03-04 20:21:48 +01:00
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
2021-02-26 22:15:48 +01:00
import javax.annotation.Nullable;
import java.util.List;
2015-08-28 21:17:09 +02:00
2019-05-02 09:10:29 +02:00
public class ItemBooklet extends ItemBase implements IHudDisplay {
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2016-11-11 21:07:18 +01:00
public static IBookletPage forcedPage;
2015-08-28 21:17:09 +02:00
public ItemBooklet() {
super(ActuallyItems.defaultProps().stacksTo(1));
2015-08-28 21:17:09 +02:00
}
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult useOn(UseOnContext context) {
2021-11-13 20:09:55 +01:00
// if (context.getPlayer().isShiftKeyDown()) {
// BlockState state = context.getLevel().getBlockState(context.getClickedPos());
// Block block = state.getBlock();
// ItemStack blockStack = new ItemStack(block);
// IBookletPage page = BookletUtils.findFirstPageForStack(blockStack);
// if (page != null) {
// if (context.getLevel().isClientSide) {
// forcedPage = page;
// }
// this.use(context.getLevel(), context.getPlayer(), context.getHand());
// return ActionResultType.SUCCESS;
// }
// }
2024-03-02 21:23:08 +01:00
return InteractionResult.FAIL;
}
2016-02-01 17:49:55 +01:00
@Override
2024-03-02 21:23:08 +01:00
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
if (!world.isClientSide) {
2024-03-02 21:23:08 +01:00
ServerPlayer serverPlayer = (ServerPlayer) player;
PlayerAdvancements advancements = serverPlayer.getAdvancements();
2024-03-02 21:23:08 +01:00
ServerAdvancementManager manager = player.getServer().getAdvancements();
2024-03-04 20:21:48 +01:00
AdvancementHolder advancement = manager.get(new ResourceLocation(ActuallyAdditions.MODID, "root"));
if (advancement != null && !advancements.getOrStartProgress(advancement).isDone()) {
advancements.award(advancement, "right_click");
}
2024-03-06 00:10:49 +01:00
} else {
vazkii.patchouli.api.PatchouliAPI.get().openBookGUI(new ResourceLocation(ActuallyAdditions.MODID, "booklet"));
}
2021-11-13 20:09:55 +01:00
// player.openGui(ActuallyAdditions.INSTANCE, GuiHandler.GuiTypes.BOOK.ordinal(), world, (int) player.posX, (int) player.posY, (int) player.posZ);
//
// if (!world.isClientSide) {
// //TheAchievements.OPEN_BOOKLET.get(player);
// //TheAchievements.OPEN_BOOKLET_MILESTONE.get(player);
// }
2024-03-02 21:23:08 +01:00
return InteractionResultHolder.success(player.getItemInHand(hand));
2016-02-01 17:49:55 +01:00
}
2021-12-30 18:30:01 +01:00
@OnlyIn(Dist.CLIENT)
2015-12-01 19:48:09 +01:00
@Override
2024-03-02 21:23:08 +01:00
public void appendHoverText(ItemStack stack, @Nullable Level playerIn, List<Component> tooltip, TooltipFlag advanced) {
2024-03-06 00:10:49 +01:00
tooltip.add(Component.translatable("tooltip." + ActuallyAdditions.MODID + ".item_booklet.desc").withStyle(ChatFormatting.GRAY));
2021-04-19 21:20:23 +02:00
// TODO: this is bad
2019-05-02 09:10:29 +02:00
for (int i = 1; i <= 4; i++) {
2024-03-06 00:10:49 +01:00
tooltip.add(Component.translatable("tooltip." + ActuallyAdditions.MODID + ".item_booklet.sub." + i).withStyle(i == 4
2024-03-02 21:23:08 +01:00
? ChatFormatting.GOLD
: ChatFormatting.RESET).withStyle(i == 4
? ChatFormatting.ITALIC
: ChatFormatting.RESET));
}
2015-12-01 19:48:09 +01:00
}
@Override
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2024-03-03 01:20:53 +01:00
public void displayHud(GuiGraphics guiGraphics, Minecraft minecraft, Player player, ItemStack stack, HitResult rayCast, Window resolution) {
2021-11-13 20:09:55 +01:00
// if (rayCast != null && rayCast.getBlockPos() != null) {
// BlockState state = minecraft.level.getBlockState(rayCast.getBlockPos());
// Block block = state.getBlock();
// if (block != null && !block.isAir(minecraft.level.getBlockState(rayCast.getBlockPos()), minecraft.level, rayCast.getBlockPos())) {
// ItemStack blockStack = new ItemStack(block, 1, block.getMetaFromState(state));
// int height = resolution.getGuiScaledHeight() / 5 * 3;
// if (player.isShiftKeyDown()) {
// IBookletPage page = BookletUtils.findFirstPageForStack(blockStack);
// if (page != null) {
// String strg1 = page.getChapter().getLocalizedName();
// String strg2 = StringUtil.localize("info." + ActuallyAdditions.MODID + ".booklet.hudDisplay.page") + " " + (page.getChapter().getPageIndex(page) + 1);
// String strg3 = StringUtil.localize("info." + ActuallyAdditions.MODID + ".booklet.hudDisplay.open");
//
// AssetUtil.renderStackToGui(StackUtil.isValid(page.getChapter().getDisplayItemStack())
// ? page.getChapter().getDisplayItemStack()
// : new ItemStack(ActuallyItems.ITEM_BOOKLET.get()), resolution.getGuiScaledWidth() / 2 - 10, height + 41, 1F);
// minecraft.font.drawShadow(TextFormatting.YELLOW + "" + TextFormatting.ITALIC + strg1, resolution.getGuiScaledWidth() / 2 - minecraft.font.width(strg1) / 2, height + 20, StringUtil.DECIMAL_COLOR_WHITE);
// minecraft.font.drawShadow(TextFormatting.YELLOW + "" + TextFormatting.ITALIC + strg2, resolution.getGuiScaledWidth() / 2 - minecraft.font.width(strg2) / 2, height + 30, StringUtil.DECIMAL_COLOR_WHITE);
// minecraft.font.drawShadow(TextFormatting.GOLD + strg3, resolution.getGuiScaledWidth() / 2 - minecraft.font.width(strg3) / 2, height + 60, StringUtil.DECIMAL_COLOR_WHITE);
// } else {
// String strg1 = TextFormatting.DARK_RED + StringUtil.localize("info." + ActuallyAdditions.MODID + ".booklet.hudDisplay.noInfo");
// String strg2 = TextFormatting.DARK_GREEN + "" + TextFormatting.ITALIC + StringUtil.localize("info." + ActuallyAdditions.MODID + ".booklet.hudDisplay.noInfo.desc.1");
// String strg3 = TextFormatting.DARK_GREEN + "" + TextFormatting.ITALIC + StringUtil.localize("info." + ActuallyAdditions.MODID + ".booklet.hudDisplay.noInfo.desc.2");
//
// minecraft.font.drawShadow(strg1, resolution.getGuiScaledWidth() / 2 - minecraft.font.width(strg1) / 2, height + 30, StringUtil.DECIMAL_COLOR_WHITE);
// minecraft.font.drawShadow(strg2, resolution.getGuiScaledWidth() / 2 - minecraft.font.width(strg2) / 2, height + 50, StringUtil.DECIMAL_COLOR_WHITE);
// minecraft.font.drawShadow(strg3, resolution.getGuiScaledWidth() / 2 - minecraft.font.width(strg3) / 2, height + 60, StringUtil.DECIMAL_COLOR_WHITE);
// }
// }
// }
// }
}
2015-08-28 21:17:09 +02:00
}