Search in sources :

Example 91 with Redirect

use of org.spongepowered.asm.mixin.injection.Redirect in project SpongeCommon by SpongePowered.

the class MixinNetHandlerPlayServer method onDisconnectHandler.

@Redirect(method = "onDisconnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/management/PlayerList;sendMessage(Lnet/minecraft/util/text/ITextComponent;)V"))
public void onDisconnectHandler(PlayerList this$0, ITextComponent component) {
    // be fired either.
    if (this.player.connection == null) {
        return;
    }
    final Player player = ((Player) this.player);
    final Text message = SpongeTexts.toText(component);
    final MessageChannel originalChannel = player.getMessageChannel();
    Sponge.getCauseStackManager().pushCause(player);
    final ClientConnectionEvent.Disconnect event = SpongeEventFactory.createClientConnectionEventDisconnect(Sponge.getCauseStackManager().getCurrentCause(), originalChannel, Optional.of(originalChannel), new MessageEvent.MessageFormatter(message), player, false);
    SpongeImpl.postEvent(event);
    Sponge.getCauseStackManager().popCause();
    if (!event.isMessageCancelled()) {
        event.getChannel().ifPresent(channel -> channel.send(player, event.getMessage()));
    }
    ((IMixinEntityPlayerMP) this.player).getWorldBorderListener().onPlayerDisconnect();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) IMixinEntityPlayer(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) CPacketPlayer(net.minecraft.network.play.client.CPacketPlayer) IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) MessageEvent(org.spongepowered.api.event.message.MessageEvent) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) Text(org.spongepowered.api.text.Text) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 92 with Redirect

use of org.spongepowered.asm.mixin.injection.Redirect in project SpongeCommon by SpongePowered.

the class MixinNetHandlerPlayServer method throwMoveEvent.

/**
 * @author gabizou - June 22nd, 2016
 * @reason Sponge has to throw the movement events before we consider moving the player and there's
 * no clear way to go about it with the target position being null and the last position update checks.
 * @param packetIn
 */
