Skip to content

Instantly share code, notes, and snippets.

@cab404
Created January 5, 2017 03:29
Show Gist options
  • Save cab404/4143e51ae4add080bebfc2a806aa601b to your computer and use it in GitHub Desktop.
Save cab404/4143e51ae4add080bebfc2a806aa601b to your computer and use it in GitHub Desktop.
package com.cab404.moblightlevel;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.lwjgl.opengl.GL11;
import java.util.ArrayList;
import java.util.List;
@Mod(
modid = LightLevelMod.MODID,
version = LightLevelMod.VERSION,
clientSideOnly = true,
canBeDeactivated = true
)
public class LightLevelMod {
public static final String MODID = "com.cab404.moblightlevelmod";
public static final String VERSION = "1.0";
int glCross = -1;
List<BlockPos> spawnPoints;
boolean disabled = false;
@EventHandler
public void init(FMLInitializationEvent event) {
disabled = false;
MinecraftForge.EVENT_BUS.register(this);
}
BlockPos.MutableBlockPos tmp = new BlockPos.MutableBlockPos();
BlockPos.MutableBlockPos last = new BlockPos.MutableBlockPos();
@SubscribeEvent
public void render(RenderWorldLastEvent ev) {
if (disabled) return;
if (spawnPoints == null) return;
EntityPlayerSP player = Minecraft.getMinecraft().player;
float refx = (float) (player.lastTickPosX + (player.posX - player.lastTickPosX) * ev.getPartialTicks());
float refy = (float) (player.lastTickPosY + (player.posY - player.lastTickPosY) * ev.getPartialTicks());
float refz = (float) (player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * ev.getPartialTicks());
if (glCross == -1) {
glCross = GL11.glGenLists(1);
GlStateManager.glNewList(glCross, GL11.GL_COMPILE);
GlStateManager.glBegin(GL11.GL_QUADS);
GlStateManager.glVertex3f(0.3f, .1f, 0.3f);
GlStateManager.glVertex3f(0.3f, .1f, 0.7f);
GlStateManager.glVertex3f(0.7f, .1f, 0.7f);
GlStateManager.glVertex3f(0.7f, .1f, 0.3f);
GlStateManager.glEnd();
GlStateManager.glEndList();
}
GlStateManager.pushMatrix();
GlStateManager.enableBlend();
GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GlStateManager.enableDepth();
GlStateManager.depthMask(true);
GlStateManager.glLineWidth(2.0f);
GL11.glEnable(GL11.GL_LINE_SMOOTH);
GlStateManager.translate(-refx, -refy, -refz);
GlStateManager.color(1, 0, 0, 0.7f);
for (BlockPos pos : spawnPoints) {
GlStateManager.translate(pos.getX(), pos.getY(), pos.getZ());
GlStateManager.callList(glCross);
GlStateManager.translate(-pos.getX(), -pos.getY(), -pos.getZ());
}
GlStateManager.translate(refx, refy, refz);
GlStateManager.glLineWidth(1.0f);
GL11.glDisable(GL11.GL_LINE_SMOOTH);
GlStateManager.enableTexture2D();
GlStateManager.disableBlend();
GlStateManager.enableTexture2D();
GlStateManager.popMatrix();
}
@SubscribeEvent
public void show(BlockEvent ev) {
updateData();
}
@SubscribeEvent
public void show(TickEvent.PlayerTickEvent ev) {
updateData();
}
private void updateData() {
if (disabled) return;
Minecraft minecraft = Minecraft.getMinecraft();
EntityPlayerSP player = minecraft.player;
if (player == null) return;
World world = player.getEntityWorld();
minecraft.mcProfiler.startSection("moblightlevels");
BlockPos ppos = player.getPosition();
int px = ppos.getX();
int py = ppos.getY();
int pz = ppos.getZ();
if (last.getX() == px && last.getY() == py && last.getZ() == pz) return;
last.setPos(ppos);
ArrayList<BlockPos> positions = new ArrayList<BlockPos>();
for (int x = -10; x < 10; x++) {
for (int y = -10; y < 10; y++) {
for (int z = -10; z < 10; z++) {
if (py + y <= 0 || py + y >= world.getActualHeight())
continue;
tmp.setPos(x + ppos.getX(), y + ppos.getY(), z + ppos.getZ());
if (!world.isAirBlock(tmp))
continue;
if (world.getLightFromNeighborsFor(EnumSkyBlock.BLOCK, tmp) > 7)
continue;
tmp.setPos(x + px, y + py - 1, z + pz);
if (!world.isBlockFullCube(tmp))
continue;
positions.add(new BlockPos(x + px, y + py, z + pz));
}
}
}
spawnPoints = positions;
minecraft.mcProfiler.endSection();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment