Enchantment Procedure Bug/Problem Client-Serverside (SOLVED)

Started by StellaeLux on

Topic category: Troubleshooting, bugs, and solutions

Last seen on 20:25, 8. May 2020
Joined Aug 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Enchantment Procedure Bug/Problem Client-Serverside (SOLVED)
Mon, 09/23/2019 - 18:37 (edited)

This is the procedure of a sword that is called upon being crafted/smelted:

https://ibb.co/1ZP63PB

If random < 0.5

do set local Rand to random

     if get local rand < 0.25

     do Add enchantment 1

          Play sound x

     else if get local rand < 0.5

     do Add enchantment 2

          Play sound x

     else if get local rand < 0.5

     do Add enchantment 3

          Play sound x

     else

     do Add enchantment 4

          Play sound x

So basically there's a 50% chance that the weapon gets an enchantment. When it gets an enchantment it gets either 1 of 4 by random and always plays a sound.

However when testing I noticed some strange behaviours:

1. When crafting the item, it would play a sound and have an enchantment. Yet when the crafting table closes the enchantment would disappear.

2. When crafting the item, it would play a sound, have 1 enchantment, yet when closing the crafting table, it would change to another enchantment and keep that one instead.

Is this a server client side issue or a bug?

 

EDIT:

I also tried wrapping the procedure in an if statement checking for client-side only or server side only. When in clientside only: No enchantment ever stays on the item. Only during dragging the item to your inventory will you see it. No sounds will play either.

On server side only: This fixes most of the issue. However, you can no longer see the enchantment while dragging it out of the inventory. For strange reasons, this is only visible on clientside.

 

SOLUTION:

When adding an enchantment to an item on being crafted, the part where you drag it to your inventory is client-side, where as the enchantment you get is server side. Because this procedure randomises which enchantment you get, you'll get bugged feedback of what enchantment you get as you may drag out Sharpness 5 (Client-Side result) while you get Smite 5 (server side result) in your inventory.

Making the procedure Client-Side only prevents it from having any visible enchantment while dragging it out, but will correctly display your enchantment once in the inventory + playing the sound

 

Edited by StellaeLux on Mon, 09/23/2019 - 18:37
Last seen on 14:12, 3. Jun 2023
Joined Nov 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This is because the random…
Mon, 09/23/2019 - 19:33

This is because the random value can be different between client side and server side.

  1. The first random value is <0.5 on client side, but >=0.5 on server side. The enchantment is "applied" on client side only, so you end with a ghost enchantment
  2. The first random is <0.5 on server side, but the value for the second random is different enough between client and server. Client shows you an enchantment, instead of the actual server-side enchantment.

Best thing to do with random things is to keep it server side if it changes the world (placing/removing blocks), entities or items. Use client side for decorations (e.g. showing some particles, playing a sound)