Jump to content

Recommended Posts

Posted

I am having trouble syncing Capabilities.

 

So in PlayerEvent.Clone I set a field in a capability. This field change in a capability causes it to send data to the client (if logically on the server) using my packets.

 

However, I must be doing something wrong, because it isn't changing.

 

Here is my message handler (it is intended solely for the logical client):

 

public class ResearchMessageHandler implements IMessageHandler<ResearchMessage, IMessage>
{
@Override
public IMessage onMessage(ResearchMessage message, MessageContext ctx)
{
	EntityPlayer player = Minecraft.getMinecraft().thePlayer;

	if (player.hasCapability(CommonProxy.RESEARCH_CAP, null)) //This is true
	{
		IPlayerResearchCapability cap = player.getCapability(CommonProxy.RESEARCH_CAP, null);

		cap.setResearchStateNoCheck(null, message.research, message.state); //This works successfully.
	}

	return null; //Here it still works. But after here the changes mysteriously vanish.
}
}

Posted

http://greyminecraftcoder.blogspot.com.au/2015/01/thread-safety-with-network-messages.html

 

Additionally, idk if Clone event allows syncing player cloned with himself (his client).

 

Player clonning happens e.g when you respawn, and in that moment server has two players (old and new), and client has only old. Sending data about new player while client didn't spwn him yet kills updates. Thread safety MIGHT solve this (but I am not sure).

 

What I am onto - Clone event should never be used to sync data.

Use EntityJoinedWorldEvent - it is called always when you join world and is best for syncing initial data.

1.7.10 is no longer supported by forge, you are on your own.

Posted

As it turns out it was succeeding because of a bad if-statement.

 

Same scenario.

 

public class ResearchMessageHandler implements IMessageHandler<ResearchMessage, IMessage>
{
@Override
public IMessage onMessage(final ResearchMessage message, MessageContext ctx)
{
	Minecraft.getMinecraft().addScheduledTask(new Runnable()
	{
		@Override
		public void run()
		{
			EntityPlayer player = Minecraft.getMinecraft().thePlayer;

			if (player.hasCapability(CommonProxy.RESEARCH_CAP, null))
			{
				IPlayerResearchCapability cap = player.getCapability(CommonProxy.RESEARCH_CAP, null);

				cap.setResearchState(null, message.research, message.state);
			}
		}
	});

	return null;
}
}

Posted

Where are you sending the packet from? Show that code, too.

 

Btw, you should not be using Minecraft.getMinecraft() directly - use your proxies:

// CommonProxy
/**
 * Returns a side-appropriate EntityPlayer for use during message handling
 */
public EntityPlayer getPlayerEntity(MessageContext ctx) {
	return ctx.getServerHandler().playerEntity;
}

/**
 * Returns the current thread based on side during message handling,
 * used for ensuring that the message is being handled by the main thread
 */
public IThreadListener getThreadFromContext(MessageContext ctx) {
	return ctx.getServerHandler().playerEntity.getServerForPlayer();
}

// ClientProxy:
@Override
public EntityPlayer getPlayerEntity(MessageContext ctx) {
	return (ctx.side.isClient() ? mc.thePlayer : super.getPlayerEntity(ctx));
}

@Override
public IThreadListener getThreadFromContext(MessageContext ctx) {
	return (ctx.side.isClient() ? mc : super.getThreadFromContext(ctx));
}

// message code:
YourMod.proxy.getThreadFromContext(ctx).addScheduledTask(...

EntityPlayer player = YourMod.proxy.getPlayerEntity(ctx);

Posted

To be honest it is okay to use it directly in netty context, because if you have a packet that you send from server to client side you always know that you are safe to use Minecraft.getMinecraft()

Posted

The sending of the message is complicated. The code which initiates the sending of the message is some test code in an EntityJoinWorldEvent, which ensures that the logical side is SERVER. The part which actually sends the code is:

 

NETWORK_WRAPPER.sendTo(new ResearchMessage(research, state), (EntityPlayerMP)player);

Posted

I am having another issue: I am having the client send a request to the server, and the server is supposed to respond with a response message. However, it then proceeds to crash. For some reason, the request message is being re-processed (I think) on the incorrect side (I think). I have no idea what is happening.

 

package com.phantasma.phantasma.network;

import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;

public class RequestMessage implements IMessage
{
public RequestType type;

public RequestMessage()
{

}

public RequestMessage(RequestType type)
{
	this.type = type;
}

@Override
public void fromBytes(ByteBuf buf)
{
	int ordinal = buf.readInt(); //java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(2): SlicedByteBuf(ridx: 0, widx: 2, cap: 2/2, unwrapped: UnpooledHeapByteBuf(ridx: 0, widx: 3, cap: 256))
	this.type = RequestType.values()[ordinal];
}

@Override
public void toBytes(ByteBuf buf)
{
	int ordinal = this.type.ordinal();
	buf.writeInt(ordinal);
}

public static enum RequestType
{
	RESEARCH
}
}

 

package com.phantasma.phantasma.network;

import com.phantasma.phantasma.Phantasma;
import com.phantasma.phantasma.network.RequestMessage.RequestType;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side;

public class RequestMessageHandler implements IMessageHandler<RequestMessage, IMessage>
{
@Override
public IMessage onMessage(RequestMessage message, MessageContext ctx)
{
	if (ctx.side == Side.SERVER)
	{
		EntityPlayer player = Phantasma.proxy.getEntityPlayer(ctx);

		switch(message.type)
		{
		case RESEARCH:
		{
			return new ResearchMessage(player.getCapability(Phantasma.RESEARCH_CAP, null).serialize());
		}
		}
	}
	return null;
}
}

Posted

This is why I need to stop using literals as unique ids...

 

As it turns out I had registered both my messages under the same id. I am now using a counter and incrementing.

Posted

You have not implemented the thread-safety in your server handler. And using a request message here is pointless, just push the message out from the server without request when the player logs in (PlayerLoggedInEvent).

 

Whenever I respawn I would lose all the capability data, unless there is an event I am missing.

Posted

PlayerEvent.Clone, but you shouldn't send packets from there; instead, use EntityJoinWorldEvent (or possibly RespawnEvent, though I've never tried that one) if you need to sync data after player death and respawn.

 

