Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3228]Enable the openGui data longer than 128 bits #58

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,7 @@
public void func_70108_f(Entity p_70108_1_)
{
if (!this.func_70608_bn())
@@ -2604,6 +2533,168 @@
@@ -2604,6 +2533,192 @@
{
return this.field_71075_bZ.field_75098_d && this.func_70003_b(2, "");
}
Expand All @@ -1646,6 +1646,30 @@
+ net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.openGui(this, mod, modGuiId, world, x, y, z);
+ }
+
+ /**
+ * Opens a GUI with this player, uses FML's IGuiHandler system.
+ * Allows for extension by modders.
+ *
+ * @param mod The mod trying to open a GUI
+ * @param modGuiId GUI ID
+ * @param world Current World
+ * @param x Passed directly to IGuiHandler, data meaningless Typically world X position
+ * @param y Passed directly to IGuiHandler, data meaningless Typically world Y position
+ * @param z Passed directly to IGuiHandler, data meaningless Typically world Z position
+ * @param customData The custom data to IGuiHandler. For the information longer than 128 bits.
+ */
+ public void openGui(Object mod, int modGuiId, World world, int x, int y, int z, io.netty.buffer.ByteBuf customData)
+ {
+ net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.openGui(this, mod, modGuiId, world, x, y, z, customData);
+ }
+
+ public void openGui(Object mod, int modGuiId, World world, int x, int y, int z, java.util.function.Consumer<io.netty.buffer.ByteBuf> encoder)
+ {
+ io.netty.buffer.ByteBuf buf = io.netty.buffer.Unpooled.buffer();
+ encoder.accept(buf);
+ this.openGui(mod, modGuiId, world, x, y, z, buf);
+ }
+
+
+ /* ======================================== FORGE START =====================================*/
+ /**
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/minecraftforge/common/util/FakePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.mojang.authlib.GameProfile;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.Entity;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraft.entity.player.EntityPlayer;
Expand All @@ -37,6 +38,8 @@
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;

import java.util.function.Consumer;

//Preliminary, simple Fake Player class
public class FakePlayer extends EntityPlayerMP
{
Expand All @@ -51,6 +54,8 @@ public FakePlayer(WorldServer world, GameProfile name)
@Override public void sendMessage(ITextComponent component) {}
@Override public void addStat(StatBase par1StatBase, int par2){}
@Override public void openGui(Object mod, int modGuiId, World world, int x, int y, int z){}
@Override public void openGui(Object mod, int modGuiId, World world, int x, int y, int z, Consumer<ByteBuf> encoder) {}
@Override public void openGui(Object mod, int modGuiId, World world, int x, int y, int z, ByteBuf customData) {}
@Override public boolean isEntityInvulnerable(DamageSource source){ return true; }
@Override public boolean canAttackPlayer(EntityPlayer player){ return false; }
@Override public void onDeath(DamageSource source){ return; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package net.minecraftforge.fml.common.network;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;

Expand Down Expand Up @@ -55,4 +56,41 @@ public interface IGuiHandler
*/
@Nullable
Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z);


/**
* Returns a Server side Container to be displayed to the user.
*
* @param ID The Gui ID Number
* @param player The player viewing the Gui
* @param world The current world
* @param x X Position
* @param y Y Position
* @param z Z Position
* @param customData The customData
* @return A GuiScreen/Container to be displayed to the user, null if none.
*/
@Nullable
default Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z, ByteBuf customData){
return null;
}

/**
* Returns a Container to be displayed to the user. On the client side, this
* needs to return a instance of GuiScreen On the server side, this needs to
* return a instance of Container
*
* @param ID The Gui ID Number
* @param player The player viewing the Gui
* @param world The current world
* @param x X Position
* @param y Y Position
* @param z Z Position
* @param customData The customData
* @return A GuiScreen/Container to be displayed to the user, null if none.
*/
@Nullable
default Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z, ByteBuf customData){
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package net.minecraftforge.fml.common.network;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
Expand Down Expand Up @@ -258,6 +259,32 @@ public Container getRemoteGuiContainer(ModContainer mc, EntityPlayerMP player, i
}
}

