[Feature] API to modify speed of DiggerItems
This commit is contained in:
parent
c346e1df65
commit
c429ee3a38
3 changed files with 61 additions and 0 deletions
35
src/main/java/org/betterx/bclib/api/v2/DiggerItemSpeed.java
Normal file
35
src/main/java/org/betterx/bclib/api/v2/DiggerItemSpeed.java
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package org.betterx.bclib.api.v2;
|
||||||
|
|
||||||
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class DiggerItemSpeed {
|
||||||
|
public static final List<SpeedModifier> modifiers = new LinkedList<>();
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface SpeedModifier {
|
||||||
|
Optional<Float> calculateSpeed(ItemStack stack, BlockState state, float initialSpeed, float currentSpeed);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addModifier(SpeedModifier mod) {
|
||||||
|
modifiers.add(mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Optional<Float> getModifiedSpeed(ItemStack stack, BlockState state, float initialSpeed) {
|
||||||
|
float currentSpeed = initialSpeed;
|
||||||
|
Optional<Float> speed = Optional.empty();
|
||||||
|
for (SpeedModifier mod : modifiers) {
|
||||||
|
Optional<Float> res = mod.calculateSpeed(stack, state, initialSpeed, currentSpeed);
|
||||||
|
if (res.isPresent()) {
|
||||||
|
currentSpeed = res.get();
|
||||||
|
speed = res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package org.betterx.bclib.mixin.common;
|
||||||
|
|
||||||
|
import org.betterx.bclib.api.v2.DiggerItemSpeed;
|
||||||
|
|
||||||
|
import net.minecraft.world.item.DiggerItem;
|
||||||
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Mixin(DiggerItem.class)
|
||||||
|
public class DiggerItemMixin {
|
||||||
|
@Inject(method = "getDestroySpeed", at = @At(value = "RETURN"), cancellable = true)
|
||||||
|
void bn_getDestroySpeed(ItemStack stack, BlockState state, CallbackInfoReturnable<Float> cir) {
|
||||||
|
final Optional<Float> newSpeed = DiggerItemSpeed.getModifiedSpeed(stack, state, cir.getReturnValue());
|
||||||
|
if (newSpeed.isPresent()) {
|
||||||
|
cir.setReturnValue(newSpeed.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,7 @@
|
||||||
"CraftingMenuMixin",
|
"CraftingMenuMixin",
|
||||||
"DedicatedServerPropertiesMixin",
|
"DedicatedServerPropertiesMixin",
|
||||||
"DiggerItemAccessor",
|
"DiggerItemAccessor",
|
||||||
|
"DiggerItemMixin",
|
||||||
"EnchantingTableBlockMixin",
|
"EnchantingTableBlockMixin",
|
||||||
"ItemStackMixin",
|
"ItemStackMixin",
|
||||||
"LayerLightSectionStorageMixin",
|
"LayerLightSectionStorageMixin",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue