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

124 lines
7 KiB
Java
Raw Normal View History

2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.items;
2015-08-28 21:17:09 +02:00
2019-05-02 09:10:29 +02:00
import java.util.List;
import javax.annotation.Nullable;
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;
2016-07-04 20:15:41 +02:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
2017-06-17 00:48:49 +02:00
import net.minecraft.client.util.ITooltipFlag;
2015-08-28 21:17:09 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.TextFormatting;
2015-08-28 21:17:09 +02:00
import net.minecraft.world.World;
2016-11-11 21:07:18 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-08-28 21:17:09 +02:00
2019-05-02 09:10:29 +02:00
public class ItemBooklet extends ItemBase implements IHudDisplay {
2016-11-11 21:07:18 +01:00
@SideOnly(Side.CLIENT)
public static IBookletPage forcedPage;
2015-08-28 21:17:09 +02:00
2019-05-02 09:10:29 +02:00
public ItemBooklet(String name) {
super(name);
this.setMaxStackSize(1);
2015-08-28 21:17:09 +02:00
this.setMaxDamage(0);
}
@Override
2019-05-02 09:10:29 +02:00
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing face, float hitX, float hitY, float hitZ) {
if (player.isSneaking()) {
2016-07-04 20:15:41 +02:00
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
2016-11-11 21:07:18 +01:00
ItemStack blockStack = new ItemStack(block, 1, block.damageDropped(state));
IBookletPage page = BookletUtils.findFirstPageForStack(blockStack);
2019-05-02 09:10:29 +02:00
if (page != null) {
if (world.isRemote) {
2016-11-11 21:07:18 +01:00
forcedPage = page;
}
2016-11-19 21:11:17 +01:00
this.onItemRightClick(world, player, hand);
2016-05-16 22:52:27 +02:00
return EnumActionResult.SUCCESS;
}
}
2016-03-18 23:47:22 +01:00
return EnumActionResult.FAIL;
}
2016-02-01 17:49:55 +01:00
@Override
2019-05-02 09:10:29 +02:00
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
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
}
2019-02-27 19:53:05 +01:00
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
2016-02-01 17:49:55 +01:00
}
2015-12-01 19:48:09 +01:00
@Override
2019-05-02 09:10:29 +02:00
public void addInformation(ItemStack stack, @Nullable World playerIn, List<String> tooltip, ITooltipFlag advanced) {
tooltip.add(StringUtil.localize("tooltip." + ActuallyAdditions.MODID + "." + this.getBaseName() + ".desc"));
2019-05-02 09:10:29 +02:00
for (int i = 1; i <= 4; i++) {
String format = i == 4 ? TextFormatting.GOLD.toString() + TextFormatting.ITALIC : TextFormatting.RESET.toString();
tooltip.add(format + StringUtil.localize("tooltip." + ActuallyAdditions.MODID + "." + this.getBaseName() + ".sub." + i));
}
2015-12-01 19:48:09 +01:00
}
2015-08-28 21:17:09 +02:00
@Override
2019-05-02 09:10:29 +02:00
public EnumRarity getRarity(ItemStack stack) {
return EnumRarity.EPIC;
2015-08-28 21:17:09 +02:00
}
@Override
2017-02-18 00:54:58 +01:00
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public void displayHud(Minecraft minecraft, EntityPlayer player, ItemStack stack, RayTraceResult posHit, ScaledResolution resolution) {
if (posHit != null && posHit.getBlockPos() != null) {
2016-11-26 21:32:27 +01:00
IBlockState state = minecraft.world.getBlockState(posHit.getBlockPos());
2016-07-04 20:15:41 +02:00
Block block = state.getBlock();
2019-05-02 09:10:29 +02:00
if (block != null && !block.isAir(minecraft.world.getBlockState(posHit.getBlockPos()), minecraft.world, posHit.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");
AssetUtil.renderStackToGui(StackUtil.isValid(page.getChapter().getDisplayItemStack()) ? page.getChapter().getDisplayItemStack() : new ItemStack(InitItems.itemBooklet), resolution.getScaledWidth() / 2 - 10, height + 41, 1F);
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
}