Jump to content

Rendering textures through the LivingHurtEvent event handler


Recommended Posts

Posted

So here's what I want to do: render a texture on the screen every time you hit a zombie. BUT, I can't figure out how to do this!

 

My current code is (and everything that's not declared in the method is a private field):

 

@SubscribeEvent(priority = EventPriority.NORMAL)

public void onEvent(LivingHurtEvent event)

{

if (event.entity instanceof EntityChicken)

{

scaledresolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);

int scalefactor = scaledresolution.getScaleFactor();

int x = 1920 / scalefactor;

int y = 1080 / scalefactor;

 

this.mc.renderEngine.bindTexture(new ResourceLocation("pvp:textures/mod/modid.png"));

this.drawTexturedModalRect((int) (scaledresolution.getScaledWidth_double()/ 2) + (x / 2) - 80, (int) (scaledresolution.getScaledHeight_double()/ 2) + (y / 2) - 22, 0, 0, 128, 32);

 

}

 

but it just crashes and says that there's no gl context! help pls :P

Posted

Yes, it's right, there's no gl context!

And also your code will crash on server (unless you are registering event in client proxy).

 

How mc runs (Minecraft.runTick()):

-Update key bindings an mouse

-Draw screen

-Update world

Where Draw screen includes:

-Create gl context

-Render world (if present)

-Render game overlay (hotbar, hunger bar...)

-Render current gui

-Remove gl context

 

So, you're drawing in Update world part, where gl context is not present!

So, you must do it in one of place where gl context is present.

Posted

What gl context should I use instead of that specific eventhandler to check whether what entity the player hit, and draw the screen based off that? Sorry, I'm new at this :P

Posted

I do believe this is why particles were invented.

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
  On 8/3/2015 at 4:34 PM, Draco18s said:

I do believe this is why particles were invented.

 

I feel like such a dumbass right now. I had literally thought of every render event besides particles...

Thank you Draco. What's the method for drawing particles? (I could view the code on eclipse but my computer isn't with me for the next few days, so yeah)

Posted

Particles are weird.  You need a whole class for them.

 

http://jabelarminecraft.blogspot.com/p/minecraft-forge-1721710-modding-tips.html

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

Draco, but I want this all to be client side and not server side. I was looking over that tutorial and it looks that particle spawning is server side. I want it just to render for the player. What I'm making is a generic pvp hud and therefore I'm not working with the server here ;-; just displaying client info and rendering stuff based on events

Posted

render a texture on the screen every time you hit a zombie

I don't see how it this a particle, only thing I can think of that you want to make particles of damage done to zombie.

 

Anyway - NO, particles are client side and can be client-only.

As to why you were thinking they are server sided: Most of partcile spawtning is dictated by server, but its server->packet->client(spawn).

  Quote

1.7.10 is no longer supported by forge, you are on your own.

Posted

Oh, okay. But how would I spawn this custom particle without having access to server-side? I want it to show up whenever I hit the zombie

Posted

You need to read what they said.

 

Particle spawning is on client side. 

 

If you want just your current player to see it, then just spawn the particle from the client when hitting the zombie.

 

If you want all players to see it, do the above, send a packet to the server with the particle info, then on server, distribute to all clients.

Long time Bukkit & Forge Programmer

Happy to try and help

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Make a test with another Launcher like the Curseforge Launcher, MultiMC or AT Launcher
    • can anyone help me i am opening forge and add modpacks and then it says unable to update native luancher and i redownlaod java and the luancher it self?
    • The problem occurs also in 1.20.1 Forge, but with an "Error executing task on client" instead. I have "Sinytra Connector" installed. On 1.21.5 Fabric, there is no problem. When this happens, the chat message before the death screen appears gets sent, with an extra dash added.
    • Well, as usual, it was user error. Naming mismatch in sounds.json.  Please delete this post if you find it necessary. 
    • Hello Forge community.  I'm running into an issue with a mod I'm working on.  To preface, I can call /playsound modId:name music @a and I can hear the sound I registered being played in game. Great!  However, I cannot get it to trigger via my mod code.    Registration: public static final RegistryObject<SoundEvent> A_WORLD_OF_MADNESS = SOUND_EVENTS.register("a_world_of_madness", () -> new SoundEvent(new ResourceLocation("tetheredsouls", "a_world_of_madness")));   Playback: Minecraft mc = Minecraft.getInstance(); if (!(mc.player instanceof LocalPlayer) || mc.level == null) return; LocalPlayer player = (LocalPlayer) mc.player; BlockPos pos = player.blockPosition(); SoundEvent track = ModSounds.A_WORLD_OF_MADNESS.get(); System.out.println(track); System.out.println(pos); System.out.println(player); // play exactly like the tutorial: client-only, at the player's position try { mc.level.playLocalSound( player.getX(), player.getY(), player.getZ(), track, SoundSource.MUSIC, // Or MASTER if needed 1f, 1f, false ); System.out.println("[DEBUG] playSound success: " + track.getLocation()); } catch (Exception e) { System.err.println("[ERROR] Failed to play sound: " + track.getLocation()); e.printStackTrace(); } Sounds.json:   { "theme_of_laura": { "category": "music", "sounds": [ { "name": "tetheredsouls:a_world_of_madness", "stream": true } ] } } Things I have tried: - multiple .ogg files. Short .ogg files (5 seconds, <100KB).  - default minecraft sounds imported from import net.minecraft.sounds.SoundEvents; These work given my code. No idea why these are different.  - playSound() method, as well as several others in past iterations that did not work   I would be forever grateful if somebody could point me in the right direction. I've looked at several mod github repositories and found extremely similar code to what I'm doing. I've also found several threads in this forum that did not solve my issue. I just cannot figure out what I'm doing differently, and why I'm able to queue sounds manually with playsound but the code won't play it (despite confirming the code is being run with the debug statements.)
  • Topics

×
×
  • Create New...

Important Information

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