From 1260f0486c0e9629e68cf3d71737212cec0483d3 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 10 May 2020 19:52:43 +0200 Subject: [PATCH] The bag of holding will now affect the player's gravity (weight) --- .../unicopia/command/GravityCommand.java | 2 +- .../unicopia/entity/EntityPhysics.java | 29 ++++++++++--------- .../unicopia/entity/Physics.java | 2 +- .../entity/player/PlayerAttributes.java | 10 +++---- .../entity/player/PlayerInventory.java | 10 +++++++ .../unicopia/entity/player/PlayerPhysics.java | 9 ++++++ .../unicopia/entity/player/Pony.java | 2 +- 7 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java b/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java index 71d10d07..cc6569fc 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java @@ -54,7 +54,7 @@ class GravityCommand { Pony iplayer = Pony.of(player); - iplayer.getPhysics().setGravityModifier(gravity); + iplayer.getPhysics().setBaseGravityModifier(gravity); iplayer.setDirty(); if (isSelf) { diff --git a/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java b/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java index cb42fc1f..45b8e918 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java @@ -30,33 +30,34 @@ public class EntityPhysics & Owned> impl @Override public double calcGravity(double worldConstant) { - return worldConstant * gravity; + return worldConstant * getGravityModifier(); } @Override public BlockPos getHeadPosition() { - pony.getOwner().onGround = false; + Entity entity = pony.getOwner(); - int i = MathHelper.floor(pony.getOwner().getX()); - int j = MathHelper.floor(pony.getOwner().getY() + pony.getOwner().getHeight() + 0.20000000298023224D); - int k = MathHelper.floor(pony.getOwner().getZ()); + entity.onGround = false; - BlockPos blockPos = new BlockPos(i, j, k); + BlockPos pos = new BlockPos( + MathHelper.floor(entity.getX()), + MathHelper.floor(entity.getY() + entity.getHeight() + 0.20000000298023224D), + MathHelper.floor(entity.getZ()) + ); - if (pony.getOwner().world.getBlockState(blockPos).isAir()) { - BlockPos blockPos2 = blockPos.down(); - BlockState blockState = pony.getOwner().world.getBlockState(blockPos2); - Block block = blockState.getBlock(); + if (entity.world.getBlockState(pos).isAir()) { + BlockPos below = pos.down(); + Block block = entity.world.getBlockState(below).getBlock(); if (block.matches(BlockTags.FENCES) || block.matches(BlockTags.WALLS) || block instanceof FenceGateBlock) { - pony.getOwner().onGround = true; - return blockPos2; + entity.onGround = true; + return below; } } else { pony.getOwner().onGround = true; } - return blockPos; + return pos; } @Override @@ -74,7 +75,7 @@ public class EntityPhysics & Owned> impl } @Override - public void setGravityModifier(float constant) { + public void setBaseGravityModifier(float constant) { gravity = constant; } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Physics.java b/src/main/java/com/minelittlepony/unicopia/entity/Physics.java index f3b4f703..4439c049 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Physics.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Physics.java @@ -10,7 +10,7 @@ public interface Physics extends NbtSerialisable { float getGravityModifier(); - void setGravityModifier(float constant); + 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 d925a9ca..5edd7b2a 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerAttributes.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerAttributes.java @@ -3,7 +3,6 @@ package com.minelittlepony.unicopia.entity.player; import java.util.UUID; import com.minelittlepony.unicopia.Race; -import com.minelittlepony.unicopia.container.HeavyInventory; import com.minelittlepony.unicopia.mixin.Walker; import net.minecraft.entity.attribute.ClampedEntityAttribute; @@ -25,12 +24,11 @@ class PlayerAttributes { private static final EntityAttributeModifier PEGASUS_REACH = new EntityAttributeModifier(UUID.fromString("707b50a8-03e8-40f4-8553-ecf67025fd6d"), "Pegasus Reach", 1.5, Operation.ADDITION); - private double loadStrength = 0; + public void applyAttributes(Pony pony) { + PlayerEntity entity = pony.getOwner(); + Race race = pony.getSpecies(); - public void applyAttributes(PlayerEntity entity, Race race) { - loadStrength = HeavyInventory.getContentsTotalWorth(entity.inventory, false); - - ((Walker)entity.abilities).setWalkSpeed(0.1F - (float)(loadStrength / 100000)); + ((Walker)entity.abilities).setWalkSpeed(0.1F - (float)pony.getInventory().getCarryingWeight()); toggleAttribute(entity, EntityAttributes.ATTACK_DAMAGE, EARTH_PONY_STRENGTH, race.canUseEarth()); toggleAttribute(entity, EntityAttributes.KNOCKBACK_RESISTANCE, EARTH_PONY_STRENGTH, race.canUseEarth()); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerInventory.java b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerInventory.java index 702ad1a4..5c2bcd09 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerInventory.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerInventory.java @@ -4,6 +4,7 @@ import java.util.Iterator; import java.util.Map; import com.google.common.collect.Maps; +import com.minelittlepony.unicopia.container.HeavyInventory; import com.minelittlepony.unicopia.item.MagicGemItem; import com.minelittlepony.unicopia.magic.AddictiveMagicalItem; import com.minelittlepony.unicopia.magic.MagicalItem; @@ -23,6 +24,8 @@ public class PlayerInventory implements Tickable, NbtSerialisable { private final Pony player; + private double carryingWeight; + PlayerInventory(Pony player) { this.player = player; } @@ -67,6 +70,7 @@ public class PlayerInventory implements Tickable, NbtSerialisable { @Override public synchronized void tick() { + carryingWeight = HeavyInventory.getContentsTotalWorth(player.getOwner().inventory, false); Iterator> iterator = dependencies.entrySet().iterator(); @@ -106,6 +110,10 @@ public class PlayerInventory implements Tickable, NbtSerialisable { return false; } + public double getCarryingWeight() { + return carryingWeight / 100000D; + } + @Override public void toNBT(CompoundTag compound) { ListTag items = new ListTag(); @@ -115,6 +123,7 @@ public class PlayerInventory implements Tickable, NbtSerialisable { } compound.put("dependencies", items); + compound.putDouble("weight", carryingWeight); } @Override @@ -130,6 +139,7 @@ public class PlayerInventory implements Tickable, NbtSerialisable { dependencies.put(entry.item, entry); } }); + carryingWeight = compound.getDouble("weight"); } class Entry implements Tickable, NbtSerialisable { 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 9455e632..d07470ab 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java @@ -42,6 +42,15 @@ public class PlayerPhysics extends EntityPhysics implements Tickable, Moti dimensions = new PlayerDimensions(pony, this); } + @Override + public float getGravityModifier() { + float modifier = super.getGravityModifier(); + + modifier *= (1 + pony.getInventory().getCarryingWeight() * 3); + + return modifier; + } + private boolean checkCanFly() { if (pony.getOwner().abilities.creativeMode) { return true; diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java index 405d4886..b61169e8 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -246,7 +246,7 @@ public class Pony implements Caster, Ponylike, Trans mana.addExertion(-1); mana.addEnergy(-1); - attributes.applyAttributes(entity, getSpecies()); + attributes.applyAttributes(this); if (dirty) { sendCapabilities(true);