Jump to content

Recommended Posts

Posted

I was wondering if it is possible to stop a specific sound from playing. I need that to create some realistic motor sound effect. Also im searching for a possibility to change playing speed. Is there a way to do that, cuz i didn't find one yet.

Posted

So, i created the class, but somehow it does not work.

Here is the class and where i call it:

 

 

package itsamysterious.mods.reallifemod.core.sounds;

 

import net.minecraft.client.Minecraft;

import net.minecraft.client.audio.ISound;

import net.minecraft.client.audio.MovingSound;

import net.minecraft.entity.Entity;

import net.minecraft.util.ResourceLocation;

 

public class CustomSound extends MovingSound {

 

 

public CustomSound(ResourceLocation location) {

super(location);

}

 

@Override

public void update() {

}

 

public void stop(){

donePlaying=true;

}

 

public void setPitch(float newPitch){

this.pitch=newPitch;

}

 

public void addPitch(float f){

this.pitch+=f;

}

 

}

 

 

 

 

I call it like this in the entityclass (onUpdate()):

 

 

Minecraft.getMinecraft().getSoundHandler().playSound(this.file.startsound);

 

 

 

Posted

Here it is, but it could be that you want the VehicleFile class, cuz that stores the data about it.

 

 

package itsamysterious.mods.reallifemod.core.vehicles;

 

import javax.vecmath.Vector2d;

 

import org.lwjgl.input.Keyboard;

import org.lwjgl.util.vector.Vector3f;

 

import itsamysterious.mods.reallifemod.core.sounds.SoundPlayer;

import net.minecraft.client.Minecraft;

import net.minecraft.entity.Entity;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.util.AxisAlignedBB;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.DamageSource;

import net.minecraft.world.World;

import net.minecraftforge.fml.relauncher.Side;

import net.minecraftforge.fml.relauncher.SideOnly;

 

