generated from TropheusJ/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve BreakSpeed event inject for better mod compat
- Loading branch information
Showing
5 changed files
with
50 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...n/java/io/github/fabricators_of_create/porting_lib/entity/extensions/PlayerExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.github.fabricators_of_create.porting_lib.entity.extensions; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public interface PlayerExtension { | ||
default float getDigSpeed(BlockState state, @Nullable BlockPos pos) { | ||
setDigSpeedContext(pos); | ||
return ((Player) this).getDestroySpeed(state); | ||
} | ||
|
||
default void setDigSpeedContext(@Nullable BlockPos pos) { | ||
throw new RuntimeException("this should be overridden via mixin. what?"); | ||
} | ||
} |
29 changes: 8 additions & 21 deletions
29
.../io/github/fabricators_of_create/porting_lib/entity/mixin/common/BlockBehaviourMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,20 @@ | ||
package io.github.fabricators_of_create.porting_lib.entity.mixin.common; | ||
|
||
import io.github.fabricators_of_create.porting_lib.entity.events.PlayerEvents; | ||
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 net.minecraft.core.BlockPos; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.level.BlockGetter; | ||
import net.minecraft.world.level.block.state.BlockBehaviour; | ||
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 org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
@Mixin(BlockBehaviour.class) | ||
public abstract class BlockBehaviourMixin { | ||
@Inject(method = "getDestroyProgress", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getDestroySpeed(Lnet/minecraft/world/level/block/state/BlockState;)F", shift = At.Shift.AFTER), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true) | ||
public void getDestroySpeed(BlockState state, Player player, BlockGetter level, BlockPos pos, CallbackInfoReturnable<Float> cir, float f) { | ||
float original = player.getDestroySpeed(state); | ||
PlayerEvents.BreakSpeed breakSpeed = new PlayerEvents.BreakSpeed(player, state, original, pos); | ||
breakSpeed.sendEvent(); | ||
float newSpeed = breakSpeed.getNewSpeed(); | ||
if (newSpeed != original) { | ||
if (f == -1.0F) { | ||
cir.setReturnValue(0.0F); | ||
} else { | ||
int i = player.hasCorrectToolForDrops(state) ? 30 : 100; | ||
cir.setReturnValue(newSpeed / f / (float) i); | ||
} | ||
} | ||
@Inject(method = "getDestroyProgress", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;hasCorrectToolForDrops(Lnet/minecraft/world/level/block/state/BlockState;)Z")) | ||
public void getDestroySpeed(BlockState blockState, Player player, BlockGetter blockGetter, BlockPos pos, CallbackInfoReturnable<Float> cir) { | ||
player.setDigSpeedContext(pos); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters