22
33import dev .architectury .event .EventResult ;
44import dev .architectury .event .events .common .BlockEvent ;
5+ import dev .architectury .utils .value .IntValue ;
56import net .minecraft .ChatFormatting ;
67import net .minecraft .core .BlockPos ;
78import net .minecraft .core .Direction ;
1112import net .minecraft .tags .BlockTags ;
1213import net .minecraft .tags .TagKey ;
1314import net .minecraft .world .entity .EquipmentSlot ;
15+ import net .minecraft .world .entity .ExperienceOrb ;
1416import net .minecraft .world .entity .LivingEntity ;
1517import net .minecraft .world .item .*;
18+ import net .minecraft .world .item .enchantment .EnchantmentHelper ;
19+ import net .minecraft .world .item .enchantment .Enchantments ;
20+ import net .minecraft .world .level .GameRules ;
1621import net .minecraft .world .level .Level ;
1722import net .minecraft .world .level .block .Block ;
23+ import net .minecraft .world .level .block .Blocks ;
1824import net .minecraft .world .level .block .state .BlockState ;
25+ import net .minecraft .world .level .gameevent .GameEvent ;
1926import net .minecraft .world .level .levelgen .structure .BoundingBox ;
27+ import net .minecraft .world .level .material .FluidState ;
2028import net .minecraft .world .phys .BlockHitResult ;
2129import net .minecraft .world .phys .HitResult ;
30+ import net .minecraft .world .phys .Vec3 ;
2231import org .jetbrains .annotations .Nullable ;
32+ import pro .mikey .justhammers .Config ;
33+ import pro .mikey .justhammers .HammerItems ;
34+ import pro .mikey .justhammers .HammersPlatform ;
2335
2436import java .util .HashSet ;
2537import java .util .Iterator ;
@@ -34,7 +46,11 @@ public class HammerItem extends PickaxeItem {
3446 private TagKey <Block > blocks ;
3547
3648 public HammerItem (Tier tier , int radius , int depth , int level ) {
37- super (tier , 1 , -2.8f , HammerItems .DEFAULT_PROPERTIES .durability (computeDurability (tier , level )));
49+ super (tier , 1 , -2.8f ,
50+ tier == Tiers .NETHERITE ?
51+ HammerItems .DEFAULT_PROPERTIES .durability (computeDurability (tier , level )).fireResistant ()
52+ : HammerItems .DEFAULT_PROPERTIES .durability (computeDurability (tier , level ))
53+ );
3854
3955 this .blocks = BlockTags .MINEABLE_WITH_PICKAXE ;
4056 this .depth = depth ;
@@ -45,7 +61,11 @@ public HammerItem(Tier tier, int radius, int depth, int level) {
4561 public void appendHoverText (ItemStack itemStack , @ Nullable Level level , List <Component > list , TooltipFlag tooltipFlag ) {
4662 list .add (Component .translatable ("justhammers.tooltip.size" , this .radius , this .radius , this .depth ).withStyle (ChatFormatting .GRAY ));
4763
48- int damage = itemStack .getDamageValue ();
64+ if (Config .INSTANCE .config .disableFancyDurability ()) {
65+ return ;
66+ }
67+
68+ int damage = Math .max (0 , itemStack .getDamageValue ());
4969 int maxDamage = itemStack .getMaxDamage ();
5070 int durabilityPercentage = (int ) (((float ) (maxDamage - damage ) / (float ) maxDamage ) * 100 );
5171
@@ -99,6 +119,10 @@ private static int computeDurability(Tier tier, int level) {
99119
100120 @ Override
101121 public float getDestroySpeed (ItemStack itemStack , BlockState blockState ) {
122+ if (Config .INSTANCE .config .allowBreaking ()) {
123+ return super .getDestroySpeed (itemStack , blockState );
124+ }
125+
102126 if (itemStack .getMaxDamage () - itemStack .getDamageValue () <= 1 ) {
103127 return -1f ;
104128 }
@@ -179,7 +203,12 @@ public void findAndBreakNearBlocks(BlockHitResult pick, BlockPos blockPos, ItemS
179203 var pos = iterator .next ();
180204
181205 // Prevent the hammer from breaking if the damage is too high
182- if (!player .isCreative () && (hammerStack .getDamageValue () + (damage + 1 )) >= hammerStack .getMaxDamage () - 1 ) {
206+ boolean isBroken = (hammerStack .getDamageValue () + (damage + 1 )) >= hammerStack .getMaxDamage () - 1 ;
207+ if (Config .INSTANCE .config .allowBreaking ()) {
208+ isBroken = hammerStack .getDamageValue () + (damage + 1 ) >= hammerStack .getMaxDamage ();
209+ }
210+
211+ if (!player .isCreative () && isBroken ) {
183212 break ;
184213 }
185214
@@ -194,29 +223,53 @@ public void findAndBreakNearBlocks(BlockHitResult pick, BlockPos blockPos, ItemS
194223 }
195224
196225 // Throw event out there and let mods block us breaking this block
197- EventResult eventResult = BlockEvent .BREAK .invoker ().breakBlock (level , pos , targetState , (ServerPlayer ) livingEntity , null );
226+ var silkTouchLevel = EnchantmentHelper .getItemEnchantmentLevel (Enchantments .SILK_TOUCH , hammerStack );
227+ var fortuneLevel = EnchantmentHelper .getItemEnchantmentLevel (Enchantments .BLOCK_FORTUNE , hammerStack );
228+ final int [] xp = {HammersPlatform .getBlockXpAmount (pos , targetState , level , fortuneLevel , silkTouchLevel )};
229+ EventResult eventResult = BlockEvent .BREAK .invoker ().breakBlock (level , pos , targetState , (ServerPlayer ) livingEntity , xp [0 ] == -1 ? null : new IntValue () {
230+ @ Override
231+ public void accept (int value ) {
232+ xp [0 ] = value ;
233+ }
234+
235+ @ Override
236+ public int getAsInt () {
237+ return xp [0 ];
238+ }
239+ });
240+
198241 if (eventResult .isFalse ()) {
199242 continue ;
200243 }
201244
202- removedPos . add ( pos ) ;
203- level . destroyBlock ( pos , false , livingEntity );
245+ final int outputXpLevel = xp [ 0 ] ;
246+
204247 if (!player .isCreative ()) {
205248 boolean correctToolForDrops = hammerStack .isCorrectToolForDrops (targetState );
206249 if (correctToolForDrops ) {
207250 targetState .spawnAfterBreak ((ServerLevel ) level , pos , hammerStack , true );
208251 List <ItemStack > drops = Block .getDrops (targetState , (ServerLevel ) level , pos , level .getBlockEntity (pos ), livingEntity , hammerStack );
209- drops .forEach (e -> Block .popResourceFromFace (level , pos , ((BlockHitResult ) pick ).getDirection (), e ));
252+ drops .forEach (e -> Block .popResourceFromFace (level , pos , pick .getDirection (), e ));
253+
254+ if (outputXpLevel != -1 && level .getGameRules ().getBoolean (GameRules .RULE_DOBLOCKDROPS )) {
255+ ExperienceOrb .award ((ServerLevel ) level , Vec3 .atCenterOf (blockPos ), outputXpLevel );
256+ }
210257 }
211258 }
212259
260+ removedPos .add (pos );
261+ targetState .getBlock ().destroy (level , pos , targetState );
262+ BlockState newState = Blocks .AIR .defaultBlockState ();
263+ var setResult = level .setBlock (pos , newState , 3 );
264+ if (setResult ) {
265+ level .gameEvent (GameEvent .BLOCK_DESTROY , blockPos , GameEvent .Context .of (livingEntity , newState ));
266+ }
267+
213268 damage ++;
214269 }
215270
216271 if (damage != 0 && !player .isCreative ()) {
217- hammerStack .hurtAndBreak (damage , livingEntity , (livingEntityx ) -> {
218- livingEntityx .broadcastBreakEvent (EquipmentSlot .MAINHAND );
219- });
272+ hammerStack .hurtAndBreak (damage , livingEntity , (livingEntityx ) -> {});
220273 }
221274 }
222275
0 commit comments