[Scarpet] Added ability to cancel events (#1497)
* Added ability to cancel events * Made more events cancellable * Added event_priority to __config * Check for string values for performance * Made player_finishes_using_item cancellable * Made player_chooses_recipe cancellable * Made player_swaps_hands cancellable * Made player_message cancellable * Added __on_player_placing_block (#1498) * Added event to docs, keywords, event_test * Added docs for event cancellation
This commit is contained in:
1 parent
4bb4b557e5
commit
24b9c386ec
14 files changed
+243
-99
No files matched your search
@@ -1,11 +1,13 @@
|
||||
package carpet.mixins;
|
||||
|
||||
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 static carpet.script.CarpetEventServer.Event.PLAYER_PLACES_BLOCK;
|
||||
import static carpet.script.CarpetEventServer.Event.PLAYER_PLACING_BLOCK;
|
||||
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
@@ -25,4 +27,14 @@ public class BlockItem_scarpetEventMixin
|
||||
if (context.getPlayer() instanceof ServerPlayer && PLAYER_PLACES_BLOCK.isNeeded())
|
||||
PLAYER_PLACES_BLOCK.onBlockPlaced((ServerPlayer) context.getPlayer(), context.getClickedPos(), context.getHand(), context.getItemInHand());
|
||||
}
|
||||
|
||||
@Inject(method = "placeBlock", at = @At("HEAD"), cancellable = true)
|
||||
private void beforePlacement(BlockPlaceContext blockPlaceContext, BlockState blockState, CallbackInfoReturnable<Boolean> cir) {
|
||||
if (blockPlaceContext.getPlayer() instanceof ServerPlayer && PLAYER_PLACING_BLOCK.isNeeded()) {
|
||||
if (PLAYER_PLACING_BLOCK.onBlockPlaced((ServerPlayer) blockPlaceContext.getPlayer(), blockPlaceContext.getClickedPos(), blockPlaceContext.getHand(), blockPlaceContext.getItemInHand())) {
|
||||
cir.setReturnValue(false);
|
||||
cir.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public abstract class LivingEntity_scarpetEventsMixin extends Entity implements
|
||||
((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.Event.ON_DEATH, damageSource_1.msgId);
|
||||
}
|
||||
|
||||
@Inject(method = "actuallyHurt", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(
|
||||
@Inject(method = "actuallyHurt", cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD, at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraft/world/entity/LivingEntity;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F",
|
||||
shift = At.Shift.BEFORE
|
||||
@@ -52,7 +52,9 @@ public abstract class LivingEntity_scarpetEventsMixin extends Entity implements
|
||||
//}
|
||||
if (source.getEntity() instanceof ServerPlayer && PLAYER_DEALS_DAMAGE.isNeeded())
|
||||
{
|
||||
PLAYER_DEALS_DAMAGE.onDamage(this, amount, source);
|
||||
if(PLAYER_DEALS_DAMAGE.onDamage(this, amount, source)) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ public abstract class Player_scarpetEventsMixin extends LivingEntity
|
||||
super(type, world);
|
||||
}
|
||||
|
||||
@Inject(method = "actuallyHurt", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(
|
||||
@Inject(method = "actuallyHurt", cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD, at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraft/world/entity/player/Player;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F"
|
||||
))
|
||||
@@ -42,11 +42,15 @@ public abstract class Player_scarpetEventsMixin extends LivingEntity
|
||||
((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.Event.ON_DAMAGE, amount, source);
|
||||
if (PLAYER_TAKES_DAMAGE.isNeeded())
|
||||
{
|
||||
PLAYER_TAKES_DAMAGE.onDamage(this, amount, source);
|
||||
if(PLAYER_TAKES_DAMAGE.onDamage(this, amount, source)) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
if (source.getEntity() instanceof ServerPlayer && PLAYER_DEALS_DAMAGE.isNeeded())
|
||||
{
|
||||
PLAYER_DEALS_DAMAGE.onDamage(this, amount, source);
|
||||
if(PLAYER_DEALS_DAMAGE.onDamage(this, amount, source)) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,21 +63,26 @@ public abstract class Player_scarpetEventsMixin extends LivingEntity
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "interactOn", at = @At("HEAD"))
|
||||
@Inject(method = "interactOn", cancellable = true, at = @At("HEAD"))
|
||||
private void doInteract(Entity entity, InteractionHand hand, CallbackInfoReturnable<InteractionResult> cir)
|
||||
{
|
||||
if (!level.isClientSide && PLAYER_INTERACTS_WITH_ENTITY.isNeeded())
|
||||
{
|
||||
PLAYER_INTERACTS_WITH_ENTITY.onEntityHandAction((ServerPlayer) (Object)this, entity, hand);
|
||||
if(PLAYER_INTERACTS_WITH_ENTITY.onEntityHandAction((ServerPlayer) (Object)this, entity, hand)) {
|
||||
cir.setReturnValue(InteractionResult.PASS);
|
||||
cir.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "attack", at = @At("HEAD"))
|
||||
@Inject(method = "attack", at = @At("HEAD"), cancellable = true)
|
||||
private void onAttack(Entity target, CallbackInfo ci)
|
||||
{
|
||||
if (!level.isClientSide && PLAYER_ATTACKS_ENTITY.isNeeded() && target.isAttackable())
|
||||
{
|
||||
PLAYER_ATTACKS_ENTITY.onEntityHandAction((ServerPlayer) (Object)this, target, null);
|
||||
if(PLAYER_ATTACKS_ENTITY.onEntityHandAction((ServerPlayer) (Object)this, target, null)) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class ServerGamePacketListenerImpl_scarpetEventsMixin
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "handlePlayerAction", at = @At(
|
||||
@Inject(method = "handlePlayerAction", cancellable = true, at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraft/server/level/ServerPlayer;drop(Z)Z", // dropSelectedItem
|
||||
ordinal = 0,
|
||||
@@ -69,10 +69,12 @@ public class ServerGamePacketListenerImpl_scarpetEventsMixin
|
||||
))
|
||||
private void onQItem(ServerboundPlayerActionPacket playerActionC2SPacket_1, CallbackInfo ci)
|
||||
{
|
||||
PLAYER_DROPS_ITEM.onPlayerEvent(player);
|
||||
if(PLAYER_DROPS_ITEM.onPlayerEvent(player)) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "handlePlayerAction", at = @At(
|
||||
@Inject(method = "handlePlayerAction", cancellable = true, at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraft/server/level/ServerPlayer;getItemInHand(Lnet/minecraft/world/InteractionHand;)Lnet/minecraft/world/item/ItemStack;",
|
||||
ordinal = 0,
|
||||
@@ -80,10 +82,10 @@ public class ServerGamePacketListenerImpl_scarpetEventsMixin
|
||||
))
|
||||
private void onHandSwap(ServerboundPlayerActionPacket playerActionC2SPacket_1, CallbackInfo ci)
|
||||
{
|
||||
PLAYER_SWAPS_HANDS.onPlayerEvent(player);
|
||||
if(PLAYER_SWAPS_HANDS.onPlayerEvent(player)) ci.cancel();
|
||||
}
|
||||
|
||||
@Inject(method = "handlePlayerAction", at = @At(
|
||||
@Inject(method = "handlePlayerAction", cancellable = true, at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraft/server/level/ServerPlayer;drop(Z)Z", // dropSelectedItem
|
||||
ordinal = 1,
|
||||
@@ -91,7 +93,9 @@ public class ServerGamePacketListenerImpl_scarpetEventsMixin
|
||||
))
|
||||
private void onCtrlQItem(ServerboundPlayerActionPacket playerActionC2SPacket_1, CallbackInfo ci)
|
||||
{
|
||||
PLAYER_DROPS_STACK.onPlayerEvent(player);
|
||||
if(PLAYER_DROPS_STACK.onPlayerEvent(player)) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +108,7 @@ public class ServerGamePacketListenerImpl_scarpetEventsMixin
|
||||
PLAYER_JUMPS.onPlayerEvent(player);
|
||||
}
|
||||
|
||||
@Inject(method = "handlePlayerAction", at = @At(
|
||||
@Inject(method = "handlePlayerAction", cancellable = true, at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraft/server/level/ServerPlayerGameMode;handleBlockBreakAction(Lnet/minecraft/core/BlockPos;Lnet/minecraft/network/protocol/game/ServerboundPlayerActionPacket$Action;Lnet/minecraft/core/Direction;II)V",
|
||||
shift = At.Shift.BEFORE
|
||||
@@ -112,7 +116,9 @@ public class ServerGamePacketListenerImpl_scarpetEventsMixin
|
||||
private void onClicked(ServerboundPlayerActionPacket packet, CallbackInfo ci)
|
||||
{
|
||||
if (packet.getAction() == ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK)
|
||||
PLAYER_CLICKS_BLOCK.onBlockAction(player, packet.getPos(), packet.getDirection());
|
||||
if(PLAYER_CLICKS_BLOCK.onBlockAction(player, packet.getPos(), packet.getDirection())) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@Redirect(method = "handlePlayerAction", at = @At(
|
||||
@@ -134,7 +140,7 @@ public class ServerGamePacketListenerImpl_scarpetEventsMixin
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "handleUseItemOn", at = @At(
|
||||
@Inject(method = "handleUseItemOn", cancellable = true, at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraft/server/level/ServerPlayerGameMode;useItemOn(Lnet/minecraft/server/level/ServerPlayer;Lnet/minecraft/world/level/Level;Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/InteractionHand;Lnet/minecraft/world/phys/BlockHitResult;)Lnet/minecraft/world/InteractionResult;"
|
||||
))
|
||||
@@ -144,11 +150,13 @@ public class ServerGamePacketListenerImpl_scarpetEventsMixin
|
||||
{
|
||||
InteractionHand hand = playerInteractBlockC2SPacket_1.getHand();
|
||||
BlockHitResult hitRes = playerInteractBlockC2SPacket_1.getHitResult();
|
||||
PLAYER_RIGHT_CLICKS_BLOCK.onBlockHit(player, hand, hitRes);
|
||||
if(PLAYER_RIGHT_CLICKS_BLOCK.onBlockHit(player, hand, hitRes)) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "handleUseItem", at = @At(
|
||||
@Inject(method = "handleUseItem", cancellable = true, at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraft/server/level/ServerPlayer;resetLastActionTime()V"
|
||||
))
|
||||
@@ -157,7 +165,9 @@ public class ServerGamePacketListenerImpl_scarpetEventsMixin
|
||||
if (PLAYER_USES_ITEM.isNeeded())
|
||||
{
|
||||
InteractionHand hand = playerInteractItemC2SPacket_1.getHand();
|
||||
PLAYER_USES_ITEM.onItemAction(player, hand, player.getItemInHand(hand).copy());
|
||||
if(PLAYER_USES_ITEM.onItemAction(player, hand, player.getItemInHand(hand).copy())) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,12 +261,12 @@ public class ServerGamePacketListenerImpl_scarpetEventsMixin
|
||||
// crafts not int the crafting window
|
||||
//CarpetSettings.LOG.error("Player clicks button "+packet.getButtonId());
|
||||
}
|
||||
@Inject(method = "handlePlaceRecipe", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;resetLastActionTime()V"))
|
||||
@Inject(method = "handlePlaceRecipe", cancellable = true, at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;resetLastActionTime()V"))
|
||||
private void onRecipeSelectedInRecipeManager(ServerboundPlaceRecipePacket packet, CallbackInfo ci)
|
||||
{
|
||||
if (PLAYER_CHOOSES_RECIPE.isNeeded())
|
||||
{
|
||||
PLAYER_CHOOSES_RECIPE.onRecipeSelected(player, packet.getRecipe(), packet.isShiftDown());
|
||||
if(PLAYER_CHOOSES_RECIPE.onRecipeSelected(player, packet.getRecipe(), packet.isShiftDown())) ci.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,12 +293,13 @@ public class ServerGamePacketListenerImpl_scarpetEventsMixin
|
||||
}
|
||||
|
||||
@Inject(method = "handleChat(Lnet/minecraft/network/protocol/game/ServerboundChatPacket;)V",
|
||||
at = @At(value = "HEAD")
|
||||
at = @At(value = "HEAD"),
|
||||
cancellable = true
|
||||
)
|
||||
private void onChatMessage(ServerboundChatPacket serverboundChatPacket, CallbackInfo ci) {
|
||||
if (PLAYER_MESSAGE.isNeeded())
|
||||
{
|
||||
PLAYER_MESSAGE.onPlayerMessage(player, serverboundChatPacket.message());
|
||||
if(PLAYER_MESSAGE.onPlayerMessage(player, serverboundChatPacket.message())) ci.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,14 +38,18 @@ public class ServerPlayerGameMode_scarpetEventsMixin implements ServerPlayerInte
|
||||
|
||||
@Shadow public ServerLevel level;
|
||||
|
||||
@Inject(method = "destroyBlock", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(
|
||||
@Inject(method = "destroyBlock", locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true, at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraft/world/level/block/Block;playerWillDestroy(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/entity/player/Player;)V",
|
||||
shift = At.Shift.BEFORE
|
||||
))
|
||||
private void onBlockBroken(BlockPos blockPos_1, CallbackInfoReturnable<Boolean> cir, BlockState blockState_1, BlockEntity be, Block b)
|
||||
{
|
||||
PLAYER_BREAK_BLOCK.onBlockBroken(player, blockPos_1, blockState_1);
|
||||
if(PLAYER_BREAK_BLOCK.onBlockBroken(player, blockPos_1, blockState_1)) {
|
||||
this.level.sendBlockUpdated(blockPos_1, blockState_1, blockState_1, 3);
|
||||
cir.setReturnValue(false);
|
||||
cir.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "useItemOn", at = @At(
|
||||
|
||||
@@ -56,9 +56,10 @@ public abstract class ServerPlayer_scarpetEventMixin extends Player implements S
|
||||
if (PLAYER_FINISHED_USING_ITEM.isNeeded())
|
||||
{
|
||||
InteractionHand hand = getUsedItemHand();
|
||||
PLAYER_FINISHED_USING_ITEM.onItemAction((ServerPlayer) (Object)this, hand, getUseItem());
|
||||
// do vanilla
|
||||
super.completeUsingItem();
|
||||
if(!PLAYER_FINISHED_USING_ITEM.onItemAction((ServerPlayer) (Object)this, hand, getUseItem())) {
|
||||
// do vanilla
|
||||
super.completeUsingItem();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user