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

133 lines
7.4 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().maxStackSize(1));
2015-08-28 21:17:09 +02:00
}
@Override
public ActionResultType onItemUse(ItemUseContext context) {
if (context.getPlayer().isSneaking()) {
BlockState state = context.getWorld().getBlockState(context.getPos());
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.getWorld().isRemote) {
2016-11-11 21:07:18 +01:00
forcedPage = page;
}
this.onItemRightClick(context.getWorld(), context.getPlayer(), context.getHand());
return ActionResultType.SUCCESS;
}
}
return ActionResultType.FAIL;
}
2016-02-01 17:49:55 +01:00
@Override
2021-02-26 22:15:48 +01:00
public ActionResult<ItemStack> onItemRightClick(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
2019-05-02 09:10:29 +02:00
if (!world.isRemote) {
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
}
2021-04-19 21:20:23 +02:00
return ActionResult.resultSuccess(player.getHeldItem(hand));
2016-02-01 17:49:55 +01:00
}
2015-12-01 19:48:09 +01:00
@Override
2021-04-19 21:20:23 +02:00
public void addInformation(ItemStack stack, @Nullable World playerIn, List<ITextComponent> tooltip, ITooltipFlag advanced) {
tooltip.add(new TranslationTextComponent("tooltip." + ActuallyAdditions.MODID + "." + this.getName().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++) {
2021-04-19 21:20:23 +02:00
tooltip.add(new TranslationTextComponent("tooltip." + ActuallyAdditions.MODID + "." + this.getName().getString() + ".sub." + i).mergeStyle(i == 4
? TextFormatting.GOLD
: TextFormatting.RESET).mergeStyle(i == 4
? 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.world.getBlockState(rayCast.getBlockPos());
2016-07-04 20:15:41 +02:00
Block block = state.getBlock();
2021-02-27 21:24:26 +01:00
if (block != null && !block.isAir(minecraft.world.getBlockState(rayCast.getBlockPos()), minecraft.world, rayCast.getBlockPos())) {
2016-07-04 20:15:41 +02:00
ItemStack blockStack = new ItemStack(block, 1, block.getMetaFromState(state));
2019-05-02 09:10:29 +02:00
int height = resolution.getScaledHeight() / 5 * 3;
if (player.isSneaking()) {
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()
2021-05-02 18:10:21 +02:00
: new ItemStack(ActuallyItems.ITEM_BOOKLET.get()), resolution.getScaledWidth() / 2 - 10, height + 41, 1F);
2019-05-02 09:10:29 +02:00
minecraft.fontRenderer.drawStringWithShadow(TextFormatting.YELLOW + "" + TextFormatting.ITALIC + strg1, resolution.getScaledWidth() / 2 - minecraft.fontRenderer.getStringWidth(strg1) / 2, height + 20, StringUtil.DECIMAL_COLOR_WHITE);
minecraft.fontRenderer.drawStringWithShadow(TextFormatting.YELLOW + "" + TextFormatting.ITALIC + strg2, resolution.getScaledWidth() / 2 - minecraft.fontRenderer.getStringWidth(strg2) / 2, height + 30, StringUtil.DECIMAL_COLOR_WHITE);
minecraft.fontRenderer.drawStringWithShadow(TextFormatting.GOLD + strg3, resolution.getScaledWidth() / 2 - minecraft.fontRenderer.getStringWidth(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.fontRenderer.drawStringWithShadow(strg1, resolution.getScaledWidth() / 2 - minecraft.fontRenderer.getStringWidth(strg1) / 2, height + 30, StringUtil.DECIMAL_COLOR_WHITE);
minecraft.fontRenderer.drawStringWithShadow(strg2, resolution.getScaledWidth() / 2 - minecraft.fontRenderer.getStringWidth(strg2) / 2, height + 50, StringUtil.DECIMAL_COLOR_WHITE);
minecraft.fontRenderer.drawStringWithShadow(strg3, resolution.getScaledWidth() / 2 - minecraft.fontRenderer.getStringWidth(strg3) / 2, height + 60, StringUtil.DECIMAL_COLOR_WHITE);
}
}
}
}
}
2015-08-28 21:17:09 +02:00
}