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

    • Slot depo 5k merupakan situs slot depo 5k yang menyediakan slot minimal deposit 5rb atau 5k via dana yang super gacor, dimana para pemain hanya butuh modal depo sebesar 5k untuk bisa bermain di link slot gacor thailand terbaru tahun 2024 yang gampang menang ini.   DAFTAR & LOGIN AKUN PRO SLOT DEPO 5K ⭐⭐⭐ KLIK DISINI ⭐⭐⭐  
    • Slot deposit 3000 adalah situs slot deposit 3000 via dana yang super gacor dimana para pemain dijamin garansi wd hari ini juga hanya dengan modal receh berupa deposit sebesar 3000 baik via dana, ovo, gopay maupun linkaja untuk para pemain pengguna e-wallet di seluruh Indonesia.   DAFTAR & LOGIN AKUN PRO SLOT DEPOSIT 3000 ⭐⭐⭐ KLIK DISINI ⭐⭐⭐  
    • OLXTOTO: Menikmati Sensasi Bermain Togel dan Slot dengan Aman dan Mengasyikkan Dunia perjudian daring terus berkembang dengan cepat, dan salah satu situs yang telah menonjol dalam pasar adalah OLXTOTO. Sebagai platform resmi untuk permainan togel dan slot, OLXTOTO telah memenangkan kepercayaan banyak pemain dengan menyediakan pengalaman bermain yang aman, adil, dan mengasyikkan. DAFTAR OLXTOTO DISINI Keamanan Sebagai Prioritas Utama Salah satu aspek utama yang membuat OLXTOTO begitu menonjol adalah komitmennya terhadap keamanan pemain. Dengan menggunakan teknologi enkripsi terkini, situs ini memastikan bahwa semua informasi pribadi dan keuangan para pemain tetap aman dan terlindungi dari akses yang tidak sah. Beragam Permainan yang Menarik Di OLXTOTO, pemain dapat menemukan beragam permainan yang menarik untuk dinikmati. Mulai dari permainan klasik seperti togel hingga slot modern dengan fitur-fitur inovatif, ada sesuatu untuk setiap selera dan preferensi. Grafik yang memukau dan efek suara yang mengagumkan menambah keseruan setiap putaran. Peluang Menang yang Tinggi Salah satu hal yang paling menarik bagi para pemain adalah peluang menang yang tinggi yang ditawarkan oleh OLXTOTO. Dengan pembayaran yang adil dan peluang yang setara bagi semua pemain, setiap taruhan memberikan kesempatan nyata untuk memenangkan hadiah besar. Layanan Pelanggan yang Responsif Tim layanan pelanggan OLXTOTO siap membantu para pemain dengan setiap pertanyaan atau masalah yang mereka hadapi. Dengan layanan yang ramah dan responsif, pemain dapat yakin bahwa mereka akan mendapatkan bantuan yang mereka butuhkan dengan cepat dan efisien. Kesimpulan OLXTOTO telah membuktikan dirinya sebagai salah satu situs terbaik untuk penggemar togel dan slot online. Dengan fokus pada keamanan, beragam permainan yang menarik, peluang menang yang tinggi, dan layanan pelanggan yang luar biasa, tidak mengherankan bahwa situs ini telah menjadi pilihan utama bagi banyak pemain. Jadi, jika Anda mencari pengalaman bermain yang aman, adil, dan mengasyikkan, jangan ragu untuk bergabung dengan OLXTOTO hari ini dan rasakan sensasi kemenangan!
    • Slot deposit dana adalah situs slot deposit dana yang juga menerima dari e-wallet lain seperti deposit via dana, ovo, gopay & linkaja terlengkap saat ini, sehingga para pemain yang tidak memiliki rekening bank lokal bisa tetap bermain slot dan terbantu dengan adanya fitur tersebut.   DAFTAR & LOGIN AKUN PRO SLOT DEPOSIT DANA ⭐⭐⭐ KLIK DISINI ⭐⭐⭐  
    • Slot deposit dana adalah situs slot deposit dana minimal 5000 yang dijamin garansi super gacor dan gampang menang, dimana para pemain yang tidak memiliki rekening bank lokal tetap dalam bermain slot dengan melakukan deposit dana serta e-wallet lainnya seperti ovo, gopay maupun linkaja lengkap. Agar para pecinta slot di seluruh Indonesia tetap dapat menikmati permainan tanpa halangan apapun khususnya metode deposit, dimana ketersediaan cara deposit saat ini yang lebih beragam tentunya sangat membantu para pecinta slot.   DAFTAR & LOGIN AKUN PRO SLOT DEPOSIT DANA ⭐⭐⭐ KLIK DISINI ⭐⭐⭐  
  • Topics

×
×
  • Create New...

Important Information

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