Advertisement
Benjamin_Loison

EntityVehicle and initialization

Oct 27th, 2016
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.11 KB | None | 0 0
  1. // In post-init of my main class
  2.  
  3. EntityRegistry.registerGlobalEntityID(EntityVehicle.class, "Vehicle", EntityRegistry.findGlobalUniqueEntityId());
  4. EntityRegistry.registerModEntity(EntityVehicle.class, "Vehicle", 95, this, 250, 10, false);
  5.  
  6. // Class of EntityVehicle (Server Side)
  7.  
  8. package fr.altiscraft.benjaminloison.common.driveables;
  9.  
  10. import com.flansmod.common.vector.Vector3f;
  11.  
  12. import fr.altiscraft.benjaminloison.api.FileAPI;
  13. import fr.altiscraft.benjaminloison.api.IExplodeable;
  14. import fr.altiscraft.benjaminloison.common.AltisCraft;
  15. import fr.altiscraft.benjaminloison.common.guns.ItemTool;
  16. import fr.altiscraft.benjaminloison.packets.PacketMessage;
  17. import fr.altiscraft.benjaminloison.packets.PacketPlaySound;
  18. import fr.altiscraft.benjaminloison.packets.PacketVehicleControl;
  19. import io.netty.buffer.ByteBuf;
  20. import net.minecraft.client.resources.I18n;
  21. import net.minecraft.entity.player.EntityPlayer;
  22. import net.minecraft.entity.player.EntityPlayerMP;
  23. import net.minecraft.item.ItemStack;
  24. import net.minecraft.nbt.NBTTagCompound;
  25. import net.minecraft.util.ChatComponentText;
  26. import net.minecraft.util.DamageSource;
  27. import net.minecraft.util.EnumChatFormatting;
  28. import net.minecraft.util.Vec3;
  29. import net.minecraft.world.World;
  30.  
  31. public class EntityVehicle extends EntityDriveable implements IExplodeable
  32. {
  33.     public int maxFuel, shellDelay, gunDelay, soundPosition, toggleTimer;
  34.     public float wheelsYaw, maxSpeed, wheelsAngle;
  35.     private int ticksSinceUsed;
  36.     public EntityDriveable driveable;
  37.     public boolean good;
  38.  
  39.     public EntityVehicle(World world)
  40.     {
  41.         super(world);
  42.         stepHeight = 1;
  43.     }
  44.  
  45.     public EntityVehicle(World world, double x, double y, double z, VehicleType type, DriveableData data)
  46.     {
  47.         super(world, type, data);
  48.         stepHeight = 1;
  49.         setPosition(x, y, z);
  50.         initType(type, false);
  51.     }
  52.  
  53.     public EntityVehicle(World world, double x, double y, double z, int placer, VehicleType type, DriveableData data, String player)
  54.     {
  55.         super(world, type, data);
  56.         stepHeight = 1;
  57.         setPosition(x, y, z);
  58.         rotateYaw(placer);
  59.         initType(type, false);
  60.         owners[0] = player;
  61.         data.fuelInTank = type.fuelTankSize;
  62.     }
  63.  
  64.     protected void initType(DriveableType type, boolean clientSide)
  65.     {
  66.         super.initType(type, clientSide);
  67.         maxFuel = type.fuelTankSize;
  68.         maxSpeed = type.maxThrottle;
  69.     }
  70.  
  71.     @Override
  72.     public void readSpawnData(ByteBuf data)
  73.     {
  74.         super.readSpawnData(data);
  75.     }
  76.  
  77.     @Override
  78.     protected void writeEntityToNBT(NBTTagCompound tag)
  79.     {
  80.         super.writeEntityToNBT(tag);
  81.         tag.setBoolean("VarDoor", varDoor);
  82.         tag.setBoolean("Rideable", rideable);
  83.         for(int i = 0; i < owners.length; i++)
  84.             tag.setString("Owners" + i, owners[i]);
  85.     }
  86.  
  87.     @Override
  88.     protected void readEntityFromNBT(NBTTagCompound tag)
  89.     {
  90.         super.readEntityFromNBT(tag);
  91.         varDoor = tag.getBoolean("VarDoor");
  92.         rideable = tag.getBoolean("Rideable");
  93.         for(int i = 0; i < owners.length; i++)
  94.             owners[i] = tag.getString("Owners" + i);
  95.     }
  96.  
  97.     @Override
  98.     public void onMouseMoved(int deltaX, int deltaY)
  99.     {}
  100.  
  101.     @Override
  102.     public void setPositionRotationAndMotion(double x, double y, double z, float yaw, float pitch, float roll, double motX, double motY, double motZ, float velYaw, float velPitch, float velRoll, float throt, float steeringYaw)
  103.     {
  104.         super.setPositionRotationAndMotion(x, y, z, yaw, pitch, roll, motX, motY, motZ, velYaw, velPitch, velRoll, throt, steeringYaw);
  105.         wheelsYaw = steeringYaw;
  106.     }
  107.  
  108.     @Override
  109.     public boolean interactFirst(EntityPlayer entityplayer)
  110.     {
  111.         if(isDead)
  112.             return false;
  113.         ItemStack currentItem = entityplayer.getCurrentEquippedItem();
  114.         if(currentItem != null && currentItem.getItem() instanceof ItemTool && ((ItemTool)currentItem.getItem()).type.healDriveables)
  115.             return true;
  116.         VehicleType type = getVehicleType();
  117.         for(int i = 0; i <= type.numPassengers; i++)
  118.             if(seats[i] != null)
  119.                 if(seats[i].interactFirst(entityplayer))
  120.                 {
  121.                     if(i == 0)
  122.                         shellDelay = type.vehicleShellDelay;
  123.                     return true;
  124.                 }
  125.         return false;
  126.     }
  127.  
  128.     @Override
  129.     public boolean pressKey(int key, EntityPlayer player)
  130.     {
  131.         VehicleType type = getVehicleType();
  132.         switch(key)
  133.         {
  134.             case 0:
  135.             {
  136.                 throttle += 0.01;
  137.                 if(throttle > 1)
  138.                     throttle = 1;
  139.                 return true;
  140.             }
  141.             case 1:
  142.             {
  143.                 throttle -= 0.01;
  144.                 if(throttle < -1)
  145.                     throttle = -1;
  146.                 if(throttle < 0 && type.maxNegativeThrottle == 0)
  147.                     throttle = 0;
  148.                 return true;
  149.             }
  150.             case 2:
  151.             {
  152.                 wheelsYaw -= 1;
  153.                 return true;
  154.             }
  155.             case 3:
  156.             {
  157.                 wheelsYaw += 1;
  158.                 return true;
  159.             }
  160.             case 4:
  161.                 return true;
  162.             case 5:
  163.                 return true;
  164.             case 6:
  165.                 if(toggleTimer <= 0)
  166.                 {
  167.                     if(varDoor)
  168.                     {
  169.                         seats[0].riddenByEntity.mountEntity(null);
  170.                         return true;
  171.                     }
  172.                     toggleTimer = 10;
  173.                 }
  174.             case 7:
  175.                 return true;
  176.             case 9:
  177.                 return super.pressKey(key, player);
  178.             case 10:
  179.                 return true;
  180.             case 11:
  181.                 return true;
  182.             case 12:
  183.                 return true;
  184.             case 13:
  185.                 return true;
  186.             case 14:
  187.             {
  188.                 if(toggleTimer <= 0)
  189.                 {
  190.                     for(int i = 0; i < seats[0].driveable.owners.length; i++)
  191.                         if(!good)
  192.                             if(player.getDisplayName().equals(seats[0].driveable.owners[i]))
  193.                             {
  194.                                 if(seats[0].driveable.varDoor)
  195.                                 {
  196.                                     seats[0].driveable.rideable = false;
  197.                                     seats[0].driveable.varDoor = false;
  198.                                     AltisCraft.instance.network.sendTo(new PacketMessage("vehicle.locked"), (EntityPlayerMP)player);
  199.                                     good = true;
  200.                                 }
  201.                                 else if(!seats[0].driveable.varDoor)
  202.                                 {
  203.                                     seats[0].driveable.rideable = true;
  204.                                     seats[0].driveable.varDoor = true;
  205.                                     AltisCraft.instance.network.sendTo(new PacketMessage("vehicle.unlocked"), (EntityPlayerMP)player);
  206.                                     good = true;
  207.                                 }
  208.                             }
  209.                     toggleTimer = 10;
  210.                     AltisCraft.getPacketHandler().sendToServer(new PacketVehicleControl(this));
  211.                     good = false;
  212.                 }
  213.                 return true;
  214.             }
  215.             case 15:
  216.                 return true;
  217.             case 16:
  218.                 return true;
  219.             case 17:
  220.                 break;
  221.         }
  222.         return false;
  223.     }
  224.  
  225.     @Override
  226.     public Vector3f getLookVector(DriveablePosition dp)
  227.     {
  228.         return rotate(seats[0].looking.getXAxis());
  229.     }
  230.  
  231.     @Override
  232.     public void onUpdate()
  233.     {
  234.         super.onUpdate();
  235.         varDoor = rideable;
  236.         VehicleType type = this.getVehicleType();
  237.         DriveableData data = getDriveableData();
  238.         if(type == null)
  239.         {
  240.             AltisCraft.print("Vehicle type null. Not ticking vehicle");
  241.             return;
  242.         }
  243.         ticksSinceUsed++;
  244.         if(seats[0].riddenByEntity != null)
  245.             ticksSinceUsed = 0;
  246.         if(FileAPI.configNumber("life.vehicle") > 0 && ticksSinceUsed > FileAPI.configNumber("life.vehicle") * 20)
  247.             setDead();
  248.         if(shellDelay > 0)
  249.             shellDelay--;
  250.         if(gunDelay > 0)
  251.             gunDelay--;
  252.         if(toggleTimer > 0)
  253.             toggleTimer--;
  254.         if(soundPosition > 0)
  255.             soundPosition--;
  256.         if(hasEnoughFuel())
  257.             wheelsAngle += throttle / 7;
  258.         wheelsYaw *= 0.9;
  259.         if(wheelsYaw > 20)
  260.             wheelsYaw = 20;
  261.         if(wheelsYaw < -20)
  262.             wheelsYaw = -20;
  263.         Vector3f amountToMoveCar = new Vector3f();
  264.         for(EntityWheel wheel : wheels)
  265.         {
  266.             if(wheel == null)
  267.                 continue;
  268.             onGround = true;
  269.             wheel.onGround = true;
  270.             wheel.rotationYaw = axes.getYaw();
  271.             if(!type.tank && (wheel.ID == 2 || wheel.ID == 3))
  272.                 wheel.rotationYaw += wheelsYaw;
  273.             wheel.motionX *= 0.9;
  274.             wheel.motionY *= 0.9;
  275.             wheel.motionZ *= 0.9;
  276.             wheel.motionY -= 0.98 / 20;
  277.             if(!FileAPI.configBoolean("vehicles.need.fuel") || (seats != null && seats[0] != null && seats[0].riddenByEntity instanceof EntityPlayer && ((EntityPlayer)seats[0].riddenByEntity).capabilities.isCreativeMode) || data.fuelInTank > data.engine.fuelConsumption * throttle)
  278.             {
  279.                 if(getVehicleType().tank)
  280.                 {
  281.                     boolean left = wheel.ID == 0 || wheel.ID == 3;
  282.                     float turningDrag = 0.02F;
  283.                     wheel.motionX *= 1 - (Math.abs(wheelsYaw) * turningDrag);
  284.                     wheel.motionZ *= 1 - (Math.abs(wheelsYaw) * turningDrag);
  285.                     float velocityScale = 0.04F * (throttle > 0 ? type.maxThrottle : type.maxNegativeThrottle) * data.engine.engineSpeed, steeringScale = 0.1F * (wheelsYaw > 0 ? type.turnLeftModifier : type.turnRightModifier), effectiveWheelSpeed = (throttle + (wheelsYaw * (left ? 1 : -1) * steeringScale)) * velocityScale;
  286.                     wheel.motionX += effectiveWheelSpeed * Math.cos(wheel.rotationYaw * (float)Math.PI / 180);
  287.                     wheel.motionZ += effectiveWheelSpeed * Math.sin(wheel.rotationYaw * (float)Math.PI / 180);
  288.                 }
  289.                 else
  290.                 {
  291.                     {
  292.                         float velocityScale = 0.1F * throttle * (throttle > 0 ? type.maxThrottle : type.maxNegativeThrottle) * data.engine.engineSpeed;
  293.                         wheel.motionX += Math.cos(wheel.rotationYaw * (float)Math.PI / 180) * velocityScale;
  294.                         wheel.motionZ += Math.sin(wheel.rotationYaw * (float)Math.PI / 180) * velocityScale;
  295.                     }
  296.                     if(wheel.ID == 2 || wheel.ID == 3)
  297.                     {
  298.                         float velocityScale = 0.01F * (wheelsYaw > 0 ? type.turnLeftModifier : type.turnRightModifier) * (throttle > 0 ? 1 : -1);
  299.                         wheel.motionX -= wheel.getSpeedXZ() * Math.sin(wheel.rotationYaw * (float)Math.PI / 180) * velocityScale * wheelsYaw;
  300.                         wheel.motionZ += wheel.getSpeedXZ() * Math.cos(wheel.rotationYaw * (float)Math.PI / 180) * velocityScale * wheelsYaw;
  301.                     }
  302.                     else
  303.                     {
  304.                         wheel.motionX *= 0.9;
  305.                         wheel.motionZ *= 0.9;
  306.                     }
  307.                 }
  308.             }
  309.             if(type.floatOnWater && worldObj.isAnyLiquid(wheel.boundingBox))
  310.                 wheel.motionY += type.buoyancy;
  311.             wheel.moveEntity(wheel.motionX, wheel.motionY, wheel.motionZ);
  312.             Vector3f targetWheelPos = axes.findLocalVectorGlobally(getVehicleType().wheelPositions[wheel.ID].position), currentWheelPos = new Vector3f(wheel.posX - posX, wheel.posY - posY, wheel.posZ - posZ), dPos = ((Vector3f)Vector3f.sub(targetWheelPos, currentWheelPos, null).scale(getVehicleType().wheelSpringStrength));
  313.             if(dPos.length() > 0.001)
  314.             {
  315.                 wheel.moveEntity(dPos.x, dPos.y, dPos.z);
  316.                 dPos.scale(0.5F);
  317.                 Vector3f.sub(amountToMoveCar, dPos, amountToMoveCar);
  318.             }
  319.         }
  320.         moveEntity(amountToMoveCar.x, amountToMoveCar.y, amountToMoveCar.z);
  321.         if(wheels[0] != null && wheels[1] != null && wheels[2] != null && wheels[3] != null)
  322.         {
  323.             Vector3f frontAxleCentre = new Vector3f((wheels[2].posX + wheels[3].posX) / 2, (wheels[2].posY + wheels[3].posY) / 2, (wheels[2].posZ + wheels[3].posZ) / 2), backAxleCentre = new Vector3f((wheels[0].posX + wheels[1].posX) / 2, (wheels[0].posY + wheels[1].posY) / 2, (wheels[0].posZ + wheels[1].posZ) / 2);
  324.             float dx = frontAxleCentre.x - backAxleCentre.x, dy = frontAxleCentre.y - backAxleCentre.y, dz = frontAxleCentre.z - backAxleCentre.z, dxz = (float)Math.sqrt(dx * dx + dz * dz), yaw = (float)Math.atan2(dz, dx), pitch = -(float)Math.atan2(dy, dxz), roll = 0;
  325.             if(type.tank)
  326.                 yaw = (float)Math.atan2(wheels[3].posZ - wheels[2].posZ, wheels[3].posX - wheels[2].posX) + (float)Math.PI / 2;
  327.             axes.setAngles(yaw * 180 / (float)Math.PI, pitch * 180 / (float)Math.PI, roll * 180 / (float)Math.PI);
  328.         }
  329.         checkForCollisions();
  330.         if(throttle > 0.01 && throttle < 0.2 && soundPosition == 0 && hasEnoughFuel())
  331.         {
  332.             PacketPlaySound.sendSoundPacket(posX, posY, posZ, 50, dimension, type.startSound, false);
  333.             soundPosition = type.startSoundLength;
  334.         }
  335.         if(throttle > 0.2 && soundPosition == 0 && hasEnoughFuel())
  336.         {
  337.             PacketPlaySound.sendSoundPacket(posX, posY, posZ, 50, dimension, type.engineSound, false);
  338.             soundPosition = type.engineSoundLength;
  339.         }
  340.         for(EntitySeat seat : seats)
  341.             if(seat != null)
  342.                 seat.updatePosition();
  343.         if(ticksExisted % 5 == 0)
  344.             AltisCraft.getPacketHandler().sendToAllAround(new PacketVehicleControl(this), posX, posY, posZ, AltisCraft.driveableUpdateRange, dimension);
  345.     }
  346.  
  347.     private float averageAngles(float a, float b)
  348.     {
  349.         AltisCraft.print("Pre  " + a + " " + b);
  350.         float pi = (float)Math.PI;
  351.         for(; a > b + pi; a -= 2 * pi);
  352.         for(; a < b - pi; a += 2 * pi);
  353.         float avg = (a + b) / 2;
  354.         for(; avg > pi; avg -= 2 * pi);
  355.         for(; avg < -pi; avg += 2 * pi);
  356.         AltisCraft.print("Post " + a + " " + b + " " + avg);
  357.         return avg;
  358.     }
  359.  
  360.     private Vec3 subtract(Vec3 a, Vec3 b)
  361.     {
  362.         return Vec3.createVectorHelper(a.xCoord - b.xCoord, a.yCoord - b.yCoord, a.zCoord - b.zCoord);
  363.     }
  364.  
  365.     private Vec3 crossProduct(Vec3 a, Vec3 b)
  366.     {
  367.         return Vec3.createVectorHelper(a.yCoord * b.zCoord - a.zCoord * b.yCoord, a.zCoord * b.xCoord - a.xCoord * b.zCoord, a.xCoord * b.yCoord - a.yCoord * b.xCoord);
  368.     }
  369.  
  370.     @Override
  371.     public boolean landVehicle()
  372.     {
  373.         return true;
  374.     }
  375.  
  376.     @Override
  377.     public boolean attackEntityFrom(DamageSource damagesource, float i)
  378.     {
  379.         return true;
  380.     }
  381.  
  382.     public VehicleType getVehicleType()
  383.     {
  384.         return VehicleType.getVehicle(driveableType);
  385.     }
  386.  
  387.     @Override
  388.     public float getPlayerRoll()
  389.     {
  390.         return axes.getRoll();
  391.     }
  392.  
  393.     @Override
  394.     protected void dropItemsOnPartDeath(Vector3f midpoint, DriveablePart part)
  395.     {}
  396.  
  397.     @Override
  398.     public String getBombInventoryName()
  399.     {
  400.         return "Mines";
  401.     }
  402.  
  403.     @Override
  404.     public String getMissileInventoryName()
  405.     {
  406.         return "Shells";
  407.     }
  408.  
  409.     @Override
  410.     public boolean hasMouseControlMode()
  411.     {
  412.         return false;
  413.     }
  414.  
  415.     @Override
  416.     public void setDead()
  417.     {
  418.         super.setDead();
  419.         for(EntityWheel wheel : wheels)
  420.             if(wheel != null)
  421.                 wheel.setDead();
  422.     }
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement