2015-08-29 14:33:25 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("ItemLeafBlower.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-03-29 15:29:05 +02:00
|
|
|
|
2016-06-06 21:27:09 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.misc.IDisplayStandItem;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.sounds.SoundEvents;
|
|
|
|
import net.minecraft.sounds.SoundSource;
|
|
|
|
import net.minecraft.util.Mth;
|
|
|
|
import net.minecraft.world.InteractionHand;
|
|
|
|
import net.minecraft.world.InteractionResultHolder;
|
|
|
|
import net.minecraft.world.entity.LivingEntity;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.item.UseAnim;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.Block;
|
|
|
|
import net.minecraft.world.level.block.Blocks;
|
|
|
|
import net.minecraft.world.level.block.BushBlock;
|
|
|
|
import net.minecraft.world.level.block.LeavesBlock;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2021-05-04 19:50:50 +02:00
|
|
|
import net.minecraftforge.common.IForgeShearable;
|
2015-03-29 15:29:05 +02:00
|
|
|
|
2022-06-18 17:53:23 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2021-02-26 22:15:48 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class ItemLeafBlower extends ItemBase implements IDisplayStandItem {
|
2015-03-29 15:29:05 +02:00
|
|
|
|
|
|
|
private final boolean isAdvanced;
|
|
|
|
|
2021-03-01 21:23:52 +01:00
|
|
|
public ItemLeafBlower(boolean isAdvanced) {
|
2021-08-22 17:09:06 +02:00
|
|
|
super(ActuallyItems.defaultProps().stacksTo(1));
|
2015-03-29 15:29:05 +02:00
|
|
|
this.isAdvanced = isAdvanced;
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:53:23 +02:00
|
|
|
@Nonnull
|
2015-10-03 10:19:40 +02:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public InteractionResultHolder<ItemStack> use(@Nonnull Level world, Player player, @Nonnull InteractionHand hand) {
|
2021-08-22 17:09:06 +02:00
|
|
|
player.startUsingItem(hand);
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResultHolder.success(player.getItemInHand(hand));
|
2015-10-03 10:19:40 +02:00
|
|
|
}
|
|
|
|
|
2022-06-18 17:53:23 +02:00
|
|
|
@Nonnull
|
2015-10-03 10:19:40 +02:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public UseAnim getUseAnimation(@Nonnull ItemStack stack) {
|
|
|
|
return UseAnim.BOW;
|
2015-10-03 10:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-18 17:53:23 +02:00
|
|
|
public int getUseDuration(@Nonnull ItemStack stack) {
|
2015-10-03 10:19:40 +02:00
|
|
|
return Integer.MAX_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-03 01:20:53 +01:00
|
|
|
public void onUseTick(Level level, LivingEntity livingEntity, ItemStack stack, int remainingUseDuration) {
|
|
|
|
this.doUpdate(level, Mth.floor(livingEntity.getX()), Mth.floor(livingEntity.getY()), Mth.floor(livingEntity.getZ()), remainingUseDuration, stack);
|
2016-06-06 21:27:09 +02:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
private boolean doUpdate(Level world, int x, int y, int z, int time, ItemStack stack) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!world.isClientSide) {
|
2021-05-04 19:50:50 +02:00
|
|
|
if (time <= this.getUseDuration(stack) && (this.isAdvanced || time % 3 == 0)) {
|
2015-07-17 07:59:06 +02:00
|
|
|
//Breaks the Blocks
|
2016-06-06 21:27:09 +02:00
|
|
|
boolean broke = this.breakStuff(world, x, y, z);
|
2015-07-17 07:59:06 +02:00
|
|
|
//Plays a Minecart sounds (It really sounds like a Leaf Blower!)
|
2024-03-02 21:23:08 +01:00
|
|
|
world.playSound(null, x, y, z, SoundEvents.MINECART_RIDING, SoundSource.PLAYERS, 0.2F, 0.001F);
|
2016-06-06 21:27:09 +02:00
|
|
|
return broke;
|
2015-03-29 15:29:05 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-06 21:27:09 +02:00
|
|
|
return false;
|
2015-03-29 15:29:05 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 07:59:06 +02:00
|
|
|
/**
|
|
|
|
* Breaks (harvests) Grass and Leaves around
|
2015-10-02 16:48:01 +02:00
|
|
|
*
|
2015-07-17 07:59:06 +02:00
|
|
|
* @param world The World
|
2015-10-02 16:48:01 +02:00
|
|
|
* @param x The X Position of the Player
|
|
|
|
* @param y The Y Position of the Player
|
|
|
|
* @param z The Z Position of the Player
|
2015-07-17 07:59:06 +02:00
|
|
|
*/
|
2024-03-02 21:23:08 +01:00
|
|
|
public boolean breakStuff(Level world, int x, int y, int z) {
|
2019-02-27 19:53:05 +01:00
|
|
|
ArrayList<BlockPos> breakPositions = new ArrayList<>();
|
2015-09-29 16:16:53 +02:00
|
|
|
|
2015-11-28 19:02:01 +01:00
|
|
|
int rangeSides = 5;
|
|
|
|
int rangeUp = 1;
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int reachX = -rangeSides; reachX < rangeSides + 1; reachX++) {
|
|
|
|
for (int reachZ = -rangeSides; reachZ < rangeSides + 1; reachZ++) {
|
2021-02-26 22:15:48 +01:00
|
|
|
for (int reachY = this.isAdvanced
|
|
|
|
? -rangeSides
|
|
|
|
: -rangeUp; reachY < (this.isAdvanced
|
|
|
|
? rangeSides
|
|
|
|
: rangeUp) + 1; reachY++) {
|
2015-07-17 07:59:06 +02:00
|
|
|
//The current Block to break
|
2019-05-02 09:10:29 +02:00
|
|
|
BlockPos pos = new BlockPos(x + reachX, y + reachY, z + reachZ);
|
2016-07-04 20:15:41 +02:00
|
|
|
Block block = world.getBlockState(pos).getBlock();
|
2016-12-06 13:40:51 +01:00
|
|
|
|
2023-01-14 23:02:33 +01:00
|
|
|
if ((block instanceof BushBlock || block instanceof IForgeShearable) && (this.isAdvanced || !(block instanceof LeavesBlock))) {
|
2016-01-07 21:41:28 +01:00
|
|
|
breakPositions.add(pos);
|
2015-09-29 16:16:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 19:22:03 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!breakPositions.isEmpty()) {
|
2015-09-29 16:16:53 +02:00
|
|
|
Collections.shuffle(breakPositions);
|
2015-04-24 19:22:03 +02:00
|
|
|
|
2022-06-18 17:53:23 +02:00
|
|
|
BlockPos pos = breakPositions.get(0);
|
|
|
|
BlockState theState = world.getBlockState(pos);
|
2015-09-29 16:16:53 +02:00
|
|
|
|
2022-06-18 17:53:23 +02:00
|
|
|
world.destroyBlock(pos, true);
|
2021-05-04 19:50:50 +02:00
|
|
|
// theState.getBlock().dropBlockAsItem(world, theCoord, theState, 0);
|
2015-09-29 16:16:53 +02:00
|
|
|
//Plays the Breaking Sound
|
2022-06-18 17:53:23 +02:00
|
|
|
world.levelEvent(2001, pos, Block.getId(theState));
|
2015-09-29 16:16:53 +02:00
|
|
|
|
2016-01-23 23:29:19 +01:00
|
|
|
//Deletes the Block
|
2022-06-18 17:53:23 +02:00
|
|
|
world.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
|
2016-01-23 23:29:19 +01:00
|
|
|
|
2016-06-06 21:27:09 +02:00
|
|
|
return true;
|
2015-03-29 15:29:05 +02:00
|
|
|
}
|
2016-06-06 21:27:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public boolean update(ItemStack stack, BlockEntity tile, int elapsedTicks) {
|
2021-08-22 17:09:06 +02:00
|
|
|
return this.doUpdate(tile.getLevel(), tile.getBlockPos().getX(), tile.getBlockPos().getY(), tile.getBlockPos().getZ(), elapsedTicks, stack);
|
2016-06-06 21:27:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public int getUsePerTick(ItemStack stack, BlockEntity tile, int elapsedTicks) {
|
2016-06-06 21:27:09 +02:00
|
|
|
return 60;
|
2015-03-29 15:29:05 +02:00
|
|
|
}
|
|
|
|
}
|