Search in sources :

Example 96 with Redirect

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

the class MixinCommandDifficulty method onSetDifficulty.

/**
 * @author Minecrell - September 28, 2016
 * @reason Change difficulty only in the world the command was executed in
 */
@Redirect(method = "execute", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;" + "setDifficultyForAllWorlds(Lnet/minecraft/world/EnumDifficulty;)V"))
private void onSetDifficulty(MinecraftServer server, EnumDifficulty difficulty, MinecraftServer server2, ICommandSender sender, String[] args) {
    World world = sender.getEntityWorld();
    // Coped from MinecraftServer.setDifficultyForAllWorlds
    if (world.getWorldInfo().isHardcoreModeEnabled()) {
        world.getWorldInfo().setDifficulty(EnumDifficulty.HARD);
        world.setAllowedSpawnTypes(true, true);
    } else if (server.isSinglePlayer()) {
        world.getWorldInfo().setDifficulty(difficulty);
        world.setAllowedSpawnTypes(world.getDifficulty() != EnumDifficulty.PEACEFUL, true);
    } else {
        world.getWorldInfo().setDifficulty(difficulty);
        world.setAllowedSpawnTypes(server.allowSpawnMonsters(), server.getCanSpawnAnimals());
    }
}
Also used : World(net.minecraft.world.World) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 97 with Redirect

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

the class MixinCommandToggleDownfall method onToggleDownfall.

@Redirect(method = "execute", at = @At(value = "INVOKE", target = "Lnet/minecraft/command/CommandToggleDownfall;toggleRainfall(Lnet/minecraft/server/MinecraftServer;)V"))
private void onToggleDownfall(CommandToggleDownfall self, MinecraftServer server, MinecraftServer server2, ICommandSender sender, String[] args) {
    WorldInfo worldInfo = sender.getEntityWorld().getWorldInfo();
    worldInfo.setRaining(!worldInfo.isRaining());
}
Also used : WorldInfo(net.minecraft.world.storage.WorldInfo) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 98 with Redirect

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

the class MixinPlayerAdvancements method onUnregisterListenersGetProgress.

@Nullable
@Redirect(method = "unregisterListeners", at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/advancements/AdvancementProgress;getCriterionProgress(Ljava/lang/String;)Lnet/minecraft/advancements/CriterionProgress;"))
private CriterionProgress onUnregisterListenersGetProgress(AdvancementProgress advancementProgress, String criterion) {
    final org.spongepowered.api.advancement.Advancement advancement = ((org.spongepowered.api.advancement.AdvancementProgress) advancementProgress).getAdvancement();
    final AdvancementCriterion advancementCriterion = (AdvancementCriterion) ((Advancement) advancement).getCriteria().get(criterion);
    final IMixinCriterion mixinCriterion = (IMixinCriterion) advancementCriterion;
    // Only remove the trigger once the goal is reached
    if (mixinCriterion.getScoreCriterion() != null && !((org.spongepowered.api.advancement.AdvancementProgress) advancementProgress).get(mixinCriterion.getScoreCriterion()).get().achieved()) {
        return null;
    }
    return advancementProgress.getCriterionProgress(criterion);
}
Also used : AdvancementProgress(net.minecraft.advancements.AdvancementProgress) IMixinAdvancementProgress(org.spongepowered.common.interfaces.advancement.IMixinAdvancementProgress) IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) Advancement(net.minecraft.advancements.Advancement) Redirect(org.spongepowered.asm.mixin.injection.Redirect) Nullable(javax.annotation.Nullable)

Example 99 with Redirect

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

the class MixinBlockDispenser method onSetInventoryContents.

@Redirect(method = "dispense", at = @At(value = "INVOKE", target = "Lnet/minecraft/tileentity/TileEntityDispenser;setInventorySlotContents(ILnet/minecraft/item/ItemStack;)V"))
public void onSetInventoryContents(TileEntityDispenser dispenser, int index, @Nullable ItemStack stack) {
    final PhaseData phaseData = PhaseTracker.getInstance().getCurrentPhaseData();
    final PhaseContext<?> context = phaseData.context;
    // If we captured nothing, simply set the slot contents and return
    if (context.getCapturedItemsOrEmptyList().isEmpty()) {
        dispenser.setInventorySlotContents(index, stack);
        return;
    }
    final ItemStack dispensedItem = context.getCapturedItems().get(0).getItem();
    final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(dispensedItem);
    final List<ItemStackSnapshot> original = new ArrayList<>();
    original.add(snapshot);
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(dispenser);
        final DropItemEvent.Pre dropEvent = SpongeEventFactory.createDropItemEventPre(Sponge.getCauseStackManager().getCurrentCause(), ImmutableList.of(snapshot), original);
        SpongeImpl.postEvent(dropEvent);
        if (dropEvent.isCancelled()) {
            dispenser.setInventorySlotContents(index, (net.minecraft.item.ItemStack) this.originalItem.createStack());
            context.getCapturedItems().clear();
            return;
        }
        if (dropEvent.getDroppedItems().isEmpty()) {
            context.getCapturedItems().clear();
        }
        dispenser.setInventorySlotContents(index, stack);
    }
}
Also used : DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) PhaseData(org.spongepowered.common.event.tracking.PhaseData) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ArrayList(java.util.ArrayList) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(net.minecraft.item.ItemStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 100 with Redirect

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

the class MixinBlockFalling method onIsAreaLoadedCheck.

@Redirect(method = "checkFallable", at = @At(value = "INVOKE", target = WORLD_IS_AREA_LOADED))
private boolean onIsAreaLoadedCheck(World world, BlockPos pos, BlockPos to) {
    if (world.isAreaLoaded(pos, to)) {
        if (!world.isRemote) {
            BlockPos actualPos = pos.add(32, 32, 32);
            EntityType fallingBlock = EntityTypes.FALLING_BLOCK;
            Vector3d position = new Vector3d(actualPos.getX() + 0.5D, actualPos.getY(), actualPos.getZ() + 0.5D);
            BlockSnapshot snapshot = ((org.spongepowered.api.world.World) world).createSnapshot(actualPos.getX(), actualPos.getY(), actualPos.getZ());
            try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
                Sponge.getCauseStackManager().pushCause(snapshot);
                Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.FALLING_BLOCK);
                Transform<org.spongepowered.api.world.World> worldTransform = new Transform<>((org.spongepowered.api.world.World) world, position);
                ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), fallingBlock, worldTransform);
                SpongeImpl.postEvent(event);
                return !event.isCancelled();
            }
        }
    }
    return false;
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(net.minecraft.world.World) EntityType(org.spongepowered.api.entity.EntityType) ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) Vector3d(com.flowpowered.math.vector.Vector3d) CauseStackManager(org.spongepowered.api.event.CauseStackManager) BlockPos(net.minecraft.util.math.BlockPos) Transform(org.spongepowered.api.entity.Transform) 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