Jump to content

Recommended Posts

Posted

I have a custom gui overlay that tells the player the durability of the gas mask item while they are wearing it. It works perfectly in single player, but the server crashes when it loads. I know that this is because the server is trying to load the overlay, despite the fact that it is only able to be loaded on the client side. I know that I need to tell the server to not try to load it but I can't figure out how to do that. I've read a few different answers to similar questions and problems, but none of them fixed it. I don't know if I was doing it right when I tried to do what they said. My relevant code is

Quote

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import com.jonispatented.moarmor.init.ModItems;
import com.jonispatented.moarmor.util.Reference;

public class GasMaskBar extends Gui {

    private final ResourceLocation bar = new ResourceLocation(Reference.MOD_ID, "textures/gui/gasmaskbar.png");
    private final int tex_width = 102, tex_height = 8, bar_width = 100, bar_height = 6;
    
    @SubscribeEvent
    public void renderOverlay(RenderGameOverlayEvent event) {
        if (event.getType() == RenderGameOverlayEvent.ElementType.TEXT) {
            Minecraft mc = Minecraft.getMinecraft();
            mc.renderEngine.bindTexture(bar);
            float oneUnit = (float)bar_width / mc.player.inventory.armorItemInSlot(3).getMaxDamage();
            int currentWidth = (int)(oneUnit * (mc.player.inventory.armorItemInSlot(3).getMaxDamage() - mc.player.inventory.armorItemInSlot(3).getItemDamage()));
            
            if (mc.player.inventory.armorItemInSlot(3).getItem() == (ModItems.GAS_MASK))
            {
                drawTexturedModalRect(0, 0, 0, 0, tex_width, tex_height);
                drawTexturedModalRect(1, 0, 1, tex_height, currentWidth, tex_height);
            }
        }
    }
}

and also the way I register it is

Quote

@EventHandler
    public static void Postinit(FMLPostInitializationEvent event)
    {
        MinecraftForge.EVENT_BUS.register(new GasMaskBar());
    }

 

Posted
1 hour ago, eatthenight said:

@SideOnly(Side.CLIENT) also register your gui in your client proxy

This does not do what you think it does. Adding this to the Gui class will just cause a class not found exception in another place.

Omitting it and registering the gui only from the client proxy, meanwhile, works just fine.

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.

Posted
7 minutes ago, Draco18s said:

This does not do what you think it does. Adding this to the Gui class will just cause a class not found exception in another place.

Omitting it and registering the gui only from the client proxy, meanwhile, works just fine.

i answered his question how you load something on only one side. i know that registering the gui in the client proxy is enough. and well no you can set the side of the event too with SideOnly(Side.Client) so the event will only be called on the client side but you’re right registering in a client proxy would be the most convenient way probably..

Posted
32 minutes ago, eatthenight said:

you can set the side of the event too with SideOnly(Side.Client) so the event will only be called on the client side

Again, that's not what side only does.

If you want an event to only be registered on one side, you use the value=Dist.CLIENT parameter in the @EventBusSubscriber annotation.

Also, its not called SideOnly any more. Its OnlyIn now.

.

  • Thanks 1

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.

Posted
5 hours ago, eatthenight said:

i answered his question how you load something on only one side. i know that registering the gui in the client proxy is enough. and well no you can set the side of the event too with SideOnly(Side.Client) so the event will only be called on the client side but you’re right registering in a client proxy would be the most convenient way probably..

No.

The @SideOnly is used if the associated object should not exist on sides not specified in the parameters.

This means classes annotated with @SideOnly(Side.Client) will not exist on the server side.

It does nothing in loading something only on one side.

Annotating an event subscriber with @SideOnly might work, but only due to the absence of the annotated object on the other side (which is considered hacky). One should use a side-specific event bus subscriber instead.

 

9 hours ago, JonIsPatented said:

and also the way I register it is

Quote

@EventHandler
    public static void Postinit(FMLPostInitializationEvent event)
    {
        MinecraftForge.EVENT_BUS.register(new GasMaskBar());
    }

GasMaskBar is client only. Assuming your event handler is triggered on both sides, this will cause a crash on the server.

