Jackhammers should have correct speed. Closes #2223, #2217

This commit is contained in:
drcrazy 2020-09-16 03:17:43 +03:00
parent b1fee2e741
commit 0b13213489
4 changed files with 57 additions and 25 deletions

View file

@ -27,6 +27,7 @@ package techreborn.items.tool;
import net.fabricmc.fabric.api.tool.attribute.v1.DynamicAttributeTool;
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
import net.minecraft.block.BlockState;
import net.minecraft.block.Material;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.LivingEntity;
@ -64,14 +65,25 @@ public class JackhammerItem extends PickaxeItem implements EnergyHolder, ItemDur
// PickaxeItem
@Override
public float getMiningSpeedMultiplier(ItemStack stack, BlockState state) {
if (state.getMaterial() == Material.STONE && Energy.of(stack).getEnergy() >= cost) {
return miningSpeed;
} else {
return 0.5F;
}
}
/*
Fabric API doesn't allow to have mining speed less then the one from vanilla ToolMaterials
@Override
public float getMiningSpeedMultiplier(Tag<Item> tag, BlockState state, ItemStack stack, LivingEntity user) {
if (tag.equals(FabricToolTags.PICKAXES) && stack.getItem().isEffectiveOn(state)) {
if (Energy.of(stack).getEnergy() >= cost) {
return miningSpeed;
}
}
return 0;
}
return 0.5F;
}*/
@Override
public int getMiningLevel(Tag<Item> tag, BlockState state, ItemStack stack, LivingEntity user) {
@ -96,11 +108,6 @@ public class JackhammerItem extends PickaxeItem implements EnergyHolder, ItemDur
return true;
}
@Override
public boolean isDamageable() {
return false;
}
// ToolItem
@Override
public boolean canRepair(ItemStack stack, ItemStack ingredient) {
@ -108,6 +115,11 @@ public class JackhammerItem extends PickaxeItem implements EnergyHolder, ItemDur
}
// Item
@Override
public boolean isDamageable() {
return false;
}
@Override
public boolean isEnchantable(ItemStack stack) {
return true;

View file

@ -28,6 +28,7 @@ import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.BlockState;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
@ -63,13 +64,10 @@ public class AdvancedJackhammerItem extends JackhammerItem implements MultiBlock
if (originalPos.equals(pos)) {
return false;
}
BlockState blockState = worldIn.getBlockState(pos);
if (ToolsUtil.JackHammerSkippedBlocks(blockState)){
return false;
}
return (stack.getItem().isEffectiveOn(blockState));
}
@ -88,17 +86,6 @@ public class AdvancedJackhammerItem extends JackhammerItem implements MultiBlock
return super.postMine(stack, worldIn, stateIn, pos, entityLiving);
}
@Override
public Set<BlockPos> getBlocksToBreak(ItemStack stack, World worldIn, BlockPos pos, @Nullable LivingEntity entityLiving) {
if (!stack.getItem().isEffectiveOn(worldIn.getBlockState(pos))) {
return Collections.emptySet();
}
return ToolsUtil.getAOEMiningBlocks(worldIn, pos, entityLiving, 1, false)
.stream()
.filter((blockPos -> shouldBreak(worldIn, pos, blockPos, stack)))
.collect(Collectors.toSet());
}
// Item
@Override
public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) {
@ -111,7 +98,7 @@ public class AdvancedJackhammerItem extends JackhammerItem implements MultiBlock
}
@Override
public void usageTick(World world, LivingEntity entity, ItemStack stack, int i) {
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
ItemUtils.checkActive(stack, cost, entity.world.isClient, MessageIDs.poweredToolID);
}
@ -120,4 +107,16 @@ public class AdvancedJackhammerItem extends JackhammerItem implements MultiBlock
public void appendTooltip(ItemStack stack, @Nullable World worldIn, List<Text> tooltip, TooltipContext flagIn) {
ItemUtils.buildActiveTooltip(stack, tooltip);
}
// MultiBlockBreakingTool
@Override
public Set<BlockPos> getBlocksToBreak(ItemStack stack, World worldIn, BlockPos pos, @Nullable LivingEntity entityLiving) {
if (!stack.getItem().isEffectiveOn(worldIn.getBlockState(pos))) {
return Collections.emptySet();
}
return ToolsUtil.getAOEMiningBlocks(worldIn, pos, entityLiving, 1, false)
.stream()
.filter((blockPos -> shouldBreak(worldIn, pos, blockPos, stack)))
.collect(Collectors.toSet());
}
}

View file

@ -29,6 +29,7 @@ import net.fabricmc.api.Environment;
import net.minecraft.block.BlockState;
import net.minecraft.block.Material;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
@ -155,7 +156,7 @@ public class IndustrialJackhammerItem extends JackhammerItem implements MultiBlo
}
@Override
public void usageTick(World world, LivingEntity entity, ItemStack stack, int i) {
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
ItemUtils.checkActive(stack, cost, entity.world.isClient, MessageIDs.poweredToolID);
}

View file

@ -25,8 +25,7 @@
package techreborn.utils;
import com.google.common.collect.ImmutableSet;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.*;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.LivingEntity;
@ -167,4 +166,25 @@ public class ToolsUtil {
}
return targetBlocks;
}
/**
* Check if JackHammer shouldn't break block. JackHammers should be good on stone, dirt, sand. And shouldn't break ores.
*
* @param blockState BlockState to check
* @return boolean True if block shouldn't be breakable by JackHammer
*/
public static boolean JackHammerSkippedBlocks(BlockState blockState){
if (blockState.getMaterial() == Material.AIR) {
return true;
}
if (blockState.getMaterial().isLiquid()) {
return true;
}
if (blockState.getBlock() instanceof OreBlock) {
return true;
}
return blockState.getBlock() instanceof RedstoneOreBlock;
}
}