Search in sources :

Example 26 with Redirect

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

the class MixinLoader method discoverMods.

@Redirect(method = "identifyMods", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/discovery/ModDiscoverer;findModDirMods(Ljava/io/File;[Ljava/io/File;)V", remap = false))
private void discoverMods(ModDiscoverer modDiscoverer, File modsDir, File[] additionalMods) {
    modDiscoverer.findModDirMods(modsDir, additionalMods);
    File pluginsDir = this.getPluginsDir();
    if (pluginsDir.isDirectory() && !pluginsDir.equals(modsDir)) {
        FMLLog.info("Searching %s for plugins", pluginsDir.getAbsolutePath());
        this.discoverPlugins(modDiscoverer, pluginsDir);
    }
}
Also used : File(java.io.File) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 27 with Redirect

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

the class MixinEventBus method onUnregisterListener.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Redirect(method = "unregister", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/eventhandler/ListenerList;unregisterAll(ILnet/minecraftforge/fml/common/eventhandler/IEventListener;)V", remap = false))
public void onUnregisterListener(int id, IEventListener listener) {
    ListenerList.unregisterAll(id, listener);
    SpongeModEventManager manager = ((SpongeModEventManager) SpongeImpl.getGame().getEventManager());
    for (Class clazz : TypeToken.of(checkNotNull(this.forgeListenerRegistry.remove(listener))).getTypes().rawTypes()) {
        Collection<Class<? extends org.spongepowered.api.event.Event>> spongeEvents = manager.forgeToSpongeEventMapping.get(clazz);
        if (spongeEvents != null) {
            for (Class<? extends org.spongepowered.api.event.Event> event : spongeEvents) {
                manager.checker.unregisterListenerFor(event);
            }
        }
    }
}
Also used : SpongeModEventManager(org.spongepowered.mod.event.SpongeModEventManager) Event(net.minecraftforge.fml.common.eventhandler.Event) WorldEvent(net.minecraftforge.event.world.WorldEvent) LivingDropsEvent(net.minecraftforge.event.entity.living.LivingDropsEvent) AttackEntityEvent(net.minecraftforge.event.entity.player.AttackEntityEvent) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 28 with Redirect

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

the class MixinAdvancementManager method onLoadAdvancements.

@Redirect(method = "reload", at = @At(value = "INVOKE", target = "Lnet/minecraft/advancements/AdvancementList;loadAdvancements(Ljava/util/Map;)V"))
private void onLoadAdvancements(AdvancementList advancementList, Map<ResourceLocation, Advancement.Builder> advancements) {
    if (!SpongeImpl.isInitialized()) {
        return;
    }
    for (PluginContainer pluginContainer : Sponge.getPluginManager().getPlugins()) {
        // Skip the minecraft advancements
        if (pluginContainer.getId().equals("minecraft")) {
            continue;
        }
        final Optional<Path> optSource = pluginContainer.getSource();
        if (optSource.isPresent()) {
            final Path source = optSource.get();
            final String base = "assets/" + pluginContainer.getId() + "/advancements";
            Path root;
            if (Files.isDirectory(source)) {
                root = source.resolve(base);
            } else {
                try {
                    final FileSystem fileSystem = FileSystems.newFileSystem(source, null);
                    root = fileSystem.getPath("/" + base);
                } catch (IOException e) {
                    SpongeImpl.getLogger().error("Error loading FileSystem from jar: ", e);
                    continue;
                }
            }
            if (!Files.exists(root)) {
                continue;
            }
            try {
                Files.walk(root).forEach(path -> {
                    if (!FilenameUtils.getExtension(path.getFileName().toString()).equals("json")) {
                        return;
                    }
                    final Path relPath = root.relativize(path);
                    final String id = FilenameUtils.removeExtension(relPath.toString()).replaceAll("\\\\", "/");
                    final ResourceLocation resourceLocation = new ResourceLocation(pluginContainer.getId(), id);
                    if (!advancements.containsKey(resourceLocation)) {
                        try (BufferedReader reader = Files.newBufferedReader(path)) {
                            final Advancement.Builder advancementBuilder = JsonUtils.fromJson(GSON, reader, Advancement.Builder.class);
                            advancements.put(resourceLocation, advancementBuilder);
                        } catch (IOException e) {
                            SpongeImpl.getLogger().error("Failed to read advancement " + resourceLocation + " from path " + path, e);
                        }
                    }
                });
            } catch (IOException e) {
                SpongeImpl.getLogger().error("Failed to walk path: " + root, e);
            }
        }
    }
    advancementList.loadAdvancements(advancements);
}
Also used : Path(java.nio.file.Path) PluginContainer(org.spongepowered.api.plugin.PluginContainer) FileSystem(java.nio.file.FileSystem) ResourceLocation(net.minecraft.util.ResourceLocation) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Advancement(net.minecraft.advancements.Advancement) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 29 with Redirect

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

the class MixinEntityLivingBase method onSetHeldItem.

@Redirect(method = "onItemUseFinish", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/EntityLivingBase;" + "setHeldItem(Lnet/minecraft/util/EnumHand;Lnet/minecraft/item/ItemStack;)V"))
private void onSetHeldItem(EntityLivingBase self, EnumHand hand, @Nullable ItemStack stack) {
    ItemStackSnapshot activeItemStackSnapshot = ItemStackUtil.snapshotOf(this.activeItemStack);
    Sponge.getCauseStackManager().pushCause(this);
    UseItemStackEvent.Replace event = SpongeEventFactory.createUseItemStackEventReplace(Sponge.getCauseStackManager().getCurrentCause(), this.activeItemStackUseCount, this.activeItemStackUseCount, activeItemStackSnapshot, new Transaction<>(activeItemStackSnapshot, ItemStackUtil.snapshotOf(stack)));
    if (SpongeImpl.postEvent(event)) {
        Sponge.getCauseStackManager().popCause();
        // Don't touch the held item if event is cancelled
        return;
    }
    Sponge.getCauseStackManager().popCause();
    if (!event.getItemStackResult().isValid()) {
        return;
    }
    setHeldItem(hand, ItemStackUtil.fromSnapshotToNative(event.getItemStackResult().getFinal()));
}
Also used : UseItemStackEvent(org.spongepowered.api.event.item.inventory.UseItemStackEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 30 with Redirect

use of org.spongepowered.asm.mixin.injection.Redirect in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class MixinWorld method rayTraceBlocksForGetBlockDensity.

/**
 * Use Bresenham's tracing algorithm instead of raytracing for getBlockDensity, makes it way faster
 */
@Redirect(method = "getBlockDensity", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;rayTraceBlocks(Lnet/minecraft/util/math/Vec3d;Lnet/minecraft/util/math/Vec3d;)Lnet/minecraft/util/math/RayTraceResult;"))
private RayTraceResult rayTraceBlocksForGetBlockDensity(World world, Vec3d start, Vec3d end) {
    if (VSConfig.explosionMode == ExplosionMode.VANILLA) {
        // Vanilla raytrace, ignore ships and perform the function like normal
        this.shouldInterceptRayTrace = false;
        RayTraceResult result = rayTraceBlocks(start, end);
        this.shouldInterceptRayTrace = true;
        return result;
    } else if (VSConfig.explosionMode == ExplosionMode.SLOW_VANILLA) {
        // Vanilla raytrace, include ships and perform the function like normal
        return rayTraceBlocks(start, end);
    }
    java.util.function.Predicate<BlockPos> canCollide = pos -> {
        IBlockState blockState = world.getBlockState(pos);
        return blockState.getBlock().canCollideCheck(blockState, false);
    };
    // Get all the blocks between start and end
    List<BlockPos> blocks = VSMath.generateLineBetween(start, end, BlockPos::new);
    // Whether or not this ray trace hit a block that was collidable.
    boolean collided = blocks.stream().anyMatch(canCollide);
    IPhysObjectWorld physObjectWorld = ((IHasShipManager) (this)).getManager();
    if (physObjectWorld != null) {
        List<PhysicsObject> nearbyShips = physObjectWorld.getPhysObjectsInAABB(new AxisAlignedBB(start.x, start.y, start.z, end.x, end.y, end.z));
        for (PhysicsObject obj : nearbyShips) {
            Vec3d transformedStart = obj.transformVector(start, TransformType.GLOBAL_TO_SUBSPACE);
            Vec3d transformedEnd = obj.transformVector(end, TransformType.GLOBAL_TO_SUBSPACE);
            // Transform the raytrace into ship space and check whether or not it hit a block
            List<BlockPos> physoBlocks = VSMath.generateLineBetween(transformedStart, transformedEnd, BlockPos::new);
            collided |= physoBlocks.stream().anyMatch(canCollide);
        }
    }
    // important information in the ray trace result.
    return collided ? DUMMY_RAYTRACE_RESULT : null;
}
Also used : Polygon(org.valkyrienskies.mod.common.collision.Polygon) Inject(org.spongepowered.asm.mixin.injection.Inject) IHasShipManager(org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager) IWorldEventListener(net.minecraft.world.IWorldEventListener) ShipPolygon(org.valkyrienskies.mod.common.collision.ShipPolygon) MixinWorldIntrinsicMethods(org.valkyrienskies.mod.fixes.MixinWorldIntrinsicMethods) Function(java.util.function.Function) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) IPhysObjectWorld(org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld) ArrayList(java.util.ArrayList) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) Lists(com.google.common.collect.Lists) EntityPolygonCollider(org.valkyrienskies.mod.common.collision.EntityPolygonCollider) ExplosionMode(org.valkyrienskies.mod.common.config.VSConfig.ExplosionMode) org.spongepowered.asm.mixin(org.spongepowered.asm.mixin) Vector3d(org.joml.Vector3d) Chunk(net.minecraft.world.chunk.Chunk) Nullable(javax.annotation.Nullable) At(org.spongepowered.asm.mixin.injection.At) Entity(net.minecraft.entity.Entity) Iterator(java.util.Iterator) VSMath(org.valkyrienskies.mod.common.util.VSMath) net.minecraft.util.math(net.minecraft.util.math) World(net.minecraft.world.World) Redirect(org.spongepowered.asm.mixin.injection.Redirect) TransformType(valkyrienwarfare.api.TransformType) EnumFacing(net.minecraft.util.EnumFacing) Remap(org.spongepowered.asm.mixin.Interface.Remap) Type(net.minecraft.util.math.RayTraceResult.Type) IBlockState(net.minecraft.block.state.IBlockState) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) List(java.util.List) ValkyrienUtils(org.valkyrienskies.mod.common.util.ValkyrienUtils) VSConfig(org.valkyrienskies.mod.common.config.VSConfig) Predicate(com.google.common.base.Predicate) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IWorldVS(org.valkyrienskies.mod.common.ships.ship_world.IWorldVS) Optional(java.util.Optional) IBlockState(net.minecraft.block.state.IBlockState) IPhysObjectWorld(org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) IHasShipManager(org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager) 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