Skip to content

Commit

Permalink
Reimplement sound & fix sound CME
Browse files Browse the repository at this point in the history
Fix CMEs on offscreen pings
  • Loading branch information
GirafiStudios committed Dec 8, 2023
1 parent 03b7b49 commit da16040
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 155 deletions.
1 change: 0 additions & 1 deletion common/src/main/java/com/girafi/ping/PingCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ public static void loadCommon(Path configPath) {
public static TempConfig config() {
return config;
}

}
216 changes: 106 additions & 110 deletions common/src/main/java/com/girafi/ping/client/PingHandlerHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.girafi.ping.data.PingType;
import com.girafi.ping.data.PingWrapper;
import com.girafi.ping.network.packet.ServerBroadcastPing;
import com.girafi.ping.util.PingSounds;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.mojang.math.Axis;
Expand All @@ -18,7 +19,9 @@
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Blocks;
Expand All @@ -41,8 +44,9 @@ public static void onPingPacket(ServerBroadcastPing packet) {
Minecraft mc = Minecraft.getInstance();
if (mc.player != null && Mth.sqrt((float) mc.player.distanceToSqr(packet.ping.pos.getX(), packet.ping.pos.getY(), packet.ping.pos.getZ())) <= PingCommon.config().pingAcceptDistance) {
if (PingCommon.config().playPingSound) {
//TODO Reimplement sound, when having figured out registration
//mc.getSoundManager().play(new SimpleSoundInstance(PingSounds.BLOOP.get(), SoundSource.PLAYERS, 0.25F, 1.0F, mc.player.level().random, packet.ping.pos.getX(), packet.ping.pos.getY(), packet.ping.pos.getZ()));
synchronized (PingSounds.BLOOP.get()) { //Workaround for crash when playing a lot of ping "bloop" sounds in rapid succession
mc.getSoundManager().play(new SimpleSoundInstance(PingSounds.BLOOP.get(), SoundSource.PLAYERS, 0.25F, 1.0F, mc.player.getRandom(), packet.ping.pos.getX(), packet.ping.pos.getY(), packet.ping.pos.getZ()));
}
}
packet.ping.timer = PingCommon.config().pingDuration;
ACTIVE_PINGS.add(packet.ping);
Expand All @@ -58,106 +62,109 @@ public static void translateWorldPing(PoseStack poseStack, Matrix4f projectionMa
Frustum clippingHelper = new Frustum(poseStack.last().pose(), projectionMatrix);
clippingHelper.prepare(cameraPos.x(), cameraPos.y(), cameraPos.z());

ACTIVE_PINGS.forEach(ping -> {

double px = ping.pos.getX() + 0.5D - cameraPos.x();
double py = ping.pos.getY() + 0.5D - cameraPos.y();
double pz = ping.pos.getZ() + 0.5D - cameraPos.z();

if (clippingHelper.isVisible(ping.getAABB())) {
ping.isOffscreen = false;
if (PingCommon.config().renderBlockOverlay) {
renderPingOverlay(ping.pos.getX() - cameraPos.x(), ping.pos.getY() - cameraPos.y(), ping.pos.getZ() - cameraPos.z(), poseStack, ping);
synchronized (ACTIVE_PINGS) {
ACTIVE_PINGS.forEach(ping -> {

double px = ping.pos.getX() + 0.5D - cameraPos.x();
double py = ping.pos.getY() + 0.5D - cameraPos.y();
double pz = ping.pos.getZ() + 0.5D - cameraPos.z();

if (clippingHelper.isVisible(ping.getAABB())) {
ping.isOffscreen = false;
if (PingCommon.config().renderBlockOverlay) {
renderPingOverlay(ping.pos.getX() - cameraPos.x(), ping.pos.getY() - cameraPos.y(), ping.pos.getZ() - cameraPos.z(), poseStack, ping);
}
renderPing(px, py, pz, poseStack, camera, ping);
} else {
ping.isOffscreen = true;
translatePingCoordinates(px, py, pz, ping);
}
renderPing(px, py, pz, poseStack, camera, ping);
} else {
ping.isOffscreen = true;
translatePingCoordinates(px, py, pz, ping);
}
});
});
}
}

public static void renderPingOffscreen() {
Minecraft mc = Minecraft.getInstance();
for (PingWrapper ping : ACTIVE_PINGS) {
if (!ping.isOffscreen || mc.screen != null || mc.getDebugOverlay().showDebugScreen()) {
continue;
}
int width = mc.getWindow().getScreenWidth();
int height = mc.getWindow().getScreenHeight();

int x1 = -(width / 2) + 32;
int y1 = -(height / 2) + 32;
int x2 = (width / 2) - 32;
int y2 = (height / 2) - 32;

double pingX = ping.screenX;
double pingY = ping.screenY;

pingX -= width * 0.5D;
pingY -= height * 0.5D;

//TODO Fix that player rotation is not being taken into account. Been an issue since the creation of the mod
double angle = Math.atan2(pingY, pingX);
angle += (Math.toRadians(90));
double cos = Math.cos(angle);
double sin = Math.sin(angle);
double m = cos / sin;

if (cos > 0) {
pingX = y2 / m;
pingY = y2;
} else {
pingX = y1 / m;
pingY = y1;
}
public static void renderPingOffscreen(PoseStack poseStack) {
synchronized (ACTIVE_PINGS) {
Minecraft mc = Minecraft.getInstance();
for (PingWrapper ping : ACTIVE_PINGS) {
if (!ping.isOffscreen || mc.screen != null || mc.getDebugOverlay().showDebugScreen()) {
continue;
}
int width = mc.getWindow().getScreenWidth();
int height = mc.getWindow().getScreenHeight();

int x1 = -(width / 2) + 32;
int y1 = -(height / 2) + 32;
int x2 = (width / 2) - 32;
int y2 = (height / 2) - 32;

double pingX = ping.screenX;
double pingY = ping.screenY;

pingX -= width * 0.5D;
pingY -= height * 0.5D;

//TODO Fix that player rotation is not being taken into account. Been an issue since the creation of the mod
double angle = Math.atan2(pingY, pingX);
angle += (Math.toRadians(90));
double cos = Math.cos(angle);
double sin = Math.sin(angle);
double m = cos / sin;

if (cos > 0) {
pingX = y2 / m;
pingY = y2;
} else {
pingX = y1 / m;
pingY = y1;
}

if (pingX > x2) {
pingX = x2;
pingY = x2 * m;
} else if (pingX < x1) {
pingX = x1;
pingY = x1 * m;
}
if (pingX > x2) {
pingX = x2;
pingY = x2 * m;
} else if (pingX < x1) {
pingX = x1;
pingY = x1 * m;
}

pingX += width * 0.5D;
pingY += height * 0.5D;

PoseStack poseStack = new PoseStack();
poseStack.pushPose();
PoseStack.Pose matrixEntry = poseStack.last();
poseStack.translate(pingX / 2, pingY / 2, 0);
RenderSystem.applyModelViewMatrix();

Tesselator tesselator = Tesselator.getInstance();
BufferBuilder vertexBuilder = tesselator.getBuilder();
RenderSystem.setShader(GameRenderer::getPositionTexColorShader);
RenderSystem.setShaderTexture(0, TEXTURE);
final Matrix4f matrix4f = matrixEntry.pose();

vertexBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR);
float min = -8;
float max = 8;

// Ping Notice Background
int r = ping.color >> 16 & 255;
int g = ping.color >> 8 & 255;
int b = ping.color & 255;
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, min, max, PingType.BACKGROUND.getMinU(), PingType.BACKGROUND.getMaxV(), r, g, b, 255);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, max, max, PingType.BACKGROUND.getMaxU(), PingType.BACKGROUND.getMaxV(), r, g, b, 255);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, max, min, PingType.BACKGROUND.getMaxU(), PingType.BACKGROUND.getMinV(), r, g, b, 255);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, min, min, PingType.BACKGROUND.getMinU(), PingType.BACKGROUND.getMinV(), r, g, b, 255);

// Ping Notice Icon
float alpha = ping.type == PingType.ALERT ? mc.level != null ? (float) (1.0F + (0.01D * Math.sin(mc.level.getDayTime()))) : 0.85F : 0.85F;
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, min, max, ping.type.getMinU(), ping.type.getMaxV(), 1.0F, 1.0F, 1.0F, alpha);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, max, max, ping.type.getMaxU(), ping.type.getMaxV(), 1.0F, 1.0F, 1.0F, alpha);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, max, min, ping.type.getMaxU(), ping.type.getMinV(), 1.0F, 1.0F, 1.0F, alpha);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, min, min, ping.type.getMinU(), ping.type.getMinV(), 1.0F, 1.0F, 1.0F, alpha);
tesselator.end();

