Skip to content

Events

Events let your mod react to what portals do. They work the same on every loader: each event is a static field holding an ApiEvent, and a listener is a plain functional interface, so a lambda is enough.

TeleportEvents.AFTER.register((entity, portal, from, to) -> { /* ... */ });

Register listeners while your mod initialises. Registering does not need ReforgedApi.get(), so it works even before Immersive Portals Reforged has finished its own setup. All events on this page are fired on the server thread.

TeleportEvents covers entities passing through portals: players, once the server has accepted the crossing their client reported, and every other entity.

Fired before an entity is moved. The listener receives a TeleportEvents.Pending:

Member Returns
entity() The entity about to go through.
portal() The PortalSnapshot of the surface it is crossing, as it was at that moment.
destinationLevel() The ResourceKey<Level> of the level it is going to.
destination() Where its feet will land, as a Vec3.
cancel() Prevents the teleport.
isCancelled() Whether a listener cancelled it.

Listeners run in the order they were registered, and the first one that cancels stops the rest. When a player’s teleport is cancelled, the player is sent back to the side they came from.

Fired after an entity arrived, with the entity, the portal and the keys of the level it left and the level it entered:

void onAfterTeleport(Entity entity, PortalSnapshot portal, ResourceKey<Level> from, ResourceKey<Level> to)

For entities other than players, entity may be a new instance: when vanilla moves an entity to another dimension it creates a copy there and removes the original. Use the entity you receive, not one you stored before.

PortalEvents follows portal groups. Both events give you the group id; members are listed by role.

void onCreated(ServerLevel level, UUID group, Map<GroupRole, PortalSnapshot> members)

Fired once every surface of a new group has been spawned, whatever created it: the /ipr make command, a lit Nether frame or PortalBuilder.build(). level is the level of the primary surface.

void onRemoved(ServerLevel level, UUID group, PortalEvents.RemovalCause cause)

Fired when a group is removed and at least one of its surfaces was loaded and taken away. level is the first level a surface was removed from. cause says why:

RemovalCause When
COMMAND A player ran /ipr remove.
FRAME_BROKEN A lit frame was broken or its opening was blocked.
API Reserved for removal through the API (not part of API level 1 yet).
DIMENSION_REMOVED Reserved: the destination dimension no longer exists.
INVALID Reserved: the group was found to be invalid.

In 0.1.0-alpha only COMMAND and FRAME_BROKEN are fired.

A mod that keeps bosses out of portals, announces arrivals and keeps a count of the portal groups it has seen:

PortalRules.java
package com.example.portals;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
import net.minecraft.world.entity.boss.wither.WitherBoss;
import net.night.ipreforged.api.event.PortalEvents;
import net.night.ipreforged.api.event.TeleportEvents;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public final class PortalRules {
private static final Logger LOGGER = LoggerFactory.getLogger("examplemod");
private static final Set<UUID> GROUPS = ConcurrentHashMap.newKeySet();
private PortalRules() {
}
/** Call once, while your mod initialises. */
public static void register() {
TeleportEvents.BEFORE.register(pending -> {
Entity entity = pending.entity();
if (entity instanceof WitherBoss || entity instanceof EnderDragon) {
pending.cancel();
}
});
TeleportEvents.AFTER.register((entity, portal, from, to) ->
LOGGER.info("{} went through portal {} into {}", entity.getName().getString(), portal.id(), to));
PortalEvents.CREATED.register((level, group, members) -> {
GROUPS.add(group);
LOGGER.info("Portal group {} created with {} surfaces", group, members.size());
});
PortalEvents.REMOVED.register((level, group, cause) -> {
GROUPS.remove(group);
LOGGER.info("Portal group {} removed ({})", group, cause);
});
}
public static int knownGroups() {
return GROUPS.size();
}
}

Every event above is an ApiEvent<T>, where T is the listener interface.

Member Does
register(T listener) Adds a listener. Thread-safe.
invoker() One object implementing T that calls every listener in order. This is what the mod calls to fire the event.
hasListeners() Whether anyone is listening.
ApiEvent.create(Class<T>, Function<T[], T>) Creates an event, given the listener type and a function that builds the invoker from the current listeners.

Firing an event does not allocate, and invoker() always reflects the listeners registered so far. You can use ApiEvent.create for events of your own add-on if you like the same style.