Jump to content

[1.8.9][TileEntity] Making a block that drains all player experience


Major Squirrel

Recommended Posts

Good evening,

 

I'm currently trying to make a block, an "experience container" that drains the player experience when rightclicking on it. I think I'm managing well for now but I have few questions about player exp management :

 

  • Do I have to check the isRemote variable inside the drainExperienceFromPlayer ?
  • In the method bolded above, do I have to cast the EntityPlayer to a multiplayer one ? I feel it is "safer" to perform operations on variables, such as experience, on multiplayer entities (it is for a private server)

 

As you can see in the same method, I'm managing player experience super simply : I just make an exp transfer in the block and I reset the player exp variables just putting them at 0 :

    public boolean          drainExperienceFromPlayer(EntityPlayer playerIn) {
        if (!this.worldObj.isRemote && playerIn instanceof EntityPlayerMP) {
            EntityPlayerMP  playerMP;

            playerMP = (EntityPlayerMP) playerIn;
            if (playerMP.experienceTotal > 0) {
                this.experienceTotal += playerMP.experienceTotal;
                playerMP.experienceTotal = 0;
                playerMP.experienceLevel = 0;
                playerMP.experience = 0.0f;
            } else return (false);
        }
        return (true);
    }

 

I made some googling such as this gamepedia link, this topic and the EntityPlayer class to see that leveling up is specially treated, and I'd like not to f*ck up player experience performing this kind of operation.

 

The following files :

BlockExpContainer.java

TileEntityBlockExpContainer.java

 

Thank you in advance for your help !  ;D

Squirrel ! Squirrel ! Squirrel !

Link to comment
Share on other sites

1) Yes. But not necesserily. It will be synced from server to client anyway.

2) Do NOT cast player to client/server/singleplayer specific unless you really have to. If you can perform operations on player, without casting to specific type, do not cast it.

 

Sode note: why are you doing

(false)

? Just wondering. First time seeing this style.

Link to comment
Share on other sites

1) Yes. But not necesserily. It will be synced from server to client anyway.

2) Do NOT cast player to client/server/singleplayer specific unless you really have to. If you can perform operations on player, without casting to specific type, do not cast it.

 

Sode note: why are you doing

(false)

? Just wondering. First time seeing this style.

 

Hi elix, thanks for your reply. :)

 

I will modify my code, considering what you said above.

 

About return (false), I like to get a clear view of what I am returning inside my code. It's an habit I got since college. ;D

 

What about the player experience management I did ? Is it ok ? I took a look earlier on the EntityPlayer class and it seems that these variables are initialized with negative values, so I don't really know if it's ok to do the way I did.

Squirrel ! Squirrel ! Squirrel !

Link to comment
Share on other sites

1) Yes. But not necesserily. It will be synced from server to client anyway.

2) Do NOT cast player to client/server/singleplayer specific unless you really have to. If you can perform operations on player, without casting to specific type, do not cast it.

 

Sode note: why are you doing

(false)

? Just wondering. First time seeing this style.

 

Hi elix, thanks for your reply. :)

 

I will modify my code, considering what you said above.

 

About return (false), I like to get a clear view of what I am returning inside my code. It's an habit I got since college. ;D

 

What about the player experience management I did ? Is it ok ? I took a look earlier on the EntityPlayer class and it seems that these variables are initialized with negative values, so I don't really know if it's ok to do the way I did.

I'm not able to tell you anything about this part, as i've never worked with experience (:D).

You'll have to try or to ask/wait someone else to answer.

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



×
×
  • Create New...

Important Information

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