/**
* INTERNAL method for accessing the Gui registry
* @param mc Mod Container
* @param player Player
* @param modGuiId guiId
* @param world World
* @param x X coord
* @param y Y coord
* @param z Z coord
* @return The server side GUI object (An instance of {@link Container})
*/
@Nullable
public Container getRemoteGuiContainer(ModContainer mc, EntityPlayerMP player, int modGuiId, World world, int x, int y, int z, ByteBuf customData)
{
IGuiHandler handler = serverGuiHandlers.get(mc);

if (handler != null)
{
return (Container)handler.getServerGuiElement(modGuiId, player, world, x, y, z ,customData);
}
else
{
return null;
}
}

/**
* INTERNAL method for accessing the Gui registry
* @param mc Mod Container
Expand All @@ -276,6 +303,25 @@ public Object getLocalGuiContainer(ModContainer mc, EntityPlayer player, int mod
return handler.getClientGuiElement(modGuiId, player, world, x, y, z);
}

/**
* INTERNAL method for accessing the Gui registry
* @param mc Mod Container
* @param player Player
* @param modGuiId guiId
* @param world World
* @param x X coord
* @param y Y coord
* @param z Z coord
* @param customData Custom Data
* @return The client side GUI object (An instance of {@link net.minecraft.client.gui.Gui})
*/
@Nullable
public Object getLocalGuiContainer(ModContainer mc, EntityPlayer player, int modGuiId, World world, int x, int y, int z, ByteBuf customData)
{
IGuiHandler handler = clientGuiHandlers.get(mc);
return handler.getClientGuiElement(modGuiId, player, world, x, y, z, customData);
}

