Properly propagate the DimensionType information from server to client

at client connection time. This means the client now knows about the
dimensiontype properly and can access the ModDimension information on
the client.

Signed-off-by: cpw <[email protected]>
This commit is contained in:
cpw committed 2019-10-02 21:54:02 -04:00
1 parent c985225d0c
commit 48735c3946
16 files changed
+220 -409

No files matched your search

@@ -34,12 +34,13 @@ import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraftforge.fml.ExtensionPoint;
import net.minecraft.world.dimension.DimensionType;
import net.minecraftforge.common.ModDimension;
import net.minecraftforge.fml.LogicalSidedProvider;
import net.minecraftforge.fml.ModList;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
import net.minecraftforge.registries.ForgeRegistries;
import java.util.Optional;
import java.util.UUID;
@@ -256,7 +257,7 @@ public class FMLPlayMessages
this(Registry.MENU.getId(id), windowId, name, additionalData);
}
private OpenContainer(int id, int windowId, ITextComponent name, PacketBuffer additionalData)
private OpenContainer(int id, int windowId, ITextComponent name, PacketBuffer additionalData)
{
this.id = id;
this.windowId = windowId;
@@ -308,4 +309,60 @@ public class FMLPlayMessages
return additionalData;
}
}
public static class DimensionInfoMessage
{
private ResourceLocation dimName;
private boolean skylight;
private int id;
private ResourceLocation modDimensionName;
private PacketBuffer extraData;
DimensionInfoMessage(DimensionType type) {
id = type.getId() + 1;
dimName = type.getRegistryName();
modDimensionName = type.getModType().getRegistryName();
skylight = type.func_218272_d();
extraData = new PacketBuffer(Unpooled.buffer());
type.getModType().write(extraData, true);
}
DimensionInfoMessage(final int dimId, final ResourceLocation dimname, final ResourceLocation modDimensionName, final boolean skylight, final PacketBuffer extraData) {
id = dimId;
this.dimName = dimname;
this.modDimensionName = modDimensionName;
this.skylight = skylight;
this.extraData = extraData;
}
public static DimensionInfoMessage decode(PacketBuffer buffer) {
int dimId = buffer.readInt();
ResourceLocation dimname = buffer.readResourceLocation();
ResourceLocation moddimname = buffer.readResourceLocation();
boolean skylight = buffer.readBoolean();
PacketBuffer pb = new PacketBuffer(Unpooled.wrappedBuffer(buffer.readByteArray()));
return new DimensionInfoMessage(dimId, dimname, moddimname, skylight, pb);
}
public static void encode(DimensionInfoMessage message, PacketBuffer buffer) {
buffer.writeInt(message.id);
buffer.writeResourceLocation(message.dimName);
buffer.writeResourceLocation(message.modDimensionName);
buffer.writeBoolean(message.skylight);
buffer.writeByteArray(message.extraData.array());
}
private DimensionType makeDummyDimensionType() {
final ModDimension modDim = ForgeRegistries.MOD_DIMENSIONS.getValue(modDimensionName);
// default to overworld if no moddim found
if (modDim == null) return DimensionType.OVERWORLD;
modDim.read(extraData, true);
return new DimensionType(id, "dummy", "dummy", modDim.getFactory(), skylight, modDim, extraData);
}
public static boolean handle(final DimensionInfoMessage message, final Supplier<NetworkEvent.Context> contextSupplier) {
contextSupplier.get().enqueueWork(()-> NetworkHooks.addCachedDimensionType(message.makeDummyDimensionType(), message.dimName));
return true;
}
}
}
@@ -25,6 +25,11 @@ import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.dimension.DimensionType;
import net.minecraftforge.registries.ClearableRegistry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -89,6 +94,12 @@ public class NetworkHooks
FMLMCRegisterPacketHandler.INSTANCE.sendRegistry(manager, NetworkDirection.valueOf(direction));
}
public synchronized static void sendDimensionDataPacket(NetworkManager manager, ServerPlayerEntity player) {
// don't send vanilla dims
if (player.dimension.isVanilla()) return;
FMLNetworkConstants.playChannel.sendTo(new FMLPlayMessages.DimensionInfoMessage(player.dimension), manager, NetworkDirection.PLAY_TO_CLIENT);
}
public static void handleClientLoginSuccess(NetworkManager manager) {
if (manager == null || manager.channel() == null) throw new NullPointerException("ARGH! Network Manager is null (" + manager != null ? "CHANNEL" : "MANAGER"+")" );
if (getConnectionType(()->manager) == ConnectionType.VANILLA) {
@@ -181,4 +192,16 @@ public class NetworkHooks
player.openContainer.addListener(player);
MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, c));
}
// internal tracking map for custom dimensions received from servers for use on client.
private static Int2ObjectMap<DimensionType> trackingMap = new Int2ObjectOpenHashMap<>();
public static DimensionType getDummyDimType(final int dimension) {
return trackingMap.getOrDefault(dimension, DimensionType.OVERWORLD);
}
static void addCachedDimensionType(final DimensionType dimensionType, final ResourceLocation dimName) {
trackingMap.put(dimensionType.getId(), dimensionType);
final ClearableRegistry<DimensionType> dimtypereg = (ClearableRegistry<DimensionType>) Registry.DIMENSION_TYPE;
dimtypereg.register(dimensionType.getId(), dimName, dimensionType);
}
}
@@ -98,6 +98,12 @@ class NetworkInitialization {
consumer(FMLPlayMessages.OpenContainer::handle).
add();
playChannel.messageBuilder(FMLPlayMessages.DimensionInfoMessage.class, 2)
.decoder(FMLPlayMessages.DimensionInfoMessage::decode)
.encoder(FMLPlayMessages.DimensionInfoMessage::encode)
.consumer(FMLPlayMessages.DimensionInfoMessage::handle)
.add();
return playChannel;
}