Jump to content

[1.7.10]Set a block on a server from client gui


TominoCZ

Recommended Posts

Well the you can see the block on the server, but when you click it, its not there anymore.. That's because the block is only placed on the Client side world.. but how do I set the block Server Side from a client?

 

My Gui class:

 

package com.tominocz.PAYDAY2.gui;

import org.lwjgl.opengl.GL11;

import com.tominocz.PAYDAY2.Blocks;
import com.tominocz.PAYDAY2.Main;
import com.tominocz.PAYDAY2.packet.Packet;

import cpw.mods.fml.relauncher.Side;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;

public class GuiLobbyHost extends GuiScreen {

public static int blockPosX;
public static int blockPosY;
public static int blockPosZ;
public static EntityPlayer player;
public static World world;

GuiButton b1;
GuiButton b2;

public GuiLobbyHost() {
	if (blockPosX == 0 && blockPosY == 0 && blockPosZ == 0) {
		Packet packet = new Packet();
		blockPosX = packet.x;
		blockPosY = packet.y;
		blockPosZ = packet.z;
	}
}

public boolean doesGuiPauseGame() {
	return false;
}

int guiWidth = 256;
int guiHeight = 168;

@Override
public void drawScreen(int x, int y, float ticks) {
	int guiX = (width - guiWidth) / 2;
	int guiY = (height - guiHeight) / 2;

	this.buttonList.clear();
	b1 = new GuiButton(0, guiX + 5, guiY + 5, 98, 20, I18n.format("DISCONNECT", new Object[0]));

	b2 = new GuiButton(1, guiX + 5, guiY + 45, 98, 20, I18n.format("START GAME", new Object[0]));
	this.buttonList.add(b1);
	this.buttonList.add(b2);

	GL11.glColor4f(1, 1, 1, 1);
	drawDefaultBackground();
	mc.renderEngine.bindTexture(new ResourceLocation(Main.MODID + ":" + "textures/gui/Lobby.png"));
	drawTexturedModalRect(guiX, guiY, 0, 0, guiWidth, guiHeight);

	super.drawScreen(x, y, ticks);
}

@Override
protected void keyTyped(char letter, int number) {
	switch (number) {
	case 1:
		this.mc.displayGuiScreen((GuiScreen) null);
		this.mc.setIngameFocus();
		break;
	}

	switch (letter) {
	case 'e':
	case 'E':
		this.mc.displayGuiScreen((GuiScreen) null);
		this.mc.setIngameFocus();
		break;
	}
}

@Override
protected void actionPerformed(GuiButton button) {
	if (button.id == 0) {
		world.setBlock(blockPosX, blockPosY, blockPosZ, Blocks.LobbyBlock);

		try {
			Main.snw.sendToServer(new Packet(0, 0, 0, 0));
		} catch (Exception e) {

		}

		NBTTagCompound tag = player.getEntityData();
		tag.setBoolean("IsJoined", false);

		this.mc.displayGuiScreen((GuiScreen) null);
		this.mc.setIngameFocus();
	}
	if (button.id == 1) {
		NBTTagCompound tag = player.getEntityData();
	}
}
}

 

 

My Block class:

 

package com.tominocz.PAYDAY2.block;

import com.tominocz.PAYDAY2.Main;
import com.tominocz.PAYDAY2.TileEntityData;
import com.tominocz.PAYDAY2.gui.GuiLobbyHost;
import com.tominocz.PAYDAY2.gui.GuiLobbyHosted;
import com.tominocz.PAYDAY2.gui.GuiLobbyJoined;
import com.tominocz.PAYDAY2.packet.Packet;

