Skip to content

Commit a5b9698

Browse files
committed
added harvest ability
1 parent 26d0d62 commit a5b9698

File tree

6 files changed

+130
-4
lines changed

6 files changed

+130
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package dev.sterner.mixin;
2+
3+
import com.mojang.datafixers.util.Pair;
4+
import net.minecraft.world.item.HoeItem;
5+
import net.minecraft.world.item.context.UseOnContext;
6+
import net.minecraft.world.level.block.Block;
7+
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.gen.Accessor;
9+
10+
import java.util.Map;
11+
import java.util.function.Consumer;
12+
import java.util.function.Predicate;
13+
14+
@Mixin(HoeItem.class)
15+
public interface HoeItemTillablesAccessor {
16+
17+
@Accessor("TILLABLES")
18+
public static Map<Block, Pair<Predicate<UseOnContext>, Consumer<UseOnContext>>> getTILLABLES() {
19+
throw new AssertionError();
20+
}
21+
22+
}

Diff for: src/main/kotlin/dev/sterner/api/item/ItemAbility.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.sterner.api.item
22

33
import dev.sterner.common.item.tool.ichor.IchoriumTerraformer
4+
import dev.sterner.common.item.tool.ichor.IchoriumVorpal
45
import dev.sterner.registry.VoidBoundTags
56
import net.minecraft.nbt.CompoundTag
67
import net.minecraft.tags.TagKey
@@ -16,7 +17,7 @@ enum class ItemAbility: StringRepresentable {
1617
MINING_3X3,//Fully implemented
1718
MINING_5X5,//Fully implemented
1819
VAMPIRISM,//TODO implement
19-
HARVEST,
20+
HARVEST,//TODO implement
2021
OPENER;//TODO implement
2122

2223
override fun getSerializedName(): String {
@@ -42,7 +43,7 @@ enum class ItemAbility: StringRepresentable {
4243
list.add(MINING_3X3)
4344
list.add(MINING_5X5)
4445
}
45-
if (item is IchoriumTerraformer) {
46+
if (item is IchoriumVorpal) {
4647
list.add(VAMPIRISM)
4748
list.add(OPENER)
4849
list.add(HARVEST)

Diff for: src/main/kotlin/dev/sterner/common/item/tool/ichor/IchoriumVorpal.kt

+102-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
package dev.sterner.common.item.tool.ichor
22

3+
import com.mojang.datafixers.util.Pair
4+
import dev.sterner.api.VoidBoundApi
5+
import dev.sterner.api.item.ItemAbility
36
import dev.sterner.common.item.tool.GalesEdgeItem.Companion.ascend
7+
import dev.sterner.mixin.HoeItemTillablesAccessor
8+
import net.minecraft.core.BlockPos
9+
import net.minecraft.core.NonNullList
10+
import net.minecraft.server.level.ServerLevel
11+
import net.minecraft.sounds.SoundEvents
12+
import net.minecraft.sounds.SoundSource
13+
import net.minecraft.tags.BlockTags
14+
import net.minecraft.world.Containers
415
import net.minecraft.world.InteractionHand
16+
import net.minecraft.world.InteractionResult
517
import net.minecraft.world.InteractionResultHolder
618
import net.minecraft.world.entity.LivingEntity
719
import net.minecraft.world.entity.player.Player
20+
import net.minecraft.world.item.HoeItem
821
import net.minecraft.world.item.ItemStack
922
import net.minecraft.world.item.Tier
23+
import net.minecraft.world.item.context.UseOnContext
1024
import net.minecraft.world.level.Level
25+
import net.minecraft.world.level.block.Block
26+
import net.minecraft.world.level.block.state.BlockState
27+
import net.minecraft.world.phys.BlockHitResult
1128
import team.lodestar.lodestone.systems.item.tools.magic.MagicSwordItem
29+
import java.util.function.Consumer
30+
import java.util.function.Predicate
1231

1332
class IchoriumVorpal(tier: Tier, attackDamageModifier: Int, attackSpeedModifier: Float, magicDamage: Float, properties: Properties) : MagicSwordItem(tier, attackDamageModifier,
1433
attackSpeedModifier,
@@ -21,12 +40,93 @@ class IchoriumVorpal(tier: Tier, attackDamageModifier: Int, attackSpeedModifier:
2140
}
2241

2342
override fun use(level: Level, player: Player, usedHand: InteractionHand): InteractionResultHolder<ItemStack> {
24-
player.startUsingItem(usedHand)
43+
if (VoidBoundApi.getActiveAbility(player.mainHandItem) != ItemAbility.HARVEST) {
44+
player.startUsingItem(usedHand)
45+
}
46+
2547
return super.use(level, player, usedHand)
2648
}
2749

50+
override fun useOn(context: UseOnContext): InteractionResult {
51+
val player = context.player
52+
53+
if (player!!.isShiftKeyDown) {
54+
return super.useOn(context)
55+
}
56+
if (VoidBoundApi.getActiveAbility(player.mainHandItem) == ItemAbility.HARVEST) {
57+
for (xx in -1..1) {
58+
for (zz in -1..1) {
59+
useHoeOn(
60+
UseOnContext(
61+
player, player.usedItemHand,
62+
BlockHitResult(
63+
context.clickLocation,
64+
context.horizontalDirection,
65+
context.clickedPos.offset(xx, 0, zz),
66+
context.isInside
67+
)
68+
)
69+
)
70+
}
71+
}
72+
}
73+
74+
return InteractionResult.SUCCESS
75+
}
76+
77+
78+
fun useHoeOn(context: UseOnContext): InteractionResult {
79+
val level = context.level
80+
val blockPos = context.clickedPos
81+
val pair = HoeItemTillablesAccessor.getTILLABLES()[level.getBlockState(blockPos).block] as Pair<Predicate<UseOnContext>, Consumer<UseOnContext>>?
82+
if (pair == null) {
83+
return InteractionResult.PASS
84+
} else {
85+
val predicate = pair.first
86+
val consumer = pair.second
87+
if (predicate.test(context)) {
88+
val player = context.player
89+
level.playSound(player, blockPos, SoundEvents.HOE_TILL, SoundSource.BLOCKS, 1.0f, 1.0f)
90+
if (!level.isClientSide) {
91+
consumer.accept(context)
92+
if (player != null) {
93+
context.itemInHand.hurtAndBreak(
94+
1, player
95+
) { playerx: Player ->
96+
playerx.broadcastBreakEvent(
97+
context.hand
98+
)
99+
}
100+
}
101+
}
102+
103+
return InteractionResult.sidedSuccess(level.isClientSide)
104+
} else {
105+
return InteractionResult.PASS
106+
}
107+
}
108+
}
109+
110+
override fun mineBlock(
111+
stack: ItemStack,
112+
level: Level,
113+
state: BlockState,
114+
pos: BlockPos,
115+
miningEntity: LivingEntity
116+
): Boolean {
117+
if (level is ServerLevel && state.`is`(BlockTags.CROPS)) {
118+
val list = NonNullList.create<ItemStack>()
119+
list.addAll(Block.getDrops(state, level, pos, null, miningEntity, stack))
120+
Containers.dropContents(level, pos, list)
121+
}
122+
return super.mineBlock(stack, level, state, pos, miningEntity)
123+
}
124+
28125
override fun onUseTick(level: Level, player: LivingEntity, stack: ItemStack, remainingUseDuration: Int) {
29-
ascend(player, stack, this.getUseDuration(stack) - remainingUseDuration)
126+
if (VoidBoundApi.getActiveAbility(player.mainHandItem) != ItemAbility.HARVEST) {
127+
ascend(player, stack, this.getUseDuration(stack) - remainingUseDuration)
128+
}
129+
30130
super.onUseTick(level, player, stack, remainingUseDuration)
31131
}
32132

Diff for: src/main/kotlin/dev/sterner/networking/AbilityUpdatePacket.kt

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class AbilityUpdatePacket(val uuid: UUID, val itemAbility: ItemAbility) : Lodest
3030
server?.execute {
3131
if (player?.uuid == uuid) {
3232
val item = player.mainHandItem
33+
VoidBoundApi.addItemAbility(item, itemAbility, true)
3334
VoidBoundApi.setActiveAbility(item, itemAbility)
3435
}
3536
}

Diff for: src/main/resources/assets/voidbound/lang/en_us.json

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"voidbound.ability.vampirism": "Vampirism",
6060
"voidbound.ability.opener": "Opener",
6161
"voidbound.ability.none": "None",
62+
"voidbound.ability.harvest": "Harvest",
6263
"voidbound.ability.autosmelt": "Autosmelt",
6364
"voidbound.ability.mining_3x3": "3x3 Mining",
6465
"voidbound.ability.mining_5x5": "5x5 Mining",

Diff for: src/main/resources/voidbound.mixins.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"package": "dev.sterner.mixin",
44
"compatibilityLevel": "JAVA_17",
55
"mixins": [
6+
"HoeItemTillablesAccessor",
67
"ItemEntityMixin",
78
"LodestoneBlockEntityRegistryMixin",
89
"ReboundEnchantmentMixin",

0 commit comments

Comments
 (0)