Search in sources :

Example 81 with Redirect

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

the class CommandsMixin method impl$preventPutIntoMapIfNodeIsComplex.

@SuppressWarnings("unchecked")
@Redirect(method = "fillUsableCommands", at = @At(value = "INVOKE", target = "Ljava/util/Map;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", remap = false))
private <K, V> V impl$preventPutIntoMapIfNodeIsComplex(final Map<K, V> map, final K key, final V value, final CommandNode<CommandSourceStack> rootCommandSource, final CommandNode<SharedSuggestionProvider> rootSuggestion, final CommandSourceStack source, final Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> commandNodeToSuggestionNode) {
    if (!map.containsKey(key)) {
        // done here because this check is applicable
        final ServerPlayer e = (ServerPlayer) source.getEntity();
        final Map<CommandNode<CommandSourceStack>, List<CommandNode<SharedSuggestionProvider>>> playerNodes = this.impl$playerNodeCache.get(e);
        if (!playerNodes.containsKey(key)) {
            final List<CommandNode<SharedSuggestionProvider>> children = new ArrayList<>();
            children.add((CommandNode<SharedSuggestionProvider>) value);
            playerNodes.put((CommandNode<CommandSourceStack>) key, children);
        }
        // we need to swap it out.
        if (value instanceof ArgumentCommandNode && CommandUtil.checkForCustomSuggestions(rootSuggestion)) {
            rootSuggestion.addChild(this.impl$cloneArgumentCommandNodeWithoutSuggestions((ArgumentCommandNode<SharedSuggestionProvider, ?>) value));
        } else {
            rootSuggestion.addChild((CommandNode<SharedSuggestionProvider>) value);
        }
        return map.put(key, value);
    }
    // it's ignored anyway.
    return null;
}
Also used : ArgumentCommandNode(com.mojang.brigadier.tree.ArgumentCommandNode) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) ArgumentCommandNode(com.mojang.brigadier.tree.ArgumentCommandNode) RootCommandNode(com.mojang.brigadier.tree.RootCommandNode) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) CommandNode(com.mojang.brigadier.tree.CommandNode) ServerPlayer(net.minecraft.server.level.ServerPlayer) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SharedSuggestionProvider(net.minecraft.commands.SharedSuggestionProvider) CommandSourceStack(net.minecraft.commands.CommandSourceStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 82 with Redirect

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

the class MixinEntityArmorStand method fireDamageEventDamage.

@Redirect(method = "attackEntityFrom", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/item/EntityArmorStand;damageArmorStand(F)V"))
private void fireDamageEventDamage(EntityArmorStand self, float effectiveAmount, DamageSource source, float originalAmount) {
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        DamageEventHandler.generateCauseFor(source);
        DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), new ArrayList<>(), this, effectiveAmount);
        if (!SpongeImpl.postEvent(event)) {
            this.damageArmorStand((float) event.getFinalDamage());
        }
    }
}
Also used : DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 83 with Redirect

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

the class MixinEntityParrot method onTame.

@Redirect(method = "processInteract", at = @At(value = "INVOKE", target = "Ljava/util/Random;nextInt(I)I", ordinal = 0, remap = false))
public int onTame(Random rand, int bound, EntityPlayer player, EnumHand hand) {
    ItemStack stack = player.getHeldItem(hand);
    int random = rand.nextInt(bound);
    if (random == 0) {
        stack.setCount(stack.getCount() + 1);
        try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            Sponge.getCauseStackManager().pushCause(player);
            if (!SpongeImpl.postEvent(SpongeEventFactory.createTameEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), this))) {
                stack.setCount(stack.getCount() - 1);
                return random;
            }
        }
    }
    return 1;
}
Also used : StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) ItemStack(net.minecraft.item.ItemStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 84 with Redirect

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

the class MixinEntityWolf method onTame.

@Redirect(method = "processInteract", at = @At(value = "INVOKE", target = "Ljava/util/Random;nextInt(I)I", ordinal = 0, remap = false))
public int onTame(Random rand, int bound, EntityPlayer player, EnumHand hand) {
    int random = rand.nextInt(bound);
    ItemStack stack = player.getHeldItem(hand);
    if (random == 0) {
        stack.shrink(1);
        try {
            Sponge.getCauseStackManager().pushCause(((org.spongepowered.api.item.inventory.ItemStack) stack).createSnapshot());
            Sponge.getCauseStackManager().pushCause(player);
            if (!SpongeImpl.postEvent(SpongeEventFactory.createTameEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), this))) {
                stack.grow(1);
                return random;
            }
        } finally {
            Sponge.getCauseStackManager().popCauses(2);
        }
    }
    return 1;
}
Also used : ItemStack(net.minecraft.item.ItemStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 85 with Redirect

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

the class MixinInventoryPlayer method onAdd.

@Redirect(method = "storePartialItemStack", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/InventoryPlayer;addResource(ILnet/minecraft/item/ItemStack;)I"))
public int onAdd(InventoryPlayer inv, int index, ItemStack stack) {
    if (this.doCapture) {
        // Capture items getting picked up
        Slot slot = index == 40 ? this.getOffhand() : getSpongeSlot(index);
        ItemStackSnapshot original = ItemStackUtil.snapshotOf(this.getStackInSlot(index));
        int result = this.addResource(index, stack);
        ItemStackSnapshot replacement = ItemStackUtil.snapshotOf(this.getStackInSlot(index));
        this.capturedTransactions.add(new SlotTransaction(slot, original, replacement));
        return result;
    }
    return this.addResource(index, stack);
}
Also used : Slot(org.spongepowered.api.item.inventory.Slot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) 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