@Redirect(method = "processPlayer", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/player/EntityPlayerMP;queuedEndExit:Z"))
private boolean throwMoveEvent(EntityPlayerMP playerMP, CPacketPlayer packetIn) {
    if (!playerMP.queuedEndExit) {
        // We don't fire an event to avoid confusing plugins.
        if (!packetIn.moving && !packetIn.rotating) {
            return playerMP.queuedEndExit;
        }
        // Sponge Start - Movement event
        Player player = (Player) this.player;
        IMixinEntityPlayerMP mixinPlayer = (IMixinEntityPlayerMP) this.player;
        Vector3d fromrot = player.getRotation();
        // If Sponge used the player's current location, the delta might never be triggered which could be exploited
        Location<World> from = player.getLocation();
        if (this.lastMoveLocation != null) {
            from = this.lastMoveLocation;
        }
        Vector3d torot = new Vector3d(packetIn.pitch, packetIn.yaw, 0);
        Location<World> to = new Location<>(player.getWorld(), packetIn.x, packetIn.y, packetIn.z);
        // Minecraft sends a 0, 0, 0 position when rotation only update occurs, this needs to be recognized and corrected
        boolean rotationOnly = !packetIn.moving && packetIn.rotating;
        if (rotationOnly) {
            // Correct the to location so it's not misrepresented to plugins, only when player rotates without moving
            // In this case it's only a rotation update, which isn't related to the to location
            from = player.getLocation();
            to = from;
        }
        // Minecraft does the same with rotation when it's only a positional update
        boolean positionOnly = packetIn.moving && !packetIn.rotating;
        if (positionOnly) {
            // Correct the new rotation to match the old rotation
            torot = fromrot;
        }
        ((IMixinEntityPlayerMP) this.player).setVelocityOverride(to.getPosition().sub(from.getPosition()));
        double deltaSquared = to.getPosition().distanceSquared(from.getPosition());
        double deltaAngleSquared = fromrot.distanceSquared(torot);
        // eventually it would be nice to not have them
        if (deltaSquared > ((1f / 16) * (1f / 16)) || deltaAngleSquared > (.15f * .15f)) {
            Transform<World> fromTransform = player.getTransform().setLocation(from).setRotation(fromrot);
            Transform<World> toTransform = player.getTransform().setLocation(to).setRotation(torot);
            Sponge.getCauseStackManager().pushCause(player);
            MoveEntityEvent event = SpongeEventFactory.createMoveEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), fromTransform, toTransform, player);
            SpongeImpl.postEvent(event);
            Sponge.getCauseStackManager().popCause();
            if (event.isCancelled()) {
                mixinPlayer.setLocationAndAngles(fromTransform);
                this.lastMoveLocation = from;
                ((IMixinEntityPlayerMP) this.player).setVelocityOverride(null);
                return true;
            } else if (!event.getToTransform().equals(toTransform)) {
                mixinPlayer.setLocationAndAngles(event.getToTransform());
                this.lastMoveLocation = event.getToTransform().getLocation();
                ((IMixinEntityPlayerMP) this.player).setVelocityOverride(null);
                return true;
            } else if (!from.equals(player.getLocation()) && this.justTeleported) {
                this.lastMoveLocation = player.getLocation();
                // Prevent teleports during the move event from causing odd behaviors
                this.justTeleported = false;
                ((IMixinEntityPlayerMP) this.player).setVelocityOverride(null);
                return true;
            } else {
                this.lastMoveLocation = event.getToTransform().getLocation();
            }
            this.resendLatestResourcePackRequest();
        }
    }
    return playerMP.queuedEndExit;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) IMixinEntityPlayer(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) CPacketPlayer(net.minecraft.network.play.client.CPacketPlayer) IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) Vector3d(com.flowpowered.math.vector.Vector3d) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) World(org.spongepowered.api.world.World) Location(org.spongepowered.api.world.Location) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 93 with Redirect

use of org.spongepowered.asm.mixin.injection.Redirect in project SpongeCommon by SpongePowered.

the class MixinNetHandlerPlayServer method onProcessRightClickBlock.