/**
* Is there a channel with this name on this side?
* @param channelName The name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
Expand Down Expand Up @@ -65,7 +66,6 @@ public static class OpenGui extends FMLMessage {
int x;
int y;
int z;

public OpenGui() {}
OpenGui(int windowId, String modId, int modGuiId, int x, int y, int z)
{
Expand All @@ -76,6 +76,47 @@ public OpenGui() {}
this.y = y;
this.z = z;
}
@Override
void toBytes(ByteBuf buf)
{
buf.writeInt(windowId);
ByteBufUtils.writeUTF8String(buf, modId);
buf.writeInt(modGuiId);
buf.writeInt(x);
buf.writeInt(y);
buf.writeInt(z);
}
@Override
void fromBytes(ByteBuf buf)
{
windowId = buf.readInt();
modId = ByteBufUtils.readUTF8String(buf);
modGuiId = buf.readInt();
x = buf.readInt();
y = buf.readInt();
z = buf.readInt();
}
}
public static class OpenGuiExpand extends FMLMessage {
int windowId;
String modId;
int modGuiId;
int x;
int y;
int z;
ByteBuf customData;

public OpenGuiExpand() {}
OpenGuiExpand(int windowId, String modId, int modGuiId, int x, int y, int z, ByteBuf customData)
{
this.windowId = windowId;
this.modId = modId;
this.modGuiId = modGuiId;
this.x = x;
this.y = y;
this.z = z;
this.customData = customData;
}

@Override
void toBytes(ByteBuf buf)
Expand All @@ -86,6 +127,7 @@ void toBytes(ByteBuf buf)
buf.writeInt(x);
buf.writeInt(y);
buf.writeInt(z);
buf.writeBytes(customData);
}

@Override
Expand All @@ -97,6 +139,8 @@ void fromBytes(ByteBuf buf)
x = buf.readInt();
y = buf.readInt();
z = buf.readInt();
customData=Unpooled.buffer();
buf.readBytes(customData);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@

package net.minecraftforge.fml.common.network.internal;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.embedded.EmbeddedChannel;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
Expand All @@ -37,29 +32,26 @@
import net.minecraft.network.Packet;
import net.minecraft.server.management.PlayerList;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.FMLContainer;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
import net.minecraftforge.fml.common.*;
import net.minecraftforge.fml.common.network.FMLEmbeddedChannel;
import net.minecraftforge.fml.common.network.FMLOutboundHandler;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.FMLOutboundHandler.OutboundTarget;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.handshake.FMLHandshakeMessage;
import net.minecraftforge.fml.common.network.handshake.NetworkDispatcher;
import net.minecraftforge.fml.common.network.internal.FMLMessage.CompleteHandshake;
import net.minecraftforge.fml.common.registry.EntityRegistry;
import net.minecraftforge.fml.common.registry.EntityRegistry.EntityRegistration;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;
import java.util.*;
import java.util.stream.Collectors;

public class FMLNetworkHandler
{
Expand Down Expand Up @@ -99,7 +91,7 @@ public static void openGui(EntityPlayer entityPlayer, Object mod, int modGuiId,
entityPlayerMP.openContainer = remoteGuiContainer;
entityPlayerMP.openContainer.windowId = windowId;
entityPlayerMP.openContainer.addListener(entityPlayerMP);
net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.entity.player.PlayerContainerEvent.Open(entityPlayer, entityPlayer.openContainer));
MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(entityPlayer, entityPlayer.openContainer));
}
}
else if (entityPlayer instanceof FakePlayer)
Expand All @@ -116,6 +108,44 @@ else if (FMLCommonHandler.instance().getSide().equals(Side.CLIENT))
FMLLog.log.debug("Invalid attempt to open a local GUI on a dedicated server. This is likely a bug. GUI ID: {},{}", mc.getModId(), modGuiId);
}

}
public static void openGui(EntityPlayer entityPlayer, Object mod, int modGuiId, World world, int x, int y, int z, ByteBuf customData)
{
ModContainer mc = FMLCommonHandler.instance().findContainerFor(mod);
if (entityPlayer instanceof EntityPlayerMP && !(entityPlayer instanceof FakePlayer))
{
EntityPlayerMP entityPlayerMP = (EntityPlayerMP) entityPlayer;
Container remoteGuiContainer = NetworkRegistry.INSTANCE.getRemoteGuiContainer(mc, entityPlayerMP, modGuiId, world, x, y, z, customData);
if (remoteGuiContainer != null)
{
entityPlayerMP.getNextWindowId();
entityPlayerMP.closeContainer();
int windowId = entityPlayerMP.currentWindowId;
FMLMessage.OpenGuiExpand openGui = new FMLMessage.OpenGuiExpand(windowId, mc.getModId(), modGuiId, x, y, z, customData);
EmbeddedChannel embeddedChannel = channelPair.get(Side.SERVER);
embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(OutboundTarget.PLAYER);
embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(entityPlayerMP);
embeddedChannel.writeOutbound(openGui);
entityPlayerMP.openContainer = remoteGuiContainer;
entityPlayerMP.openContainer.windowId = windowId;
entityPlayerMP.openContainer.addListener(entityPlayerMP);
MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(entityPlayer, entityPlayer.openContainer));
}
}
else if (entityPlayer instanceof FakePlayer)
{
// NO OP - I won't even log a message!
}
else if (FMLCommonHandler.instance().getSide().equals(Side.CLIENT))
{
Object guiContainer = NetworkRegistry.INSTANCE.getLocalGuiContainer(mc, entityPlayer, modGuiId, world, x, y, z, customData);
FMLCommonHandler.instance().showGuiScreen(guiContainer);
}
else
{
FMLLog.log.debug("Invalid attempt to open a local GUI on a dedicated server. This is likely a bug. GUI ID: {},{}", mc.getModId(), modGuiId);
}

}

@Nullable
Expand Down Expand Up @@ -179,6 +209,7 @@ private static void addClientHandlers()
String targetName = channelPair.get(Side.CLIENT).findChannelHandlerNameForType(FMLRuntimeCodec.class);
pipeline.addAfter(targetName, "GuiHandler", new OpenGuiHandler());
pipeline.addAfter(targetName, "EntitySpawnHandler", new EntitySpawnHandler());
pipeline.addAfter(targetName, "GuiExpandHandler", new OpenGuiExpandHandler());
}
public static void registerChannel(FMLContainer container, Side side)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public FMLRuntimeCodec()
addDiscriminator(0,FMLMessage.CompleteHandshake.class);
addDiscriminator(1,FMLMessage.OpenGui.class);
addDiscriminator(2,FMLMessage.EntitySpawnMessage.class);
addDiscriminator(3,FMLMessage.OpenGuiExpand.class);
}
@Override
public void encodeInto(ChannelHandlerContext ctx, FMLMessage msg, ByteBuf target) throws Exception
Expand Down
Loading