Remote calls (RPC)
Mods often need the other side of the connection to do something: the server tells a client to open a screen or show an effect, a client asks the server to act on what the player clicked. With a loader’s own networking, every such message needs a payload type, code to encode and decode it, registration on each loader, and a handler that hops onto the right thread.
Remote calls replace all of that with one line. You name a public static method and pass its
arguments; Immersive Portals Reforged encodes them, carries the call over the network and runs the
method on the other side, on its main thread. It works the same on every loader and every supported
Minecraft version, and nothing has to be registered.
The classes are in net.night.ipreforged.api.remote. They are marked
@Experimental while the first releases collect feedback.
A first call
Section titled “A first call”Mark the methods that may be called from the other side with @RemoteEntry, saying where they
run, and call them with RemoteCalls. On the server:
package com.example;
import java.util.List;import net.minecraft.core.BlockPos;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.phys.Vec3;import net.night.ipreforged.api.remote.RemoteCalls;import net.night.ipreforged.api.remote.RemoteEntry;import net.night.ipreforged.api.remote.RemoteSide;
public final class GateCalls {
// Server code: show the gate's menu on one player's screen. public static void offerMenu(ServerPlayer player, BlockPos gate) { RemoteCalls.callClient(player, "com.example.client.GateClientCalls#showGateMenu", gate, List.of("Home", "Mine")); }
// Runs on the server when a client asks. The first parameter is that client's player. @RemoteEntry(RemoteSide.SERVER) public static void openGate(ServerPlayer player, BlockPos gate, int colour) { if (player.distanceToSqr(Vec3.atCenterOf(gate)) > 8 * 8) { return; // a client can send anything: check it } // ...open the gate }}And in a class that only the client loads:
package com.example.client;
import java.util.List;import net.minecraft.client.Minecraft;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.night.ipreforged.api.remote.RemoteCalls;import net.night.ipreforged.api.remote.RemoteEntry;import net.night.ipreforged.api.remote.RemoteSide;
// The server names this class in a string: it never loads it.public final class GateClientCalls {
// Runs on this game when the server asks. @RemoteEntry(RemoteSide.CLIENT) public static void showGateMenu(BlockPos gate, List<String> destinations) { Minecraft mc = Minecraft.getInstance(); if (mc.player != null) { mc.player.sendSystemMessage(Component.literal( "Gate at " + gate.toShortString() + ": " + String.join(", ", destinations))); } }
// Client code, e.g. when the player clicks "Open" in the menu: ask the server. public static void askToOpen(BlockPos gate) { RemoteCalls.callServer("com.example.GateCalls#openGate", gate, 0xFF8800); }}The server-side method receives the calling player first, then the arguments the client passed.
Naming the method
Section titled “Naming the method”The target is a string, "<class>#<method>", the form IDEs copy as a method reference
(in IntelliJ IDEA: right click on the method, Copy Reference). A dot works too:
"com.example.GateCalls.openGate". A nested class is written "com.example.Outer$Inner#method",
or with dots.
The class is looked up on the receiving side only. That is what lets the server name a client-only class, and a client name a class that only matters on the server.
@RemoteEntry
Section titled “@RemoteEntry”Only methods that carry @RemoteEntry can be called remotely. The mark says on which side the
method runs, so it can only be called from the other one:
| Mark | Runs on | Called with | First parameter |
|---|---|---|---|
@RemoteEntry(RemoteSide.SERVER) |
the server thread | RemoteCalls.callServer |
the calling player: ServerPlayer (or a supertype such as Player) |
@RemoteEntry(RemoteSide.CLIENT) |
the client thread | RemoteCalls.callClient and friends |
the first argument |
The method must be public static. A call that names a method without the mark, or marked for the
other side, is refused, and the class it names is not even initialised: none of its code runs.
Overloads are allowed. The call runs the one whose parameters fit the arguments best: an exact
type beats a supertype, a primitive parameter also takes a narrower number (an int argument fits
a long or double parameter), and a value read through Gson (see below) fits last. A call that
fits two overloads equally well is refused.
Making calls
Section titled “Making calls”All methods are static on RemoteCalls. They can be called from any thread.
| Method | Called on | What it does |
|---|---|---|
callClient(ServerPlayer player, String target, Object... args) |
server | Runs target on that player’s game. Returns false, and sends nothing, when that game does not have Immersive Portals Reforged. |
callClients(Iterable<? extends ServerPlayer> players, String target, Object... args) |
server | The same for several players, encoding the arguments once. Returns how many players it was sent to. |
clientCallPacket(MinecraftServer server, String target, Object... args) |
server | The packet callClient sends, for the game’s own ways of reaching many players: everyone tracking an entity or a chunk, everyone in a dimension. |
callServer(String target, Object... args) |
client | Runs target on the server. Throws IllegalStateException when no world is open. |
serverCallPacket(String target, Object... args) |
client | The packet callServer sends, to send it later or more than once. |
Send the packets from clientCallPacket only to players whose game has the mod (callClient and
callClients check that for you). On NeoForge that is every player connected to a server with the
mod.
Arguments
Section titled “Arguments”Arguments are encoded when the call is made, on the calling thread. These types are supported:
| Type | Notes |
|---|---|
null |
For any parameter that is not a primitive. |
boolean, byte, short, char, int, long, float, double |
And their boxes. A primitive parameter also takes a narrower number. |
String, UUID, byte[] |
|
| enum constants | Sent by name, matched against the parameter’s enum type. |
| identifiers | ResourceLocation; Identifier from Minecraft 1.21.11. |
ResourceKey |
Of any registry, e.g. ResourceKey<Level> for dimensions. |
BlockPos, Vec3 |
|
CompoundTag |
Up to the usual NBT size limit (2 MiB). |
Component |
Styles and all. |
ItemStack |
With its data components (NBT on 1.20.1). |
Item, Block, BlockState |
|
| anything else | Written by Gson as JSON and read back as the parameter’s declared type, generics included: records, plain classes, List<String>, Map<String, Integer>, arrays. |
Gson knows nothing about game types: a List<BlockPos> will not come back as block positions.
Send such values one by one, as long[] (BlockPos.asLong()), or inside a CompoundTag. Live
game objects (entities, levels, block entities) are refused: send what identifies them, such as
an entity’s UUID, a dimension’s ResourceKey or a block entity’s BlockPos.
A call that is too big once encoded throws IllegalArgumentException from the call: the limit is
32 KiB towards the server (what vanilla lets a client send) and 1 MiB towards a client. So does an
argument Gson cannot write.
On the other side
Section titled “On the other side”The method runs on the main thread of the receiving side, the server thread or the client thread, in the order the calls were made.
When a call cannot run (no fitting @RemoteEntry method, arguments that cannot be read) or the
method throws, the receiving side logs it: the first 64 problems of a session, with the stack
trace of exceptions your method threw. The first five are also shown in red in the chat of the
player concerned (the calling player on the server, the local player on a client). A failed call
never disconnects anyone.
Security
Section titled “Security”A @RemoteEntry(RemoteSide.SERVER) method is a door into your server: any client can call it with
any arguments, including clients that are not the real game. Check everything it receives:
- that the player may do what is asked (permissions, game mode, ownership);
- that positions are loaded, in the player’s dimension and within reach;
- that sizes, counts and strings are sane;
- that it is not called too often, when it does something expensive.
Client methods only receive calls from the server the client is connected to.
When to write your own payload
Section titled “When to write your own payload”Remote calls look methods up by name (once, then cached), tag every argument with its kind, and fall back to JSON for unknown types. That is cheap for actions, menus, effects and state changes. For data sent every tick to many players, or large data that needs its own compression, a dedicated payload on your loader’s networking is still the better tool.
