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

133 lines
7.2 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
2021-02-27 21:24:26 +01:00
import com.mojang.blaze3d.matrix.MatrixStack;
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;
import de.ellpeck.actuallyadditions.mod.booklet.misc.BookletUtils;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.inventory.GuiHandler;
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import net.minecraft.block.Block;
2021-02-27 21:24:26 +01:00
import net.minecraft.block.BlockState;
2021-02-27 17:22:03 +01:00
import net.minecraft.client.MainWindow;
import net.minecraft.client.Minecraft;
2017-06-17 00:48:49 +02:00
import net.minecraft.client.util.ITooltipFlag;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
2015-08-28 21:17:09 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
2021-02-26 22:15:48 +01:00
import net.minecraft.util.Hand;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.RayTraceResult;
2021-04-19 21:20:23 +02:00
import net.minecraft.util.text.ITextComponent;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.text.TextFormatting;
2021-04-19 21:20:23 +02:00
import net.minecraft.util.text.TranslationTextComponent;
2015-08-28 21:17:09 +02:00
import net.minecraft.world.World;
2021-02-27 21:24:26 +01:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.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
public ActionResultType useOn(ItemUseContext context) {
if (context.getPlayer().isShiftKeyDown()) {
BlockState state = context.getLevel().getBlockState(context.getClickedPos());
2016-07-04 20:15:41 +02:00
Block block = state.getBlock();
ItemStack blockStack = new ItemStack(block);
IBookletPage page = BookletUtils.findFirstPageForStack(blockStack);
2019-05-02 09:10:29 +02:00
if (page != null) {
if (context.getLevel().isClientSide) {
2016-11-11 21:07:18 +01:00
forcedPage = page;
}
this.use(context.getLevel(), context.getPlayer(), context.getHand());
return ActionResultType.SUCCESS;
}
}
return ActionResultType.FAIL;
}
2016-02-01 17:49:55 +01:00
@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
2019-05-02 09:10:29 +02:00
player.openGui(ActuallyAdditions.INSTANCE, GuiHandler.GuiTypes.BOOK.ordinal(), world, (int) player.posX, (int) player.posY, (int) player.posZ);
2016-02-01 17:49:55 +01:00
if (!world.isClientSide) {
2017-06-17 00:48:49 +02:00
//TheAchievements.OPEN_BOOKLET.get(player);
//TheAchievements.OPEN_BOOKLET_MILESTONE.get(player);
2016-02-01 17:49:55 +01:00
}
return ActionResult.success(player.getItemInHand(hand));
2016-02-01 17:49:55 +01:00
}
2015-12-01 19:48:09 +01:00
@Override
public void appendHoverText(ItemStack stack, @Nullable World playerIn, List<ITextComponent> tooltip, ITooltipFlag advanced) {
tooltip.add(new TranslationTextComponent("tooltip." + ActuallyAdditions.MODID + "." + this.getDescription().getString() + ".desc"));
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++) {
tooltip.add(new TranslationTextComponent("tooltip." + ActuallyAdditions.MODID + "." + this.getDescription().getString() + ".sub." + i).withStyle(i == 4
2021-04-19 21:20:23 +02:00
? TextFormatting.GOLD
: TextFormatting.RESET).withStyle(i == 4
2021-04-19 21:20:23 +02:00
? TextFormatting.ITALIC
: TextFormatting.RESET));
}
2015-12-01 19:48:09 +01:00
}
@Override
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2021-02-27 21:24:26 +01:00
public void displayHud(MatrixStack matrices, Minecraft minecraft, PlayerEntity player, ItemStack stack, RayTraceResult rayCast, MainWindow resolution) {
if (rayCast != null && rayCast.getBlockPos() != null) {
BlockState state = minecraft.level.getBlockState(rayCast.getBlockPos());
2016-07-04 20:15:41 +02:00
Block block = state.getBlock();
if (block != null && !block.isAir(minecraft.level.getBlockState(rayCast.getBlockPos()), minecraft.level, rayCast.getBlockPos())) {
2016-07-04 20:15:41 +02:00
ItemStack blockStack = new ItemStack(block, 1, block.getMetaFromState(state));
int height = resolution.getGuiScaledHeight() / 5 * 3;
if (player.isShiftKeyDown()) {
IBookletPage page = BookletUtils.findFirstPageForStack(blockStack);
2019-05-02 09:10:29 +02:00
if (page != null) {
2016-05-16 22:52:27 +02:00
String strg1 = page.getChapter().getLocalizedName();
2019-05-02 09:10:29 +02:00
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");
2021-02-26 22:15:48 +01:00
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);
2019-05-02 09:10:29 +02:00
} 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
}