Search in sources :

Example 6 with Redirect

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

the class MixinBlockStateContainer method setFixedBlockState.

/**
 * @author barteks2x
 *
 * Attempts to fix invalid block metadata instead of completely throwing away the block.
 * When block state lookup returns null - gets block by ID and attempts to use the default state.
 */
@Redirect(method = "setDataFromNBT", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/chunk/BlockStateContainer;set(ILnet/minecraft/block/state/IBlockState;)V"))
private void setFixedBlockState(BlockStateContainer this_, int i, IBlockState state, byte[] id, NibbleArray meta, @Nullable NibbleArray add) {
    IBlockState newState;
    if (state != null) {
        newState = state;
    } else {
        int x = i & 15;
        int y = i >> 8 & 15;
        int z = i >> 4 & 15;
        int idAdd = add == null ? 0 : add.get(x, y, z);
        int blockId = idAdd << 8 | (id[i] & 255);
        Block block = Block.REGISTRY.getObjectById(blockId);
        if (block != null) {
            newState = block.getDefaultState();
        } else {
            newState = null;
        }
    }
    this.set(i, newState);
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) Block(net.minecraft.block.Block) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 7 with Redirect

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

the class MixinWorldGenDungeons method onSetEntityName.

@Redirect(method = "generate", at = @At(value = "INVOKE", target = "Lnet/minecraft/tileentity/MobSpawnerBaseLogic;setEntityId(Lnet/minecraft/util/ResourceLocation;)V"))
public void onSetEntityName(MobSpawnerBaseLogic logic, ResourceLocation mobName) {
    if (this.data != null) {
        // Use custom spawner data
        SpawnerUtils.applyData(logic, this.data);
        return;
    }
    if (this.choices != null) {
        // Use custom choices
        WeightedTable<EntityArchetype> choices = this.getChoices().get();
        EntityArchetype entity = choices.get(logic.getSpawnerWorld().rand).stream().findFirst().orElse(null);
        if (entity == null) {
            // No choices to choose from? Use default instead.
            return;
        }
        SpawnerUtils.setNextEntity(logic, new WeightedSerializableObject<>(entity, 1));
        return;
    }
    // Just use the given mobName
    logic.setEntityId(mobName);
}
Also used : EntityArchetype(org.spongepowered.api.entity.EntityArchetype) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 8 with Redirect

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

the class MixinPlayerAdvancements method onUnregisterListenersGetProgress.

@Redirect(method = "registerListeners(Lnet/minecraft/advancements/Advancement;)V", at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/advancements/CriterionProgress;isObtained()Z"))
private boolean onUnregisterListenersGetProgress(CriterionProgress progress) {
    final AdvancementCriterion criterion = ((org.spongepowered.api.advancement.criteria.CriterionProgress) progress).getCriterion();
    final IMixinCriterion mixinCriterion = (IMixinCriterion) criterion;
    // Only remove the trigger once the goal is reached
    if (mixinCriterion.getScoreCriterion() != null) {
        return ((IMixinCriterionProgress) progress).getAdvancementProgress().get(mixinCriterion.getScoreCriterion()).get().achieved();
    }
    return progress.isObtained();
}
Also used : IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) CriterionProgress(net.minecraft.advancements.CriterionProgress) IMixinCriterionProgress(org.spongepowered.common.interfaces.advancement.IMixinCriterionProgress) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 9 with Redirect

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

the class MixinSaveHandler method spongeReadPlayerData.

/**
 * Redirects the reader such that since the player file existed already, we can safely assume
 * we can grab the file attributes and check if the first join time exists in the sponge compound,
 * if it does not, then we add it to the sponge data part of the compound.
 *
 * @param inputStream The input stream to direct to compressed stream tools
 * @return The compound that may be modified
 * @throws IOException
 */
@Redirect(method = READ_PLAYER_DATA, at = @At(value = "INVOKE", target = COMPRESSED_READ_FILE))
private NBTTagCompound spongeReadPlayerData(InputStream inputStream) throws IOException {
    Instant creation = this.file == null ? Instant.now() : Files.readAttributes(this.file, BasicFileAttributes.class).creationTime().toInstant();
    NBTTagCompound compound = CompressedStreamTools.readCompressed(inputStream);
    Instant lastPlayed = Instant.now();
    // first try to migrate bukkit join data stuff
    if (compound.hasKey(NbtDataUtil.BUKKIT, NbtDataUtil.TAG_COMPOUND)) {
        final NBTTagCompound bukkitCompound = compound.getCompoundTag(NbtDataUtil.BUKKIT);
        creation = Instant.ofEpochMilli(bukkitCompound.getLong(NbtDataUtil.BUKKIT_FIRST_PLAYED));
        lastPlayed = Instant.ofEpochMilli(bukkitCompound.getLong(NbtDataUtil.BUKKIT_LAST_PLAYED));
    }
    UUID playerId = null;
    if (compound.hasUniqueId(NbtDataUtil.UUID)) {
        playerId = compound.getUniqueId(NbtDataUtil.UUID);
    }
    if (playerId != null) {
        Optional<Instant> savedFirst = SpongePlayerDataHandler.getFirstJoined(playerId);
        if (savedFirst.isPresent()) {
            creation = savedFirst.get();
        }
        Optional<Instant> savedJoined = SpongePlayerDataHandler.getLastPlayed(playerId);
        if (savedJoined.isPresent()) {
            lastPlayed = savedJoined.get();
        }
        SpongePlayerDataHandler.setPlayerInfo(playerId, creation, lastPlayed);
    }
    this.file = null;
    return compound;
}
Also used : Instant(java.time.Instant) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) UUID(java.util.UUID) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 10 with Redirect

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

the class MixinCommandScoreboard method onGetUUIDLeave.

@Redirect(method = "leaveTeam", at = @At(value = "INVOKE", target = ITERATOR_NEXT, ordinal = 0, remap = false))
private Object onGetUUIDLeave(Iterator<Entity> iterator) {
    Entity entity = iterator.next();
    this.onGetUUID(entity);
    return entity;
}
Also used : Entity(net.minecraft.entity.Entity) 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