From 147c32d657c5bdaf65397abce80638e912af2e8d Mon Sep 17 00:00:00 2001 From: Vasco Lavos Date: Wed, 26 Oct 2016 23:33:45 +0100 Subject: [PATCH] WIP astrolabe --- .../handler/ItemsRemainingRenderHandler.java | 34 ++- .../botania/common/item/ItemAstrolabe.java | 247 ++++++++++++++++++ .../vazkii/botania/common/item/ModItems.java | 4 +- .../botania/common/lib/LibItemNames.java | 3 +- .../assets/botania/models/item/astrolabe.json | 6 + 5 files changed, 282 insertions(+), 12 deletions(-) create mode 100644 src/main/java/vazkii/botania/common/item/ItemAstrolabe.java create mode 100644 src/main/resources/assets/botania/models/item/astrolabe.json diff --git a/src/main/java/vazkii/botania/client/core/handler/ItemsRemainingRenderHandler.java b/src/main/java/vazkii/botania/client/core/handler/ItemsRemainingRenderHandler.java index 1369dec76..bcb9e3418 100644 --- a/src/main/java/vazkii/botania/client/core/handler/ItemsRemainingRenderHandler.java +++ b/src/main/java/vazkii/botania/client/core/handler/ItemsRemainingRenderHandler.java @@ -30,6 +30,7 @@ public final class ItemsRemainingRenderHandler { private static final int leaveTicks = 20; private static ItemStack stack; + private static String customString; private static int ticks, count; @SideOnly(Side.CLIENT) @@ -60,17 +61,21 @@ public final class ItemsRemainingRenderHandler { GlStateManager.color(1F, 1F, 1F, 1F); GlStateManager.enableBlend(); - String text = TextFormatting.GREEN + stack.getDisplayName(); - if(count >= 0) { - int max = stack.getMaxStackSize(); - int stacks = count / max; - int rem = count % max; + String text; - if(stacks == 0) - text = "" + count; - else text = count + " (" + TextFormatting.AQUA + stacks + TextFormatting.RESET + "*" + TextFormatting.GRAY + max + TextFormatting.RESET + "+" + TextFormatting.YELLOW + rem + TextFormatting.RESET + ")"; - } else if(count == -1) - text = "\u221E"; + if(customString == null) { + text = TextFormatting.GREEN + stack.getDisplayName(); + if(count >= 0) { + int max = stack.getMaxStackSize(); + int stacks = count / max; + int rem = count % max; + + if(stacks == 0) + text = "" + count; + else text = count + " (" + TextFormatting.AQUA + stacks + TextFormatting.RESET + "*" + TextFormatting.GRAY + max + TextFormatting.RESET + "+" + TextFormatting.YELLOW + rem + TextFormatting.RESET + ")"; + } else if(count == -1) + text = "\u221E"; + } else text = customString; int color = 0x00FFFFFF | (int) (alpha * 0xFF) << 24; mc.fontRendererObj.drawStringWithShadow(text, x + 20, y + 6, color); @@ -86,9 +91,18 @@ public final class ItemsRemainingRenderHandler { --ticks; } + public static void set(ItemStack stack, String str) { + set(stack, 0, str); + } + public static void set(ItemStack stack, int count) { + set(stack, count, null); + } + + public static void set(ItemStack stack, int count, String str) { ItemsRemainingRenderHandler.stack = stack; ItemsRemainingRenderHandler.count = count; + ItemsRemainingRenderHandler.customString = str; ticks = stack == null ? 0 : maxTicks; } diff --git a/src/main/java/vazkii/botania/common/item/ItemAstrolabe.java b/src/main/java/vazkii/botania/common/item/ItemAstrolabe.java new file mode 100644 index 000000000..3d7d94995 --- /dev/null +++ b/src/main/java/vazkii/botania/common/item/ItemAstrolabe.java @@ -0,0 +1,247 @@ +/** + * This class was created by . It's distributed as + * part of the Botania Mod. Get the Source Code in github: + * https://github.com/Vazkii/Botania + * + * Botania is Open Source and distributed under the + * Botania License: http://botaniamod.net/license.php + * + * File Created @ [26/10/2016, 18:23:11 (GMT)] + */ +package vazkii.botania.common.item; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; +import net.minecraft.client.resources.I18n; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; +import net.minecraft.init.SoundEvents; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ActionResult; +import net.minecraft.util.EnumActionResult; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; +import net.minecraft.util.SoundCategory; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.RayTraceResult; +import net.minecraft.world.World; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; +import vazkii.botania.api.item.IBlockProvider; +import vazkii.botania.client.core.handler.ItemsRemainingRenderHandler; +import vazkii.botania.common.core.helper.ItemNBTHelper; +import vazkii.botania.common.item.equipment.tool.ToolCommons; +import vazkii.botania.common.lib.LibItemNames; + +public class ItemAstrolabe extends ItemMod { + + private static final String TAG_BLOCK_NAME = "blockName"; + private static final String TAG_BLOCK_META = "blockMeta"; + private static final String TAG_SIZE = "size"; + + public ItemAstrolabe() { + super(LibItemNames.ASTROLABE); + setMaxStackSize(1); + } + + @Override + public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { + IBlockState state = worldIn.getBlockState(pos); + Block block = state.getBlock(); + int meta = block.getMetaFromState(state); + + if(playerIn.isSneaking()) { + if(setBlock(stack, block, meta)) + return EnumActionResult.SUCCESS; + } else placeAllBlocks(stack, playerIn); + + return EnumActionResult.PASS; + } + + @Override + public ActionResult onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) { + if(playerIn.isSneaking()) { + int size = getSize(itemStackIn); + int newSize = size == 11 ? 3 : size + 2; + setSize(itemStackIn, newSize); + ItemsRemainingRenderHandler.set(itemStackIn, newSize + "x" + newSize); + worldIn.playSound(playerIn, playerIn.getPosition(), SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 0.5F, 1F); + + return new ActionResult(EnumActionResult.SUCCESS, itemStackIn); + } + + return new ActionResult(EnumActionResult.PASS, itemStackIn); + } + + public void placeAllBlocks(ItemStack stack, EntityPlayer player) { + BlockPos[] blocksToPlace = getBlocksToPlace(stack, player); + if(!hasBlocks(stack, player, blocksToPlace)) + return; + + ItemStack stackToPlace = new ItemStack(getBlock(stack), 1, getBlockMeta(stack)); + for(BlockPos coords : blocksToPlace) + placeBlockAndConsume(player, stack, stackToPlace, coords); + +// player.worldObj.playSoundAtEntity(player, "thaumcraft:wand", 1F, 1F); TODO + } + + private void placeBlockAndConsume(EntityPlayer player, ItemStack requestor, ItemStack blockToPlace, BlockPos coords) { + if(blockToPlace.getItem() == null) + return; + + Block block = Block.getBlockFromItem(blockToPlace.getItem()); + int meta = blockToPlace.getItemDamage(); + player.worldObj.setBlockState(coords, block.getStateFromMeta(meta), 1 | 2); + + if (player.capabilities.isCreativeMode) + return; + + List stacksToCheck = new ArrayList(); + for(int i = 0; i < player.inventory.getSizeInventory(); i++) { + ItemStack stackInSlot = player.inventory.getStackInSlot(i); + if(stackInSlot != null && stackInSlot.getItem() == blockToPlace.getItem() && stackInSlot.getItemDamage() == blockToPlace.getItemDamage()) { + stackInSlot.stackSize--; + if(stackInSlot.stackSize == 0) + player.inventory.setInventorySlotContents(i, null); + + return; + } + + if (stackInSlot != null && stackInSlot.getItem() instanceof IBlockProvider) + stacksToCheck.add(stackInSlot); + } + + for(ItemStack providerStack : stacksToCheck) { + IBlockProvider prov = (IBlockProvider) providerStack.getItem(); + + if(prov.provideBlock(player, requestor, providerStack, block, meta, false)) { + prov.provideBlock(player, requestor, providerStack, block, meta, true); + return; + } + } + } + + public static boolean hasBlocks(ItemStack stack, EntityPlayer player, BlockPos[] blocks) { + if (player.capabilities.isCreativeMode) + return true; + + + Block block = getBlock(stack); + int meta = getBlockMeta(stack); + ItemStack reqStack = new ItemStack(block, 1, meta); + + int required = blocks.length; + int current = 0; + List stacksToCheck = new ArrayList(); + for (int i = 0; i < player.inventory.getSizeInventory(); i++) { + ItemStack stackInSlot = player.inventory.getStackInSlot(i); + if (stackInSlot != null && stackInSlot.getItem() == reqStack.getItem() && stackInSlot.getItemDamage() == reqStack.getItemDamage()) { + current += stackInSlot.stackSize; + if (current >= required) + return true; + } + if(stackInSlot != null && stackInSlot.getItem() instanceof IBlockProvider) + stacksToCheck.add(stackInSlot); + } + + for(ItemStack providerStack : stacksToCheck) { + IBlockProvider prov = (IBlockProvider) providerStack.getItem(); + current += prov.getBlockCount(player, stack, providerStack, block, meta); + + if(current >= required) + return true; + } + + return false; + } + + public static BlockPos[] getBlocksToPlace(ItemStack stack, EntityPlayer player) { + List coords = new ArrayList(); + RayTraceResult pos = ToolCommons.raytraceFromEntity(player.worldObj, player, true, 5); + if(pos != null) { + BlockPos bpos = pos.getBlockPos(); + IBlockState state = player.worldObj.getBlockState(bpos); + Block block = state.getBlock(); + if(block != null && block.isReplaceable(player.worldObj, bpos)) + bpos = bpos.down();; + + EnumFacing dir = pos.sideHit; + int rotation = MathHelper.floor_double(player.rotationYaw * 4F / 360F + 0.5D) & 3; + int range = (getSize(stack) ^ 1) / 2; + + boolean topOrBottom = dir == EnumFacing.UP || dir == EnumFacing.DOWN; + + int xOff = !(dir == EnumFacing.WEST || dir == EnumFacing.EAST) ? topOrBottom ? player.rotationPitch > 75 || (rotation & 1) == 0 ? range : 0 : range : 0; + int yOff = topOrBottom ? player.rotationPitch > 75 ? 0 : range : range; + int zOff = !(dir == EnumFacing.SOUTH || dir == EnumFacing.NORTH) ? topOrBottom ? player.rotationPitch > 75 || (rotation & 1) == 1 ? range : 0 : range : 0; + + for(int x = -xOff; x < xOff + 1; x++) + for(int y = 0; y < yOff * 2 + 1; y++) { + for(int z = -zOff; z < zOff + 1; z++) { + int xp = bpos.getX() + x + dir.getFrontOffsetX(); + int yp = bpos.getY() + y + dir.getFrontOffsetY(); + int zp = bpos.getZ() + z + dir.getFrontOffsetZ(); + + BlockPos newPos = new BlockPos(xp, yp, zp); + IBlockState state1 = player.worldObj.getBlockState(newPos); + Block block1 = state1.getBlock(); + if(block1 == null || block1.isAir(state1, player.worldObj, newPos) || block1.isReplaceable(player.worldObj, newPos)) + coords.add(new BlockPos(xp, yp, zp)); + } + } + + } + + return coords.toArray(new BlockPos[coords.size()]); + } + + private boolean setBlock(ItemStack stack, Block block, int meta) { + if(Item.getItemFromBlock(block) != null) { + ItemNBTHelper.setString(stack, TAG_BLOCK_NAME, Block.REGISTRY.getNameForObject(block).toString()); + ItemNBTHelper.setInt(stack, TAG_BLOCK_META, meta); + return true; + } + return false; + } + + private static void setSize(ItemStack stack, int size) { + ItemNBTHelper.setInt(stack, TAG_SIZE, size | 1); + } + + public static int getSize(ItemStack stack) { + return ItemNBTHelper.getInt(stack, TAG_SIZE, 3) | 1; + } + + public static Block getBlock(ItemStack stack) { + Block block = Block.getBlockFromName(getBlockName(stack)); + if(block == null) + return Blocks.AIR; + + return block; + } + + public static String getBlockName(ItemStack stack) { + return ItemNBTHelper.getString(stack, TAG_BLOCK_NAME, ""); + } + + public static int getBlockMeta(ItemStack stack) { + return ItemNBTHelper.getInt(stack, TAG_BLOCK_META, 0); + } + + @Override + @SideOnly(Side.CLIENT) + public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4) { + Block block = getBlock(par1ItemStack); + int size = getSize(par1ItemStack); + + par3List.add(size + " x " + size); + if (block != null && block != Blocks.AIR) + par3List.add(new ItemStack(block, 1, getBlockMeta(par1ItemStack)).getDisplayName()); + } + +} diff --git a/src/main/java/vazkii/botania/common/item/ModItems.java b/src/main/java/vazkii/botania/common/item/ModItems.java index 95d0f80ad..284a2293b 100644 --- a/src/main/java/vazkii/botania/common/item/ModItems.java +++ b/src/main/java/vazkii/botania/common/item/ModItems.java @@ -277,6 +277,7 @@ public final class ModItems { public static Item cloudPendant; public static Item superCloudPendant; public static Item thirdEye; + public static Item astrolabe; public static void init() { lexicon = new ItemLexicon(); @@ -430,7 +431,8 @@ public final class ModItems { cloudPendant = new ItemCloudPendant(); superCloudPendant = new ItemSuperCloudPendant(); thirdEye = new ItemThirdEye(); - + astrolabe = new ItemAstrolabe(); + OreDictionary.registerOre(LibOreDict.LEXICON, lexicon); for(int i = 0; i < 16; i++) { OreDictionary.registerOre(LibOreDict.PETAL[i], new ItemStack(petal, 1, i)); diff --git a/src/main/java/vazkii/botania/common/lib/LibItemNames.java b/src/main/java/vazkii/botania/common/lib/LibItemNames.java index e5bfd46ac..513481750 100644 --- a/src/main/java/vazkii/botania/common/lib/LibItemNames.java +++ b/src/main/java/vazkii/botania/common/lib/LibItemNames.java @@ -165,7 +165,8 @@ public final class LibItemNames { public static final String CLOUD_PENDANT = "cloudPendant"; public static final String SUPER_CLOUD_PENDANT = "superCloudPendant"; public static final String THIRD_EYE = "thirdEye"; - + public static final String ASTROLABE = "astrolabe"; + public static final String[] LENS_NAMES = new String[] { "lensNormal", "lensSpeed", diff --git a/src/main/resources/assets/botania/models/item/astrolabe.json b/src/main/resources/assets/botania/models/item/astrolabe.json new file mode 100644 index 000000000..75a5d0663 --- /dev/null +++ b/src/main/resources/assets/botania/models/item/astrolabe.json @@ -0,0 +1,6 @@ +{ + "parent": "item/generated", + "textures": { + "layer0": "botania:items/astrolabe" + } +}