From 659a837fb016672424df7f0ca1c132e07fd34905 Mon Sep 17 00:00:00 2001 From: bloodshot Date: Wed, 22 May 2013 22:29:00 -0400 Subject: [PATCH] Add support for servers to register dimensions on client. In order to support multi-worlds such as MultiVerse, the server needs the ability to register dimensions on client or many forge mods such as IC2 will not function correctly. This has been an issue for MCPC which provides both Forge and Bukkit support to players. By adding the DimensionRegisterPacket class, MCPC now has the ability to send the required packet to client to register a dimension with DimensionManager. --- .../common/DimensionManager.java | 5 ++ .../network/DimensionRegisterPacket.java | 72 +++++++++++++++++++ .../common/network/ForgePacket.java | 5 +- 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 common/net/minecraftforge/common/network/DimensionRegisterPacket.java diff --git a/common/net/minecraftforge/common/DimensionManager.java b/common/net/minecraftforge/common/DimensionManager.java index cadc9b27b..8d3972329 100644 --- a/common/net/minecraftforge/common/DimensionManager.java +++ b/common/net/minecraftforge/common/DimensionManager.java @@ -145,6 +145,11 @@ public class DimensionManager dimensions.remove(id); } + public static boolean isDimensionRegistered(int dim) + { + return dimensions.containsKey(dim); + } + public static int getProviderType(int dim) { if (!dimensions.containsKey(dim)) diff --git a/common/net/minecraftforge/common/network/DimensionRegisterPacket.java b/common/net/minecraftforge/common/network/DimensionRegisterPacket.java new file mode 100644 index 000000000..037ed8958 --- /dev/null +++ b/common/net/minecraftforge/common/network/DimensionRegisterPacket.java @@ -0,0 +1,72 @@ +/** + * This software is provided under the terms of the Minecraft Forge Public + * License v1.0. + */ + +package net.minecraftforge.common.network; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.network.INetworkManager; +import net.minecraft.network.packet.NetHandler; +import net.minecraftforge.common.DimensionManager; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; + +/** + * This class offers the ability for servers to register dimensions on client. + */ +public class DimensionRegisterPacket extends ForgePacket +{ + + /** The dimension ID to register on client */ + public int dimensionId; + /** The provider ID to register with dimension on client */ + public int providerId; + + // nullary constructor required by ForgePacket.make() + public DimensionRegisterPacket() {} + + public DimensionRegisterPacket(int dimensionId, int providerId) + { + this.dimensionId = dimensionId; + this.providerId = providerId; + } + + @Override + public byte[] generatePacket(Object... data) + { + ByteArrayDataOutput dat = ByteStreams.newDataOutput(); + dat.writeInt(this.dimensionId); + dat.writeInt(this.providerId); + return dat.toByteArray(); + } + + @Override + public ForgePacket consumePacket(byte[] data) + { + ByteArrayDataInput dat = ByteStreams.newDataInput(data); + dimensionId = dat.readInt(); + providerId = dat.readInt(); + return this; + } + + @Override + public void execute(INetworkManager network, EntityPlayer player) + { + if (!(player instanceof EntityPlayerMP)) + { + if (!DimensionManager.isDimensionRegistered(dimensionId)) + { + DimensionManager.registerDimension(dimensionId, providerId); + } + if (player != null && player.worldObj != null) + { + player.worldObj.provider.dimensionId = dimensionId; + } + } + } + +} \ No newline at end of file diff --git a/common/net/minecraftforge/common/network/ForgePacket.java b/common/net/minecraftforge/common/network/ForgePacket.java index ee7df1f47..f8b49601d 100644 --- a/common/net/minecraftforge/common/network/ForgePacket.java +++ b/common/net/minecraftforge/common/network/ForgePacket.java @@ -25,7 +25,10 @@ public abstract class ForgePacket public static final String CHANNEL_ID = "FORGE"; enum Type { - FAKE_TEMP(ForgePacket.class); + /** + * Registers a dimension for a provider on client + */ + REGISTERDIMENSION(DimensionRegisterPacket.class); private Class packetType; private ConcurrentMap partTracker;