Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.11.2] [SOLVED] Some Questions Regarding Minecraft::getMinecraft


Draconwolver
 Share

Recommended Posts

As I've not modded for a while, I'd like to begin again on the right foot. I apologize in advance if there's some misunderstanding of Java on my part.

 

1. Is it safe to save the value from Minecraft::getMinecraft in a static variable? The return type is static so I'd assume so, but then wouldn't this be the same across all instances of Minecraft that are run? If a player decides to log into two different accounts on two different instances of Minecraft, how would Minecraft::getMinecraft::player return due to Minecraft::getMinecraft being static?

 

2. I'm confident that Minecraft::getMinecraft can be used safely as soon as the mod is loaded, but when is Minecraft::getMinecraft::player not null? Does the player have to load a world first? Is there an event I can listen to to set a non-null Minecraft::getMinecraft::player to a variable then not have to worry about it?

 

3. Likewise, when in the process of loading up a world does Minecraft::getMinecraft::world gain a value? Is there, again, an event that would let me know when this becomes non-null?

 

Thanks, and please pardon my noobiness. :P

Edited by Draconwolver
Link to comment
Share on other sites

10 minutes ago, Draconwolver said:

1. Is it safe to save the value from Minecraft#getMinecraft() in a static variable? The return type is static so I'd assume so, but then wouldn't this be the same across all instances of Minecraft that are run? If a player decides to log into two different accounts on two different instances of Minecraft, how would Minecraft#getMinecraft()#player return due to Minecraft#getMinecraft() being static?

Each instance of Minecraft initializes the mod separately, so they won't overlap ever. I can't answer to whether saving it statically is safe, but I'd assume so. However, you should only ever use Minecraft#getMinecraft() if you absolutely have to - there are almost always better and safer ways to get the information you want.

10 minutes ago, Draconwolver said:

2. I'm confident that Minecraft#getMinecraft() can be used safely as soon as the mod is loaded, but when is Minecraft#getMinecraft()#player not null? Does the player have to load a world first? Is there an event I can listen to to set a non-null Minecraft#getMinecraft()#player to a variable then not have to worry about it?

The player can only be gotten from a Minecraft instance on the client side, not on the server, so be very careful where you do this. If you can avoid using Minecraft#player at all, do. The relevant player will usually be passed along with events and method calls.

12 minutes ago, Draconwolver said:

3. Likewise, when in the process of loading up a world does Minecraft#getMinecraft()#world gain a value? Is there, again, an event that would let me know when this becomes non-null?

I can't answer to this for sure, but again, Minecraft#getMinecraft() is usually unnecessary and the same effects can be achieved in a different and safer way.

 

Hope that helps!

  • Like 1
Link to comment
Share on other sites

The first thing to note is that the Minecraft class is @SIdeOnly(Side.CLIENT). This means, you can access the class with a client or a remote server, but not on a dedicated server. Trying to load it in a dedicated server will cause Minecraft to crash with a ClassNotFoundException. So be careful to use this for client-side things only, like rendering.

 

1) 2 separate instance will have 2 seperate Minecraft classes, and they don't know about each other. So the first instance won't return the Minecraft as the other instance.

 

2) The Minecraft#player variable is initialized when the world is loaded. I don't think there's an event for that.

 

3) The Minecraft#world variable is initialized in the same method as the player, so they'll be accessible at the same time. Again, there's no event to catch that.

  • Like 1

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

@tripl3dogdare @larsgerrits Thank you, this is very informational. I should've mentioned that I'm aware that all of this is client-side only, but thank you for the heads ups. Much of what I'm trying to do involves sending messages to the user, so it's hard to come by an event that provides a reference to the player returned by Minecraft::getMinecraft::player.

Edited by Draconwolver
Link to comment
Share on other sites

Glad I could help! That is one of the most common uses of it that's actually legitimately needed, yes. Just be careful to check 

world.isRemote

to make sure you're on the client side before you try to use Minecraft#getMinecraft, and you should be fine =)

Link to comment
Share on other sites

9 hours ago, tripl3dogdare said:

Glad I could help! That is one of the most common uses of it that's actually legitimately needed, yes. Just be careful to check 


world.isRemote

to make sure you're on the client side before you try to use Minecraft#getMinecraft, and you should be fine =)

Ehm... no, not really. You must encapsulate references to client-only classes in your own client-only classes, such as your ClientProxy and common code must not reference those classes directly.

Link to comment
Share on other sites

17 minutes ago, diesieben07 said:

Ehm... no, not really. You must encapsulate references to client-only classes in your own client-only classes, such as your ClientProxy and common code must not reference those classes directly.

My bad, I guess I'm just a bit too used to Scala where I can just