poseStack.popPose();
RenderSystem.applyModelViewMatrix();
pingX += width * 0.5D;
pingY += height * 0.5D;

poseStack.pushPose();
PoseStack.Pose matrixEntry = poseStack.last();
poseStack.translate(pingX / 2, pingY / 2, 0);
RenderSystem.applyModelViewMatrix();

Tesselator tesselator = Tesselator.getInstance();
BufferBuilder vertexBuilder = tesselator.getBuilder();
RenderSystem.setShader(GameRenderer::getPositionTexColorShader);
RenderSystem.setShaderTexture(0, TEXTURE);
final Matrix4f matrix4f = matrixEntry.pose();

vertexBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR);
float min = -8;
float max = 8;

// Ping Notice Background
int r = ping.color >> 16 & 255;
int g = ping.color >> 8 & 255;
int b = ping.color & 255;
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, min, max, PingType.BACKGROUND.getMinU(), PingType.BACKGROUND.getMaxV(), r, g, b, 255);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, max, max, PingType.BACKGROUND.getMaxU(), PingType.BACKGROUND.getMaxV(), r, g, b, 255);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, max, min, PingType.BACKGROUND.getMaxU(), PingType.BACKGROUND.getMinV(), r, g, b, 255);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, min, min, PingType.BACKGROUND.getMinU(), PingType.BACKGROUND.getMinV(), r, g, b, 255);

// Ping Notice Icon
float alpha = ping.type == PingType.ALERT ? mc.level != null ? (float) (1.0F + (0.01D * Math.sin(mc.level.getDayTime()))) : 0.85F : 0.85F;
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, min, max, ping.type.getMinU(), ping.type.getMaxV(), 1.0F, 1.0F, 1.0F, alpha);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, max, max, ping.type.getMaxU(), ping.type.getMaxV(), 1.0F, 1.0F, 1.0F, alpha);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, max, min, ping.type.getMaxU(), ping.type.getMinV(), 1.0F, 1.0F, 1.0F, alpha);
VertexHelper.renderPosTexColorNoZ(vertexBuilder, matrix4f, min, min, ping.type.getMinU(), ping.type.getMinV(), 1.0F, 1.0F, 1.0F, alpha);
tesselator.end();

poseStack.popPose();
RenderSystem.applyModelViewMatrix();
}
}
}

Expand Down Expand Up @@ -229,18 +236,7 @@ public static void renderPingOverlay(double x, double y, double z, PoseStack pos
}

public static void translatePingCoordinates(double px, double py, double pz, PingWrapper ping) {
FloatBuffer screenCoords = BufferUtils.createFloatBuffer(4);
IntBuffer viewport = BufferUtils.createIntBuffer(16);
FloatBuffer modelView = BufferUtils.createFloatBuffer(16);
FloatBuffer projection = BufferUtils.createFloatBuffer(16);

//GL11.glGetFloatv(GL11.GL_MODELVIEW_MATRIX, modelView);
//GL11.glGetFloatv(GL11.GL_PROJECTION_MATRIX, projection);
//GL11.glGetIntegerv(GL11.GL_VIEWPORT, viewport);

if (GLUUtils.gluProject((float) px, (float) py, (float) pz, modelView, projection, viewport, screenCoords)) {
ping.screenX = screenCoords.get(0);
ping.screenY = screenCoords.get(1);
}
ping.screenX = 0;
ping.screenY = 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//package com.girafi.ping.util;
//
//import net.minecraftforge.common.ForgeConfigSpec;
//import net.neoforged.neoforge.common.ModConfigSpec;
//
//public class Config {
// private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
//public class PingConfig {
// private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder();
// public static final General GENERAL = new General(BUILDER);
// public static final Visual VISUAL = new Visual(BUILDER);
//
// public static class General {
// public ForgeConfigSpec.DoubleValue pingAcceptDistance;
// public ForgeConfigSpec.IntValue pingDuration;
// public ForgeConfigSpec.BooleanValue sound;
// public ModConfigSpec.DoubleValue pingAcceptDistance;
// public ModConfigSpec.IntValue pingDuration;
// public ModConfigSpec.BooleanValue sound;
//
// General(ForgeConfigSpec.Builder builder) {
// General(ModConfigSpec.Builder builder) {
// builder.push("general");
// pingAcceptDistance = builder
// .comment("Maximum distance a Ping can be from you and still be received")
Expand All @@ -31,13 +31,13 @@
// }
//
// public static class Visual {
// public ForgeConfigSpec.IntValue pingR;
// public ForgeConfigSpec.IntValue pingG;
// public ForgeConfigSpec.IntValue pingB;
// public ForgeConfigSpec.BooleanValue blockOverlay;
// public ForgeConfigSpec.BooleanValue menuBackground;
// public ModConfigSpec.IntValue pingR;
// public ModConfigSpec.IntValue pingG;
// public ModConfigSpec.IntValue pingB;
// public ModConfigSpec.BooleanValue blockOverlay;
// public ModConfigSpec.BooleanValue menuBackground;
//
// Visual(ForgeConfigSpec.Builder builder) {
// Visual(ModConfigSpec.Builder builder) {
// builder.push("visual");
// blockOverlay = builder
// .comment("Whether to render a colored overlay on the Pinged block")
Expand All @@ -61,5 +61,5 @@
// }
// }
//
// public static final ForgeConfigSpec spec = BUILDER.build();
// public static final ModConfigSpec spec = BUILDER.build();
//}
44 changes: 23 additions & 21 deletions common/src/main/java/com/girafi/ping/util/PingSounds.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
//package com.girafi.ping.util;
//
//import net.minecraft.resources.ResourceLocation;
//import net.minecraft.sounds.SoundEvent;
//
//public class PingSounds {
// public static final DeferredRegister<SoundEvent> SOUND_DEFERRED = DeferredRegister.create(ForgeRegistries.SOUND_EVENTS, Ping.MOD_ID);
// public static final RegistryObject<SoundEvent> BLOOP = registerSound("bloop");
//
// /**
// * Registers a sound
// *
// * @param name The name to register the sound with
// * @return The Sound that was registered
// */
// public static RegistryObject<SoundEvent> registerSound(String name) {
// ResourceLocation resourceLocation = new ResourceLocation(Ping.MOD_ID, name);
// SoundEvent sound = SoundEvent.createVariableRangeEvent(resourceLocation);
// return SOUND_DEFERRED.register(name, () -> sound);
// }
//}
package com.girafi.ping.util;

import com.girafi.ping.Constants;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;

import java.util.function.Supplier;

public class PingSounds {
public static final Supplier<SoundEvent> BLOOP = registerSound("bloop");

/**
* Registers a sound
*
* @param name The name to register the sound with
* @return The Sound that was registered
*/
public static Supplier<SoundEvent> registerSound(String name) {
ResourceLocation resourceLocation = new ResourceLocation(Constants.MOD_ID, name);
SoundEvent sound = SoundEvent.createVariableRangeEvent(resourceLocation);
return () -> sound;
}
}

0 comments on commit da16040

Please sign in to comment.