Java Code Examples for net.minecraft.entity.player.PlayerEntity#getHeldItem()
The following examples show how to use
net.minecraft.entity.player.PlayerEntity#getHeldItem() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MiningGadget.java From MiningGadgets with MIT License | 6 votes |
@Override public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) { ItemStack itemstack = player.getHeldItem(hand); // Only perform the shift action if (player.isShiftKeyDown()) return this.onItemShiftRightClick(world, player, hand, itemstack); if (world.isRemote) { float volume = MiningProperties.getVolume(itemstack); if (volume != 0.0f) player.playSound(OurSounds.LASER_START.getSound(), volume * 0.5f, 1f); return new ActionResult<>(ActionResultType.PASS, itemstack); } if (!canMine(itemstack)) return new ActionResult<>(ActionResultType.FAIL, itemstack); player.setActiveHand(hand); return new ActionResult<>(ActionResultType.PASS, itemstack); }
Example 2
Source File: RockItem.java From Survivalist with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand hand) { ItemStack stack = playerIn.getHeldItem(hand); if (!playerIn.abilities.isCreativeMode) { stack.grow(-1); } worldIn.playSound(null, playerIn.getPosX(), playerIn.getPosY(), playerIn.getPosZ(), SoundEvents.ENTITY_SNOWBALL_THROW, SoundCategory.NEUTRAL, 0.5F, 0.4F / (random.nextFloat() * 0.4F + 0.8F)); if (!worldIn.isRemote) { RockEntity entity = new RockEntity(worldIn, playerIn, this); entity.func_234612_a_(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 0.0F, 1.5F, 1.0F); worldIn.addEntity(entity); } //playerIn.addStat(StatList.getObjectUseStats(this)); return new ActionResult<>(ActionResultType.SUCCESS, stack); }
Example 3
Source File: ItemEnderPouch.java From EnderStorage with MIT License | 5 votes |
@Override public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) { ItemStack stack = player.getHeldItem(hand); if (player.isShiftKeyDown()) { return new ActionResult<>(ActionResultType.PASS, stack); } if (!world.isRemote) { Frequency frequency = Frequency.readFromStack(stack); EnderStorageManager.instance(world.isRemote).getStorage(frequency, EnderItemStorage.TYPE).openContainer((ServerPlayerEntity) player, new TranslationTextComponent(stack.getTranslationKey())); } return new ActionResult<>(ActionResultType.SUCCESS, stack); }
Example 4
Source File: TorchFireEventHandling.java From Survivalist with BSD 3-Clause "New" or "Revised" License | 5 votes |
@SubscribeEvent public void onAttackEntity(AttackEntityEvent ev) { if (!ConfigManager.SERVER.enableTorchFire.get()) return; if (!ev.getTarget().func_230279_az_() && !ev.getTarget().world.isRemote) { PlayerEntity player = ev.getPlayer(); ItemStack stack = player.getHeldItem(Hand.MAIN_HAND); if (stack.getCount() > 0 && stack.getItem() instanceof BlockItem) { BlockItem b = (BlockItem) stack.getItem(); Block bl = b.getBlock(); if (bl == Blocks.TORCH) { ev.getTarget().setFire(2); if (!ev.getPlayer().isCreative() && rnd.nextFloat() > 0.25) { stack.grow(-1); if (stack.getCount() <= 0) { player.inventory.setInventorySlotContents(player.inventory.currentItem, ItemStack.EMPTY); } } } } } }
Example 5
Source File: RenderMiningLaser.java From MiningGadgets with MIT License | 4 votes |
private static void drawLasers(RenderWorldLastEvent event, Vec3d from, RayTraceResult trace, double xOffset, double yOffset, double zOffset, float r, float g, float b, float thickness, PlayerEntity player, float ticks, float speedModifier) { Hand activeHand; if (player.getHeldItemMainhand().getItem() instanceof MiningGadget) { activeHand = Hand.MAIN_HAND; } else if (player.getHeldItemOffhand().getItem() instanceof MiningGadget) { activeHand = Hand.OFF_HAND; } else { return; } ItemStack stack = player.getHeldItem(activeHand); double distance = from.subtract(trace.getHitVec()).length(); long gameTime = player.world.getGameTime(); double v = gameTime * speedModifier; float additiveThickness = (thickness * 3.5f) * calculateLaserFlickerModifier(gameTime); BufferBuilder wr = Tessellator.getInstance().getBuffer(); Vec3d view = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView(); MatrixStack matrix = event.getMatrixStack(); matrix.translate(view.getX(), view.getY(), view.getZ()); if( trace.getType() == RayTraceResult.Type.MISS ) matrix.translate(-from.x, -from.y, -from.z); RenderSystem.pushMatrix(); RenderSystem.multMatrix(matrix.getLast().getMatrix()); RenderSystem.enableColorMaterial(); // This makes it so we don't clip into the world, we're effectively drawing on it RenderSystem.disableDepthTest(); RenderSystem.enableBlend(); //This makes it so multiplayer doesn't matter which side the player is standing on to see someone elses laser RenderSystem.disableCull(); RenderSystem.enableTexture(); RenderSystem.rotatef(MathHelper.lerp(ticks, -player.rotationYaw, -player.prevRotationYaw), 0, 1, 0); RenderSystem.rotatef(MathHelper.lerp(ticks, player.rotationPitch, player.prevRotationPitch), 1, 0, 0); // additive laser beam RenderSystem.blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); RenderSystem.color4f(r, g, b, 0.7f); Minecraft.getInstance().getTextureManager().bindTexture(laserBeamGlow); drawBeam(xOffset, yOffset, zOffset, additiveThickness, activeHand, distance, wr, 0.5, 1, ticks); // main laser, colored part RenderSystem.blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); RenderSystem.color4f(r, g, b, 1.0f); Minecraft.getInstance().getTextureManager().bindTexture(laserBeam2); drawBeam(xOffset, yOffset, zOffset, thickness, activeHand, distance, wr, v, v + distance * 1.5, ticks); // white core RenderSystem.color4f(MiningProperties.getColor(stack, MiningProperties.COLOR_RED_INNER) / 255f, MiningProperties.getColor(stack, MiningProperties.COLOR_GREEN_INNER) / 255f, MiningProperties.getColor(stack, MiningProperties.COLOR_BLUE_INNER) / 255f, 1.0f); Minecraft.getInstance().getTextureManager().bindTexture(laserBeam); drawBeam(xOffset, yOffset, zOffset, thickness / 2, activeHand, distance, wr, v, v + distance * 1.5, ticks); RenderSystem.enableDepthTest(); RenderSystem.enableCull(); RenderSystem.popMatrix(); }
Example 6
Source File: RenderMiningLaser2.java From MiningGadgets with MIT License | 4 votes |
private static void drawLasers(RenderWorldLastEvent event, Vec3d from, RayTraceResult trace, double xOffset, double yOffset, double zOffset, float r, float g, float b, float thickness, PlayerEntity player, float ticks, float speedModifier) { Hand activeHand; if (player.getHeldItemMainhand().getItem() instanceof MiningGadget) { activeHand = Hand.MAIN_HAND; } else if (player.getHeldItemOffhand().getItem() instanceof MiningGadget) { activeHand = Hand.OFF_HAND; } else { return; } IVertexBuilder builder; ItemStack stack = player.getHeldItem(activeHand); double distance = from.subtract(trace.getHitVec()).length(); long gameTime = player.world.getGameTime(); double v = gameTime * speedModifier; float additiveThickness = (thickness * 3.5f) * calculateLaserFlickerModifier(gameTime); float beam2r = MiningProperties.getColor(stack, MiningProperties.COLOR_RED_INNER) / 255f; float beam2g = MiningProperties.getColor(stack, MiningProperties.COLOR_GREEN_INNER) / 255f; float beam2b =MiningProperties.getColor(stack, MiningProperties.COLOR_BLUE_INNER) / 255f; Vec3d view = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView(); IRenderTypeBuffer.Impl buffer = Minecraft.getInstance().getRenderTypeBuffers().getBufferSource(); MatrixStack matrix = event.getMatrixStack(); matrix.push(); matrix.translate(-view.getX(), -view.getY(), -view.getZ()); matrix.translate(from.x, from.y, from.z); matrix.rotate(Vector3f.YP.rotationDegrees(MathHelper.lerp(ticks, -player.rotationYaw, -player.prevRotationYaw))); matrix.rotate(Vector3f.XP.rotationDegrees(MathHelper.lerp(ticks, player.rotationPitch, player.prevRotationPitch))); MatrixStack.Entry matrixstack$entry = matrix.getLast(); Matrix3f matrixNormal = matrixstack$entry.getNormal(); Matrix4f positionMatrix = matrixstack$entry.getMatrix(); //additive laser beam builder = buffer.getBuffer(MyRenderType.LASER_MAIN_ADDITIVE); drawBeam(xOffset, yOffset, zOffset, builder, positionMatrix, matrixNormal, additiveThickness, activeHand, distance, 0.5, 1, ticks, r,g,b,0.7f); //main laser, colored part builder = buffer.getBuffer(MyRenderType.LASER_MAIN_BEAM); drawBeam(xOffset, yOffset, zOffset, builder, positionMatrix, matrixNormal, thickness, activeHand, distance, v, v + distance * 1.5, ticks, r,g,b,1f); //core builder = buffer.getBuffer(MyRenderType.LASER_MAIN_CORE); drawBeam(xOffset, yOffset, zOffset, builder, positionMatrix, matrixNormal, thickness/2, activeHand, distance, v, v + distance * 1.5, ticks, beam2r,beam2g,beam2b,1f); matrix.pop(); RenderSystem.disableDepthTest(); buffer.finish(); }
Example 7
Source File: ChoppingBlock.java From Survivalist with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Deprecated @Override public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult blockRayTraceResult) { ItemStack heldItem = player.getHeldItem(hand); if (worldIn.isRemote) { return (heldItem.getCount() <= 0) || ChoppingRecipe.getRecipe(worldIn, heldItem).isPresent() ? ActionResultType.SUCCESS : ActionResultType.PASS; } TileEntity tileEntity = worldIn.getTileEntity(pos); if (!(tileEntity instanceof ChoppingBlockTileEntity) || player.isSneaking()) return ActionResultType.PASS; ChoppingBlockTileEntity chopper = (ChoppingBlockTileEntity) tileEntity; if (heldItem.getCount() <= 0) { ItemStack extracted = chopper.getSlotInventory().extractItem(0, 1, false); if (extracted.getCount() > 0) { ItemHandlerHelper.giveItemToPlayer(player, extracted); return ActionResultType.SUCCESS; } return ActionResultType.PASS; } if (ChoppingRecipe.getRecipe(worldIn, heldItem) .isPresent()) { ItemStack remaining = chopper.getSlotInventory().insertItem(0, heldItem, false); if (!player.isCreative()) { if (remaining.getCount() > 0) { player.setHeldItem(hand, remaining); } else { player.setHeldItem(hand, ItemStack.EMPTY); } } return remaining.getCount() < heldItem.getCount() ? ActionResultType.SUCCESS : ActionResultType.PASS; } return ActionResultType.PASS; }
Example 8
Source File: ChoppingBlock.java From Survivalist with BSD 3-Clause "New" or "Revised" License | 4 votes |
private boolean interceptClick(World worldIn, BlockPos pos, BlockState state, PlayerEntity playerIn) { TileEntity tileentity = worldIn.getTileEntity(pos); if (!(tileentity instanceof ChoppingBlockTileEntity)) return false; ChoppingBlockTileEntity chopper = (ChoppingBlockTileEntity) tileentity; if (chopper.getSlotInventory().getStackInSlot(0).getCount() <= 0) return false; if (worldIn.isRemote) return true; ItemStack heldItem = playerIn.getHeldItem(Hand.MAIN_HAND); int harvestLevel = heldItem.getItem().getHarvestLevel(heldItem, ToolType.AXE, playerIn, null); ActionResult<ItemStack> result = chopper.chop(playerIn, harvestLevel, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, heldItem)); if (result.getType() == ActionResultType.SUCCESS) { if (worldIn.rand.nextFloat() < ConfigManager.SERVER.choppingDegradeChance.get()) { worldIn.setBlockState(pos, breaksInto.get()); } if (ConfigManager.SERVER.choppingExhaustion.get() > 0) playerIn.addExhaustion(ConfigManager.SERVER.choppingExhaustion.get().floatValue()); if (heldItem.getCount() > 0 && !playerIn.abilities.isCreativeMode) { heldItem.damageItem(1, playerIn, (stack) -> { stack.sendBreakAnimation(Hand.MAIN_HAND); }); } } if (result.getType() != ActionResultType.PASS) { ((ServerWorld) worldIn).spawnParticle(new ItemParticleData(ParticleTypes.ITEM, result.getResult()), pos.getX() + 0.5, pos.getY() + 0.6, pos.getZ() + 0.5, 8, 0, 0.1, 0, 0.02); } return true; }