if(world.isRemote) {
  import net.minecraft.client.Minecraft
  // do stuff
}

Please disregard my previous comments as to how to use Minecraft#getMinecraft safely, it appears I am either an idiot or really rusty when it comes to Java.

Link to comment
Share on other sites

Just now, tripl3dogdare said:

My bad, I guess I'm just a bit too used to Scala where I can just


if(world.isRemote) {
  import net.minecraft.client.Minecraft
  // do stuff
}

 

This changes nothing. Imports are just syntactic sugar, both in Java and Scala. The bytecode uses fully-qualified names everywhere.

My comment applies to your code as well.

Link to comment
Share on other sites

20 hours ago, Draconwolver said:

what I'm trying to do involves sending messages to the user

Ah then, that might be a horse of a different color (or class). The server has a notion of "accesses". Look at how the server processes sound -- sound can be initiated on the server and then (a few layers down in its processing) the server loops through the world accesses to send packets to all users. You might also look at how one player's achievement causes messages to all other players.

 

So, if you're trying to make a mod where something happens on the authority (server) and is then told to some or all players, you should be able to imitate one of these processes. On the other hand, if you're trying to write yourself a client-side mod that will tell you where the nearest diamonds are, then we can't (won't) help you.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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
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.

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Im trying to make a moddes server with my friends but every single time we get same error https://pastebin.com/UP6P6YSd Is there a way to fix it? Thanks for solutions
    • Take a break, go for a walk. Then try again. 🙂
    • Where did you see that? Sorry if I am being a bother, it's just that I have been staring at this mod for a long time and I'm probably looking over a lot of things
    • Taking a look at AbstractContainerScreen.renderSlot() I got the following code: Method created in my AbstractContainerScreen class: private void renderCustomSlot(PoseStack poseStack,ItemStack itemstack,int x, int y) { boolean visible = true; boolean hover = false; this.setBlitOffset(100); this.itemRenderer.blitOffset = 100.0F; if (visible) { if (hover) { //Draw a rectangle behind the item fill(poseStack, x, y, x + 16, y + 16, -2130706433); } //Render item icon RenderSystem.enableDepthTest(); this.itemRenderer.renderAndDecorateItem(this.minecraft.player, itemstack, x, y, x + y * this.imageWidth); //Render item string count PoseStack posestack = new PoseStack(); if (itemstack.getCount() != 1) { String s = String.valueOf(itemstack.getCount()); posestack.translate(0.0D, 0.0D, itemRenderer.blitOffset + 200.0F); MultiBufferSource.BufferSource multibuffersource$buffersource = MultiBufferSource.immediate(Tesselator.getInstance().getBuilder()); /* float scale; if (s.length()<3){ scale=1f; }else if (s.length()<4){ scale = 0.75f; }else { scale=0.5f; s = s.substring(0,s.length()-3)+"k"; } float compensatingScale = 1/scale; float stringX = (x + 19f - 2 - font.width(s)*scale)*compensatingScale; float stringY =(y + 6f + 3)*compensatingScale; posestack.scale(scale,scale,scale); */ float stringX = (x + 19f - 2 - font.width(s)); float stringY =(y + 6f + 3); font.drawInBatch(s,stringX,stringY,16777215, true, posestack.last().pose(), multibuffersource$buffersource, false, 0, 15728880); //posestack.scale(compensatingScale,compensatingScale,compensatingScale); multibuffersource$buffersource.endBatch(); } } this.itemRenderer.blitOffset = 0.0F; this.setBlitOffset(0); } Now it works correctly: Thanks to warjort for the tip!
    • Hello, I am currently trying to get a forge server setup so I can play the Pixelmon mod with some friends on a private server and have run into some issues with launching my forge server file, I downloaded the forge 12.2.2-14.23.5.2859 installer, downloaded it into a folder on my desktop, ran the Mc server file, agreed to Eula, made a run.bat file to launch the forge server at 6GBs, and when I try launching I get this issue: Pixelmon Server>java -Xmx6144M -Xms6144M -jar forge-1.12.2-14.23.5.2859-universal.jar --nogui We appear to be missing one or more essential library files. You will need to add them to your server before FML and Forge will run successfully.java.lang.ClassNotFoundException: org.objectweb.asm.Type         at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)         at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)         at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)         at java.base/java.lang.Class.forName0(Native Method)         at java.base/java.lang.Class.forName(Class.java:466)         at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.run(ServerLaunchWrapper.java:53)         at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.main(ServerLaunchWrapper.java:34)   I've tried using another minecraft server jar file from a different source to see if that would solve anything but its still coming up with this issue. Any help is greatly appreciated, thanks for your help in advance.
  • Topics

×
×
  • Create New...

Important Information

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