public class EntityVehicle extends Entity {

private static final double g = 9.81;

private VehicleFile file;

public double backWheelRotation;

public double wheelRotL;

public double steeringangle;

private boolean isEmpty;

private boolean canDoStuff;

 

private EntitySeat[] seats;

private double vehicleX;

private double vehicleY;

private double vehicleZ;

private float vehicleYaw;

private float vehiclePitch;

private double velocityY;

private double velocityX;

private double velocityZ;

public double speed;

private SoundPlayer player;

//Constants

private final double rollresistancecoeff_tarmac=0.013;

private final double rollresistancecoeff_betong=0.008;

private final double rollresistancecoeff_gravel=0.008;

private final double rollresistancecoeff_cobble=0.015;

private final double rollresistancecoeff_dirt=0.050;

private final double rollresistancecoeff_sand=0.3;

 

 

private float P;//Power Lever

private float Xb; // Brake pedal deflection

private float Xn; // Brake pedal deflection

 

private double F;//Propulsive fort

private double V;//Propulsive fort

 

 

public EntityVehicle(World world) {

super(world);

this.setSize(1, 2);

this.canDoStuff = false;

this.isEmpty = true;

this.preventEntitySpawning = true;

this.player=new SoundPlayer();

}

 

public EntityVehicle(World world, VehicleFile file, double x, double y, double z) {

this(world);

this.setFile(file);

this.seats = new EntitySeat[file.numDrivers];

this.createSeats(world);

this.setPosition(x, y, z);

this.canDoStuff = true;

}

 

private void createSeats(World w) {

for (int i = 0; i < this.seats.length; i++) {

System.out.println(seats.length);

this.seats = new EntitySeat(w);

w.spawnEntityInWorld(seats);

}

this.canDoStuff = true;

}

 

@Override

@SideOnly(Side.CLIENT)

public void setVelocity(double p_70016_1_, double p_70016_3_, double p_70016_5_) {

this.velocityX = (float) (this.motionX = p_70016_1_);

this.velocityY = (float) (this.motionY = p_70016_3_);

this.velocityZ = (float) (this.motionZ = p_70016_5_);

}

 

@Override

public boolean attackEntityFrom(DamageSource p_70097_1_, float p_70097_2_) {

return false;

}

 

@Override

public void entityInit() {

this.dataWatcher.addObject(17, new Integer(0));

this.dataWatcher.addObject(18, new Integer(1));

this.dataWatcher.addObject(19, new Float(0.0F));

}

 

@Override

public void onUpdate() {

super.onUpdate();

this.prevPosX = this.posX;

this.prevPosY = this.posY;

this.prevPosZ = this.posZ;

 

if (this.canDoStuff) {

for (int i = 0; i < seats.length; i++) {

Vector3f f = this.file.ridersPositions.get(i);

this.seats.setPosition(this.posX + f.x, this.posY + f.y + 0.5, this.posZ + f.z);

}

}

 

// ------------------------------------------------------------------------------------------

 

if (Keyboard.isKeyDown(Keyboard.KEY_RETURN)) {

if (this.worldObj.getClosestPlayer(posX, posY, posZ, 5.0F) != null) {

EntityPlayer entity = this.worldObj.getClosestPlayer(posX, posY, posZ, 5.0F);

this.interactFirst(entity);

}

}

//file.startsound.update();

//file.stopsound.update();

//file.throttlesound.update();

 

// ------------------------------------------------------------------------------------------

wheelRotL+=speed;

backWheelRotation+=speed;

this.move();

 

}

 

public void move() {

if(this.riddenByEntity!=null){

if (Keyboard.isKeyDown(Keyboard.KEY_A)) {

if(steeringangle < 30){

steeringangle += 5;

}

} else if (Keyboard.isKeyDown(Keyboard.KEY_D) ) {

if(steeringangle > -30){

steeringangle -= 5;

}

} else {

if (steeringangle > 0) {

steeringangle -= 2.5;

if (steeringangle > 0) {

steeringangle -= 2.5;

}

}

if (steeringangle < 0) {

steeringangle += 2.5;

if (steeringangle < 0) {

steeringangle += 2.5;

}

}

}

boolean pressedW;

if(Keyboard.isKeyDown(Keyboard.KEY_W)){

// Minecraft.getMinecraft().getSoundHandler().playSound(this.file.startsound);

riddenByEntity.playSound("reallifemod:lambo_throttles", 1.0f, 1.0f);

if(speed<file.maxSpeed){

speed+=file.acceleration*20;

 

}

if(P<30){

P+=2.5;

}

pressedW=true;

}else

{

 

if(P>5){

P-=0.5;

}else

P=0;

 

}

 

if(ticksExisted%2==0){

riddenByEntity.playSound("reallifemod:lambo_runs", 1.0f, 1.0f+(float)P*0.1f);

}

 

 

 

if(Keyboard.isKeyDown(Keyboard.KEY_S)){

if(speed>-file.maxReverseSpeed){

speed-=file.acceleration*1000/60/60*20;

}

}

 

if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){

if(speed<-5){

speed+=5;

}else

if(speed<0){

speed=0;

}

 

if(speed>5){

speed-=5;

}else{

speed=0;

}

}

 

double k=0;

double R=0;

if(steeringangle!=0){

R=file.dimensions.z/steeringangle;

k=1/R;

}

double m = file.mass;

//velocity

V=speed*1000/m;

double u= motionX;

double v= motionZ;//

double r=((speed*1000)/60/60/80)*k;

V=Math.pow(((u*u)+(v*v)),0.5) ;

double ax=u-r*v;//x Acceleration(longithudial and lateral)

double ay=v+r*u;//z

 

//Forces

double Pw;

Vector2d F; //Frictionforce on all wheels

double Fa;//aerodynamic drag

double W = m*g;

//Gieren

this.rotationYaw-=r;

this.motionX=-(speed*1000/60/60/80)*Math.sin(Math.toRadians(rotationYaw));

this.motionZ=(speed*1000/60/60/80)*Math.cos(Math.toRadians(rotationYaw));

moveEntity(motionX, motionY, motionZ);

speed*=0.988888881;

 

}

 

}

@Override

public void readEntityFromNBT(NBTTagCompound tagCompund) {

 

}

 

@Override

public void writeEntityToNBT(NBTTagCompound tagCompound) {

 

}

 

@Override

public boolean interactFirst(EntityPlayer player) {

if (this.riddenByEntity == null) {

player.mountEntity(this);

}

return true;

}

 

@Override

public void updateRiderPosition() {

if (riddenByEntity != null) {

Vector3f pos=file.ridersPositions.get(0);

float signed_angle =  (float) (Math.atan2(pos.x,pos.y) - Math.atan2(0,0));

double rad = Math.toRadians(-rotationYaw-signed_angle);

double newX = (float)this.posX-Math.sin(rad)*this.file.ridersPositions.get(0).x;

//double newY = (float)this.posY-Math.cos(Math.cos(rotationPitch))*this.file.ridersPositions.get(0).y;

double newZ = (float)this.posZ+Math.cos(rad)*this.file.ridersPositions.get(0).z;

this.riddenByEntity.setPosition(newX, posY, newZ);

}

};

 

