better render
This commit is contained in:
@@ -1,14 +1,21 @@
|
||||
package com.lumijiez.lumiscope;
|
||||
|
||||
import com.lumijiez.lumiscope.handlers.RadarPacketHandler;
|
||||
import com.lumijiez.lumiscope.handlers.RadarRenderer;
|
||||
import com.lumijiez.lumiscope.proxy.CommonProxy;
|
||||
import com.lumijiez.lumiscope.util.Ref;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.Mod.EventHandler;
|
||||
import net.minecraftforge.fml.common.SidedProxy;
|
||||
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.gameevent.TickEvent;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
|
||||
import static com.lumijiez.lumiscope.util.Ref.logger;
|
||||
|
||||
@@ -21,12 +28,15 @@ public class Lumiscope {
|
||||
public void preInit(FMLPreInitializationEvent event)
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
RadarPacketHandler.registerMessages();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void init(FMLInitializationEvent event)
|
||||
{
|
||||
logger.info("Radar turned on!");
|
||||
if (event.getSide() == Side.CLIENT) {
|
||||
MinecraftForge.EVENT_BUS.register(RadarRenderer.getInstance());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@@ -34,4 +44,13 @@ public class Lumiscope {
|
||||
{
|
||||
logger.info("Radar turned on!");
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onServerTick(TickEvent.ServerTickEvent event) {
|
||||
if (event.phase == TickEvent.Phase.END) {
|
||||
for (EntityPlayerMP player : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers()) {
|
||||
RadarPacketHandler.sendRadarUpdate(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.lumijiez.lumiscope.handlers;
|
||||
|
||||
import com.lumijiez.lumiscope.items.radars.ShortRadar;
|
||||
import com.lumijiez.lumiscope.network.RadarPacket;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RadarPacketHandler {
|
||||
public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel("lumiscope");
|
||||
|
||||
public static void registerMessages() {
|
||||
INSTANCE.registerMessage(RadarPacket.Handler.class, RadarPacket.class, 0, Side.CLIENT);
|
||||
}
|
||||
|
||||
public static void sendRadarUpdate(EntityPlayerMP player) {
|
||||
if (!(player.getHeldItemMainhand().getItem() instanceof ShortRadar)) {
|
||||
return;
|
||||
}
|
||||
List<RadarPacket.PlayerInfo> playerInfos = new ArrayList<>();
|
||||
for (EntityPlayerMP otherPlayer : player.getServerWorld().getMinecraftServer().getPlayerList().getPlayers()) {
|
||||
if (!otherPlayer.equals(player) && player.getDistance(otherPlayer) <= 100) {
|
||||
//String direction = getPlayerDirection(player, otherPlayer);
|
||||
double direction = getPlayerDirection(player, otherPlayer);
|
||||
playerInfos.add(new RadarPacket.PlayerInfo(otherPlayer.getName(), direction));
|
||||
}
|
||||
}
|
||||
INSTANCE.sendTo(new RadarPacket(playerInfos), player);
|
||||
}
|
||||
|
||||
private static double getPlayerDirection(EntityPlayerMP player, EntityPlayerMP otherPlayer) {
|
||||
double deltaX = otherPlayer.posX - player.posX;
|
||||
double deltaZ = otherPlayer.posZ - player.posZ;
|
||||
|
||||
double angle = MathHelper.atan2(deltaZ, deltaX) * (180 / Math.PI) - 90;
|
||||
if (angle < 0) {
|
||||
angle += 360;
|
||||
}
|
||||
|
||||
angle = (angle + 180) % 360;
|
||||
return Math.toRadians(angle);
|
||||
}
|
||||
}
|
||||
147
src/main/java/com/lumijiez/lumiscope/handlers/RadarRenderer.java
Normal file
147
src/main/java/com/lumijiez/lumiscope/handlers/RadarRenderer.java
Normal file
@@ -0,0 +1,147 @@
|
||||
package com.lumijiez.lumiscope.handlers;
|
||||
|
||||
import com.lumijiez.lumiscope.items.radars.ShortRadar;
|
||||
import com.lumijiez.lumiscope.network.RadarPacket;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.renderer.RenderHelper;
|
||||
import net.minecraft.client.renderer.entity.RenderDragon;
|
||||
import net.minecraft.client.renderer.entity.RenderManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.client.event.RenderWorldLastEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class RadarRenderer {
|
||||
private static final Minecraft mc = Minecraft.getMinecraft();
|
||||
private static final RadarRenderer INSTANCE = new RadarRenderer();
|
||||
private final ResourceLocation radarTexture = new ResourceLocation("lumiscope", "textures/gui/radar.png");
|
||||
private final ResourceLocation radarArrowTexture = new ResourceLocation("lumiscope", "textures/gui/radar_arrow.png");
|
||||
private List<RadarPacket.PlayerInfo> playerInfos;
|
||||
|
||||
private RadarRenderer() {}
|
||||
|
||||
public static RadarRenderer getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public void updatePlayerInfos(List<RadarPacket.PlayerInfo> playerInfos) {
|
||||
this.playerInfos = playerInfos;
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onRenderWorldLast(RenderWorldLastEvent event) {
|
||||
if (shouldRenderRadar()) {
|
||||
if (playerInfos != null && !playerInfos.isEmpty()) {
|
||||
renderRadar(event.getPartialTicks());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean shouldRenderRadar() {
|
||||
return mc.player.getHeldItemMainhand().getItem() instanceof ShortRadar ||
|
||||
mc.player.getHeldItemOffhand().getItem() instanceof ShortRadar;
|
||||
}
|
||||
|
||||
private void renderRadar(float partialTicks) {
|
||||
GlStateManager.pushMatrix();
|
||||
GlStateManager.translate(0, 0, 0);
|
||||
|
||||
mc.getTextureManager().bindTexture(radarTexture);
|
||||
|
||||
GlStateManager.disableDepth();
|
||||
GlStateManager.disableLighting();
|
||||
GlStateManager.disableCull();
|
||||
|
||||
drawTexturedCircle(1.4f);
|
||||
|
||||
for (RadarPacket.PlayerInfo info : playerInfos) {
|
||||
double angle = info.direction - Math.toRadians(90);
|
||||
drawTexturedLine(1.4f, angle, radarArrowTexture);
|
||||
}
|
||||
|
||||
for (RadarPacket.PlayerInfo info : playerInfos) {
|
||||
double angle = info.direction - Math.toRadians(90);
|
||||
GlStateManager.pushMatrix();
|
||||
GlStateManager.translate((float) (Math.cos(angle) * 1.2), 0.001, (float) (Math.sin(angle) * 1.2));
|
||||
GlStateManager.rotate(90f, 1, 0, 0);
|
||||
|
||||
GlStateManager.rotate((float) Math.toDegrees(angle), 0, 0, 1);
|
||||
GlStateManager.scale(0.01, 0.01, 0.01);
|
||||
|
||||
RenderHelper.disableStandardItemLighting();
|
||||
GlStateManager.enableTexture2D();
|
||||
mc.fontRenderer.drawStringWithShadow(info.name, -mc.fontRenderer.getStringWidth(info.name) - 50 , -4, 0xAAFF00);
|
||||
GlStateManager.disableTexture2D();
|
||||
RenderHelper.enableStandardItemLighting();
|
||||
GlStateManager.popMatrix();
|
||||
}
|
||||
|
||||
GlStateManager.enableDepth();
|
||||
GlStateManager.enableLighting();
|
||||
GlStateManager.enableCull();
|
||||
GlStateManager.disableBlend();
|
||||
GlStateManager.popMatrix();
|
||||
}
|
||||
|
||||
private void drawTexturedCircle(float radius) {
|
||||
mc.getTextureManager().bindTexture(radarTexture);
|
||||
|
||||
GlStateManager.enableTexture2D();
|
||||
|
||||
GlStateManager.glBegin(GL11.GL_TRIANGLE_FAN);
|
||||
|
||||
GlStateManager.glTexCoord2f(0.5f, 0.5f);
|
||||
GlStateManager.glVertex3f(0, 0, 0);
|
||||
|
||||
for (int i = 0; i <= 360; i++) {
|
||||
double rad = Math.toRadians(i);
|
||||
float x = (float) (Math.cos(rad) * radius);
|
||||
float y = (float) (Math.sin(rad) * radius);
|
||||
|
||||
float u = (x / radius + 1.0f) / 2.0f;
|
||||
float v = (y / radius + 1.0f) / 2.0f;
|
||||
|
||||
GlStateManager.glTexCoord2f(u, v);
|
||||
GlStateManager.glVertex3f(x, 0, y);
|
||||
}
|
||||
|
||||
GlStateManager.glEnd();
|
||||
|
||||
GlStateManager.disableTexture2D();
|
||||
}
|
||||
|
||||
private void drawTexturedLine(float length, double angle, ResourceLocation texture) {
|
||||
mc.getTextureManager().bindTexture(texture);
|
||||
|
||||
GlStateManager.enableTexture2D();
|
||||
|
||||
GlStateManager.pushMatrix();
|
||||
GlStateManager.translate((float) (Math.cos(angle) * length / 2), 0, (float) (Math.sin(angle) * length / 2));
|
||||
GlStateManager.rotate(90f, 1, 0, 0);
|
||||
GlStateManager.rotate((float) Math.toDegrees(angle) + 90, 0, 0, 1);
|
||||
GlStateManager.scale(0.2, length, 1);
|
||||
|
||||
GlStateManager.glBegin(GL11.GL_QUADS);
|
||||
|
||||
GlStateManager.glTexCoord2f(0, 0);
|
||||
GlStateManager.glVertex3f(-0.5f, -0.5f, 0);
|
||||
GlStateManager.glTexCoord2f(1, 0);
|
||||
GlStateManager.glVertex3f(0.5f, -0.5f, 0);
|
||||
GlStateManager.glTexCoord2f(1, 1);
|
||||
GlStateManager.glVertex3f(0.5f, 0.5f, 0);
|
||||
GlStateManager.glTexCoord2f(0, 1);
|
||||
GlStateManager.glVertex3f(-0.5f, 0.5f, 0);
|
||||
|
||||
GlStateManager.glEnd();
|
||||
|
||||
GlStateManager.popMatrix();
|
||||
|
||||
GlStateManager.disableTexture2D();
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,10 @@ package com.lumijiez.lumiscope.items.radars;
|
||||
import com.lumijiez.lumiscope.items.ItemBase;
|
||||
import net.minecraft.client.util.ITooltipFlag;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.init.MobEffects;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.potion.PotionEffect;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.EnumActionResult;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.Style;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.util.text.TextFormatting;
|
||||
@@ -23,7 +19,6 @@ public class ShortRadar extends ItemBase {
|
||||
public ShortRadar() {
|
||||
super("short_radar");
|
||||
setMaxStackSize(1);
|
||||
setMaxDamage(70);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,62 +27,8 @@ public class ShortRadar extends ItemBase {
|
||||
.setStyle(new Style().setColor(TextFormatting.AQUA)).getFormattedText());
|
||||
|
||||
tooltip.add(new TextComponentString("Does not detect invisible players!")
|
||||
.setStyle(new Style().setColor(TextFormatting.DARK_RED).setBold(true).setItalic(true)).getFormattedText());
|
||||
.setStyle(new Style().setColor(TextFormatting.DARK_RED).setBold(true).setItalic(true)).getFormattedText());
|
||||
|
||||
super.addInformation(stack, worldIn, tooltip, flagIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
|
||||
if (!world.isRemote) {
|
||||
player.getHeldItem(hand).damageItem(1, player);
|
||||
|
||||
boolean playerNearby = false;
|
||||
|
||||
for (EntityPlayer otherPlayer : world.playerEntities) {
|
||||
|
||||
if (!otherPlayer.equals(player)
|
||||
&& player.getDistance(otherPlayer) <= 100
|
||||
&& otherPlayer.getActivePotionEffects().stream()
|
||||
.noneMatch(potionEffect -> potionEffect.getPotion() == MobEffects.INVISIBILITY)) {
|
||||
|
||||
playerNearby = true;
|
||||
String direction = getPlayerDirection(player, otherPlayer);
|
||||
ITextComponent message = new TextComponentString(otherPlayer.getName() + " to the " + direction + "!")
|
||||
.setStyle(new Style().setColor(TextFormatting.GREEN));
|
||||
player.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
if (!playerNearby) {
|
||||
ITextComponent noPlayersMessage = new TextComponentString("Could not detect players within 100 meters.")
|
||||
.setStyle(new Style().setColor(TextFormatting.RED));
|
||||
player.sendMessage(noPlayersMessage);
|
||||
}
|
||||
|
||||
player.getCooldownTracker().setCooldown(this, 200);
|
||||
}
|
||||
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
|
||||
}
|
||||
|
||||
private String getPlayerDirection(EntityPlayer player, EntityPlayer otherPlayer) {
|
||||
double deltaX = otherPlayer.posX - player.posX;
|
||||
double deltaZ = otherPlayer.posZ - player.posZ;
|
||||
double angle = MathHelper.atan2(deltaZ, deltaX) * (180 / Math.PI) - 90;
|
||||
if (angle < 0) {
|
||||
angle += 360;
|
||||
}
|
||||
|
||||
angle = (angle + 180) % 360;
|
||||
|
||||
if (angle >= 337.5 || angle < 22.5) return "north";
|
||||
if (angle >= 22.5 && angle < 67.5) return "northeast";
|
||||
if (angle >= 67.5 && angle < 112.5) return "east";
|
||||
if (angle >= 112.5 && angle < 157.5) return "southeast";
|
||||
if (angle >= 157.5 && angle < 202.5) return "south";
|
||||
if (angle >= 202.5 && angle < 247.5) return "southwest";
|
||||
if (angle >= 247.5 && angle < 292.5) return "west";
|
||||
if (angle >= 292.5) return "northwest";
|
||||
return "unknown direction";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.lumijiez.lumiscope.network;
|
||||
|
||||
import com.lumijiez.lumiscope.handlers.RadarRenderer;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RadarPacket implements IMessage {
|
||||
public static class PlayerInfo {
|
||||
public String name;
|
||||
public double direction;
|
||||
|
||||
public PlayerInfo(String name, double direction) {
|
||||
this.name = name;
|
||||
this.direction = direction;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private List<PlayerInfo> playerInfos;
|
||||
|
||||
public RadarPacket() {
|
||||
this.playerInfos = new ArrayList<>();
|
||||
}
|
||||
|
||||
public RadarPacket(List<PlayerInfo> playerInfos) {
|
||||
this.playerInfos = playerInfos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeInt(playerInfos.size());
|
||||
for (PlayerInfo info : playerInfos) {
|
||||
byte[] nameBytes = info.name.getBytes();
|
||||
buf.writeInt(nameBytes.length);
|
||||
buf.writeBytes(nameBytes);
|
||||
buf.writeDouble(info.direction);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
int size = buf.readInt();
|
||||
playerInfos = new ArrayList<>(size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
int nameLength = buf.readInt();
|
||||
byte[] nameBytes = new byte[nameLength];
|
||||
buf.readBytes(nameBytes);
|
||||
String name = new String(nameBytes);
|
||||
double direction = buf.readDouble();
|
||||
playerInfos.add(new PlayerInfo(name, direction));
|
||||
}
|
||||
}
|
||||
|
||||
public static class Handler implements IMessageHandler<RadarPacket, IMessage> {
|
||||
@Override
|
||||
public IMessage onMessage(RadarPacket message, MessageContext ctx) {
|
||||
Minecraft.getMinecraft().addScheduledTask(() -> {
|
||||
RadarRenderer renderer = RadarRenderer.getInstance();
|
||||
renderer.updatePlayerInfos(message.playerInfos);
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,4 +8,6 @@ public class ClientProxy extends CommonProxy {
|
||||
public void registerItemRenderer(Item item, int meta, String id) {
|
||||
ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(item.getRegistryName(), id));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.apache.logging.log4j.Logger;
|
||||
public class Ref {
|
||||
public static final String MODID = "lumiscope";
|
||||
public static final String NAME = "Lumiscope";
|
||||
public static final String VERSION = "1.1.0";
|
||||
public static final String VERSION = "1.1.1";
|
||||
public static final String CLIENT_PROXY_CLASS = "com.lumijiez.lumiscope.proxy.ClientProxy";
|
||||
public static final String COMMON_PROXY_CLASS = "com.lumijiez.lumiscope.proxy.CommonProxy";
|
||||
public static final Logger logger = LogManager.getLogger();
|
||||
|
||||
BIN
src/main/resources/assets/lumiscope/textures/gui/radar.png
Normal file
BIN
src/main/resources/assets/lumiscope/textures/gui/radar.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 81 KiB |
BIN
src/main/resources/assets/lumiscope/textures/gui/radar_arrow.png
Normal file
BIN
src/main/resources/assets/lumiscope/textures/gui/radar_arrow.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.6 KiB |
Reference in New Issue
Block a user