From f8111205b370c883eb784b2cafc2a3bdc78464b8 Mon Sep 17 00:00:00 2001 From: reidbhuntley Date: Fri, 18 Jun 2021 20:44:15 -0400 Subject: [PATCH] Temporary turnaround - Fixed Minecart Contraption Item's initial orientation being incorrect inconsistent in certain situations --- .../structureMovement/Contraption.java | 3 +- .../OrientedContraptionEntity.java | 6 +-- .../mounted/CartAssemblerBlock.java | 15 ------- .../mounted/CartAssemblerTileEntity.java | 45 +++++++++++++++---- .../mounted/MinecartContraptionItem.java | 8 +--- 5 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java index 9480d7e04..69517cb2e 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java @@ -239,8 +239,7 @@ public abstract class Contraption { continue; } subContraption.removeBlocksFromWorld(world, BlockPos.ZERO); - OrientedContraptionEntity movedContraption = - OrientedContraptionEntity.create(world, subContraption, Optional.of(face)); + OrientedContraptionEntity movedContraption = OrientedContraptionEntity.create(world, subContraption, face); BlockPos anchor = blockFace.getConnectedPos(); movedContraption.setPosition(anchor.getX() + .5f, anchor.getY(), anchor.getZ() + .5f); world.addEntity(movedContraption); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/OrientedContraptionEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/OrientedContraptionEntity.java index 49b4d3594..f8e1a3f91 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/OrientedContraptionEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/OrientedContraptionEntity.java @@ -85,17 +85,17 @@ public class OrientedContraptionEntity extends AbstractContraptionEntity { } public static OrientedContraptionEntity create(World world, Contraption contraption, - Optional initialOrientation) { + Direction initialOrientation) { OrientedContraptionEntity entity = new OrientedContraptionEntity(AllEntityTypes.ORIENTED_CONTRAPTION.get(), world); entity.setContraption(contraption); - initialOrientation.ifPresent(entity::setInitialOrientation); + entity.setInitialOrientation(initialOrientation); entity.startAtInitialYaw(); return entity; } public static OrientedContraptionEntity createAtYaw(World world, Contraption contraption, - Optional initialOrientation, float initialYaw) { + Direction initialOrientation, float initialYaw) { OrientedContraptionEntity entity = create(world, contraption, initialOrientation); entity.startAtYaw(initialYaw); entity.manuallyPlaced = true; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerBlock.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerBlock.java index 0af6b6bea..1245feb7e 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerBlock.java @@ -2,8 +2,6 @@ package com.simibubi.create.content.contraptions.components.structureMovement.mo import java.util.ArrayList; import java.util.List; -import java.util.Optional; -import java.util.UUID; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -11,21 +9,12 @@ import javax.annotation.Nullable; import com.simibubi.create.AllBlocks; import com.simibubi.create.AllShapes; import com.simibubi.create.AllTileEntities; -import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; -import com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity; -import com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerTileEntity.CartMovementMode; -import com.simibubi.create.content.contraptions.components.structureMovement.train.CouplingHandler; -import com.simibubi.create.content.contraptions.components.structureMovement.train.capability.CapabilityMinecartController; -import com.simibubi.create.content.contraptions.components.structureMovement.train.capability.MinecartController; import com.simibubi.create.content.contraptions.components.tracks.ControllerRailBlock; import com.simibubi.create.content.contraptions.wrench.IWrenchable; import com.simibubi.create.content.schematics.ISpecialBlockItemRequirement; import com.simibubi.create.content.schematics.ItemRequirement; import com.simibubi.create.content.schematics.ItemRequirement.ItemUseType; import com.simibubi.create.foundation.block.ITE; -import com.simibubi.create.foundation.utility.Couple; -import com.simibubi.create.foundation.utility.Iterate; -import com.simibubi.create.foundation.utility.VecHelper; import net.minecraft.block.AbstractRailBlock; import net.minecraft.block.Block; @@ -41,7 +30,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUseContext; import net.minecraft.loot.LootContext; import net.minecraft.loot.LootParameters; -import net.minecraft.nbt.CompoundNBT; import net.minecraft.pathfinding.PathType; import net.minecraft.state.BooleanProperty; import net.minecraft.state.EnumProperty; @@ -60,17 +48,14 @@ import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundEvents; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockRayTraceResult; -import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.shapes.ISelectionContext; import net.minecraft.util.math.shapes.VoxelShape; import net.minecraft.util.math.shapes.VoxelShapes; import net.minecraft.util.math.vector.Vector3d; -import net.minecraft.util.math.vector.Vector3i; import net.minecraft.world.IBlockReader; import net.minecraft.world.IWorldReader; import net.minecraft.world.World; import net.minecraft.world.server.ServerWorld; -import net.minecraftforge.common.util.LazyOptional; public class CartAssemblerBlock extends AbstractRailBlock implements ITE, IWrenchable, ISpecialBlockItemRequirement { diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerTileEntity.java index 951e3c069..dbec3bf48 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerTileEntity.java @@ -1,10 +1,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement.mounted; -import java.util.HashSet; -import java.util.LinkedList; import java.util.List; import java.util.Optional; -import java.util.Set; import java.util.UUID; import com.simibubi.create.AllBlocks; @@ -27,10 +24,15 @@ import com.simibubi.create.foundation.utility.Iterate; import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.VecHelper; +import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.entity.Entity; import net.minecraft.entity.item.minecart.AbstractMinecartEntity; import net.minecraft.entity.item.minecart.FurnaceMinecartEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; import net.minecraft.nbt.CompoundNBT; import net.minecraft.state.properties.RailShape; import net.minecraft.tileentity.TileEntityType; @@ -42,7 +44,11 @@ import net.minecraft.util.math.vector.Vector3d; import net.minecraft.util.math.vector.Vector3i; import net.minecraft.world.World; import net.minecraftforge.common.util.LazyOptional; +import net.minecraftforge.event.entity.player.PlayerInteractEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod.EventBusSubscriber; +@EventBusSubscriber public class CartAssemblerTileEntity extends SmartTileEntity implements IDisplayAssemblyExceptions { private static final int assemblyCooldown = 8; @@ -51,6 +57,7 @@ public class CartAssemblerTileEntity extends SmartTileEntity implements IDisplay protected AssemblyException lastException; protected AbstractMinecartEntity cartToAssemble; + protected Direction cartInitialOrientation = Direction.NORTH; public CartAssemblerTileEntity(TileEntityType type) { super(type); @@ -140,8 +147,8 @@ public class CartAssemblerTileEntity extends SmartTileEntity implements IDisplay } boolean couplingFound = contraption.connectedCart != null; - Optional initialOrientation = cart.getMotion() - .length() < 1 / 512f ? Optional.empty() : Optional.of(cart.getAdjustedHorizontalFacing()); + Direction initialOrientation = cart.getMotion() + .length() < 1 / 512f ? cartInitialOrientation : cart.getAdjustedHorizontalFacing(); if (couplingFound) { cart.setPosition(pos.getX() + .5f, pos.getY(), pos.getZ() + .5f); @@ -155,9 +162,8 @@ public class CartAssemblerTileEntity extends SmartTileEntity implements IDisplay contraption.expandBoundsAroundAxis(Axis.Y); if (couplingFound) { - Vector3d diff = contraption.connectedCart.getPositionVec() - .subtract(cart.getPositionVec()); - initialOrientation = Optional.of(Direction.fromAngle(MathHelper.atan2(diff.z, diff.x) * 180 / Math.PI)); + Vector3d diff = contraption.connectedCart.getPositionVec().subtract(cart.getPositionVec()); + initialOrientation = Direction.fromAngle(MathHelper.atan2(diff.z, diff.x) * 180 / Math.PI); } OrientedContraptionEntity entity = OrientedContraptionEntity.create(world, contraption, initialOrientation); @@ -314,4 +320,27 @@ public class CartAssemblerTileEntity extends SmartTileEntity implements IDisplay public boolean isMinecartUpdateValid() { return ticksSinceMinecartUpdate >= assemblyCooldown; } + + // TODO: Remove these methods once we give Cart Assemblers directionality + protected void setCartInitialOrientation(Direction direction) { + cartInitialOrientation = direction; + } + @SubscribeEvent + public static void getOrientationOfStationaryCart(PlayerInteractEvent.RightClickBlock event) { + PlayerEntity player = event.getPlayer(); + if (player == null) + return; + + Item item = event.getItemStack().getItem(); + if (item != Items.MINECART && item != Items.CHEST_MINECART && item != Items.FURNACE_MINECART) + return; + Block block = event.getWorld().getBlockState(event.getPos()).getBlock(); + if (!(block instanceof CartAssemblerBlock)) + return; + CartAssemblerTileEntity te = ((CartAssemblerBlock) block).getTileEntity(event.getWorld(), event.getPos()); + if (te == null) + return; + + te.setCartInitialOrientation(player.getHorizontalFacing()); + } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MinecartContraptionItem.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MinecartContraptionItem.java index 4a14151b4..4430d1fab 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MinecartContraptionItem.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MinecartContraptionItem.java @@ -173,10 +173,7 @@ public class MinecartContraptionItem extends Item { if (tag.contains("Contraption")) { CompoundNBT contraptionTag = tag.getCompound("Contraption"); - Optional intialOrientation = Optional.empty(); - if (contraptionTag.contains("InitialOrientation")) - intialOrientation = - Optional.of(NBTHelper.readEnum(contraptionTag, "InitialOrientation", Direction.class)); + Direction intialOrientation = NBTHelper.readEnum(contraptionTag, "InitialOrientation", Direction.class); Contraption mountedContraption = Contraption.fromNBT(world, contraptionTag, false); OrientedContraptionEntity contraptionEntity = @@ -287,8 +284,7 @@ public class MinecartContraptionItem extends Item { tag.remove("Pos"); tag.remove("Motion"); - if (entity.isInitialOrientationPresent()) - NBTHelper.writeEnum(tag, "InitialOrientation", entity.getInitialOrientation()); + NBTHelper.writeEnum(tag, "InitialOrientation", entity.getInitialOrientation()); stack.getOrCreateTag() .put("Contraption", tag);