Use damage hook for mana consumption, let superclass handle axe/shovel

Fixes #3137
This commit is contained in:
Hubry committed 2020-01-13 14:55:31 +01:00
1 parent a4d2b151bd
commit e51eb07dc3
7 files changed
+47 -53

No files matched your search

@@ -26,6 +26,7 @@ import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.common.item.equipment.tool.manasteel.ItemManasteelSword;
import javax.annotation.Nonnull;
import java.util.function.Consumer;
public class ItemEnderDagger extends ItemManasteelSword {
@@ -45,6 +46,11 @@ public class ItemEnderDagger extends ItemManasteelSword {
return true;
}
@Override
public <T extends LivingEntity> int damageItem(ItemStack stack, int amount, T entity, Consumer<T> onBroken) {
return amount;
}
@Override
public void inventoryTick(ItemStack stack, World world, Entity player, int par4, boolean par5) {}
@@ -34,6 +34,7 @@ import vazkii.botania.common.item.ModItems;
import vazkii.botania.common.item.equipment.tool.ToolCommons;
import javax.annotation.Nonnull;
import java.util.function.Consumer;
public class ItemLivingwoodBow extends BowItem implements IManaUsingItem {
public static final int MANA_PER_DAMAGE = 40;
@@ -166,4 +167,9 @@ public class ItemLivingwoodBow extends BowItem implements IManaUsingItem {
public boolean usesMana(ItemStack stack) {
return true;
}
@Override
public <T extends LivingEntity> int damageItem(ItemStack stack, int amount, T entity, Consumer<T> onBroken) {
return ToolCommons.damageItemIfPossible(stack, amount, entity, MANA_PER_DAMAGE);
}
}
@@ -10,7 +10,6 @@
*/
package vazkii.botania.common.item.equipment.tool.manasteel;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
@@ -19,7 +18,6 @@ import net.minecraft.item.IItemTier;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.item.ISortableTool;
@@ -30,6 +28,7 @@ import vazkii.botania.common.core.helper.PlayerHelper;
import vazkii.botania.common.item.equipment.tool.ToolCommons;
import javax.annotation.Nonnull;
import java.util.function.Consumer;
import java.util.regex.Pattern;
public class ItemManasteelAxe extends AxeItem implements IManaUsingItem, ISortableTool {
@@ -47,17 +46,8 @@ public class ItemManasteelAxe extends AxeItem implements IManaUsingItem, ISortab
}
@Override
public boolean hitEntity(ItemStack par1ItemStack, LivingEntity par2EntityLivingBase, @Nonnull LivingEntity par3EntityLivingBase) {
ToolCommons.damageItem(par1ItemStack, 1, par3EntityLivingBase, getManaPerDamage());
return true;
}
@Override
public boolean onBlockDestroyed(@Nonnull ItemStack stack, @Nonnull World world, @Nonnull BlockState state, @Nonnull BlockPos pos, @Nonnull LivingEntity entity) {
if (state.getBlockHardness(world, pos) != 0F)
ToolCommons.damageItem(stack, 1, entity, getManaPerDamage());
return true;
public <T extends LivingEntity> int damageItem(ItemStack stack, int amount, T entity, Consumer<T> onBroken) {
return ToolCommons.damageItemIfPossible(stack, amount, entity, getManaPerDamage());
}
public int getManaPerDamage() {
@@ -73,14 +63,15 @@ public class ItemManasteelAxe extends AxeItem implements IManaUsingItem, ISortab
ItemStack stackAt = player.inventory.getStackInSlot(i);
if(!stackAt.isEmpty() && SAPLING_PATTERN.matcher(stackAt.getItem().getTranslationKey()).find()) {
ItemStack displayStack = stackAt.copy();
ActionResultType did = PlayerHelper.substituteUse(ctx, stackAt);
ItemsRemainingRenderHandler.set(player, displayStack, SAPLING_PATTERN);
return did;
if (PlayerHelper.substituteUse(ctx, stackAt) == ActionResultType.SUCCESS) {
ItemsRemainingRenderHandler.set(player, displayStack, SAPLING_PATTERN);
return ActionResultType.SUCCESS;
}
}
}
}
return ActionResultType.PASS;
return super.onItemUse(ctx);
}
@@ -30,6 +30,7 @@ import vazkii.botania.common.core.helper.PlayerHelper;
import vazkii.botania.common.item.equipment.tool.ToolCommons;
import javax.annotation.Nonnull;
import java.util.function.Consumer;
import java.util.regex.Pattern;
public class ItemManasteelPick extends PickaxeItem implements IManaUsingItem, ISortableTool {
@@ -47,9 +48,8 @@ public class ItemManasteelPick extends PickaxeItem implements IManaUsingItem, IS
}
@Override
public boolean hitEntity(ItemStack par1ItemStack, LivingEntity par2EntityLivingBase, @Nonnull LivingEntity par3EntityLivingBase) {
ToolCommons.damageItem(par1ItemStack, 1, par3EntityLivingBase, getManaPerDamage());
return true;
public <T extends LivingEntity> int damageItem(ItemStack stack, int amount, T entity, Consumer<T> onBroken) {
return ToolCommons.damageItemIfPossible(stack, amount, entity, getManaPerDamage());
}
@Override
@@ -33,6 +33,7 @@ import vazkii.botania.common.item.equipment.tool.ToolCommons;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
public class ItemManasteelShears extends ShearsItem implements IManaUsingItem {
@@ -98,6 +99,11 @@ public class ItemManasteelShears extends ShearsItem implements IManaUsingItem {
return false;
}
@Override
public <T extends LivingEntity> int damageItem(ItemStack stack, int amount, T entity, Consumer<T> onBroken) {
return ToolCommons.damageItemIfPossible(stack, amount, entity, MANA_PER_DAMAGE);
}
@Override
public void inventoryTick(ItemStack stack, World world, Entity player, int par4, boolean par5) {
if(!world.isRemote && player instanceof PlayerEntity && stack.getDamage() > 0 && ManaItemHandler.requestManaExactForTool(stack, (PlayerEntity) player, MANA_PER_DAMAGE * 2, true))
@@ -38,6 +38,7 @@ import vazkii.botania.common.lib.LibObfuscation;
import javax.annotation.Nonnull;
import java.util.Map;
import java.util.function.Consumer;
public class ItemManasteelShovel extends ShovelItem implements IManaUsingItem, ISortableTool {
@@ -59,11 +60,8 @@ public class ItemManasteelShovel extends ShovelItem implements IManaUsingItem, I
}
@Override
public boolean onBlockDestroyed(@Nonnull ItemStack stack, @Nonnull World world, @Nonnull BlockState state, @Nonnull BlockPos pos, @Nonnull LivingEntity entity) {
if (state.getBlockHardness(world, pos) != 0F)
ToolCommons.damageItem(stack, 1, entity, getManaPerDamage());
return true;
public <T extends LivingEntity> int damageItem(ItemStack stack, int amount, T entity, Consumer<T> onBroken) {
return ToolCommons.damageItemIfPossible(stack, amount, entity, getManaPerDamage());
}
public int getManaPerDamage() {
@@ -73,6 +71,9 @@ public class ItemManasteelShovel extends ShovelItem implements IManaUsingItem, I
@Nonnull
@Override
public ActionResultType onItemUse(ItemUseContext ctx) {
if (super.onItemUse(ctx) == ActionResultType.SUCCESS)
return ActionResultType.SUCCESS;
ItemStack stack = ctx.getItem();
PlayerEntity player = ctx.getPlayer();
World world = ctx.getWorld();
@@ -81,21 +82,17 @@ public class ItemManasteelShovel extends ShovelItem implements IManaUsingItem, I
if (player == null || !player.canPlayerEdit(pos, ctx.getFace(), stack))
return ActionResultType.PASS;
Block block = world.getBlockState(pos).getBlock();
BlockState converted = field_195955_e.get(block);
UseHoeEvent event = new UseHoeEvent(ctx);
if (MinecraftForge.EVENT_BUS.post(event))
return ActionResultType.FAIL;
if (converted == null) {
UseHoeEvent event = new UseHoeEvent(ctx);
if (MinecraftForge.EVENT_BUS.post(event))
return ActionResultType.FAIL;
if (event.getResult() == Event.Result.ALLOW) {
ToolCommons.damageItem(stack, 1, player, getManaPerDamage());
return ActionResultType.SUCCESS;
}
converted = HOE_LOOKUP.get(block);
if (event.getResult() == Event.Result.ALLOW) {
ToolCommons.damageItem(stack, 1, player, getManaPerDamage());
return ActionResultType.SUCCESS;
}
Block block = world.getBlockState(pos).getBlock();
BlockState converted = HOE_LOOKUP.get(block);
if (converted == null)
return ActionResultType.PASS;
@@ -10,7 +10,6 @@
*/
package vazkii.botania.common.item.equipment.tool.manasteel;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
@@ -18,14 +17,13 @@ import net.minecraft.item.IItemTier;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.mana.IManaUsingItem;
import vazkii.botania.api.mana.ManaItemHandler;
import vazkii.botania.common.item.equipment.tool.ToolCommons;
import javax.annotation.Nonnull;
import java.util.function.Consumer;
public class ItemManasteelSword extends SwordItem implements IManaUsingItem {
@@ -42,18 +40,8 @@ public class ItemManasteelSword extends SwordItem implements IManaUsingItem {
}
@Override
public boolean hitEntity(ItemStack par1ItemStack, LivingEntity par2EntityLivingBase, @Nonnull LivingEntity par3EntityLivingBase) {
if(usesMana(par1ItemStack))
ToolCommons.damageItem(par1ItemStack, 1, par3EntityLivingBase, getManaPerDamage());
return true;
}
@Override
public boolean onBlockDestroyed(@Nonnull ItemStack stack, @Nonnull World world, BlockState state, @Nonnull BlockPos pos, @Nonnull LivingEntity entity) {
if(usesMana(stack) && state.getBlockHardness(world, pos) != 0F)
ToolCommons.damageItem(stack, 1, entity, getManaPerDamage());
return true;
public <T extends LivingEntity> int damageItem(ItemStack stack, int amount, T entity, Consumer<T> onBroken) {
return ToolCommons.damageItemIfPossible(stack, amount, entity, getManaPerDamage());
}
@Override