Skip to content

Commit 0163e40

Browse files
feat: sound resolve, xp issues resolve
relates to: #57 relates to: #54 relates to: #52 relates to: #12 relates to: #47 realtes to: #44
1 parent d55b87f commit 0163e40

File tree

9 files changed

+181
-15
lines changed

9 files changed

+181
-15
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Release
33
on:
44
push:
55
tags:
6-
- "release/1.*/*"
6+
- "release/*"
77

88
jobs:
99
build:

CHANGELOG.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# Just Hammers Changelog
22

3-
## [84.0.0]
3+
## [20.4.1]
44

5-
### Changed
5+
### Added
66

7-
- Ported to 1.20.4
7+
- Added a config to disable the no-breaking checks. This means, when enabled, hammers can break in the players inventory when their damage is depleted.
8+
- Added a config option to disable the "fancy" durability in the tooltip as some other mods will already provide something similar.
9+
10+
### Fixed
11+
12+
- XP not dropping for ores and other blocks that drop XP
13+
- Sound being played for all breaking blocks which caused sound engine issues
14+
- Make netherite hammers fire resistant
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package pro.mikey.justhammers;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.nio.file.Files;
9+
import java.util.Objects;
10+
11+
public enum Config {
12+
INSTANCE;
13+
14+
private static final Logger LOGGER = LoggerFactory.getLogger(Config.class);
15+
16+
public ConfigData config = new ConfigData(false, false);
17+
18+
public void load() {
19+
var configPath = HammersPlatform.getConfigDirectory().resolve("justhammers.json");
20+
if (Files.notExists(configPath)) {
21+
try {
22+
// Write default config
23+
var jsonData = new GsonBuilder().setPrettyPrinting().create().toJson(config);
24+
Files.writeString(configPath, jsonData);
25+
} catch (Exception e) {
26+
LOGGER.error("Failed to create default config file", e);
27+
}
28+
29+
return;
30+
}
31+
32+
// Read config
33+
try {
34+
var jsonData = Files.readString(configPath);
35+
config = new Gson().fromJson(jsonData, ConfigData.class);
36+
} catch (Exception e) {
37+
LOGGER.error("Failed to read config file", e);
38+
}
39+
}
40+
41+
public static final class ConfigData {
42+
private final boolean allowBreaking;
43+
private final boolean disableFancyDurability;
44+
45+
public ConfigData(
46+
boolean allowBreaking,
47+
boolean disableFancyDurability
48+
) {
49+
this.allowBreaking = allowBreaking;
50+
this.disableFancyDurability = disableFancyDurability;
51+
}
52+
53+
public boolean allowBreaking() {
54+
return allowBreaking;
55+
}
56+
57+
public boolean disableFancyDurability() {
58+
return disableFancyDurability;
59+
}
60+
61+
@Override
62+
public boolean equals(Object obj) {
63+
if (obj == this) return true;
64+
if (obj == null || obj.getClass() != this.getClass()) return false;
65+
var that = (ConfigData) obj;
66+
return this.allowBreaking == that.allowBreaking &&
67+
this.disableFancyDurability == that.disableFancyDurability;
68+
}
69+
70+
@Override
71+
public int hashCode() {
72+
return Objects.hash(allowBreaking, disableFancyDurability);
73+
}
74+
75+
@Override
76+
public String toString() {
77+
return "ConfigData[" +
78+
"allowBreaking=" + allowBreaking + ", " +
79+
"disableFancyDurability=" + disableFancyDurability + ']';
80+
}
81+
}
82+
}

