Jump to content

Trying to sync server -> client configs, but maybe I'm doing it wrong.


Insane96MCP

Recommended Posts

I'm trying to sync server config with client, and I've came up with this IMessage. https://github.com/Insane-96/NetherGoldOre/blob/1.12/common/net/insane96mcp/nethergoldore/network/ConfigSync.java

package net.insane96mcp.nethergoldore.network;

import io.netty.buffer.ByteBuf;
import net.insane96mcp.nethergoldore.lib.Properties;
import net.minecraft.client.Minecraft;
import net.minecraft.util.IThreadListener;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class ConfigSync implements IMessage {

	int minNuggetsPerOre, maxNuggetsPerOre, minExperienceDrop, maxExperienceDrop, orePerVein, minY, maxY;
	float veinPerChunk, pigmanAggroChance, pigmanAggroRadius;
	
	@Override
	public void fromBytes(ByteBuf buf) {
		minNuggetsPerOre = buf.readInt();
		maxNuggetsPerOre = buf.readInt();
		minExperienceDrop = buf.readInt();
		maxExperienceDrop = buf.readInt();
		orePerVein = buf.readInt();
		veinPerChunk = buf.readFloat();
		minY = buf.readInt();
		maxY = buf.readInt();
		pigmanAggroChance = buf.readFloat();
		pigmanAggroRadius = buf.readFloat();
	}

	@Override
	public void toBytes(ByteBuf buf) {
		buf.writeInt(Properties.config.drops.minNuggetsPerOre);
		buf.writeInt(Properties.config.drops.maxNuggetsPerOre);
		buf.writeInt(Properties.config.drops.minExperienceDrop);
		buf.writeInt(Properties.config.drops.maxExperienceDrop);
		buf.writeInt(Properties.config.generation.orePerVein);
		buf.writeFloat(Properties.config.generation.veinPerChunk);
		buf.writeInt(Properties.config.generation.minY);
		buf.writeInt(Properties.config.generation.maxY);
		buf.writeFloat(Properties.config.oreProperties.pigmanAggroChance);
		buf.writeFloat(Properties.config.oreProperties.pigmanAggroRadius);
	}

	public class Handler implements IMessageHandler<ConfigSync, IMessage> {

		@Override
		public IMessage onMessage(ConfigSync message, MessageContext ctx) {
			IThreadListener iThreadListener = Minecraft.getMinecraft();
			iThreadListener.addScheduledTask(new Runnable() {
				
				@Override
				public void run() {
					Properties.config.drops.minNuggetsPerOre = message.minNuggetsPerOre;
					Properties.config.drops.maxNuggetsPerOre = message.maxNuggetsPerOre;
					Properties.config.drops.minExperienceDrop = message.minExperienceDrop;
					Properties.config.drops.maxExperienceDrop = message.maxExperienceDrop;
					Properties.config.generation.orePerVein = message.orePerVein;
					Properties.config.generation.veinPerChunk = message.veinPerChunk;
					Properties.config.generation.minY = message.minY;
					Properties.config.generation.maxY = message.maxY;
					Properties.config.oreProperties.pigmanAggroChance = message.pigmanAggroChance;
					Properties.config.oreProperties.pigmanAggroRadius = message.pigmanAggroRadius;
				}
			});
			return null;
		}
		
	}
}

The fact is that I think there's a better way to do so, I find this way clunky. Any advice is appreciated.

Edited by Insane96MCP
Link to comment
Share on other sites

3 hours ago, Insane96MCP said:

The fact is that I think there's a better way to do so, I find this way clunky.

This is a pretty good way to sync the config values. The only problem with this approach is that when the player leaves the server it's config values will still be the server's so I would also keep a backup copy before accepting server configs and revert to it when the player disconnects.

 