PlayerEvent.Clone was not working on the client thread, and EntityJoinWorldEvent was not working for sending from the server thread, for some reason.

Posted

PlayerEvent.Clone

Is called ONLY on server and can be used to copy server data from dead player to new player.

Then when new player joins (after respawn) you can send packet from EntityJoinWorldEvent to update client-side NEW player.

 

It WILL work if you use thread safety, which was alredy pointed out.

1.7.10 is no longer supported by forge, you are on your own.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I need to know what mod is doing this crash, i mean the mod xenon is doing the crash but i want to know who mod is incompatible with xenon, but please i need to know a solution if i need to replace xenon, i cant use optifine anymore and all the other mods i tried(sodium, lithium, vulkan, etc) doesn't work, it crash the game.
    • I have been trying to solve a consistent crashing issue on my brother's computer where it will crash during the "Scanning Mod Candidates" phase of the loading process that starts when you click the play button on the Minecraft launcher. The issue seems to stem from a missing library that it mentions in the log file I provide below. I might I'm missing the bigger issue here for a smaller one but hopefully someone can find what I'm missing. Here's all of the stuff that I've been able to figure out so far: 1. It has nothing to do with mods, the crash happened with a real modpack, and even when I made a custom modpack and launched it without putting ANY mods into it (That is where the log file comes from by the way). 2. I have tried to find this class like a file in the Minecraft folders, but I've had no luck finding it (I don't think it works like that, but since I really don't understand how it works, I just figured I'd try). 3. I haven't seen anyone else have this issue before. 4. I know that my modpack (with mods) does work since I've run it on my computer, and it works fantastic. For some reason my brother's computer can't seem to run anything through curseforge. 5. This is for Minecraft version 1.20.1, Minecraft launcher version 3.4.50-2.1.3, forge 47.3.0, and curseforge app version 1.256.0.21056 6. My brother is using a Dell laptop from 6 years ago running Windows 10 (If you think more info on this would help, please ask as I do have it. I'm just choosing not to put it here for now). 7. I have reinstalled the curseforge app and installed Minecraft version 1.20.1. I have not reinstalled Minecraft or forge 47.3.0 but I didn't know if that would help. 8. I had an error code of 1 Please let me know if there is anything else that I am missing that you would like me to add to this post/add in a comment! Lastly, many thanks in advance to whoever can help! ------------- LOG FILE (latest.log) ------------- (from /Users/<NAME OF USER>/cursforge/minecraft/Instances/<THE NAME OF MY EMPTY MODPACK>/logs/latest.log) (This was made after running an empty modpack with same versions for all apps) ("[REDACTED]" is not the actual text from the log, it is me replacing text I figured wouldn't be necessary for fixing and would hurt my privacy) https://pastebin.com/hxXvGGEK ------------- DEBUG.LOG (I realized that I should have put this here first after I had done all of the work on putting latest.log in) -------------------- (again, "[REDACTED]" is not the actual text from the log, it is me replacing text I figured wouldn't be necessary for fixing and would hurt my privacy) https://pastebin.com/Fmh8GHYs
    • Pastebin... https://pastebin.com/Y3iZ85L5   Brand new profile, does not point to a mod as far as I can tell, my fatal message just has something about mixins. Don't know much about reading logs like this, but am genuinely stuck, please help. Java updated, pc restarted.
    • I was playing minecraft, forge 47.3.0 and 1.20.1, but when i tried to play minecraft now only crashes, i need help please. here is the crash report: https://securelogger.net/files/e6640a4f-9ed0-4acc-8d06-2e500c77aaaf.txt
  • Topics

×
×
  • Create New...

Important Information

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