common/src/main/java/pro/mikey/justhammers/HammerItem.java

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.architectury.event.EventResult;
44
import dev.architectury.event.events.common.BlockEvent;
5+
import dev.architectury.utils.value.IntValue;
56
import net.minecraft.ChatFormatting;
67
import net.minecraft.core.BlockPos;
78
import net.minecraft.core.Direction;
@@ -11,15 +12,26 @@
1112
import net.minecraft.tags.BlockTags;
1213
import net.minecraft.tags.TagKey;
1314
import net.minecraft.world.entity.EquipmentSlot;
15+
import net.minecraft.world.entity.ExperienceOrb;
1416
import net.minecraft.world.entity.LivingEntity;
1517
import 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;
1621
import net.minecraft.world.level.Level;
1722
import net.minecraft.world.level.block.Block;
23+
import net.minecraft.world.level.block.Blocks;
1824
import net.minecraft.world.level.block.state.BlockState;
25+
import net.minecraft.world.level.gameevent.GameEvent;
1926
import net.minecraft.world.level.levelgen.structure.BoundingBox;
27+
import net.minecraft.world.level.material.FluidState;
2028
import net.minecraft.world.phys.BlockHitResult;
2129
import net.minecraft.world.phys.HitResult;
30+
import net.minecraft.world.phys.Vec3;
2231
import org.jetbrains.annotations.Nullable;
32+
import pro.mikey.justhammers.Config;
33+
import pro.mikey.justhammers.HammerItems;
34+
import pro.mikey.justhammers.HammersPlatform;
2335

2436
import java.util.HashSet;
2537
import 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

common/src/main/java/pro/mikey/justhammers/Hammers.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ public static void init() {
2121
CREATIVE_TABS.register();
2222
HammerItems.ITEMS.register();
2323
HammerItems.init();
24+
Config.INSTANCE.load();
2425
}
2526
}

common/src/main/java/pro/mikey/justhammers/HammersPlatform.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package pro.mikey.justhammers;
22

33
import dev.architectury.injectables.annotations.ExpectPlatform;
4+
import net.minecraft.core.BlockPos;
5+
import net.minecraft.world.level.Level;
6+
import net.minecraft.world.level.block.state.BlockState;
47

58
import java.nio.file.Path;
69

@@ -10,4 +13,9 @@ public static Path getConfigDirectory() {
1013
// Just throw an error, the content should get replaced at runtime.
1114
throw new AssertionError();
1215
}
16+
17+
@ExpectPlatform
18+
public static int getBlockXpAmount(BlockPos pos, BlockState state, Level level, int fortuneLevel, int silkTouchLevel) {
19+
throw new AssertionError();
20+
}
1321
}

fabric/src/main/java/pro/mikey/justhammers/fabric/HammersPlatformImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package pro.mikey.justhammers.fabric;
22

3+
import net.minecraft.core.BlockPos;
4+
import net.minecraft.world.level.Level;
5+
import net.minecraft.world.level.block.state.BlockState;
36
import pro.mikey.justhammers.HammersPlatform;
47
import net.fabricmc.loader.api.FabricLoader;
58

@@ -12,4 +15,8 @@ public class HammersPlatformImpl {
1215
public static Path getConfigDirectory() {
1316
return FabricLoader.getInstance().getConfigDir();
1417
}
18+
19+
public static int getBlockXpAmount(BlockPos pos, BlockState state, Level level, int fortuneLevel, int silkTouchLevel) {
20+
return -1;
21+
}
1522
}

forge/src/main/java/pro/mikey/justhammers/forge/HammersPlatformImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package pro.mikey.justhammers.forge;
22

3+
import net.minecraft.core.BlockPos;
4+
import net.minecraft.world.level.Level;
5+
import net.minecraft.world.level.LevelReader;
6+
import net.minecraft.world.level.block.state.BlockState;
37
import pro.mikey.justhammers.HammersPlatform;
48
import net.minecraftforge.fml.loading.FMLPaths;
59

@@ -12,4 +16,8 @@ public class HammersPlatformImpl {
1216
public static Path getConfigDirectory() {
1317
return FMLPaths.CONFIGDIR.get();
1418
}
19+
20+
public static int getBlockXpAmount(BlockPos pos, BlockState state, Level level, int fortuneLevel, int silkTouchLevel) {
21+
return state.getExpDrop(level, level.random, pos, fortuneLevel, silkTouchLevel);
22+
}
1523
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ minecraft_version=1.20.4
44
enabled_platforms=fabric,forge,neoforge
55

66
archives_base_name=just-hammers
7-
mod_version=84.0.0
7+
mod_version=20.4.1
88
maven_group=pro.mikey.mods
99

1010
architectury_version=11.0.9

0 commit comments

Comments
 (0)