import cpw.mods.fml.common.network.internal.FMLNetworkHandler;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class LobbyBlock1 extends Block {

public LobbyBlock1(Material rock) {
	super(rock);
	this.setBlockName("LobbyBlock1");
	this.setBlockTextureName(Main.MODID + ":" + "1");
	this.setLightLevel(1F);
	this.setLightOpacity(2555);
}

public TileEntity createTileEntity(World world, int metadata) {
	return new TileEntityData();
}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7,
		float par8, float par9) {
	TileEntityData tile = (TileEntityData) world.getTileEntity(x, y, z);

	try {
		Main.snw.sendTo(new Packet(x, y, z, new Packet().Players), (EntityPlayerMP) player);
	} catch (Exception e) {
		GuiLobbyJoined.blockPosX = GuiLobbyHosted.blockPosX = GuiLobbyHost.blockPosX = x;
		GuiLobbyJoined.blockPosY = GuiLobbyHosted.blockPosY = GuiLobbyHost.blockPosY = y;
		GuiLobbyJoined.blockPosZ = GuiLobbyHosted.blockPosZ = GuiLobbyHost.blockPosZ = z;
		GuiLobbyJoined.player = GuiLobbyHosted.player = GuiLobbyHost.player = player;
		GuiLobbyJoined.world = GuiLobbyHosted.world = GuiLobbyHost.world = world;
	}

	NBTTagCompound tag = player.getEntityData();

	if (tile.Host == player.getDisplayName())
		FMLNetworkHandler.openGui(player, Main.instance, 2, world, x, y, z);
	else {
		if (tag.getBoolean("IsJoined") == false)
			FMLNetworkHandler.openGui(player, Main.instance, 1, world, x, y, z);
		else
			FMLNetworkHandler.openGui(player, Main.instance, 3, world, x, y, z);
	}

	return true;
}
}

 

 

So how can I do this?

Link to comment
Share on other sites

BUT You can't read the variable..

cuz this is in my Packet right now:

 

public class Packet implements IMessage {
public int players;
public int x;
public int y;
public int z;
public World world;

public Packet() {}

public Packet(int X, int Y, int Z, World World, int Players) {
	this.players = Players;
        this.x = X;
        this.y = Y;
        this.z = Z;
    }

@Override
public void toBytes(ByteBuf buf) {
	buf.[writeWorld?](world)
	buf.writeInt(players);
	buf.writeInt(x);
	buf.writeInt(y);
	buf.writeInt(z);
}

@Override
public void fromBytes(ByteBuf buf) {
	this.x = buf.readInt();
	this.y = buf.readInt();
	this.z = buf.readInt();
	this.world = buf.[readWorld?]();
	this.players = buf.readInt();
}
}

 

 

So how can I store a world variable?

Link to comment
Share on other sites

you cant and you dont need. if you want to change the world the player is currently in simply go and get the worldObj from the player.

the handleMessage of the imessagehandler gives u an context ctx. so just go ctx.getServerHandler().playerEntity.worldObj. I might be wrong with some naming here, search ur ide

Link to comment
Share on other sites

you cant and you dont need. if you want to change the world the player is currently in simply go and get the worldObj from the player.

the handleMessage of the imessagehandler gives u an context ctx. so just go ctx.getServerHandler().playerEntity.worldObj. I might be wrong with some naming here, search ur ide

 

So I have to make a public variable in the messagehandler/packethandler?

Link to comment
Share on other sites

I did what Failander(or whatever his name is) told me to do and I'm having the exact same problem as I was having before.

My IMessageHandler class:

 

 

public class PacketHandler implements IMessageHandler<Packet, IMessage> {

public static World world;

@Override
public IMessage onMessage(Packet message, MessageContext ctx) {
	world = ctx.getServerHandler().playerEntity.worldObj;
	return null;
}

}

 

 

My Block class:

 

