Jump to content

[1.7.10] Calculations Server Side Only?


HalestormXV

Recommended Posts

So in my mod there is a cost to utilize items. The cost is a formula that take the Current XP level of a player and divides it by a set number, then to keep things nice and tidy, rounds it so we don't have any annoying decimals floating around and such.

 

Now a number of people have offered me feedback and asked that I make these formula's configurable. Which is fine, as I don't see any problems with setting the numbers in a configuration file as opposed to hard-coded in. However, this has presented me with a this problem. If this mod is used on a server and the Server Admin has set the formula numbers to one number and the client has their configs set to another number, which takes precedent? Would the Server Config files automatically take priority since it is calculating numbers and making changes to the player's experience level?

 

Here is an example of the code that uses the formula used in one of my entity classes.

private int aquariusSummonCost = 3;

public void onUpdate()
{
	super.onUpdate();
	/////////////////Cost Handler\\\\\\\\\\\\\\\\\\\\\
	aquariusCounter += 1;
	if (aquariusCounter == 80)
	{
		EntityLivingBase owner = this.getOwner();
		aquariusCounter = 0;
		if (owner instanceof EntityPlayer) 
		{
			EntityPlayer player = (EntityPlayer) owner;
			player.addExperienceLevel(-aquariusSummonCost);
			if (player.experienceTotal < aquariusSummonCost)
			{
				ExtendedPlayer instance = ExtendedPlayer.get(player);
				instance.setSummoned(AQUARIUS_ID, false);
				this.worldObj.removeEntity(this);
			}
		}
	}

 

And that is perfect, it works exactly as it should. If however I want to make that private int aquariusSummonCost a configurable value and the server config files have a different value then the client config files what could potentially happen? Or would nothing happen, and since this calculation is all server-side anyway the server config files will overrule whatever the client has set?

 

The same question also applies to my item class which has a formula that reaches into an array of numbers to make the calculations:

public static int SpiritCostBase[] = new int[]{8, 14, 11, 13, 12, 9, 6, 8, 10, 7, 9, 12};
public static int SpiritCostFormula[] = new int[]{7, 9, 9, 12, 10, 3, 12, 6, 8, 12, 6, 5};

 

The code will reach into the position in both arrays appropriate to what item is being used and then use those value to calculate a cost to utilize the item. Could i make each of those numbers in the array configurable and would Server Configs override Client Configs?

 

In summary what I am really hoping for is that I don't have to dabble in packet handling (yes I know eventually this is inevitable lol) because despite all of the seriously awesome tutorials out there from Debian, and CoolAlias, and such I can't for the life of me figure out how to utilize packets to make sure a client's configs values (like DimensionIDs) are in sync with the server's configs. And I know in some cases like DimensionIDs they HAVE to be in Sync or you will crash but I wasn't sure if this is the same principle with formula calculations.

Link to comment
Share on other sites

  • Replies 62
  • Created
  • Last Reply

Top Posters In This Topic

I'm pretty sure you will have to get into packets if you want to sync the information.

 

But honestly packets are not scary once have made your first one. They follow a pretty simple formula that you can practically copy paste whenever you need a new one..

Current Project: Armerger 

Planned mods: Light Drafter  | Ore Swords

Looking for help getting a mod off the ground? Coding  | Textures

Link to comment
Share on other sites

Yeah I know it is something I will inevitably have to work with. The tutorials are great that is true but it is one of those situations where I guess if I can avoid it, I want to lol.

 

However, with respect to the original question, would that data need to be synced? Since the calculations are performed by the server anyway and are dynamic? Yes the values used in the formula are not dynamic but the result is, and ultimately the result is what the server reads. So would I still need to sync the server values into the client config values? Or would the mod just read the values in the server config file and calculate them disregarding whatever the client has set since once again all of this is done on the server anyway. (Using the item, spawning the entity, calculating the cost of the entity, etc. etc.)

Link to comment
Share on other sites

it looks like you are using the onUpdate of an item, I'm not sure since I haven't done config on items yet, but I think that would cause a desync between client and server. For many things like that the client is allowed to process things on its own for a short amount of time with periodic updates from the server. It shouldn't cause any lasting or major problems, it would just be a stuttering effect as the client thinks it is so far along and then the server says nope your are here.

 

Again not sure if that is true for items.

Current Project: Armerger 

Planned mods: Light Drafter  | Ore Swords

Looking for help getting a mod off the ground? Coding  | Textures

Link to comment
Share on other sites

What is likely to happen if you don't sync the config changes is that the client will see two summonings and only one of them will persist. At worst it would require the user to leave the server and reconnect.

Current Project: Armerger 

Planned mods: Light Drafter  | Ore Swords

Looking for help getting a mod off the ground? Coding  | Textures

Link to comment
Share on other sites

There is client and there is server. You either run client connected to integrated server (SP) or you connect to others (MP/LAN).

 

Client has nothing to say (aside from player commands/movements) when it comes to data computing/saving/loading.

When you have data loaded by config - you will load different on server and client - again - while client might be using his own data, it will simply display wrong things if server does something differently.

 

Solutions are mainly:

* Sync client config using packet from server to client on connection (send config values). As of now - both sides make same computations, thus - they will be mostly correct. (mind that sometimes you need to correct client by sending additional packet).

* Sync on change - just send packet whenever server does something.

How and what really depends on situation.

 

Other problems:

Note: if(!world.isRemote) makes code run only on server. You should use it almost always if client doesn't need mirrored computing (following servers acts, which might or not be correct).

So back to problem: In snippet you gave you are modifying experience and try to remove entity from world on BOTH sides. THIS IS BAD. Both exp and entitie's death is synced and should be made on server.

Expression "this.worldObj.removeEntity(this);" is quite bad. Use this.setDead().

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

Link to comment
Share on other sites

Alright that does make sense. So I guess now the answer is painfully obvious. I am going to need a packet to hold that data in the server configs and have it sent to the client configs. Ideally I would only need to send this packet once when the player logs in, correct? Since those values don't actually change (the values that the formula uses.) Kinda like how EnderIo sends its configs on login (I always see the message on my server ederio.configmanger.sendconfigs once a player logs in)?

 

So forgive my noobishness, as now I have to make it a point to dabble in packets. Would each value need its own packet?

Link to comment
Share on other sites

You can put as many values in a packet as you want, but I would only do so if you don't anticipate needing to send any of them individually. E.g. if you need to be able to sync just one xp type (e.g. when that type is gained), then you should have a packet for just that type.

 

Nothing is stopping you from having both, though - one packet containing all the values to be used in the 'sync everything' situations, and one packet per xp type for times when you just want to sync that specific type.

Link to comment
Share on other sites

Alright so I can essentially store each of these arrays in its own packet:

public static int SpiritCostBase[] = new int[]{8, 14, 11, 13, 12, 9, 6, 8, 10, 7, 9, 12};
public static int SpiritCostFormula[] = new int[]{7, 9, 9, 12, 10, 3, 12, 6, 8, 12, 6, 5};

 

And then when the player logs in have that array sent to them and have it alter their configurations? And I can populate that array with values being pulled from my config handler like: new int[]{config.getInt1, config.getInt2, . . .

Link to comment
Share on other sites

almost, you can't send a packet to the client during any of the init events (seeing as the player/client doesn't exist yet either). You could probably hook into the world load event and send the packet from there, you will just need to have getter/setter methods for those arrays.

Current Project: Armerger 

Planned mods: Light Drafter  | Ore Swords

Looking for help getting a mod off the ground? Coding  | Textures

Link to comment
Share on other sites

This is exactly the type of situation that PlayerLoggedInEvent was made for - send the packet from there.

 

Do keep in mind that the client-side values are for display purposes only - any time you use them that will have a real effect, such as casting a spell or crafting something, you should only use the values on the server side.

Link to comment
Share on other sites

By the way, you won't want to overwrite the values from the client's config.  If they connect to a server (receiving the server's values), then disconnect, and load their own single player game, the config values should be used at that point.

 

Otherwise you'll have a disparity between logging into the server first, then playing single player, than playing single player directly.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Thanks to everyone. I am starting to grasp this and it is making sense now. I am going to create a handler I think (rather than use the simple wrapper). And @Draco18s, yes I wouldn't want to overwrite the values. So I think the last hurdle that I have to clear is this in "non-code" terms.

 

Server Config File -> Has the Values I Want -> Store them in Packet? -> Do I just need to send that packet to the players once?

 

How to I make sure that the packet is storing the "Server's Values?" Is it just as simple as doing a server side check when the player logs in? I don't know if I was being clear on that or not. If I am not please just let me know and I will try to clarify.

Link to comment
Share on other sites

You will want the data in two places.

 

Place 1 is where it gets read from the config file and stored.  I would store this in my ServerProxy (or CommonProxy, whatever you want to name it).  These values will still exist when the player logs into a server, but won't be used unless the player starts a local server.

 

Place 2 is where the client would store the values it receives from the server and uses for display.  I would store this in my ClientProxy.  Note: this is a separate field that is public in the ClientProxy along side the public field in the ServerProxy.

 

These client-side values would be overwritten every time the client receives the info packet the server sends on login (sent in the PlayerLoggedInEvent from the sever).

 

The cool thing is, you don't have to do anything special for this setup to work for a singleplayer game: the single player instance would send a packet to the single player client and the values would be updated correctly.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Okay, that actually makes sense to me. I can kinda envision how it would look also. However, one thing I noticed mentioned quite a few times in this topic is "display." Now I know that when dealing with Particles, GUIs, Custom Mana Bars, etc. these all play into account and packets are needed. However, with me all of these calculations the server does are directly added and subtracted from the players experience bar. My whole system utilizes the player's experience as power. And the experience is added and subtracted from directly, which is actually why I questioned packets to begin with because I thought all Experience gains and losses are handled strictly server side and automatically kept in sync, which in turn led me to beleive that all calculations pertaining to these adjustments to experience are only handled server side right out the gate. Does that make any difference with respect to syncing and potential desyncing issues? Or can this be classified as more of a "use the packets to be safe" situation. Granted, whatever the answer is, I still plan on using these packets as they are something I need to learn and this thread has provided a wealth of information regardless.

Link to comment
Share on other sites

If vanilla XP is kept in sync by vanilla Minecraft, then no, you don't need any additional packets for that particular aspect of your code, but if you are displaying the cost of spells or whatever like it seems you are doing with your arrays, and those values are configurable, then you will most definitely need to send that data to the clients when they connect or they will likely have incorrect values displayed to them in the GUI.

 

Same goes for any other data that is not kept in sync for you by Minecraft, so pretty much everything you add via your mod and a hefty portion of vanilla fields if you want to use them client side.

Link to comment
Share on other sites

vanilla exp is automatically synced, provided that you are only adding and removing exp using existing methods. For example if you look at what the CommandXP.class they use entityplayer.addExperienceLevel(<int>);  but that is working with levels directly not with the amount of exp.

 

If you want to work with exp but not on a level basis, then you would want to look for how exp orbs do it and see if the method they use will handle syncing for you.

Current Project: Armerger 

Planned mods: Light Drafter  | Ore Swords

Looking for help getting a mod off the ground? Coding  | Textures

Link to comment
Share on other sites

All calculations are done via built in methods of adding, subtracting, multiplying, and dividing the player XP level with math.

 

Here is actually the itemOnUse code: http://pastebin.com/aAdV3RSJ

 

The two arrays at the top are what will be configurable. So no displays are actually shown. So if I follow what you guys are saying correctly packets won't be needed. (As no information is being displayed, just being calculated)

 

Also the snippet at the top in the OP is part of the entity onUpdate class which also uses standard XP calculations which merely subtracts the EXP level from the player based on the formula which is set at the top (which will be configurable).

 

 

If however, I want to display spell costs (which sounds like an awesome idea coolAlias, thank you for that) then I will need packets. (Granted I know I can just test and see and I plan to but the more knowledge I gather and learn the better)

Link to comment
Share on other sites

yay! I made my first packet handler! But I screwed something up and have no idea what i did. Anyone tell me exactly where I screwed up?

 

PacketHandler: http://pastebin.com/jPN9Z1zR

AbstractPacket: http://pastebin.com/RNHeqCXP

ClientProxy Snippet: http://pastebin.com/ZbAU2spi

KeyHandler: http://pastebin.com/gd2m7JUQ

OpenGUIPacket: http://pastebin.com/LQzj1YzW

 

And lastly the Main where it is being registered: (I only posted the relevant parts since it is quite large)

 

 

@SidedProxy(clientSide = RefStrings.CLIENTSIDE , serverSide = RefStrings.SERVERSIDE)
public static ServerProxy proxy;
public static final PacketPipeline packetPipeline = new PacketPipeline();

@EventHandler
public static void Load(FMLInitializationEvent event) 
{
packetPipeline.initialize();	
}

@EventHandler
public static void PostLoad(FMLPostInitializationEvent PostEvent) {
packetPipeline.postInitialized();
}

 

 

 

So this loads up fine on the SinglePlayer but the packet doesn't actually work. When I press "Y" the message does not display at all.

However the even more serious issue is that if trying to start up a Server it crashes the server entirely and I have no idea why. Here is the crash report from the server: http://pastebin.com/W2wZuNFS

 

So forgive me if it is starting me in the face. But this is my first swing at packets. I specifically chose not to do the SimpleNetwork wrapper way because I want to get more control over the packets and I feel like this way does that.

Link to comment
Share on other sites

Well, it is likely because I am a fool and over-complicate things especially when I am trying to learn them lol (backwards logic). However this method was part of a YouTube tutorial and was explained in detail, so I managed to get an understanding of what each piece did. I actually looked at your SimpleNetwork handler first and quite honestly want to try both lol. I am not sure if you can have two network handlers though. But as it stands now, I only have the one I posted above.

 

Although in reading through your tutorial I see you make a notation stating:

"Let's take a quick look at the Proxy class method used above; remember this is necessary to prevent FML from attempting to load EntityClientPlayerMP on the server, even though that section of code should not even have been accessed during startup - somehow it is, and it crashes unless you do the following:"

 

Which if I am correct, is exactly what is happening in my code.

 

 

EDIT: Alright I bite - i followed your tutorial coolAlias as they have and this community has basically been my guiding light to learning modding lol and edited my handler and packets to fit your tutorial. And it works fine. I start my world up and when I press the Y key it outputs the message indicating that it works correctly and the packet was indeed sent and received. However, I run into this issue when I try to launch a server test: http://pastebin.com/6yJniY81

 

In trying to parse and understand this error code it appears that on server load we are attempted to load a client side only function which does make sense. As keybindings are strictly ClientSide. Here is the KeyHandler.class: http://pastebin.com/gd2m7JUQ

 

Do I simply need to add a server side check so that it ignores that when the Server is starting up? And if so, how could I go about doing that? I imagine it isn't as simple as isRemote. Or am I totally wrong and it is something much different.

Link to comment
Share on other sites

you need to register you keybindings in your clientproxy preinit, it seems like you are trying to register them serverside which would cause a crash.

 

if you aren't registering stuff in your proxies (which I recommend doing, it helps me keep things separated) then you can do if(proxy instanceof ClientProxy)

Current Project: Armerger 

Planned mods: Light Drafter  | Ore Swords

Looking for help getting a mod off the ground? Coding  | Textures

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

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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