public VehicleFile getFile() {

return this.file;

}

 

public void setFile(VehicleFile file) {

this.file = file;

}

 

public void setDamageTaken(float p_70266_1_) {

this.dataWatcher.updateObject(19, Float.valueOf(p_70266_1_));

}

 

public float getDamageTaken() {

return this.dataWatcher.getWatchableObjectFloat(19);

}

 

public void setTimeSinceHit(int p_70265_1_) {

this.dataWatcher.updateObject(17, Integer.valueOf(p_70265_1_));

}

 

public int getTimeSinceHit() {

return this.dataWatcher.getWatchableObjectInt(17);

}

 

public void setForwardDirection(int p_70269_1_) {

this.dataWatcher.updateObject(18, Integer.valueOf(p_70269_1_));

}

 

public int getForwardDirection() {

return this.dataWatcher.getWatchableObjectInt(18);

}

 

@Override

protected boolean canTriggerWalking() {

return false;

}

 

@Override

public AxisAlignedBB getCollisionBox(Entity entityIn) {

return entityIn.getEntityBoundingBox();

}

 

@Override

public AxisAlignedBB getBoundingBox() {

return this.getEntityBoundingBox();

}

 

public boolean canRiderInteract() {

return true;

}

 

public boolean shouldDismountInWater(Entity rider) {

return true;

}

 

}

 

 

 

Vehiclefile:

 

 

package itsamysterious.mods.reallifemod.core.vehicles;

 

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

 

import org.lwjgl.util.vector.Vector3f;

 

import itsamysterious.mods.reallifemod.client.forgeobjmodelported.AdvancedModelLoader;

import itsamysterious.mods.reallifemod.client.forgeobjmodelported.IModelCustom;

import itsamysterious.mods.reallifemod.core.sounds.CustomSound;

import itsamysterious.mods.reallifemod.init.Reference;

import net.minecraft.client.Minecraft;

import net.minecraft.client.audio.SoundManager;

import net.minecraft.client.audio.SoundRegistry;

import net.minecraft.util.ResourceLocation;

 

