Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/main/java/cam72cam/mod/entity/CustomEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import cam72cam.mod.entity.sync.EntitySync;
import cam72cam.mod.world.World;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.math.MathHelper;

import java.util.List;

Expand Down Expand Up @@ -89,4 +91,33 @@ public List<Entity> getPassengers() {
return internal.getActualPassengers();
}

@Override
public float getRotationRoll() {
return internal.getDataManager().get(ModdedEntity.ROLL);
}

@Override
public float getRotationRoll(float partialTicks) {
return (float) MathHelper.clampedLerp(getPrevRotationRoll(), getRotationRoll(), partialTicks);
}

@Override
public void setRotationRoll(float roll) {
EntityDataManager dataManager = internal.getDataManager();
float prevRoll = dataManager.get(ModdedEntity.PREV_ROLL);
while (roll - prevRoll < -180.0F)
{
prevRoll -= 360.0F;
}
while (roll - prevRoll >= 180.0F)
{
prevRoll += 360.0F;
}
dataManager.set(ModdedEntity.PREV_ROLL, prevRoll);
dataManager.set(ModdedEntity.ROLL, roll);
}

public float getPrevRotationRoll() {
return internal.getDataManager().get(ModdedEntity.PREV_ROLL);
}
}
70 changes: 56 additions & 14 deletions src/main/java/cam72cam/mod/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import cam72cam.mod.world.World;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.util.DamageSource;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.Explosion;

import java.util.List;
Expand All @@ -19,7 +19,7 @@