@Redirect(method = "processTryUseItemOnBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/management/PlayerInteractionManager;processRightClickBlock(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/world/World;Lnet/minecraft/item/ItemStack;Lnet/minecraft/util/EnumHand;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;FFF)Lnet/minecraft/util/EnumActionResult;"))
public EnumActionResult onProcessRightClickBlock(PlayerInteractionManager interactionManager, EntityPlayer player, net.minecraft.world.World worldIn, @Nullable ItemStack stack, EnumHand hand, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ) {
    EnumActionResult actionResult = interactionManager.processRightClickBlock(this.player, worldIn, stack, hand, pos, facing, hitX, hitY, hitZ);
    // since the client will send the server a CPacketTryUseItem right after this packet is done processing.
    if (actionResult != EnumActionResult.SUCCESS) {
        // If a plugin or mod has changed the item, avoid restoring
        if (!SpongeCommonEventFactory.playerInteractItemChanged) {
            final PhaseTracker phaseTracker = PhaseTracker.getInstance();
            final PhaseData peek = phaseTracker.getCurrentPhaseData();
            final ItemStack itemStack = ItemStackUtil.toNative(((PacketContext<?>) peek.context).getItemUsed());
            // cancelling item usage (e.g. eating food) that occurs while targeting a block
            if (!ItemStack.areItemStacksEqual(itemStack, player.getHeldItem(hand)) || SpongeCommonEventFactory.interactBlockEventCancelled) {
                PacketPhaseUtil.handlePlayerSlotRestore((EntityPlayerMP) player, itemStack, hand);
            }
        }
    }
    SpongeCommonEventFactory.playerInteractItemChanged = false;
    SpongeCommonEventFactory.interactBlockEventCancelled = false;
    return actionResult;
}
Also used : EnumActionResult(net.minecraft.util.EnumActionResult) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) PhaseData(org.spongepowered.common.event.tracking.PhaseData) ItemStack(net.minecraft.item.ItemStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 94 with Redirect

use of org.spongepowered.asm.mixin.injection.Redirect in project SpongeCommon by SpongePowered.

the class MixinPotion method onPotionRegister.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Redirect(method = "registerPotions", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/registry/RegistryNamespaced;register(ILjava/lang/Object;Ljava/lang/Object;)V"))
private static void onPotionRegister(RegistryNamespaced registry, int id, Object location, Object potion) {
    final ResourceLocation resource = (ResourceLocation) location;
    final Potion mcPotion = (Potion) potion;
    ((IMixinPotion) mcPotion).setId(resource.toString().toLowerCase(Locale.ENGLISH));
    PotionEffectTypeRegistryModule.getInstance().registerFromGameData(resource.toString(), (PotionEffectType) mcPotion);
    registry.register(id, location, potion);
}
Also used : IMixinPotion(org.spongepowered.common.interfaces.potion.IMixinPotion) Potion(net.minecraft.potion.Potion) ResourceLocation(net.minecraft.util.ResourceLocation) IMixinPotion(org.spongepowered.common.interfaces.potion.IMixinPotion) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 95 with Redirect

use of org.spongepowered.asm.mixin.injection.Redirect in project SpongeCommon by SpongePowered.

the class MixinCommandDefaultGameMode method onSetDefaultGameType.

/**
 * @author Minecrell - September 28, 2016
 * @author dualspiral - December 29, 2017
 * @reason Change game mode only in the world the command was executed in
 *         Only apply game mode to those without the override permission
 */
@Redirect(method = "execute", at = @At(value = "INVOKE", target = "Lnet/minecraft/command/CommandDefaultGameMode;" + "setDefaultGameType(Lnet/minecraft/world/GameType;Lnet/minecraft/server/MinecraftServer;)V"))
private void onSetDefaultGameType(CommandDefaultGameMode self, GameType type, MinecraftServer server, MinecraftServer server2, ICommandSender sender, String[] args) {
    World world = sender.getEntityWorld();
    world.getWorldInfo().setGameType(type);
    if (server.getForceGamemode()) {
        for (EntityPlayer player : world.playerEntities) {
            if (!((IMixinEntityPlayerMP) player).hasForcedGamemodeOverridePermission()) {
                player.setGameType(type);
            }
        }
    }
}
Also used : EntityPlayer(net.minecraft.entity.player.EntityPlayer) World(net.minecraft.world.World) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

Redirect (org.spongepowered.asm.mixin.injection.Redirect)165 CauseStackManager (org.spongepowered.api.event.CauseStackManager)25 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)15 ItemStack (net.minecraft.item.ItemStack)13 EffectTransactor (org.spongepowered.common.event.tracking.context.transaction.EffectTransactor)13 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)12 TransactionalCaptureSupplier (org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier)11 ArrayList (java.util.ArrayList)9 ItemStack (net.minecraft.world.item.ItemStack)9 Nullable (javax.annotation.Nullable)8 Vector3d (com.flowpowered.math.vector.Vector3d)7 World (net.minecraft.world.World)7 ServerLocation (org.spongepowered.api.world.server.ServerLocation)7 DamageSourceBridge (org.spongepowered.common.bridge.world.damagesource.DamageSourceBridge)7 IPhaseState (org.spongepowered.common.event.tracking.IPhaseState)7 IBlockState (net.minecraft.block.state.IBlockState)6 Entity (net.minecraft.entity.Entity)6 EntityPlayer (net.minecraft.entity.player.EntityPlayer)6 ServerLevel (net.minecraft.server.level.ServerLevel)6 EntityItem (net.minecraft.entity.item.EntityItem)5