Jump to content

[1.8] [Explained] Any reason to use wrappers with the data watcher?


Recommended Posts

Posted

I always see other modders use code like this with the datawatcher:

datawatcher.addObject(20, Integer.valueOf(7));
datawatcher.addObject(21, Byte.valueOf((byte) 0));

 

Why can't they just do this?

datawatcher.addObject(20, 7);
datawatcher.addObject(21, (byte) 0);

Maker of the Craft++ mod.

Posted

Because other 'modders' just use copy pasta. Those two pieces of code are LITERALLY exactly the same.

The compiler automatically adds the boxes.

And when we decompile Minecraft, we get that code.

People copy it thinking 'omg mojang does it that way' without thinking it through.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Posted

Using Integer.valueOf(Object) should be used when you are trying convert some object that you are unsure if its an integer or a string that represents an number.

 

Also if you have an object that you are unsure if its a number check it with instance of first, it more visually understandable.

 

Object obj = 900;
Integer.valueOf((Integer) obj);
            
String nbr = "72162";
Integer.valueOf(nbr);

 

They might also think that having those extra few lines of code will make there code look more "pro"?

 

Well no, when I see code like that I usually scratch my head a few times and say Wat!

 

 

 

Haha  :P

wat-meme.jpg

 

 

I require Java, both the coffee and the code :)

Posted

Belpois... No. The code examples you gave make no sense whatsoever.

 

Ermm, why not? If I had a string representation of a number you could use valueOf to convert it to an integer... or you could use parseInt which is called by valueOf(str)

 

The object one yes, it useless you could simply check with instance of...

I require Java, both the coffee and the code :)

Posted

The Object one makes no sense. I'll de-sugar all the autoboxing for you (this is what the compiler does when you compile your code):

 

Object obj = Integer.valueOf(900);
Integer.valueOf(((Integer) obj).intValue());

 

It's pretty pointless. And the string version of valueOf is kinda pointless as well, because most of the time you are dealing with int, not Integer. So the boxed version (which is what valueOf(String) is, it's the boxed version of parseInt) just produces an overhead.

If you do:

 

int i = Integer.valueOf("1234");

 

you get an unnecessary intermediate Integer object.

Yes agreed, the Integer.valueOf is pretty useless. I was just explaining what the Integer.valueOf could have been used for but yes its a useless intermediate object.

 

What we have learned today:

  • Don't copy and paste from the vanilla code
  • Integer.valueOf is useless :P use parseInt for string to int conversion

 

:P

I require Java, both the coffee and the code :)

Posted

Because other 'modders' just use copy pasta. Those two pieces of code are LITERALLY exactly the same.

The compiler automatically adds the boxes.

And when we decompile Minecraft, we get that code.

People copy it thinking 'omg mojang does it that way' without thinking it through.

Huh, thanks Lex!

Maker of the Craft++ mod.

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.