You could iterate the properties in the config, write their values to the buffer prefixing everything with the size of the property map, then read them as raw byte arrays as long as there is something to read from the buffer, then recreate the values in the handler from the byte arrays. This approach is however worse than the one you are using since

  • It assumes that all values in the config are 4-byte values(but there are ways around that but that makes the packet way longer and thus adds even more data to be transferred to the client when they log in)
  • It will not work well if the config versions differ from client to server(a new mod version or something) but to be fair your current approach won't work either and again there are ways around that but again that would make the packet even longer. 
Link to comment
Share on other sites

  • 2 weeks later...
On 10/23/2018 at 2:56 PM, V0idWa1k3r said:

The only problem with this approach is that when the player leaves the server it's config values will still be the server's so I would also keep a backup copy before accepting server configs and revert to it when the player disconnects

I'm having some slight problems reverting config back on logout. I have this event: 

@SubscribeEvent
public static void EventClientDisconnectionFromServer(ClientDisconnectionFromServerEvent event) {
  Properties.config = Properties.localConfig;
  System.out.println(Arrays.toString(Properties.config.hardness.blockHardness) + " " + Arrays.toString(Properties.localConfig.hardness.blockHardness));
}

The problem is that for some reasons the localConfig is changed to the server config too, without anyone touching it. (https://github.com/Insane-96/IguanaTweaksReborn/blob/master/common/net/insane96mcp/iguanatweaks/lib/Properties.java#L26

https://github.com/Insane-96/IguanaTweaksReborn/blob/master/common/net/insane96mcp/iguanatweaks/network/ConfigSync.java#L54)
 

My guess is that since ConfigOptions is static, changing the config object, changes the one in localConfig too.

Edited by Insane96MCP
Link to comment
Share on other sites

If you only want the variables normally controlled by your config to change on the client *for the current session* I would not directly overwrite the variables being controlled by the client's config at all (backup or no).

 

Instead, I would use two variables; the normal client config variables, and a separate set of variables sent by the server, which take precedence over the client ones.  This will prevent any possibility of the actual config file on the client being changed.

 

On the client side, you will of course need to clear the values of the "server-set" when disconnecting from the current server.

Link to comment
Share on other sites

9 hours ago, Insane96MCP said:

My guess is that since ConfigOptions is static, changing the config object, changes the one in localConfig too.

Well, yes, if the fields are static then they are not instance-based and thus you can't simply create another instance and be content. You need a different approach.

 

3 hours ago, Laike_Endaril said:

If you only want the variables normally controlled by your config to change on the client *for the current session* I would not directly overwrite the variables being controlled by the client's config at all (backup or no).

 

Instead, I would use two variables; the normal client config variables, and a separate set of variables sent by the server, which take precedence over the client ones.  This will prevent any possibility of the actual config file on the client being changed.

 

On the client side, you will of course need to clear the values of the "server-set" when disconnecting from the current server.

I kinda see your point but this is ultimately more work than the one config idea. Keeping two configs means that you now need to write the ugly:

int hardness = player.isConnectedToAServer() ? ServerConfig.hardness : ClientConfig.hardness;

everywhere(pseudo-code obviously). 

Link to comment
Share on other sites

5 minutes ago, V0idWa1k3r said:

I kinda see your point but this is ultimately more work than the one config idea. Keeping two configs means that you now need to write the ugly:

int hardness = player.isConnectedToAServer() ? ServerConfig.hardness : ClientConfig.hardness;

everywhere(pseudo-code obviously). 

I was thinking something more like this...

 

Normal config class:

import net.minecraftforge.common.config.Config;

@Config(modid = YourMod.MODID)
public class ClientConfig
{
    public static int powerLevel = 9001;
}

 

CombinedConfig, the class handling the combination ofc:

Spoiler

import io.netty.buffer.ByteBuf;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;

public class CombinedConfig implements IMessage
{
    public static int powerLevel = ClientConfig.powerLevel;

    public CombinedConfig()
    {
        MinecraftForge.EVENT_BUS.register(CombinedConfig.class);
    }

    @Override
    public void fromBytes(ByteBuf buf)
    {
        powerLevel = buf.readInt();
    }

    @SubscribeEvent
    public static void EventClientDisconnectionFromServer(FMLNetworkEvent.ClientDisconnectionFromServerEvent event)
    {
        powerLevel = ClientConfig.powerLevel;
    }

    @Override
    public void toBytes(ByteBuf buf) {}
}

 

 

At this point, you should simply be able to reference CombinedConfig.powerLevel throughout the rest of your project.  Or you can put a getter in CombinedConfig and make that variable private if you're worried about public access to it.

  • Like 1
Link to comment
Share on other sites

On 11/5/2018 at 9:15 PM, Laike_Endaril said:

I was thinking something more like this...

 

Normal config class:


import net.minecraftforge.common.config.Config;

@Config(modid = YourMod.MODID)
public class ClientConfig
{
    public static int powerLevel = 9001;
}

 

CombinedConfig, the class handling the combination ofc:

  Hide contents


import io.netty.buffer.ByteBuf;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;

public class CombinedConfig implements IMessage
{
    public static int powerLevel = ClientConfig.powerLevel;

    public CombinedConfig()
    {
        MinecraftForge.EVENT_BUS.register(CombinedConfig.class);
    }

    @Override
    public void fromBytes(ByteBuf buf)
    {
        powerLevel = buf.readInt();
    }

    @SubscribeEvent
    public static void EventClientDisconnectionFromServer(FMLNetworkEvent.ClientDisconnectionFromServerEvent event)
    {
        powerLevel = ClientConfig.powerLevel;
    }

    @Override
    public void toBytes(ByteBuf buf) {}
}

 

 

At this point, you should simply be able to reference CombinedConfig.powerLevel throughout the rest of your project.  Or you can put a getter in CombinedConfig and make that variable private if you're worried about public access to it.

With this the problem is that if I have lots of properties I have to double them.

Link to comment
Share on other sites

1 hour ago, Insane96MCP said:

With this the problem is that if I have lots of properties I have to double them.

If you mean in RAM, then yes, but if the alternative is to keep a copy of the previous value of each config option and reset them to what they were before joining the server, well...that also keeps a 2nd copy of each config option.  Both methods will also require you to reset something when leaving a server.

 

Other than that, my method only has one advantage.  Because mine never touches the client config directly, even if the client's game process is interrupted while connected to a server, their config file will not be corrupted...whereas if you are changing the actual client config, even temporarily, using the server's settings, the server's settings will be their new "client" settings from then on out until they either manually change them or log onto a different server and the same thing happens there.  You could prevent that issue by saving the entire client config to a separate file (not a config file) and deal with saving/loading that, but that would be an over-complicated workaround.

 

And ofc. when I say "the client's game process is interrupted" I mean either...

1. The game crashes

2. The client user ends the game process

3. Probably if the client user presses alt+f4, though I haven't tested that so don't take my word on it

4. The client's power goes out

5. Possibly if the client's OS decides to suddenly force a restart to install updates...

 

It's a bit of a niche advantage, but I don't really see a disadvantage.  That said it's not my mod, that's just how I would do it.

 

 

Edit 1 ===================================

Just in case you were thinking there was a 2nd config FILE, no there is not.  I may have named the class "CombinedConfig" but it doesn't generate or use a config file itself.

Edited by Laike_Endaril
Link to comment
Share on other sites

@Insane96MCP You could also just reload your config on disconnect or when the client opens another server or a singleplayer world.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

  • 2 weeks later...
8 hours ago, Insane96MCP said:

How I can reload config from file?

You could probably just post a OnConfigChangeEvent to the event bus.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

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

    • This is a MacOs related issue: https://bugs.mojang.com/browse/MC-118506     Download this lib: https://libraries.minecraft.net/ca/weblite/java-objc-bridge/1.0.0/java-objc-bridge-1.0.0.jar and put it into ~/Library/Application Support/minecraft/libraries/ca/weblite/java-objc-bridge/1.0.0  
    • I use Bisect-Hosting, and I host a relatively modded server.  There is a mod I desperately want to have in a server. https://www.curseforge.com/minecraft/mc-mods/minehoplite This is MineHop. It's a mod that replicates the movement capabilities seen in Source Engine games, such as Half-Life, and such.  https://youtu.be/SbtLo7VbOvk - A video explaining the mod, if anyone is interested.  It is a clientside mod, meaning whoever is using it can change anything about the mod that they want, with no restrictions, even when they join a server with the same mod. They can change it to where they can go infinitely fast, or do some other overpowered thing. I don't want that to happen. So I just want to know if there is some way to force the SERVER'S configuration file, onto whoever is joining.  I'm not very savvy with all this modding stuff. There are two config files: minehop-common.txt, and minehop.txt I don't really know much about how each are different. I just know what the commands relating to acceleration and stuff mean.    
    • My journey into crypto trading began tentatively, with me dipping my toes into the waters by purchasing my first Bitcoin through a seasoned trader. With an initial investment of $5,000, I watched as my investment grew, proving to be both fruitful and lucrative. Encouraged by this success, I decided to increase my investment to $150,000, eager to capitalize on the growing popularity of cryptocurrency, However, as cryptocurrency gained mainstream attention, so too did the number of self-proclaimed "experts" in the field. Suddenly, everyone seemed to be a crypto guru, and more and more people were eager to jump on the bandwagon without fully understanding the intricacies of this complex world. With promises of quick and easy profits, these con artists preyed on the uninformed, luring them into schemes that often ended in disappointment and financial loss. Unfortunately, I fell victim to one such scheme. Seduced by the allure of easy money, I entrusted my hard-earned funds to a dubious trading platform, granting them access to my accounts in the hopes of seeing my investment grow. For a brief period, everything seemed to be going according to plan, with regular withdrawals and promising returns on my investment. However, my hopes were soon dashed when, without warning, communication from the platform ceased, and my Bitcoin holdings vanished into thin air. Feeling helpless and betrayed, I confided in a family member about my predicament. They listened sympathetically and offered a glimmer of hope in the form of a recommendation for Wizard Web Recovery. Intrigued by the possibility of reclaiming what I had lost, I decided to explore this option further. From the moment I reached out to Wizard Web Recovery, I was met with professionalism and empathy. They took the time to understand my situation and reassured me that I was not alone in my plight. With their guidance, I embarked on a journey to reclaim what was rightfully mine. Wizard Web Recovery's expertise and dedication were evident from the start. They meticulously analyzed the details of my case, uncovering crucial evidence that would prove invaluable in our quest for justice. With each step forward, they kept me informed and empowered, instilling in me a newfound sense of hope and determination. Through their tireless efforts and unwavering support, Wizard Web Recovery succeeded in recovering my lost Bitcoin holdings. It was a moment of triumph and relief, knowing that justice had been served and that I could finally put this chapter behind me. In conclusion, My experience with Wizard Web Recovery  was nothing short of transformative. Their professionalism, expertise, and unwavering commitment to their clients set them apart as true leaders in the field of cryptocurrency recovery. I am forever grateful for their assistance and would highly recommend their services to anyone in need of help navigating the treacherous waters of cryptocurrency scams. 
    • Ok so: Two things to note: It got stuck due to my dimension type. It was previously the same as the overworld dimension tpye but after changing it , it didn't freeze during spawn generation. ALSO, APPARENTLY, the way I'm doing things, the game can't have two extremely-rich dimensions or it will make the new chunk generation be veeery VEEERY slow. I'm doing the dimension file genreation all in the data generation step now, so it's all good. Mostly. If anybody has any tips regarding how can i more efficently generate a biome-rich dimension, im all ears.
    • https://mclo.gs/qTo3bUE  
  • Topics

×
×
  • Create New...

Important Information

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