Skip to content

Commit

Permalink
feat(tool): add switchable functionality to blaze tools
Browse files Browse the repository at this point in the history
- Implement ISwitchable interface for blaze tools
- Add switchMode method to toggle tool activation
- Update tool usage to check and switch activation state
- Refactor IBlazeTool to ISwitchable interface
  • Loading branch information
cnlimiter committed Nov 3, 2024
1 parent 5ac4383 commit a74bb94
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 139 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package committee.nova.mods.avaritia.api.iface;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;

/**
* @Project: Avaritia
* @Author: cnlimiter
* @CreateTime: 2024/11/4 00:16
* @Description:
*/
public interface ISwitchable {
default boolean isActive(ItemStack stack) {
if (!stack.getOrCreateTag().contains("active")) return false;
return stack.getOrCreateTag().getBoolean("active");
}

default void switchMode(@NotNull Level world, Player player, @NotNull InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
CompoundTag tags = stack.getOrCreateTag();
tags.putBoolean("active", !tags.getBoolean("active"));
if(!world.isClientSide && player instanceof ServerPlayer serverPlayer) serverPlayer.sendSystemMessage(Component.translatable("tooltip.tool.active"), true);
player.swing(hand);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package committee.nova.mods.avaritia.common.item.tools.blaze;

import committee.nova.mods.avaritia.api.iface.IBlazeTool;
import committee.nova.mods.avaritia.api.iface.ISwitchable;
import committee.nova.mods.avaritia.api.iface.ITooltip;
import committee.nova.mods.avaritia.common.entity.ImmortalItemEntity;
import committee.nova.mods.avaritia.init.registry.ModEntities;
import committee.nova.mods.avaritia.init.registry.ModRarities;
import committee.nova.mods.avaritia.init.registry.ModToolTiers;
import net.minecraft.ChatFormatting;
import net.minecraft.client.resources.language.I18n;
import committee.nova.mods.avaritia.util.ItemUtils;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.AxeItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -27,7 +28,7 @@
* Date: 2022/4/2 20:00
* Version: 1.0
*/
public class BlazeAxeItem extends AxeItem implements ITooltip, IBlazeTool {
public class BlazeAxeItem extends AxeItem implements ITooltip, ISwitchable {
private final String name;
public BlazeAxeItem(String name) {
super(ModToolTiers.BLAZE_SWORD, 0, -2.4f,
Expand All @@ -48,25 +49,15 @@ public void appendHoverText(@NotNull ItemStack pStack, @Nullable Level pLevel, @
super.appendHoverText(pStack, pLevel, pTooltipComponents, pIsAdvanced);
this.appendTooltip(pStack, pLevel, pTooltipComponents, pIsAdvanced, name);
}
// @Override
// public @NotNull InteractionResultHolder<ItemStack> use(Level level, Player player, @NotNull InteractionHand hand) {
// var heldItem = player.getItemInHand(hand);
// if (!level.isClientSide) {
// List<Entity> entities = level.getEntities(player, player.getBoundingBox().deflate(10));
// double d2 = 0;
// if (!entities.isEmpty()) d2 = entities.get(0).getY(0.5D) - player.getY(0.5D);
//
// FireBallEntity fireBallEntity = ModEntities.FIRE_BALL.get().create(level);
// if (fireBallEntity != null){
// fireBallEntity.setOwner(player);
// fireBallEntity.setPos(player.getX(), player.getEyeY() + 0.1, player.getZ());
// fireBallEntity.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F);
// level.addFreshEntity(fireBallEntity);
// }
//
// }
// level.playSound(player, player.getOnPos(), SoundEvents.SNOWBALL_THROW, SoundSource.NEUTRAL, 0.5F, 0.4F / (level.random.nextFloat() * 0.4F + 0.8F));
// return InteractionResultHolder.success(heldItem);
// }

@Override
public @NotNull InteractionResultHolder<ItemStack> use(@NotNull Level world, Player player, @NotNull InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
if (player.isCrouching()) {
switchMode(world, player, hand);
return InteractionResultHolder.success(stack);
}
return super.use(world, player, hand);
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package committee.nova.mods.avaritia.common.item.tools.blaze;

import committee.nova.mods.avaritia.api.iface.IBlazeTool;
import committee.nova.mods.avaritia.api.iface.ISwitchable;
import committee.nova.mods.avaritia.api.iface.ITooltip;
import committee.nova.mods.avaritia.common.entity.ImmortalItemEntity;
import committee.nova.mods.avaritia.init.registry.ModEntities;
import committee.nova.mods.avaritia.init.registry.ModRarities;
import committee.nova.mods.avaritia.init.registry.ModToolTiers;
import net.minecraft.ChatFormatting;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.HoeItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -27,7 +26,7 @@
* Date: 2022/4/2 20:00
* Version: 1.0
*/
public class BlazeHoeItem extends HoeItem implements ITooltip, IBlazeTool {
public class BlazeHoeItem extends HoeItem implements ITooltip, ISwitchable {
private final String name;
public BlazeHoeItem(String name) {
super(ModToolTiers.BLAZE_SWORD, 0, -2.4f,
Expand All @@ -49,35 +48,13 @@ public void appendHoverText(@NotNull ItemStack pStack, @Nullable Level pLevel, @
this.appendTooltip(pStack, pLevel, pTooltipComponents, pIsAdvanced, name);
}

// @Override
// public @NotNull InteractionResultHolder<ItemStack> use(Level level, Player player, @NotNull InteractionHand hand) {
// var heldItem = player.getItemInHand(hand);
// if (!level.isClientSide) {
// List<Entity> entities = level.getEntities(player, player.getBoundingBox().deflate(10));
// double d2 = 0;
// if (!entities.isEmpty()) d2 = entities.get(0).getY(0.5D) - player.getY(0.5D);
//
// FireBallEntity fireBallEntity = ModEntities.FIRE_BALL.get().create(level);
// if (fireBallEntity != null){
// fireBallEntity.setOwner(player);
// fireBallEntity.setPos(player.getX(), player.getEyeY() + 0.1, player.getZ());
// fireBallEntity.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F);
// level.addFreshEntity(fireBallEntity);
// }
//
// }
// level.playSound(player, player.getOnPos(), SoundEvents.SNOWBALL_THROW, SoundSource.NEUTRAL, 0.5F, 0.4F / (level.random.nextFloat() * 0.4F + 0.8F));
// return InteractionResultHolder.success(heldItem);
// }

@Override
public boolean hasCustomEntity(ItemStack stack) {
return true;
}

@Nullable
@Override
public Entity createEntity(Level level, Entity location, ItemStack stack) {
return ImmortalItemEntity.create(ModEntities.IMMORTAL.get(), level, location.getX(), location.getY(), location.getZ(), stack);
public @NotNull InteractionResultHolder<ItemStack> use(@NotNull Level world, Player player, @NotNull InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
if (player.isCrouching()) {
switchMode(world, player, hand);
return InteractionResultHolder.success(stack);
}
return super.use(world, player, hand);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package committee.nova.mods.avaritia.common.item.tools.blaze;

import committee.nova.mods.avaritia.api.iface.IBlazeTool;
import committee.nova.mods.avaritia.api.iface.ISwitchable;
import committee.nova.mods.avaritia.api.iface.ITooltip;
import committee.nova.mods.avaritia.common.entity.ImmortalItemEntity;
import committee.nova.mods.avaritia.init.registry.ModEntities;
import committee.nova.mods.avaritia.init.registry.ModRarities;
import committee.nova.mods.avaritia.init.registry.ModToolTiers;
import net.minecraft.ChatFormatting;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.PickaxeItem;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -27,7 +23,7 @@
* Date: 2022/4/2 20:00
* Version: 1.0
*/
public class BlazePickaxeItem extends PickaxeItem implements ITooltip, IBlazeTool {
public class BlazePickaxeItem extends PickaxeItem implements ITooltip{
private final String name;
public BlazePickaxeItem(String name) {
super(ModToolTiers.BLAZE_SWORD, 0, -2.4f,
Expand Down Expand Up @@ -69,15 +65,4 @@ public void appendHoverText(@NotNull ItemStack pStack, @Nullable Level pLevel, @
// level.playSound(player, player.getOnPos(), SoundEvents.SNOWBALL_THROW, SoundSource.NEUTRAL, 0.5F, 0.4F / (level.random.nextFloat() * 0.4F + 0.8F));
// return InteractionResultHolder.success(heldItem);
// }

@Override
public boolean hasCustomEntity(ItemStack stack) {
return true;
}

@Nullable
@Override
public Entity createEntity(Level level, Entity location, ItemStack stack) {
return ImmortalItemEntity.create(ModEntities.IMMORTAL.get(), level, location.getX(), location.getY(), location.getZ(), stack);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package committee.nova.mods.avaritia.common.item.tools.blaze;

import committee.nova.mods.avaritia.api.iface.IBlazeTool;
import committee.nova.mods.avaritia.api.iface.ISwitchable;
import committee.nova.mods.avaritia.api.iface.ITooltip;
import committee.nova.mods.avaritia.common.entity.ImmortalItemEntity;
import committee.nova.mods.avaritia.init.registry.ModEntities;
import committee.nova.mods.avaritia.init.registry.ModRarities;
import committee.nova.mods.avaritia.init.registry.ModToolTiers;
import net.minecraft.ChatFormatting;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ShovelItem;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -27,7 +23,7 @@
* Date: 2022/4/2 20:00
* Version: 1.0
*/
public class BlazeShovelItem extends ShovelItem implements ITooltip, IBlazeTool {
public class BlazeShovelItem extends ShovelItem implements ITooltip, ISwitchable {
private final String name;
public BlazeShovelItem(String name) {
super(ModToolTiers.BLAZE_SWORD, 0, -2.4f,
Expand Down Expand Up @@ -69,15 +65,4 @@ public void appendHoverText(@NotNull ItemStack pStack, @Nullable Level pLevel, @
// level.playSound(player, player.getOnPos(), SoundEvents.SNOWBALL_THROW, SoundSource.NEUTRAL, 0.5F, 0.4F / (level.random.nextFloat() * 0.4F + 0.8F));
// return InteractionResultHolder.success(heldItem);
// }

@Override
public boolean hasCustomEntity(ItemStack stack) {
return true;
}

@Nullable
@Override
public Entity createEntity(Level level, Entity location, ItemStack stack) {
return ImmortalItemEntity.create(ModEntities.IMMORTAL.get(), level, location.getX(), location.getY(), location.getZ(), stack);
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
package committee.nova.mods.avaritia.common.item.tools.blaze;

import committee.nova.mods.avaritia.api.iface.IBlazeTool;
import committee.nova.mods.avaritia.api.iface.ISwitchable;
import committee.nova.mods.avaritia.api.iface.ITooltip;
import committee.nova.mods.avaritia.common.entity.FireBallEntity;
import committee.nova.mods.avaritia.common.entity.ImmortalItemEntity;
import committee.nova.mods.avaritia.init.registry.ModEntities;
import committee.nova.mods.avaritia.init.registry.ModRarities;
import committee.nova.mods.avaritia.init.registry.ModToolTiers;
import committee.nova.mods.avaritia.util.ItemUtils;
import net.minecraft.ChatFormatting;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -38,7 +27,7 @@
* Date: 2022/4/2 20:00
* Version: 1.0
*/
public class BlazeSwordItem extends SwordItem implements ITooltip, IBlazeTool {
public class BlazeSwordItem extends SwordItem implements ITooltip, ISwitchable {
private final String name;
public BlazeSwordItem(String name) {
super(ModToolTiers.BLAZE_SWORD, 0, -2.4f,
Expand All @@ -61,7 +50,7 @@ public boolean isFoil(@NotNull ItemStack pStack) {
}

@Override
public @NotNull InteractionResultHolder<ItemStack> use(Level level, Player player, @NotNull InteractionHand hand) {
public @NotNull InteractionResultHolder<ItemStack> use(@NotNull Level level, Player player, @NotNull InteractionHand hand) {
var heldItem = player.getItemInHand(hand);
if (!level.isClientSide) {
List<Entity> entities = level.getEntities(player, player.getBoundingBox().deflate(10));
Expand All @@ -80,18 +69,4 @@ public boolean isFoil(@NotNull ItemStack pStack) {
level.playSound(player, player.getOnPos(), SoundEvents.SNOWBALL_THROW, SoundSource.NEUTRAL, 0.5F, 0.4F / (level.random.nextFloat() * 0.4F + 0.8F));
return InteractionResultHolder.success(heldItem);
}




@Override
public boolean hasCustomEntity(ItemStack stack) {
return true;
}

@Nullable
@Override
public Entity createEntity(Level level, Entity location, ItemStack stack) {
return ImmortalItemEntity.create(ModEntities.IMMORTAL.get(), level, location.getX(), location.getY(), location.getZ(), stack);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package committee.nova.mods.avaritia.init.handler;

import committee.nova.mods.avaritia.api.iface.IBlazeTool;
import committee.nova.mods.avaritia.api.iface.ISwitchable;
import committee.nova.mods.avaritia.common.entity.ImmortalItemEntity;
import committee.nova.mods.avaritia.common.item.InfinityArmorItem;
import committee.nova.mods.avaritia.common.item.MatterClusterItem;
Expand Down Expand Up @@ -330,8 +330,11 @@ public static void toolEnchant(BlockEvent.BreakEvent event) {//炽热
BlockPos pos = event.getPos();
Block block = event.getState().getBlock();
BlockState state = event.getState();
if (tool.is(ModItems.blaze_axe.get()) || tool.is(ModItems.blaze_pickaxe.get()) || tool.is(ModItems.blaze_shovel.get())) {
ToolUtils.melting(block, state, world, pos, player, tool, event);
if (
(tool.is(ModItems.blaze_axe.get()) || tool.is(ModItems.blaze_pickaxe.get()) || tool.is(ModItems.blaze_shovel.get()))
&& tool.getItem() instanceof ISwitchable
) {
if (((ISwitchable)tool.getItem()).isActive(tool)) ToolUtils.melting(block, state, world, pos, player, tool, event);
}
}

Expand Down

0 comments on commit a74bb94

Please sign in to comment.