public class LobbyBlock1 extends Block {

public LobbyBlock1(Material rock) {
	super(rock);
	this.setBlockName("LobbyBlock1");
	this.setBlockTextureName(Main.MODID + ":" + "1");
	this.setLightLevel(1F);
	this.setLightOpacity(2555);
}

public TileEntity createTileEntity(World world, int metadata) {
	return new TileEntityData();
}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7,
		float par8, float par9) {
	TileEntityData tile = (TileEntityData) world.getTileEntity(x, y, z);

	try {
		Main.snw.sendTo(new Packet(x, y, z, new Packet().players), (EntityPlayerMP) player);
	} catch (Exception e) {
		GuiLobbyJoined.blockPosX = x;
		GuiLobbyJoined.blockPosY = y;
		GuiLobbyJoined.blockPosZ = z;
		GuiLobbyJoined.player = player;
		GuiLobbyJoined.world = world;

		GuiLobbyHosted.blockPosX = x;
		GuiLobbyHosted.blockPosY = y;
		GuiLobbyHosted.blockPosZ = z;
		GuiLobbyHosted.player = player;
		GuiLobbyHosted.world = world;

		GuiLobbyHost.blockPosX = x;
		GuiLobbyHost.blockPosY = y;
		GuiLobbyHost.blockPosZ = z;
		GuiLobbyHost.player = player;
		GuiLobbyHost.world = world;
	}

	NBTTagCompound tag = player.getEntityData();

	if (tile.Host == player.getDisplayName())
		FMLNetworkHandler.openGui(player, Main.instance, 2, world, x, y, z);
	else {
		if (tag.getBoolean("IsJoined") == false)
			FMLNetworkHandler.openGui(player, Main.instance, 1, world, x, y, z);
		else
			FMLNetworkHandler.openGui(player, Main.instance, 3, world, x, y, z);
	}

	return true;
}
}

 

 

And when you click the block, a gui opens, the Gui class:

 

public class GuiLobbyHost extends GuiScreen {

public static int blockPosX;
public static int blockPosY;
public static int blockPosZ;
public static EntityPlayer player;
public static World world = PacketHandler.world;

public GuiLobbyHost() {
	if (blockPosX == 0 && blockPosY == 0 && blockPosZ == 0) {
		Packet packet = new Packet();

		blockPosX = packet.x;
		blockPosY = packet.y;
		blockPosZ = packet.z;
	}
}

public boolean doesGuiPauseGame() {
	return false;
}

int guiWidth = 256;
int guiHeight = 168;

@Override
public void drawScreen(int x, int y, float ticks) {
	int guiX = (width - guiWidth) / 2;
	int guiY = (height - guiHeight) / 2;

	this.buttonList.clear();
	this.buttonList.add(new GuiButton(0, guiX + 5, guiY + 5, 98, 20, I18n.format("DISCONNECT", new Object[0])));
	this.buttonList.add(new GuiButton(1, guiX + 5, guiY + 45, 98, 20, I18n.format("START GAME", new Object[0])));

	GL11.glColor4f(1, 1, 1, 1);
	drawDefaultBackground();
	mc.renderEngine.bindTexture(new ResourceLocation(Main.MODID + ":" + "textures/gui/Lobby.png"));
	drawTexturedModalRect(guiX, guiY, 0, 0, guiWidth, guiHeight);

	super.drawScreen(x, y, ticks);
}

@Override
protected void keyTyped(char letter, int number) {
	switch (number) {
	case 1:
		this.mc.displayGuiScreen((GuiScreen) null);
		this.mc.setIngameFocus();
		break;
	}

	switch (letter) {
	case 'e':
	case 'E':
		this.mc.displayGuiScreen((GuiScreen) null);
		this.mc.setIngameFocus();
		break;
	}
}

@Override
protected void actionPerformed(GuiButton button) {
	if (button.id == 0) {
		world.setBlock(blockPosX, blockPosY, blockPosZ, Blocks.LobbyBlock);

		try {
			Main.snw.sendToServer(new Packet(0, 0, 0, 0));
		} catch (Exception e) {

		}

		NBTTagCompound tag = player.getEntityData();
		tag.setBoolean("IsJoined", false);

		this.mc.displayGuiScreen((GuiScreen) null);
		this.mc.setIngameFocus();
	}
	if (button.id == 1) {
		NBTTagCompound tag = player.getEntityData();
	}
}
}

 

 

As I already mentioned, the block is visible only for the client on the server, until you activate(right-click) it.

What I need to do is set a block in the server's world not in the client's one.. Because the Gui class is clientside only, it places the block clientside(on a server) aswell.

 

EDIT:

Uploaded a video: https://youtu.be/QwC14FKs0AM

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.