mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 16:27:47 +00:00
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 <cpw+github@weeksfamily.ca>
This commit is contained in:
16 files changed
+220
-409
No files matched your search
@@ -86,6 +86,21 @@ public class DimensionManager
|
||||
private static final Map<ResourceLocation, SavedEntry> savedEntries = new HashMap<>();
|
||||
private static volatile Set<World> playerWorlds = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Register or get the existing dimension type for the given dimtype name.
|
||||
*
|
||||
* Dimensions already known to the save are loaded into the DimensionType map before the {@link RegisterDimensionsEvent} is fired.
|
||||
* You can use this helper to get your existing dimension, or create it if it is not found.
|
||||
*
|
||||
* @param name Registry name
|
||||
* @param type ModDimension type data
|
||||
* @param data Extra data for the ModDimension
|
||||
* @param hasSkyLight does this dimension have a skylight?
|
||||
* @return the DimensionType for the dimension.
|
||||
*/
|
||||
public static DimensionType registerOrGetDimension(ResourceLocation name, ModDimension type, PacketBuffer data, boolean hasSkyLight) {
|
||||
return REGISTRY.getValue(name).orElseGet(()->registerDimension(name, type, data, hasSkyLight));
|
||||
}
|
||||
/**
|
||||
* Registers a real unique dimension, Should be called on server init, or when the dimension is created.
|
||||
* On the client, the list will be reset/reloaded every time a InternalServer is refreshed.
|
||||
@@ -95,6 +110,8 @@ public class DimensionManager
|
||||
* @param name Registry name for this new dimension.
|
||||
* @param type Dimension Type.
|
||||
* @param data Configuration data for this dimension, passed into
|
||||
* @param hasSkyLight skylight for this dimension
|
||||
* @return the DimensionType for the dimension.
|
||||
*/
|
||||
public static DimensionType registerDimension(ResourceLocation name, ModDimension type, PacketBuffer data, boolean hasSkyLight)
|
||||
{
|
||||
|
||||
@@ -54,4 +54,20 @@ public abstract class ModDimension extends ForgeRegistryEntry<ModDimension>
|
||||
* @param network true when sent over the network, so you can only data needed by the client.
|
||||
*/
|
||||
public void read(PacketBuffer buffer, boolean network){}
|
||||
|
||||
/**
|
||||
* Convenience method for generating a ModDimension with a specific factory but no extra
|
||||
* data handling behaviour. Extend ModDimension to override other methods.
|
||||
*
|
||||
* @param factory Factory for creating {@link Dimension} instances from DimType and World.
|
||||
* @return A custom ModDimension with that factory.
|
||||
*/
|
||||
public static ModDimension withFactory(BiFunction<World, DimensionType, ? extends Dimension> factory) {
|
||||
return new ModDimension() {
|
||||
@Override
|
||||
public BiFunction<World, DimensionType, ? extends Dimension> getFactory() {
|
||||
return factory;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2019.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.network;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import net.minecraftforge.common.DimensionManager;
|
||||
import net.minecraftforge.common.network.ForgeMessage.DimensionRegisterMessage;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class DimensionMessageHandler extends SimpleChannelInboundHandler<ForgeMessage.DimensionRegisterMessage>{
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, DimensionRegisterMessage msg) throws Exception
|
||||
{
|
||||
if (DimensionManager.getRegistry().getByValue(msg.id) == null)
|
||||
{
|
||||
DimensionManager.registerDimensionInternal(msg.id + 1, msg.name, msg.dim, msg.data, msg.skyLight);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
|
||||
{
|
||||
LOGGER.error("DimensionMessageHandler exception", cause);
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2019.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.network;
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
/* TODO fluids
|
||||
public class FluidIdRegistryMessageHandler extends SimpleChannelInboundHandler<ForgeMessage.FluidIdMapMessage> {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, ForgeMessage.FluidIdMapMessage msg) throws Exception
|
||||
{
|
||||
FluidRegistry.initFluidIDs(msg.fluidIds, msg.defaultFluids);
|
||||
}
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
|
||||
{
|
||||
LOGGER.error("FluidIdRegistryMessageHandler exception", cause);
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
@@ -1,144 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2019.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.network;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.ModDimension;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
public abstract class ForgeMessage {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
public static class DimensionRegisterMessage extends ForgeMessage {
|
||||
int id;
|
||||
ResourceLocation name;
|
||||
ModDimension dim;
|
||||
boolean skyLight;
|
||||
@Nullable
|
||||
PacketBuffer data;
|
||||
|
||||
public DimensionRegisterMessage(){}
|
||||
public DimensionRegisterMessage(int id, ResourceLocation name, ModDimension dim, boolean skyLight, @Nullable PacketBuffer data)
|
||||
{
|
||||
this.id = id;
|
||||
this.dim = dim;
|
||||
this.data = data;
|
||||
this.skyLight = skyLight;
|
||||
}
|
||||
|
||||
@Override
|
||||
void toBytes(ByteBuf buff)
|
||||
{
|
||||
PacketBuffer output = new PacketBuffer(buff);
|
||||
output.writeInt(id);
|
||||
output.writeResourceLocation(name);
|
||||
output.writeResourceLocation(dim.getRegistryName());
|
||||
output.writeBoolean(skyLight);
|
||||
|
||||
if (data == null)
|
||||
{
|
||||
output.writeShort(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
ByteBuf dup = data.duplicate();
|
||||
output.writeShort(dup.readableBytes());
|
||||
output.writeBytes(dup);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
void fromBytes(ByteBuf buff)
|
||||
{
|
||||
PacketBuffer input = new PacketBuffer(buff);
|
||||
id = input.readInt();
|
||||
name = input.readResourceLocation();
|
||||
dim = ForgeRegistries.MOD_DIMENSIONS.getValue(input.readResourceLocation());
|
||||
skyLight = input.readBoolean();
|
||||
|
||||
int len = input.readShort();
|
||||
if (len != 0)
|
||||
{
|
||||
data = new PacketBuffer(Unpooled.buffer());
|
||||
input.readBytes(data, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* TODO fluids
|
||||
public static class FluidIdMapMessage extends ForgeMessage {
|
||||
BiMap<Fluid, Integer> fluidIds = HashBiMap.create();
|
||||
Set<String> defaultFluids = Sets.newHashSet();
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
void toBytes(ByteBuf bytes)
|
||||
{
|
||||
Map<Fluid, Integer> ids = FluidRegistry.getRegisteredFluidIDs();
|
||||
bytes.writeInt(ids.size());
|
||||
for (Map.Entry<Fluid, Integer> entry : ids.entrySet())
|
||||
{
|
||||
ByteBufUtils.writeUTF8String(bytes,entry.getKey().getName());
|
||||
bytes.writeInt(entry.getValue());
|
||||
}
|
||||
for (Map.Entry<Fluid, Integer> entry : ids.entrySet())
|
||||
{
|
||||
String defaultName = FluidRegistry.getDefaultFluidName(entry.getKey());
|
||||
ByteBufUtils.writeUTF8String(bytes, defaultName);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
void fromBytes(ByteBuf bytes)
|
||||
{
|
||||
int listSize = bytes.readInt();
|
||||
for (int i = 0; i < listSize; i++) {
|
||||
String fluidName = ByteBufUtils.readUTF8String(bytes);
|
||||
int fluidId = bytes.readInt();
|
||||
fluidIds.put(FluidRegistry.getFluid(fluidName), fluidId);
|
||||
}
|
||||
// do we have a defaults list?
|
||||
|
||||
if (bytes.isReadable())
|
||||
{
|
||||
for (int i = 0; i < listSize; i++)
|
||||
{
|
||||
defaultFluids.add(ByteBufUtils.readUTF8String(bytes));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER.info("Legacy server message contains no default fluid list - there may be problems with fluids");
|
||||
defaultFluids.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
abstract void toBytes(ByteBuf bytes);
|
||||
abstract void fromBytes(ByteBuf bytes);
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2019.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.network;
|
||||
/* TODO handshake
|
||||
import java.util.EnumMap;
|
||||
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.common.ForgeMod;
|
||||
import net.minecraftforge.fml.common.network.FMLEmbeddedChannel;
|
||||
import net.minecraftforge.fml.common.network.FMLOutboundHandler;
|
||||
import net.minecraftforge.fml.common.network.FMLOutboundHandler.OutboundTarget;
|
||||
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
|
||||
public class ForgeNetworkHandler
|
||||
{
|
||||
private static EnumMap<Side, FMLEmbeddedChannel> channelPair;
|
||||
|
||||
public static void registerChannel(ForgeMod forgeMod, Side side)
|
||||
{
|
||||
channelPair = NetworkRegistry.INSTANCE.newChannel(forgeMod, "FORGE", new ForgeRuntimeCodec());
|
||||
if (side == Side.CLIENT)
|
||||
{
|
||||
addClientHandlers();
|
||||
}
|
||||
|
||||
FMLEmbeddedChannel serverChannel = channelPair.get(Side.SERVER);
|
||||
serverChannel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(OutboundTarget.NOWHERE);
|
||||
String handlerName = serverChannel.findChannelHandlerNameForType(ForgeRuntimeCodec.class);
|
||||
serverChannel.pipeline().addAfter(handlerName, "ServerToClientConnection", new ServerToClientConnectionEstablishedHandler());
|
||||
}
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
private static void addClientHandlers()
|
||||
{
|
||||
FMLEmbeddedChannel clientChannel = channelPair.get(Side.CLIENT);
|
||||
String handlerName = clientChannel.findChannelHandlerNameForType(ForgeRuntimeCodec.class);
|
||||
clientChannel.pipeline().addAfter(handlerName, "DimensionHandler", new DimensionMessageHandler());
|
||||
clientChannel.pipeline().addAfter(handlerName, "FluidIdRegistryHandler", new FluidIdRegistryMessageHandler());
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2019.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.network;
|
||||
/* TODO handshake
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import net.minecraftforge.fml.common.network.FMLIndexedMessageToMessageCodec;
|
||||
|
||||
public class ForgeRuntimeCodec extends FMLIndexedMessageToMessageCodec<ForgeMessage> {
|
||||
public ForgeRuntimeCodec()
|
||||
{
|
||||
addDiscriminator(1, ForgeMessage.DimensionRegisterMessage.class);
|
||||
addDiscriminator(2, ForgeMessage.FluidIdMapMessage.class);
|
||||
}
|
||||
@Override
|
||||
public void encodeInto(ChannelHandlerContext ctx, ForgeMessage msg, ByteBuf target) throws Exception
|
||||
{
|
||||
msg.toBytes(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decodeInto(ChannelHandlerContext ctx, ByteBuf source, ForgeMessage msg)
|
||||
{
|
||||
msg.fromBytes(source);
|
||||
}
|
||||
}
|
||||
*/
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2019.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.network;
|
||||
/* TODO handshake
|
||||
import net.minecraftforge.fml.common.network.NetworkHandshakeEstablished;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
|
||||
public class ServerToClientConnectionEstablishedHandler extends ChannelInboundHandlerAdapter {
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception
|
||||
{
|
||||
if (evt instanceof NetworkHandshakeEstablished)
|
||||
{
|
||||
ctx.writeAndFlush(new ForgeMessage.FluidIdMapMessage());
|
||||
return;
|
||||
}
|
||||
// pass it forward
|
||||
ctx.fireUserEventTriggered(evt);
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user