Either register your GUI in a client proxy or create a client-side event subscriber and register it there.

  • Thanks 1

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Posted
2 hours ago, DavidM said:

GasMaskBar is client only. Assuming your event handler is triggered on both sides, this will cause a crash on the server.

Either register your GUI in a client proxy or create a client-side event subscriber and register it there.

I can't figure out how to create a client side event subscriber. Could you explain that? Also, I tried registering it in my client proxy, but I don't think i am doing it right. Would I just do it the same way i did it in my main as I showed above?

Posted
6 hours ago, diesieben07 said:

Read the documentation on events. Then use @EventBusSubscriber with Side parameter.

I tried this:

Quote

@Mod.EventBusSubscriber(Side.CLIENT)
public class ClientEventHandler {

    @EventHandler
    public static void Postinit(FMLPostInitializationEvent event)
    {
        MinecraftForge.EVENT_BUS.register(new GasMaskBar());
    }
}

Is this correct? Because it didn't work. Now the overlay doesn't happen at all.

Posted
6 minutes ago, JonIsPatented said:

Is this correct? Because it didn't work. Now the overlay doesn't happen at all.

Read the documentation on EventBusSubscriber again.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
3 hours ago, Animefan8888 said:

Read the documentation on EventBusSubscriber again.

I went and read it again and then reread it again. I Also went and read Jabelar's related tutorials again. I still can't figure it out. I tried adding the Side.CLIENT thing into a Mod.EventBusSubscriber annotation on the client proxy and all that. I tried making my own client event subscriber and that didn't work because I don't know how to do it and the explanations always gloss over the parts I actually need help with. I can't figure out at all how I'm supposed to register it if I'm not already doing it right. Could you please just tell me what I have to do.

Posted
8 minutes ago, JonIsPatented said:

Could you please just tell me what I have to do.

Put the EventBusSubscriber annotation on your gui class. And change your event method to a static method.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
23 minutes ago, Animefan8888 said:

Put the EventBusSubscriber annotation on your gui class. And change your event method to a static method.

Quote

@Mod.EventBusSubscriber(value = Side.CLIENT)
public class GasMaskBar extends Gui {

    private static final ResourceLocation bar = new ResourceLocation(Reference.MOD_ID, "textures/gui/gasmaskbar.png");
    private static final int tex_width = 102;
    private static final int tex_height = 8;
    private static final int bar_width = 100;
    private static final int bar_height = 6;
    
    @SubscribeEvent
    public static void renderOverlay(RenderGameOverlayEvent event) {
        if (event.getType() == RenderGameOverlayEvent.ElementType.TEXT) {
            Minecraft mc = Minecraft.getMinecraft();
            mc.renderEngine.bindTexture(bar);
            float oneUnit = (float)bar_width / mc.player.inventory.armorItemInSlot(3).getMaxDamage();
            int currentWidth = (int)(oneUnit * (mc.player.inventory.armorItemInSlot(3).getMaxDamage() - mc.player.inventory.armorItemInSlot(3).getItemDamage()));
            
            if (mc.player.inventory.armorItemInSlot(3).getItem() == (ModItems.GAS_MASK))
            {
                drawTexturedModalRect(0, 0, 0, 0, tex_width, tex_height);
                drawTexturedModalRect(1, 0, 1, tex_height, currentWidth, tex_height);
            }
        }
    }
}

When I do this, the drawTexturedModalRect() stuff gives me an error saying that it can't make a static reference to the non-static method.

Posted
2 hours ago, JonIsPatented said:

extends Gui

This is a Gui class. You call this class FROM the event handler, you don't MAKE it the event handler.

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.

Posted
25 minutes ago, Draco18s said:

This is a Gui class. You call this class FROM the event handler, you don't MAKE it the event handler.

That's what I was trying to do before. I thought anime fan was telling me to do this thing. What should this look like. How should I call it from the event handler.

Posted
6 hours ago, JonIsPatented said:

When I do this, the drawTexturedModalRect() stuff gives me an error saying that it can't make a static reference to the non-static method.

Ok make a static field in your class of your gui class. Then use that field to call drawTexturedModelRect or look into the Gui class and peek at its drawing methods.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.