public class VehicleFile {

 

public double maxSpeed;

public double maxReverseSpeed;

 

public float mass;

public float acceleration;

public Vector3f dimensions;

 

public Vector3f wheelPosBack;

public Vector3f wheelPosRight;

public Vector3f wheelPosLeft;

public Vector3f steeringWheelPos;

 

public List<Vector3f> ridersPositions=new ArrayList<Vector3f>();

 

public String iconFile;

public String vehicleName;

public int numDrivers;

 

public String fileName;

public String modelName;

public String wheelsName;

public String steeringWheelName;

public String transparentpartsname;

public String textureName;

public ResourceLocation texture;

public CustomSound startsound;

public CustomSound stopsound;

public CustomSound throttlesound;

 

public IModelCustom model;

 

public VehicleFile(File f) {

this.loadFromFile(f);

}

 

private void loadFromFile(File f) {

try {

BufferedReader reader = new BufferedReader(new FileReader(f));

String line;

while ((line = reader.readLine()) != null) {

 

if (line.startsWith("maxSpeed:")) {

this.maxSpeed = Double.parseDouble(line.split(" ")[1]);

}

 

if (line.startsWith("maxReverseSpeed:")) {

this.maxReverseSpeed = Double.parseDouble(line.split(" ")[1]);

 

}

 

if (line.startsWith("wheelPosBack: ")) {

this.wheelPosBack = new Vector3f();

this.wheelPosBack.x = Float.parseFloat(line.split(" ")[1].split(",")[0]);

this.wheelPosBack.y = Float.parseFloat(line.split(",")[1]);

this.wheelPosBack.z = Float.parseFloat(line.split(",")[2]);

 

}

 

if (line.startsWith("wheelPosRight:")) {

this.wheelPosRight = new Vector3f();

this.wheelPosRight.x = Float.parseFloat(line.split(" ")[1].split(",")[0]);

this.wheelPosRight.y = Float.parseFloat(line.split(",")[1]);

this.wheelPosRight.z = Float.parseFloat(line.split(",")[2]);

 

}

 

if (line.startsWith("wheelPosLeft:")) {

this.wheelPosLeft = new Vector3f();

this.wheelPosLeft.x = Float.parseFloat(line.split(" ")[1].split(",")[0]);

this.wheelPosLeft.y = Float.parseFloat(line.split(",")[1]);

this.wheelPosLeft.z = Float.parseFloat(line.split(",")[2]);

 

}

 

if (line.startsWith("steeringWheelPos:")) {

this.steeringWheelPos = new Vector3f();

this.steeringWheelPos.x = Float.parseFloat(line.split(" ")[1].split(",")[0]);

this.steeringWheelPos.y = Float.parseFloat(line.split(",")[1]);

this.steeringWheelPos.z = Float.parseFloat(line.split(",")[2]);

 

}

 

if(line.startsWith("numDrivers: ")){

this.numDrivers = Integer.parseInt(line.split(" ")[1]);

}

 

if(line.startsWith("seatPos_")&&this.ridersPositions!=null){

 

float f1=Float.parseFloat(line.split(" ")[1].split(",")[0]);

float f2=Float.parseFloat(line.split(",")[1]);

float f3=Float.parseFloat(line.split(",")[2]);

int i=Integer.parseInt(line.split("_")[1].split(":")[0]);

ridersPositions.add(i,new Vector3f(f1, f2, f3));

}

 

if (line.startsWith("iconFile:")) {

this.iconFile = line.split(" ")[1];

}

 

if (line.startsWith("acceleration:")) {

this.acceleration = Float.parseFloat(line.split(" ")[1]);

}

 

if (line.startsWith("mass:")) {

this.mass = Float.parseFloat(line.split(" ")[1]);

}

 

if (line.startsWith("dimensions:")) {

float f1=Float.parseFloat(line.split(" ")[1].split(",")[0]);

float f2=Float.parseFloat(line.split(",")[1].split(",")[0]);

float f3=Float.parseFloat(line.split(",")[1].split(",")[0]);

this.dimensions=new Vector3f(f1, f2, f3);

}

 

if (line.startsWith("vehicleName:")) {

this.vehicleName = line.split(" ")[1];

}

 

if (line.startsWith("modelName:")) {

this.modelName = line.split(" ")[1];

}

 

if (line.startsWith("wheelsName:")) {

this.wheelsName = line.split(" ")[1];

}

 

if (line.startsWith("steeringWheelName:")) {

this.steeringWheelName = line.split(" ")[1];

}

 

if (line.startsWith("transparentName:")) {

this.transparentpartsname = line.split(" ")[1];

}

 

if (line.startsWith("textureName:")) {

this.texture = new ResourceLocation("reallifemod:textures/vehicle/"+line.split(" ")[1]+".png");

this.textureName = line.split(" ")[1];

}

 

if (line.startsWith("fileName:")) {

this.model=AdvancedModelLoader.loadModel(new ResourceLocation("reallifemod:models/vehicle/"+line.split(" ")[1]+".obj"));

this.fileName = line.split(" ")[1];

}

 

 

 

if (line.startsWith("sound_start:")) {

this.startsound = new CustomSound(new ResourceLocation(Reference.ID+":"+line.split(" ")[1]));

}

 

if (line.startsWith("sound_stop:")) {

this.stopsound = new CustomSound(new ResourceLocation(Reference.ID+":"+line.split(" ")[1]));

}

 

if (line.startsWith("sound_throttle:")) {

this.startsound = new CustomSound(new ResourceLocation(Reference.ID+":"+line.split(" ")[1]));

}

}

reader.close();

} catch (IOException e) {

}

 

}

 

}

 

 

Posted

Its experimental and i know that i have to create keybindings for it, just testing. But i have problems with entityclasses since i started coding cuz i never found a propper explanation. Can you tell me what will crash on servers?

Posted

Okay... how would the easy way look like? Two packets, one for server to client and one for the other way. But what i don't understand is why do i have to set positions on client then send a packet to server to update them on server and then send them back to client. Isn't the onUpdate method called on both sides? Cuz when im driving ingame with the car it works perfectly fine :/ Is it really nessecary to do all this packetHandling shit?

Posted

Why don't you ask the ForgeForum developer to add syntax highlighting to spoilers? This would make spoilermaking become more attractive cuz highlighted code looks cooler than the gray background and also it would help you all to read code better.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.