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

Proper place to render an effect for an item's right click?


naturaGodhead
 Share

Recommended Posts

package com.natura.minestuckarsenal;

import java.util.Optional;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

import com.natura.minestuckarsenal.item.ItemLaserWeapon;
import com.natura.minestuckarsenal.item.MinestuckArsenalItems;

import net.java.games.input.Mouse;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.EntityInteract;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class ClientEventHandler {
	
	public static final ResourceLocation LASER_LOC = new ResourceLocation(MinestuckArsenal.MODID, "textures/effects/laserbeam.png");

	public ClientEventHandler() {
		
	}
	
	@SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
	public void onRenderWorld(RenderWorldLastEvent e) {
		
			EntityPlayer player = Minecraft.getMinecraft().player;
			GameSettings gs = Minecraft.getMinecraft().gameSettings;
			
			if(player.getActiveItemStack().getItem() == MinestuckArsenalItems.ahabsCrosshairs && gs.keyBindUseItem.isKeyDown()) {
				doRenderBeam(player);
			}
		
		
	}
	
	
	public static void doRenderBeam(EntityLivingBase shooter) {
			GlStateManager.depthMask(false);
			GlStateManager.enableBlend();
			GlStateManager.blendFunc(GL11.GL_ONE, GL11.GL_ONE);
			
			GlStateManager.pushMatrix();
			
			EntityPlayer player = Minecraft.getMinecraft().player;
			
			float partialTicks = Minecraft.getMinecraft().getRenderPartialTicks();
			double doubleX = player.lastTickPosX + (player.posX - player.lastTickPosX) * partialTicks;
			double doubleY = player.lastTickPosY + (player.posY - player.lastTickPosY) * partialTicks;
			double doubleZ = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * partialTicks;
			
			Vec3d vec = new Vec3d(doubleX, doubleY+player.getEyeHeight(), doubleZ);
			Vec3d vec0 = shooter.getPositionVector().addVector(0, shooter.getEyeHeight()+0.2, 0);
			Vec3d vec1 = vec0;
			
			
			
				vec1 = Optional.ofNullable(ItemLaserWeapon.raytraceEntityPlayerLook(player, ItemLaserWeapon.range))
				.map(rtr -> rtr.hitVec)
				.orElse(vec1);
			
			GlStateManager.translate(-doubleX, -doubleY, -doubleZ);
			
			Tessellator tessellator = Tessellator.getInstance();
			BufferBuilder buffer = tessellator.getBuffer();
			
			Minecraft.getMinecraft().renderEngine.bindTexture(LASER_LOC);
			
			buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX_LMAP_COLOR);
			
			drawBeam(vec0, vec1, vec, 0.13f);
			
			tessellator.draw();
			
			GlStateManager.popMatrix();
			
			GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
			
	}
	
	
	public static void drawBeam(Vec3d S, Vec3d E, Vec3d P, float width) {
		Vec3d PS = S.subtract(P);
		Vec3d SE = E.subtract(S);
	
		Vec3d normal = PS.crossProduct(SE).normalize();
	
		Vec3d half = normal.scale(width);
		Vec3d p1 = S.add(half);
		Vec3d p2 = S.subtract(half);
		Vec3d p3 = E.add(half);
		Vec3d p4 = E.subtract(half);
	
		drawQuad(Tessellator.getInstance(), p1, p3, p4, p2);
	}

	
	private static void drawQuad(Tessellator tessellator, Vec3d p1, Vec3d p2, Vec3d p3, Vec3d p4) {
		int brightness = 240;
		int b1 = brightness >> 16 & 65535;
		int b2 = brightness & 65535;
	
		BufferBuilder buffer = tessellator.getBuffer();
		buffer.pos(p1.x, p1.y, p1.z).tex(0.0D, 0.0D).lightmap(b1, b2).color(255, 255, 255, 128)
			.endVertex();
		buffer.pos(p2.x, p2.y, p2.z).tex(1.0D, 0.0D).lightmap(b1, b2).color(255, 255, 255, 128)
			.endVertex();
		buffer.pos(p3.x, p3.y, p3.z).tex(1.0D, 1.0D).lightmap(b1, b2).color(255, 255, 255, 128)
			.endVertex();
		buffer.pos(p4.x, p4.y, p4.z).tex(0.0D, 1.0D).lightmap(b1, b2).color(255, 255, 255, 128)
			.endVertex();
	}
	
}

 

This is my rendering code right now, but this never seems to do anything? I have the event handler registered in my main class.

Link to comment
Share on other sites

have you done debugging to find out what code is called?

On 08/05/2018 at 8:55 AM, naturaGodhead said:

I have the event handler registered in my main class.

I don't know how you have done this but I believe it is impossible and that you need to put

@Mod.EventBusSubscriber OR @EventBusSubscriber above your ClientEventHandler class and make your onRenderWorld method static. Also why is there 

public ClientEventHandler() {
		
	}

?

 

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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.

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



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.