diff --git a/src/main/java/com/minelittlepony/unicopia/client/render/WorldRenderDelegate.java b/src/main/java/com/minelittlepony/unicopia/client/render/WorldRenderDelegate.java index b4e52272..b2f70166 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/render/WorldRenderDelegate.java +++ b/src/main/java/com/minelittlepony/unicopia/client/render/WorldRenderDelegate.java @@ -4,6 +4,8 @@ import javax.annotation.Nullable; import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.ability.magic.spell.DisguiseSpell; +import com.minelittlepony.unicopia.entity.Equine; +import com.minelittlepony.unicopia.entity.ItemImpl; import com.minelittlepony.unicopia.entity.Living; import com.minelittlepony.unicopia.entity.behaviour.Disguise; import com.minelittlepony.unicopia.entity.player.Pony; @@ -32,7 +34,28 @@ import net.minecraft.util.math.Vec3d; public class WorldRenderDelegate { public static final WorldRenderDelegate INSTANCE = new WorldRenderDelegate(); - public boolean onEntityRender(EntityRenderDispatcher dispatcher, Living pony, + public boolean onEntityRender(EntityRenderDispatcher dispatcher, Equine pony, + double x, double y, double z, float yaw, + float tickDelta, MatrixStack matrices, VertexConsumerProvider vertices, int light) { + + if (pony instanceof ItemImpl) { + matrices.push(); + + if (pony.getPhysics().isGravityNegative()) { + matrices.translate(0, -((ItemImpl) pony).getMaster().getHeight() * 1.1, 0); + } + + return false; + } + + if (pony instanceof Living) { + return onLivingRender(dispatcher, (Living)pony, x, y, z, yaw, tickDelta, matrices, vertices, light); + } + + return false; + } + + private boolean onLivingRender(EntityRenderDispatcher dispatcher, Living pony, double x, double y, double z, float yaw, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light) { @@ -84,12 +107,14 @@ public class WorldRenderDelegate { return false; } - public void afterEntityRender(Living pony, MatrixStack matrices) { + public void afterEntityRender(Equine pony, MatrixStack matrices) { - matrices.pop(); + if (pony instanceof ItemImpl || pony instanceof Living) { + matrices.pop(); - if (pony.getPhysics().isGravityNegative()) { - flipAngles(pony.getMaster()); + if (pony instanceof Living && pony.getPhysics().isGravityNegative()) { + flipAngles(((Living)pony).getMaster()); + } } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Creature.java b/src/main/java/com/minelittlepony/unicopia/entity/Creature.java index a4df31ec..d4dcfc53 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Creature.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Creature.java @@ -25,12 +25,13 @@ import net.minecraft.nbt.CompoundTag; public class Creature extends Living { private static final TrackedData EFFECT = DataTracker.registerData(LivingEntity.class, TrackedDataHandlerRegistry.TAG_COMPOUND); + public static final TrackedData GRAVITY = DataTracker.registerData(LivingEntity.class, TrackedDataHandlerRegistry.FLOAT); private static final LevelStore LEVELS = Levelled.fixed(0); public static void boostrap() {} - private final Physics physics = new EntityPhysics<>(this); + private final Physics physics = new EntityPhysics<>(this, GRAVITY); public Creature(LivingEntity entity) { super(entity, EFFECT); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java b/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java index bbc29424..940b1ea8 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java @@ -10,6 +10,7 @@ import net.minecraft.block.BlockState; import net.minecraft.block.FenceGateBlock; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.data.TrackedData; import net.minecraft.nbt.CompoundTag; import net.minecraft.particle.BlockStateParticleEffect; import net.minecraft.particle.ParticleTypes; @@ -20,12 +21,21 @@ import net.minecraft.util.math.Vec3d; public class EntityPhysics> implements Physics, Copieable> { - private float gravity = 1; + private final TrackedData gravity; protected final T pony; - public EntityPhysics(T pony) { + public EntityPhysics(T pony, TrackedData gravity) { + this(pony, gravity, true); + } + + public EntityPhysics(T pony, TrackedData gravity, boolean register) { this.pony = pony; + this.gravity = gravity; + + if (register) { + this.pony.getMaster().getDataTracker().startTracking(gravity, 1F); + } } @Override @@ -86,7 +96,12 @@ public class EntityPhysics> implements Physics @Override public void setBaseGravityModifier(float constant) { - gravity = constant; + pony.getMaster().getDataTracker().set(gravity, constant); + } + + @Override + public float getBaseGravityModifier() { + return pony.getMaster().getDataTracker().get(gravity); } @Override @@ -97,26 +112,26 @@ public class EntityPhysics> implements Physics if (((LivingEntity)master).getAttributes() == null) { // may be null due to order of execution in the constructor. // Will have the default (1) here in any case, so it's safe to ignore the attribute at this point. - return gravity; + return getBaseGravityModifier(); } - return gravity * (float)((LivingEntity)master).getAttributeValue(PlayerAttributes.ENTITY_GRAVTY_MODIFIER); + return getBaseGravityModifier() * (float)((LivingEntity)master).getAttributeValue(PlayerAttributes.ENTITY_GRAVTY_MODIFIER); } - return gravity; + return getBaseGravityModifier(); } @Override public void copyFrom(EntityPhysics other) { - gravity = other.gravity; + setBaseGravityModifier(other.getBaseGravityModifier()); } @Override public void toNBT(CompoundTag compound) { - compound.putFloat("gravity", gravity); + compound.putFloat("gravity", getBaseGravityModifier()); } @Override public void fromNBT(CompoundTag compound) { - gravity = compound.getFloat("gravity"); + setBaseGravityModifier(compound.getFloat("gravity")); } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java b/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java index 53029bcc..ca5c633c 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java @@ -21,26 +21,25 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.particle.ParticleEffect; import net.minecraft.particle.ParticleTypes; import net.minecraft.util.ActionResult; -import net.minecraft.util.math.BlockPos; +import net.minecraft.util.Tickable; import net.minecraft.util.math.Vec3d; public class ItemImpl implements Equine, Owned { private static final TrackedData ITEM_RACE = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.INTEGER); + private static final TrackedData ITEM_GRAVITY = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.FLOAT); private final ItemEntity owner; - private Race serverRace; - private final Physics physics = new EntityPhysics<>(this); + private final ItemPhysics physics = new ItemPhysics(); + + private Race serverRace; public ItemImpl(ItemEntity owner) { this.owner = owner; + owner.getDataTracker().startTracking(ITEM_GRAVITY, 1F); owner.getDataTracker().startTracking(ITEM_RACE, Race.HUMAN.ordinal()); } - @Override - public void tick() { - } - @Override public boolean beforeUpdate() { @@ -92,24 +91,15 @@ public class ItemImpl implements Equine, Owned { } } - if (physics.isGravityNegative()) { - owner.setNoGravity(true); - owner.setOnGround(owner.verticalCollision); - - float g = 0.98f; - if (owner.isOnGround()) { - g *= owner.world.getBlockState(owner.getBlockPos().up()).getBlock().getSlipperiness(); - } - - owner.setVelocity(owner.getVelocity() - .add(0, physics.calcGravity(-0.04D), 0) - .multiply(g, 1, g)); - - } return false; } + @Override + public void tick() { + physics.tick(); + } + @Override public Physics getPhysics() { return physics; @@ -151,6 +141,49 @@ public class ItemImpl implements Equine, Owned { ActionResult onGroundTick(IItemEntity entity); } + class ItemPhysics extends EntityPhysics implements Tickable { + + private float serverGravity; + + public ItemPhysics() { + super(ItemImpl.this, ITEM_GRAVITY, false); + } + + @Override + public void tick() { + if (!owner.world.isClient) { + float gravity = getBaseGravityModifier(); + if (gravity != serverGravity) { + serverGravity = gravity; + setBaseGravityModifier(gravity == 0 ? 1 : gravity * 2); + setBaseGravityModifier(gravity); + } + } + + if (isGravityNegative() && !owner.getStack().isEmpty()) { + owner.setNoGravity(true); + owner.addVelocity( + 0, + 0.04 + + calcGravity(-0.04D), // apply our own + 0 + ); + + if (!owner.isOnGround() + || Entity.squaredHorizontalLength(owner.getVelocity()) > 9.999999747378752E-6D) { + + float above = 0.98f; + if (owner.verticalCollision) { + above *= owner.world.getBlockState(owner.getBlockPos().up()).getBlock().getSlipperiness(); + //above /= 9; + } + + owner.setVelocity(owner.getVelocity().multiply(above, 1, above)); + } + } + } + } + public interface ClingyItem { ClingyItem DEFAULT = stack -> { return EnchantmentHelper.getLevel(UEnchantments.CLINGY, stack) > 0; diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Physics.java b/src/main/java/com/minelittlepony/unicopia/entity/Physics.java index 5bbac8ce..c7aff817 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Physics.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Physics.java @@ -13,6 +13,8 @@ public interface Physics extends NbtSerialisable { float getGravityModifier(); + float getBaseGravityModifier(); + void setBaseGravityModifier(float constant); boolean isFlying(); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerAttributes.java b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerAttributes.java index 0823ad63..bec2e32e 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerAttributes.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerAttributes.java @@ -1,5 +1,7 @@ package com.minelittlepony.unicopia.entity.player; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import com.minelittlepony.unicopia.Race; @@ -16,6 +18,8 @@ import net.minecraft.util.registry.Registry; public class PlayerAttributes implements Tickable { + public static final List REGISTRY = new ArrayList<>(); + public static final EntityAttribute EXTENDED_REACH_DISTANCE = register("unicopia.pegasus.reach", new ClampedEntityAttribute("player.reachDistance", 0, 0, 10).setTracked(true)); public static final EntityAttribute EXTRA_MINING_SPEED = register("unicopia.earth.mining_speed", new ClampedEntityAttribute("player.miningSpeed", 1, 0, 5).setTracked(true)); public static final EntityAttribute ENTITY_GRAVTY_MODIFIER = register("unicopia.player.gravity", (new EntityAttribute("player.gravityModifier", 1) {}).setTracked(true)); @@ -66,6 +70,7 @@ public class PlayerAttributes implements Tickable { } private static EntityAttribute register(String id, EntityAttribute attribute) { + REGISTRY.add(attribute); return Registry.register(Registry.ATTRIBUTE, id, attribute); } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java index fdcef7d4..1eb470c0 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java @@ -4,6 +4,7 @@ import com.minelittlepony.unicopia.FlightType; import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.USounds; import com.minelittlepony.unicopia.ability.magic.Spell; +import com.minelittlepony.unicopia.entity.Creature; import com.minelittlepony.unicopia.entity.EntityPhysics; import com.minelittlepony.unicopia.entity.Jumper; import com.minelittlepony.unicopia.entity.player.MagicReserves.Bar; @@ -47,7 +48,7 @@ public class PlayerPhysics extends EntityPhysics implements Tickable, Moti private final PlayerDimensions dimensions; public PlayerPhysics(Pony pony) { - super(pony); + super(pony, Creature.GRAVITY); dimensions = new PlayerDimensions(pony, this); } diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/client/MixinEntityRenderDispatcher.java b/src/main/java/com/minelittlepony/unicopia/mixin/client/MixinEntityRenderDispatcher.java index 7d21563f..eabc69e2 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/client/MixinEntityRenderDispatcher.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/client/MixinEntityRenderDispatcher.java @@ -12,7 +12,6 @@ import net.minecraft.client.render.VertexConsumerProvider; import net.minecraft.client.render.entity.EntityRenderDispatcher; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.Entity; -import net.minecraft.entity.LivingEntity; @Mixin(EntityRenderDispatcher.class) abstract class MixinEntityRenderDispatcher { @@ -21,9 +20,6 @@ abstract class MixinEntityRenderDispatcher { @Inject(method = RENDER, at = @At("HEAD"), cancellable = true) private void beforeRender(E entity, double x, double y, double z, float yaw, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, CallbackInfo info) { - if (!(entity instanceof LivingEntity)) { - return; - } if (WorldRenderDelegate.INSTANCE.onEntityRender((EntityRenderDispatcher)(Object)this, Equine.of(entity), x, y, z, yaw, tickDelta, matrices, vertexConsumers, light)) { info.cancel(); } @@ -31,9 +27,6 @@ abstract class MixinEntityRenderDispatcher { @Inject(method = RENDER, at = @At("RETURN")) private void afterRender(E entity, double x, double y, double z, float yaw, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, CallbackInfo info) { - if (!(entity instanceof LivingEntity)) { - return; - } WorldRenderDelegate.INSTANCE.afterEntityRender(Equine.of(entity), matrices); } } diff --git a/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java b/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java index 9156cba3..94a210dd 100644 --- a/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java @@ -46,13 +46,14 @@ import net.minecraft.world.World; public class MagicProjectileEntity extends ThrownItemEntity implements Magical, Caster { private static final TrackedData DAMAGE = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.FLOAT); + private static final TrackedData GRAVITY = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.FLOAT); private static final TrackedData HYDROPHOBIC = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.BOOLEAN); private static final TrackedData EFFECT = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.TAG_COMPOUND); private static final LevelStore LEVELS = Levelled.fixed(1); private final EffectSync effectDelegate = new EffectSync(this, EFFECT); - private final EntityPhysics physics = new EntityPhysics<>(this); + private final EntityPhysics physics = new EntityPhysics<>(this, GRAVITY, false); private BlockPos lastBlockPos; @@ -67,7 +68,8 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Magical, @Override protected void initDataTracker() { super.initDataTracker(); - getDataTracker().startTracking(DAMAGE, (float)0); + getDataTracker().startTracking(GRAVITY, 1F); + getDataTracker().startTracking(DAMAGE, 0F); getDataTracker().startTracking(EFFECT, new CompoundTag()); getDataTracker().startTracking(HYDROPHOBIC, false); }