Skip to content

Commit

Permalink
Fixed chair issues
Browse files Browse the repository at this point in the history
- Fixed the maid's yaw when sitting on the chair
- Fixed the chair's yaw render
- Fixed the y rotation of some chair model files
- Fixed mounted height issues of the chair
  • Loading branch information
TartaricAcid committed Oct 1, 2021
1 parent 3c382db commit 5784339
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ResourceLocation getTextureLocation(EntityChair entity) {

@Override
protected void setupRotations(EntityChair chair, MatrixStack matrixStackIn, float ageInTicks, float rotationYaw, float partialTicks) {
matrixStackIn.mulPose(Vector3f.YP.rotation(180 - chair.yRot));
matrixStackIn.mulPose(Vector3f.YP.rotationDegrees(180 - rotationYaw));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
public class EntityChair extends AbstractEntityFromItem {
public static final EntityType<EntityChair> TYPE = EntityType.Builder.<EntityChair>of(EntityChair::new, EntityClassification.MISC)
.sized(0.875f, 0.5f).clientTrackingRange(10).build("chair");

private static final DataParameter<String> MODEL_ID = EntityDataManager.defineId(EntityChair.class, DataSerializers.STRING);
private static final DataParameter<Float> MOUNTED_HEIGHT = EntityDataManager.defineId(EntityChair.class, DataSerializers.FLOAT);
private static final DataParameter<Boolean> TAMEABLE_CAN_RIDE = EntityDataManager.defineId(EntityChair.class, DataSerializers.BOOLEAN);

private static final String MODEL_ID_TAG = "ModelId";
private static final String MOUNTED_HEIGHT_TAG = "MountedHeight";
private static final String TAMEABLE_CAN_RIDE_TAG = "TameableCanRide";

private static final String DEFAULT_MODEL_ID = "touhou_little_maid:cushion";
private float mountedHeight = 0.0f;
private boolean tameableCanRide = true;

protected EntityChair(EntityType<EntityChair> type, World worldIn) {
super(type, worldIn);
Expand All @@ -59,6 +62,8 @@ public EntityChair(World worldIn, double x, double y, double z, float yaw) {
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(MODEL_ID, DEFAULT_MODEL_ID);
this.entityData.define(MOUNTED_HEIGHT, 0f);
this.entityData.define(TAMEABLE_CAN_RIDE, true);
}

@Override
Expand Down Expand Up @@ -119,7 +124,7 @@ public void positionRider(Entity passenger) {
// renderYawOffset 也必须同步,因为坐上的女仆朝向受 renderYawOffset 限制
// 不同步就会导致朝向出现小问题
// Fixme: 有问题,需要修改
this.yHeadRot = ((LivingEntity) passenger).yHeadRot;
// this.yHeadRot = ((LivingEntity) passenger).yHeadRot;
}
}

Expand Down Expand Up @@ -193,20 +198,20 @@ public void setModelId(String modelId) {
}

public float getMountedHeight() {
return mountedHeight;
return this.entityData.get(MOUNTED_HEIGHT);
}

public void setMountedHeight(float mountedHeight) {
// 防止有人恶意利用这一点,强行增加范围限制
this.mountedHeight = MathHelper.clamp(mountedHeight, -0.5f, 2.5f);
public void setMountedHeight(float height) {
height = MathHelper.clamp(height, -0.5f, 2.5f);
this.entityData.set(MOUNTED_HEIGHT, height);
}

public boolean isTameableCanRide() {
return tameableCanRide;
return this.entityData.get(TAMEABLE_CAN_RIDE);
}

public void setTameableCanRide(boolean tameableCanRide) {
this.tameableCanRide = tameableCanRide;
public void setTameableCanRide(boolean canRide) {
this.entityData.set(TAMEABLE_CAN_RIDE, canRide);
}

public boolean hasPassenger() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
import com.github.tartaricacid.touhoulittlemaid.client.renderer.tileentity.TileEntityItemStackChairRenderer;
import com.github.tartaricacid.touhoulittlemaid.entity.item.EntityChair;
import com.github.tartaricacid.touhoulittlemaid.init.InitItems;
import com.google.common.base.Predicates;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.entity.SpawnReason;
import net.minecraft.item.*;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.*;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.Constants;
Expand Down Expand Up @@ -50,38 +51,44 @@ public static ItemStack setData(ItemStack stack, Data data) {
}

@Override
public ActionResult<ItemStack> use(World worldIn, PlayerEntity playerIn, Hand handIn) {
return super.use(worldIn, playerIn, handIn);
}

@Override
public ActionResultType onItemUseFirst(ItemStack stack, ItemUseContext context) {
if (context.getClickedFace() == Direction.UP) {
ItemStack itemstack = context.getItemInHand();
float yaw = (float) MathHelper.floor((MathHelper.wrapDegrees(context.getRotation() - 180.0F) + 22.5F) / 45.0F) * 45.0F;
BlockPos pos = context.getClickedPos();
EntityChair chair = new EntityChair(context.getLevel(), pos.getX() + 0.5, pos.above().getY(), pos.getZ() + 0.5, yaw);
Data data = Data.deserialization(stack.getOrCreateTag());
chair.setModelId(data.getModelId());
chair.setMountedHeight(data.getHeight());
chair.setTameableCanRide(data.isCanRide());
chair.setNoGravity(data.isNoGravity());
// 应用命名
if (itemstack.hasCustomHoverName()) {
chair.setCustomName(itemstack.getDisplayName());
public ActionResultType useOn(ItemUseContext context) {
if (context.getClickedFace() != Direction.DOWN) {
World world = context.getLevel();
BlockPos clickedPos = new BlockItemUseContext(context).getClickedPos();
AxisAlignedBB boundingBox = EntityChair.TYPE.getDimensions().makeBoundingBox(Vector3d.atBottomCenterOf(clickedPos));
if (world.noCollision(null, boundingBox, Predicates.alwaysTrue()) && world.getEntities(null, boundingBox).isEmpty()) {
ItemStack stack = context.getItemInHand();
if (world instanceof ServerWorld) {
ServerWorld serverWorld = (ServerWorld) world;
ITextComponent customName = null;
if (stack.hasCustomHoverName()) {
customName = stack.getDisplayName();
}
EntityChair chair = EntityChair.TYPE.create(serverWorld, stack.getTag(), customName, context.getPlayer(), clickedPos, SpawnReason.SPAWN_EGG, true, true);
if (chair == null) {
return ActionResultType.FAIL;
}
addExtraData(context, stack, chair);
world.addFreshEntity(chair);
world.playSound(null, chair.getX(), chair.getY(), chair.getZ(), SoundEvents.WOOL_PLACE, SoundCategory.BLOCKS, 0.75F, 0.8F);
}
stack.shrink(1);
return ActionResultType.sidedSuccess(world.isClientSide);
}
// 物品消耗,实体生成
if (context.getPlayer() != null && !context.getPlayer().isCreative()) {
context.getItemInHand().shrink(1);
}
if (!context.getLevel().isClientSide) {
context.getLevel().addFreshEntity(chair);
}
chair.yHeadRot = yaw;
chair.playSound(SoundEvents.WOOL_PLACE, 1.0f, 1.0f);
return ActionResultType.SUCCESS;
}
return ActionResultType.PASS;
return ActionResultType.FAIL;
}

private void addExtraData(ItemUseContext context, ItemStack stack, EntityChair chair) {
Data data = Data.deserialization(stack.getOrCreateTag());
chair.setModelId(data.getModelId());
chair.setMountedHeight(data.getHeight());
chair.setTameableCanRide(data.isCanRide());
chair.setNoGravity(data.isNoGravity());
float yaw = (float) MathHelper.floor((MathHelper.wrapDegrees(context.getRotation() - 180) + 22.5F) / 45.0F) * 45.0F;
chair.moveTo(chair.getX(), chair.getY(), chair.getZ(), yaw, 0.0F);
chair.setYBodyRot(yaw);
chair.setYHeadRot(yaw);
}

@Override
Expand Down
Binary file not shown.

0 comments on commit 5784339

Please sign in to comment.