/**
* The base entity abstraction that wraps MC entities.
*
* <p>
* TODO: Make sure we are setting prevRot/Loc stuff correctly. Should it only be changed on a tick processing the movement?
*/
public class Entity {
Expand Down Expand Up @@ -76,29 +76,64 @@ public float getRotationYaw() {
return internal.rotationYaw;
}

public float getRotationPitch() {
return internal.rotationPitch;
}

/**
* @see CustomEntity#getRotationRoll()
*/
public float getRotationRoll() {
return 0f;
}

public float getRotationYaw(float partialTicks) {
return (float) MathHelper.clampedLerp(internal.prevRotationYaw, internal.rotationYaw, partialTicks);
}

public float getRotationPitch(float partialTicks) {
return (float) MathHelper.clampedLerp(internal.prevRotationPitch, internal.rotationPitch, partialTicks);
}

/**
* @see CustomEntity#getRotationRoll(float)
*/
public float getRotationRoll(float partialTicks) {
return 0;
}

public void setRotationYaw(float yaw) {
internal.prevRotationYaw = internal.rotationYaw;
internal.rotationYaw = yaw;
double d0 = internal.prevRotationYaw - yaw;
if (d0 < -180.0D)
{
internal.prevRotationYaw += 360.0F;
}

if (d0 >= 180.0D)
while (internal.rotationYaw - internal.prevRotationYaw < -180.0F)
{
internal.prevRotationYaw -= 360.0F;
}

}

public float getRotationPitch() {
return internal.rotationPitch;
while (internal.rotationYaw - internal.prevRotationYaw >= 180.0F)
{
internal.prevRotationYaw += 360.0F;
}
}

public void setRotationPitch(float pitch) {
internal.prevRotationPitch = internal.rotationPitch;
internal.rotationPitch = pitch;

while (internal.rotationPitch - internal.prevRotationPitch < -180.0F)
{
internal.prevRotationPitch -= 360.0F;
}
while (internal.rotationPitch - internal.prevRotationPitch >= 180.0F)
{
internal.prevRotationPitch += 360.0F;
}
}

/**
* @see CustomEntity#setRotationRoll(float)
*/
public void setRotationRoll(float roll) {
}

public float getPrevRotationYaw() {
Expand All @@ -109,6 +144,13 @@ public float getPrevRotationPitch() {
return internal.prevRotationPitch;
}

/**
* @see CustomEntity#getPrevRotationRoll()
*/
public float getPrevRotationRoll() {
return 0f;
}

Vec3d eyeCache;
public Vec3d getPositionEyes() {
if (eyeCache == null || (
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/cam72cam/mod/entity/ModdedEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
Expand All @@ -37,6 +40,15 @@ public class ModdedEntity extends Entity implements IEntityAdditionalSpawnData {
@TagField(value = "passengers", mapper = PassengerMapper.class)
private Map<UUID, Vec3d> passengerPositions = new HashMap<>();

//Data synchronization
static final DataParameter<Float> PREV_ROLL = EntityDataManager.createKey(ModdedEntity.class, DataSerializers.FLOAT);
static final DataParameter<Float> ROLL = EntityDataManager.createKey(ModdedEntity.class, DataSerializers.FLOAT);
//Data storage
@TagField
private float roll = 0;
@TagField
private float prevRoll = 0;

// All of the known seats attached to this entity
private final List<SeatEntity> seats = new ArrayList<>();

Expand Down Expand Up @@ -74,6 +86,8 @@ public ModdedEntity(World world) {

@Override
protected final void entityInit() {
this.dataManager.register(ROLL, 0f);
this.dataManager.register(PREV_ROLL, 0f);
}

/** Setup self if we have not done so already. This happens during entity data load. */
Expand Down Expand Up @@ -129,6 +143,11 @@ private void load(TagCompound data) {
ModCore.catching(e, "Error during entity load: %s - %s", this, data);
}

if (!this.world.isRemote) {
dataManager.set(ROLL, this.roll);
dataManager.set(PREV_ROLL, this.prevRoll);
}

TagCompound selfData = data.get("selfData");
if (selfData == null) {
// Old style used to save everything in one giant NBT blob. New versions save self in a sub tag.
Expand Down Expand Up @@ -162,6 +181,9 @@ protected final void writeEntityToNBT(NBTTagCompound compound) {
*/
private void save(TagCompound data) {
data.setString("custom_mob_type", type);
this.roll = dataManager.get(ROLL);
this.prevRoll = dataManager.get(PREV_ROLL);

try {
TagSerializer.serialize(data, this);
} catch (SerializationException e) {
Expand Down Expand Up @@ -225,6 +247,9 @@ public final void writeSpawnData(ByteBuf buffer) {
*/
@Override
public final void onUpdate() {
if (!world.isRemote) {
this.dataManager.set(PREV_ROLL, this.dataManager.get(ROLL));
}
iTickable.onTick();
try {
self.sync.send();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/cam72cam/mod/render/EntityRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public void doRender(ModdedEntity stock, double x, double y, double z, float ent
RenderState state = new RenderState();
state.translate(x, y, z);
state.rotate(180 - entityYaw, 0, 1, 0);
state.rotate(self.getRotationPitch(), 1, 0, 0);
state.rotate(self.getRotationPitch(partialTicks), 1, 0, 0);
state.rotate(self.getRotationRoll(partialTicks), 0, 0, 1);
state.rotate(-90, 0, 1, 0);
state.stage(RenderContext.Stage.ENTITY);
renderers.get(self.getClass()).render(self, state, partialTicks);
Expand All @@ -120,7 +121,8 @@ public void renderMultipass(ModdedEntity stock, double x, double y, double z, fl
RenderState state = new RenderState();
state.translate(x, y, z);
state.rotate(180 - entityYaw, 0, 1, 0);
state.rotate(self.getRotationPitch(), 1, 0, 0);
state.rotate(self.getRotationPitch(partialTicks), 1, 0, 0);
state.rotate(self.getRotationRoll(partialTicks), 0, 0, 1);
state.rotate(-90, 0, 1, 0);
state.stage(RenderContext.Stage.ENTITY);
renderers.get(self.getClass()).postRender(self, state, partialTicks);
Expand Down