Skip to content

Commit

Permalink
Fix sound not playing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny-Hui committed Feb 6, 2025
1 parent 83c596e commit 4f5bd56
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 57 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## (JCM v2.0.0 beta.7) for (MTR 4.0.0-beta.13) has been released!
## (JCM v2.0.0 beta.7) for (MTR 4.0.0-beta.14) has been released!

**New:**
- **Scripting**
Expand All @@ -9,6 +9,7 @@
- - The PIDS Variable `{worldPlayer}` is now implemented and displayed correctly again

**Changes:**
- Compatiiblity for MTR 4.0.0-beta.14
- **Scripting**
- - `Resources.readString` now returns null instead of an empty string if reading fails, just like previously in NTE
- - Scripts that points to an invalid location will now error out in the console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.lx862.jcm.mod.resources.MTRContentResourceManager;
import com.lx862.jcm.mod.scripting.eyecandy.EyeCandyScriptContext;
import com.lx862.jcm.mod.scripting.eyecandy.EyeCandyScriptInstance;
import com.lx862.jcm.mod.scripting.eyecandy.ModelDrawCall;
import com.lx862.mtrscripting.api.ScriptResultCall;
import com.lx862.mtrscripting.scripting.ParsedScript;
import com.lx862.mtrscripting.scripting.UniqueKey;
import com.lx862.mtrscripting.scripting.base.ScriptInstance;
Expand Down Expand Up @@ -50,24 +50,23 @@ public void renderScript(BlockEyeCandy.BlockEntity blockEntity, float tickDelta,
Direction facing = IBlock.getStatePropertySafe(blockEntity.getWorld2(), blockPos, BlockEyeCandy.FACING);

StoredMatrixTransformations blockStoredMatrixTransformations = new StoredMatrixTransformations(0.5 + (double)blockEntity.getPos2().getX(), (double)blockEntity.getPos2().getY(), 0.5 + (double)blockEntity.getPos2().getZ());
StoredMatrixTransformations storedMatrixTransformations = blockStoredMatrixTransformations.copy();

for(ModelDrawCall drawCall : new ArrayList<>(eyeCandyScriptInstance.drawCalls)) {
if(drawCall == null) continue;

StoredMatrixTransformations storedMatrixTransformations = blockStoredMatrixTransformations.copy();
if(drawCall.getTransformation() != null) {
storedMatrixTransformations.add(drawCall.getTransformation());
}

storedMatrixTransformations.add((graphicsHolderNew) -> {
graphicsHolderNew.translate(blockEntity.getTranslateX(), blockEntity.getTranslateY(), (double)blockEntity.getTranslateZ());
graphicsHolderNew.rotateYDegrees(180.0F - facing.asRotation());
graphicsHolderNew.rotateXDegrees(blockEntity.getRotateX() + 180.0F);
graphicsHolderNew.rotateYDegrees(blockEntity.getRotateY());
graphicsHolderNew.rotateZDegrees(blockEntity.getRotateZ());
});
storedMatrixTransformations.add((graphicsHolderNew) -> {
graphicsHolderNew.translate(blockEntity.getTranslateX(), blockEntity.getTranslateY(), blockEntity.getTranslateZ());
graphicsHolderNew.rotateYDegrees(180.0F - facing.asRotation());
graphicsHolderNew.rotateXDegrees(blockEntity.getRotateX() + 180.0F);
graphicsHolderNew.rotateYDegrees(blockEntity.getRotateY());
graphicsHolderNew.rotateZDegrees(blockEntity.getRotateZ());
});

drawCall.draw(storedMatrixTransformations, light);
for(ScriptResultCall drawCall : new ArrayList<>(eyeCandyScriptInstance.drawCalls)) {
if(drawCall == null) continue; // TODO: They should never be null
drawCall.run(world, graphicsHolder, storedMatrixTransformations, facing, light);
}
for(ScriptResultCall soundCall : new ArrayList<>(eyeCandyScriptInstance.soundCalls)) {
if(soundCall == null) continue; // TODO: They should never be null
soundCall.run(world, graphicsHolder, blockStoredMatrixTransformations, facing, light);
}
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.lx862.jcm.mod.Constants;
import com.lx862.jcm.mod.JCMClient;
import com.lx862.jcm.mod.block.entity.PIDSBlockEntity;
import com.lx862.jcm.mod.scripting.pids.PIDSDrawCall;
import com.lx862.jcm.mod.scripting.pids.PIDSScriptContext;
import com.lx862.jcm.mod.scripting.pids.PIDSScriptInstance;
import com.lx862.jcm.mod.scripting.pids.PIDSWrapper;
import com.lx862.mtrscripting.api.ScriptResultCall;
import com.lx862.mtrscripting.scripting.ParsedScript;
import com.lx862.mtrscripting.scripting.UniqueKey;
import com.lx862.mtrscripting.scripting.base.ScriptInstance;
Expand All @@ -20,6 +20,7 @@
import org.mtr.mapping.holder.Identifier;
import org.mtr.mapping.holder.World;
import org.mtr.mapping.mapper.GraphicsHolder;
import org.mtr.mod.render.StoredMatrixTransformations;

import javax.annotation.Nullable;
import java.util.ArrayList;
Expand Down Expand Up @@ -84,11 +85,11 @@ public void render(PIDSBlockEntity be, GraphicsHolder graphicsHolder, World worl
});

graphicsHolder.translate(0, 0, -0.5);
for(PIDSDrawCall PIDSDrawCall : new ArrayList<>(pidsScriptInstance.drawCalls)) {
if(PIDSDrawCall == null) continue;
for(ScriptResultCall resultCalls : new ArrayList<>(pidsScriptInstance.drawCalls)) {
if(resultCalls == null) continue;
graphicsHolder.translate(0, 0, -0.02);
graphicsHolder.push();
PIDSDrawCall.draw(graphicsHolder, facing);
resultCalls.run(world, graphicsHolder, new StoredMatrixTransformations(), facing, MAX_RENDER_LIGHT);
graphicsHolder.pop();
}
}
Expand Down
13 changes: 7 additions & 6 deletions fabric/src/main/java/com/lx862/jcm/mod/scripting/SoundCall.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.lx862.jcm.mod.scripting;

import org.mtr.mapping.holder.Identifier;
import org.mtr.mapping.holder.SoundCategory;
import org.mtr.mapping.holder.SoundEvent;
import org.mtr.mapping.holder.World;
import com.lx862.mtrscripting.api.ScriptResultCall;
import org.mtr.mapping.holder.*;
import org.mtr.mapping.mapper.GraphicsHolder;
import org.mtr.mapping.mapper.SoundHelper;
import org.mtr.mod.render.StoredMatrixTransformations;

public class SoundCall {
public class SoundCall extends ScriptResultCall {
private final SoundEvent soundEvent;
private final double x;
private final double y;
Expand All @@ -23,7 +23,8 @@ public SoundCall(Identifier id, double x, double y, double z, float volume, floa
this.pitch = pitch;
}

public void draw(World world) {
@Override
public void run(World world, GraphicsHolder graphicsHolder, StoredMatrixTransformations storedMatrixTransformations, Direction facing, int light) {
world.playSound(x, y, z, soundEvent, SoundCategory.MASTER, volume, pitch, false);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lx862.jcm.mod.scripting.eyecandy;

import com.lx862.jcm.mod.scripting.SoundCall;
import com.lx862.mtrscripting.api.ScriptResultCall;
import com.lx862.mtrscripting.scripting.ParsedScript;
import com.lx862.mtrscripting.scripting.base.ScriptInstance;
import org.mtr.mod.block.BlockEyeCandy;
Expand All @@ -10,8 +11,8 @@

public class EyeCandyScriptInstance extends ScriptInstance<BlockEyeCandy.BlockEntity> {
private final BlockEyeCandy.BlockEntity be;
public final List<ModelDrawCall> drawCalls;
public final List<SoundCall> soundCalls;
public final List<ScriptResultCall> drawCalls;
public final List<ScriptResultCall> soundCalls;

public EyeCandyScriptInstance(EyeCandyScriptContext context, BlockEyeCandy.BlockEntity be, ParsedScript script) {
super(context, script);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.lx862.jcm.mod.scripting.eyecandy;

import com.lx862.mtrscripting.api.ScriptResultCall;
import com.lx862.mtrscripting.scripting.util.ScriptedModel;
import org.mtr.mapping.holder.Direction;
import org.mtr.mapping.holder.World;
import org.mtr.mapping.mapper.GraphicsHolder;
import org.mtr.mod.render.StoredMatrixTransformations;

public class ModelDrawCall {
public class ModelDrawCall extends ScriptResultCall {
private final ScriptedModel model;
private final StoredMatrixTransformations storedMatrixTransformations;

Expand All @@ -12,11 +16,12 @@ public ModelDrawCall(ScriptedModel model, StoredMatrixTransformations storedMatr
this.storedMatrixTransformations = storedMatrixTransformations;
}

public void draw(StoredMatrixTransformations storedMatrixTransformations, int light) {
this.model.draw(storedMatrixTransformations, light);
}
@Override
public void run(World world, GraphicsHolder graphicsHolder, StoredMatrixTransformations storedMatrixTransformations, Direction facing, int light) {
if(this.storedMatrixTransformations != null) {
storedMatrixTransformations.add(this.storedMatrixTransformations);
}

public StoredMatrixTransformations getTransformation() {
return this.storedMatrixTransformations;
this.model.draw(storedMatrixTransformations, light);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.lx862.jcm.mod.scripting.pids;

import com.lx862.mtrscripting.api.ScriptResultCall;
import com.lx862.mtrscripting.scripting.util.Matrices;
import org.mtr.mapping.holder.Direction;
import org.mtr.mapping.holder.Vector3d;
import org.mtr.mapping.holder.World;
import org.mtr.mapping.mapper.GraphicsHolder;
import org.mtr.mod.render.StoredMatrixTransformations;

public abstract class PIDSDrawCall {
public abstract class PIDSDrawCall extends ScriptResultCall {
public StoredMatrixTransformations storedMatrixTransformations;
public double x;
public double y;
Expand Down Expand Up @@ -40,7 +42,8 @@ public void draw(PIDSScriptContext ctx) {
ctx.draw(this);
}

public void draw(GraphicsHolder graphicsHolder, Direction facing) {
@Override
public void run(World world, GraphicsHolder graphicsHolder, StoredMatrixTransformations storedMatrixTransformations, Direction facing, int light) {
graphicsHolder.push();
storedMatrixTransformations.transform(graphicsHolder, Vector3d.getZeroMapped());
graphicsHolder.translate(this.x, this.y, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lx862.jcm.mod.scripting.pids;

import com.lx862.mtrscripting.api.ScriptResultCall;
import com.lx862.mtrscripting.scripting.ParsedScript;
import com.lx862.mtrscripting.scripting.base.ScriptInstance;
import org.mtr.mapping.holder.BlockEntity;
Expand All @@ -11,7 +12,7 @@

public class PIDSScriptInstance extends ScriptInstance<PIDSWrapper> {
private final BlockEntity be;
public final List<PIDSDrawCall> drawCalls;
public final List<ScriptResultCall> drawCalls;

public PIDSScriptInstance(BlockPos pos, ParsedScript script) {
super(new PIDSScriptContext(), script);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.lx862.mtrscripting.api;

import org.mtr.mapping.holder.Direction;
import org.mtr.mapping.holder.World;
import org.mtr.mapping.mapper.GraphicsHolder;
import org.mtr.mod.render.StoredMatrixTransformations;

public abstract class ScriptResultCall {
public abstract void run(World world, GraphicsHolder graphicsHolder, StoredMatrixTransformations storedMatrixTransformations, Direction facing, int light);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.lx862.jcm.mod.resources.MTRContentResourceManager;
import com.lx862.jcm.mod.scripting.eyecandy.EyeCandyScriptContext;
import com.lx862.jcm.mod.scripting.eyecandy.EyeCandyScriptInstance;
import com.lx862.jcm.mod.scripting.eyecandy.ModelDrawCall;
import com.lx862.mtrscripting.api.ScriptResultCall;
import com.lx862.mtrscripting.scripting.ParsedScript;
import com.lx862.mtrscripting.scripting.UniqueKey;
import com.lx862.mtrscripting.scripting.base.ScriptInstance;
Expand Down Expand Up @@ -47,25 +47,23 @@ public void renderScript(BlockEyeCandy.BlockEntity blockEntity, float tickDelta,
BlockPos blockPos = blockEntity.getPos2();
Direction facing = IBlock.getStatePropertySafe(blockEntity.getWorld2(), blockPos, BlockEyeCandy.FACING);

StoredMatrixTransformations blockStoredMatrixTransformations = new StoredMatrixTransformations(0.5 + (double)blockEntity.getPos2().getX(), (double)blockEntity.getPos2().getY(), 0.5 + (double)blockEntity.getPos2().getZ());
StoredMatrixTransformations blockStoredMatrixTransformations = new StoredMatrixTransformations(0.5 + (double)blockEntity.getPos2().getX(), blockEntity.getPos2().getY(), 0.5 + (double)blockEntity.getPos2().getZ());
StoredMatrixTransformations storedMatrixTransformations = blockStoredMatrixTransformations.copy();
storedMatrixTransformations.add((graphicsHolderNew) -> {
graphicsHolderNew.translate(blockEntity.getTranslateX(), blockEntity.getTranslateY(), (double)blockEntity.getTranslateZ());
graphicsHolderNew.rotateYDegrees(180.0F - facing.asRotation());
graphicsHolderNew.rotateXDegrees(blockEntity.getRotateX() + 180.0F);
graphicsHolderNew.rotateYDegrees(blockEntity.getRotateY());
graphicsHolderNew.rotateZDegrees(blockEntity.getRotateZ());
});

for(ModelDrawCall drawCall : new ArrayList<>(eyeCandyScriptInstance.drawCalls)) {
for(ScriptResultCall drawCall : new ArrayList<>(eyeCandyScriptInstance.drawCalls)) {
if(drawCall == null) continue;

StoredMatrixTransformations storedMatrixTransformations = blockStoredMatrixTransformations.copy();
if(drawCall.getTransformation() != null) {
storedMatrixTransformations.add(drawCall.getTransformation());
}

storedMatrixTransformations.add((graphicsHolderNew) -> {
graphicsHolderNew.translate(blockEntity.getTranslateX(), blockEntity.getTranslateY(), (double)blockEntity.getTranslateZ());
graphicsHolderNew.rotateYDegrees(180.0F - facing.asRotation());
graphicsHolderNew.rotateXDegrees(blockEntity.getRotateX() + 180.0F);
graphicsHolderNew.rotateYDegrees(blockEntity.getRotateY());
graphicsHolderNew.rotateZDegrees(blockEntity.getRotateZ());
});

drawCall.draw(storedMatrixTransformations, light);
drawCall.run(world, graphicsHolder, storedMatrixTransformations, facing, light);
}
for(ScriptResultCall soundCall : new ArrayList<>(eyeCandyScriptInstance.soundCalls)) {
if(soundCall == null) continue;
soundCall.run(world, graphicsHolder, storedMatrixTransformations, facing, light);
}
ci.cancel();
}
Expand Down

0 comments on commit 4f5bd56

Please sign in to comment.