use of net.minecraft.state.properties.RailShape in project minecolonies by ldtteam.
the class MinecoloniesAdvancedPathNavigate method handlePathOnRails.
/**
* Handle pathing on rails.
*
* @param pEx the current path point.
* @param pExNext the next path point.
* @return if go to next point.
*/
private boolean handlePathOnRails(final PathPointExtended pEx, final PathPointExtended pExNext) {
if (pEx.isRailsEntry()) {
final BlockPos blockPos = new BlockPos(pEx.x, pEx.y, pEx.z);
if (!spawnedPos.equals(blockPos)) {
final BlockState blockstate = level.getBlockState(blockPos);
RailShape railshape = blockstate.getBlock() instanceof AbstractRailBlock ? ((AbstractRailBlock) blockstate.getBlock()).getRailDirection(blockstate, level, blockPos, null) : RailShape.NORTH_SOUTH;
double yOffset = 0.0D;
if (railshape.isAscending()) {
yOffset = 0.5D;
}
if (mob.vehicle instanceof MinecoloniesMinecart) {
((MinecoloniesMinecart) mob.vehicle).setHurtDir(1);
} else {
MinecoloniesMinecart minecart = (MinecoloniesMinecart) ModEntities.MINECART.create(level);
final double x = pEx.x + 0.5D;
final double y = pEx.y + 0.625D + yOffset;
final double z = pEx.z + 0.5D;
minecart.setPos(x, y, z);
minecart.setDeltaMovement(Vector3d.ZERO);
minecart.xo = x;
minecart.yo = y;
minecart.zo = z;
level.addFreshEntity(minecart);
minecart.setHurtDir(1);
mob.startRiding(minecart, true);
}
spawnedPos = blockPos;
}
} else {
spawnedPos = BlockPos.ZERO;
}
if (mob.vehicle instanceof MinecoloniesMinecart && pExNext != null) {
final BlockPos blockPos = new BlockPos(pEx.x, pEx.y, pEx.z);
final BlockPos blockPosNext = new BlockPos(pExNext.x, pExNext.y, pExNext.z);
final Vector3d motion = mob.vehicle.getDeltaMovement();
double forward;
switch(BlockPosUtil.getXZFacing(blockPos, blockPosNext).getOpposite()) {
case EAST:
forward = Math.min(Math.max(motion.x() - 1 * 0.01D, -1), 0);
mob.vehicle.setDeltaMovement(motion.add(forward == -1 ? -1 : -0.01D, 0.0D, 0.0D));
break;
case WEST:
forward = Math.max(Math.min(motion.x() + 0.01D, 1), 0);
mob.vehicle.setDeltaMovement(motion.add(forward == 1 ? 1 : 0.01D, 0.0D, 0.0D));
break;
case NORTH:
forward = Math.max(Math.min(motion.z() + 0.01D, 1), 0);
mob.vehicle.setDeltaMovement(motion.add(0.0D, 0.0D, forward == 1 ? 1 : 0.01D));
break;
case SOUTH:
forward = Math.min(Math.max(motion.z() - 1 * 0.01D, -1), 0);
mob.vehicle.setDeltaMovement(motion.add(0.0D, 0.0D, forward == -1 ? -1 : -0.01D));
break;
case DOWN:
case UP:
// unreachable
break;
}
}
return false;
}
use of net.minecraft.state.properties.RailShape in project minecolonies by ldtteam.
the class MinecoloniesMinecart method moveAlongTrack.
@Override
protected void moveAlongTrack(BlockPos pos, BlockState state) {
this.fallDistance = 0.0F;
double x = this.getX();
double y = this.getY();
double z = this.getZ();
Vector3d posVec = this.getPos(x, y, z);
y = pos.getY();
boolean isPowered = false;
boolean flag = false;
AbstractRailBlock abstractrailblock = (AbstractRailBlock) state.getBlock();
if (abstractrailblock instanceof PoweredRailBlock && !((PoweredRailBlock) abstractrailblock).isActivatorRail()) {
isPowered = state.getValue(PoweredRailBlock.POWERED);
flag = !isPowered;
}
RailShape railshape = ((AbstractRailBlock) state.getBlock()).getRailDirection(state, this.level, pos, this);
switch(railshape) {
case ASCENDING_EAST:
case ASCENDING_WEST:
case ASCENDING_NORTH:
case ASCENDING_SOUTH:
++y;
break;
default:
break;
}
Vector3d motion = this.getDeltaMovement();
Pair<Vector3i, Vector3i> pair = getShapeMatrix(railshape);
Vector3i vecIn = pair.getFirst();
Vector3i vecOut = pair.getSecond();
double xDif = (vecOut.getX() - vecIn.getX());
double zDif = (vecOut.getZ() - vecIn.getZ());
double difSq = Math.sqrt(xDif * xDif + zDif * zDif);
double difMotion = motion.x * xDif + motion.z * zDif;
if (difMotion < 0.0D) {
xDif = -xDif;
zDif = -zDif;
}
double veloc = Math.min(2.0D, Math.sqrt(getHorizontalDistanceSqr(motion)));
motion = new Vector3d(veloc * xDif / difSq, motion.y, veloc * zDif / difSq);
this.setDeltaMovement(motion);
if (flag && shouldDoRailFunctions()) {
double tempMot = Math.sqrt(getHorizontalDistanceSqr(this.getDeltaMovement()));
if (tempMot < 0.03D) {
this.setDeltaMovement(Vector3d.ZERO);
} else {
this.setDeltaMovement(this.getDeltaMovement().multiply(0.5D, 0.0D, 0.5D));
}
}
double xInDif = (double) pos.getX() + 0.5D + (double) vecIn.getX() * 0.5D;
double zInDif = (double) pos.getZ() + 0.5D + (double) vecIn.getZ() * 0.5D;
double xOutDif = (double) pos.getX() + 0.5D + (double) vecOut.getX() * 0.5D;
double zOutDif = (double) pos.getZ() + 0.5D + (double) vecOut.getZ() * 0.5D;
xDif = xOutDif - xInDif;
zDif = zOutDif - zInDif;
double xzDif;
if (xDif == 0.0D) {
xzDif = z - (double) pos.getZ();
} else if (zDif == 0.0D) {
xzDif = x - (double) pos.getX();
} else {
double d15 = x - xInDif;
double d16 = z - zInDif;
xzDif = (d15 * xDif + d16 * zDif) * 2.0D;
}
x = xInDif + xDif * xzDif;
z = zInDif + zDif * xzDif;
this.setPos(x, y, z);
this.moveMinecartOnRail(pos);
if (vecIn.getY() != 0 && MathHelper.floor(this.getX()) - pos.getX() == vecIn.getX() && MathHelper.floor(this.getZ()) - pos.getZ() == vecIn.getZ()) {
this.setPos(this.getX(), this.getY() + (double) vecIn.getY(), this.getZ());
} else if (vecOut.getY() != 0 && MathHelper.floor(this.getX()) - pos.getX() == vecOut.getX() && MathHelper.floor(this.getZ()) - pos.getZ() == vecOut.getZ()) {
this.setPos(this.getX(), this.getY() + (double) vecOut.getY(), this.getZ());
}
this.applyNaturalSlowdown();
Vector3d newPos = this.getPos(this.getX(), this.getY(), this.getZ());
if (newPos != null && posVec != null) {
double yMot = (posVec.y - newPos.y) * 0.05D;
Vector3d tempMot = this.getDeltaMovement();
double tempVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
if (tempVeloc > 0.0D) {
this.setDeltaMovement(tempMot.multiply((tempVeloc + yMot) / tempVeloc, 1.0D, (tempVeloc + yMot) / tempVeloc));
}
this.setPos(this.getX(), newPos.y, this.getZ());
}
int xFloor = MathHelper.floor(this.getX());
int zFloor = MathHelper.floor(this.getZ());
if (xFloor != pos.getX() || zFloor != pos.getZ()) {
Vector3d tempMot = this.getDeltaMovement();
double temoVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
this.setDeltaMovement(temoVeloc * (double) (xFloor - pos.getX()), tempMot.y, temoVeloc * (double) (zFloor - pos.getZ()));
}
if (shouldDoRailFunctions()) {
((AbstractRailBlock) state.getBlock()).onMinecartPass(state, level, pos, this);
}
if (isPowered && shouldDoRailFunctions()) {
Vector3d tempMot = this.getDeltaMovement();
double tempVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
if (tempVeloc > 0.01D) {
this.setDeltaMovement(tempMot.add(tempMot.x / tempVeloc * 0.06D, 0.0D, tempMot.z / tempVeloc * 0.06D));
} else {
Vector3d mot = this.getDeltaMovement();
double tempX = mot.x;
double tempZ = mot.z;
if (railshape == RailShape.EAST_WEST) {
if (this.isNormalCube(pos.west())) {
tempX = 0.02D;
} else if (this.isNormalCube(pos.east())) {
tempX = -0.02D;
}
} else {
if (railshape != RailShape.NORTH_SOUTH) {
return;
}
if (this.isNormalCube(pos.north())) {
tempZ = 0.02D;
} else if (this.isNormalCube(pos.south())) {
tempZ = -0.02D;
}
}
this.setDeltaMovement(tempX, mot.y, tempZ);
}
}
}
use of net.minecraft.state.properties.RailShape in project minecolonies by Minecolonies.
the class MinecoloniesMinecart method moveAlongTrack.
@Override
protected void moveAlongTrack(BlockPos pos, BlockState state) {
this.fallDistance = 0.0F;
double x = this.getX();
double y = this.getY();
double z = this.getZ();
Vector3d posVec = this.getPos(x, y, z);
y = pos.getY();
boolean isPowered = false;
boolean flag = false;
AbstractRailBlock abstractrailblock = (AbstractRailBlock) state.getBlock();
if (abstractrailblock instanceof PoweredRailBlock && !((PoweredRailBlock) abstractrailblock).isActivatorRail()) {
isPowered = state.getValue(PoweredRailBlock.POWERED);
flag = !isPowered;
}
RailShape railshape = ((AbstractRailBlock) state.getBlock()).getRailDirection(state, this.level, pos, this);
switch(railshape) {
case ASCENDING_EAST:
case ASCENDING_WEST:
case ASCENDING_NORTH:
case ASCENDING_SOUTH:
++y;
break;
default:
break;
}
Vector3d motion = this.getDeltaMovement();
Pair<Vector3i, Vector3i> pair = getShapeMatrix(railshape);
Vector3i vecIn = pair.getFirst();
Vector3i vecOut = pair.getSecond();
double xDif = (vecOut.getX() - vecIn.getX());
double zDif = (vecOut.getZ() - vecIn.getZ());
double difSq = Math.sqrt(xDif * xDif + zDif * zDif);
double difMotion = motion.x * xDif + motion.z * zDif;
if (difMotion < 0.0D) {
xDif = -xDif;
zDif = -zDif;
}
double veloc = Math.min(2.0D, Math.sqrt(getHorizontalDistanceSqr(motion)));
motion = new Vector3d(veloc * xDif / difSq, motion.y, veloc * zDif / difSq);
this.setDeltaMovement(motion);
if (flag && shouldDoRailFunctions()) {
double tempMot = Math.sqrt(getHorizontalDistanceSqr(this.getDeltaMovement()));
if (tempMot < 0.03D) {
this.setDeltaMovement(Vector3d.ZERO);
} else {
this.setDeltaMovement(this.getDeltaMovement().multiply(0.5D, 0.0D, 0.5D));
}
}
double xInDif = (double) pos.getX() + 0.5D + (double) vecIn.getX() * 0.5D;
double zInDif = (double) pos.getZ() + 0.5D + (double) vecIn.getZ() * 0.5D;
double xOutDif = (double) pos.getX() + 0.5D + (double) vecOut.getX() * 0.5D;
double zOutDif = (double) pos.getZ() + 0.5D + (double) vecOut.getZ() * 0.5D;
xDif = xOutDif - xInDif;
zDif = zOutDif - zInDif;
double xzDif;
if (xDif == 0.0D) {
xzDif = z - (double) pos.getZ();
} else if (zDif == 0.0D) {
xzDif = x - (double) pos.getX();
} else {
double d15 = x - xInDif;
double d16 = z - zInDif;
xzDif = (d15 * xDif + d16 * zDif) * 2.0D;
}
x = xInDif + xDif * xzDif;
z = zInDif + zDif * xzDif;
this.setPos(x, y, z);
this.moveMinecartOnRail(pos);
if (vecIn.getY() != 0 && MathHelper.floor(this.getX()) - pos.getX() == vecIn.getX() && MathHelper.floor(this.getZ()) - pos.getZ() == vecIn.getZ()) {
this.setPos(this.getX(), this.getY() + (double) vecIn.getY(), this.getZ());
} else if (vecOut.getY() != 0 && MathHelper.floor(this.getX()) - pos.getX() == vecOut.getX() && MathHelper.floor(this.getZ()) - pos.getZ() == vecOut.getZ()) {
this.setPos(this.getX(), this.getY() + (double) vecOut.getY(), this.getZ());
}
this.applyNaturalSlowdown();
Vector3d newPos = this.getPos(this.getX(), this.getY(), this.getZ());
if (newPos != null && posVec != null) {
double yMot = (posVec.y - newPos.y) * 0.05D;
Vector3d tempMot = this.getDeltaMovement();
double tempVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
if (tempVeloc > 0.0D) {
this.setDeltaMovement(tempMot.multiply((tempVeloc + yMot) / tempVeloc, 1.0D, (tempVeloc + yMot) / tempVeloc));
}
this.setPos(this.getX(), newPos.y, this.getZ());
}
int xFloor = MathHelper.floor(this.getX());
int zFloor = MathHelper.floor(this.getZ());
if (xFloor != pos.getX() || zFloor != pos.getZ()) {
Vector3d tempMot = this.getDeltaMovement();
double temoVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
this.setDeltaMovement(temoVeloc * (double) (xFloor - pos.getX()), tempMot.y, temoVeloc * (double) (zFloor - pos.getZ()));
}
if (shouldDoRailFunctions()) {
((AbstractRailBlock) state.getBlock()).onMinecartPass(state, level, pos, this);
}
if (isPowered && shouldDoRailFunctions()) {
Vector3d tempMot = this.getDeltaMovement();
double tempVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
if (tempVeloc > 0.01D) {
this.setDeltaMovement(tempMot.add(tempMot.x / tempVeloc * 0.06D, 0.0D, tempMot.z / tempVeloc * 0.06D));
} else {
Vector3d mot = this.getDeltaMovement();
double tempX = mot.x;
double tempZ = mot.z;
if (railshape == RailShape.EAST_WEST) {
if (this.isNormalCube(pos.west())) {
tempX = 0.02D;
} else if (this.isNormalCube(pos.east())) {
tempX = -0.02D;
}
} else {
if (railshape != RailShape.NORTH_SOUTH) {
return;
}
if (this.isNormalCube(pos.north())) {
tempZ = 0.02D;
} else if (this.isNormalCube(pos.south())) {
tempZ = -0.02D;
}
}
this.setDeltaMovement(tempX, mot.y, tempZ);
}
}
}
use of net.minecraft.state.properties.RailShape in project minecolonies by Minecolonies.
the class MinecoloniesAdvancedPathNavigate method handlePathOnRails.
/**
* Handle pathing on rails.
*
* @param pEx the current path point.
* @param pExNext the next path point.
* @return if go to next point.
*/
private boolean handlePathOnRails(final PathPointExtended pEx, final PathPointExtended pExNext) {
if (pEx.isRailsEntry()) {
final BlockPos blockPos = new BlockPos(pEx.x, pEx.y, pEx.z);
if (!spawnedPos.equals(blockPos)) {
final BlockState blockstate = level.getBlockState(blockPos);
RailShape railshape = blockstate.getBlock() instanceof AbstractRailBlock ? ((AbstractRailBlock) blockstate.getBlock()).getRailDirection(blockstate, level, blockPos, null) : RailShape.NORTH_SOUTH;
double yOffset = 0.0D;
if (railshape.isAscending()) {
yOffset = 0.5D;
}
if (mob.vehicle instanceof MinecoloniesMinecart) {
((MinecoloniesMinecart) mob.vehicle).setHurtDir(1);
} else {
MinecoloniesMinecart minecart = (MinecoloniesMinecart) ModEntities.MINECART.create(level);
final double x = pEx.x + 0.5D;
final double y = pEx.y + 0.625D + yOffset;
final double z = pEx.z + 0.5D;
minecart.setPos(x, y, z);
minecart.setDeltaMovement(Vector3d.ZERO);
minecart.xo = x;
minecart.yo = y;
minecart.zo = z;
level.addFreshEntity(minecart);
minecart.setHurtDir(1);
mob.startRiding(minecart, true);
}
spawnedPos = blockPos;
}
} else {
spawnedPos = BlockPos.ZERO;
}
if (mob.vehicle instanceof MinecoloniesMinecart && pExNext != null) {
final BlockPos blockPos = new BlockPos(pEx.x, pEx.y, pEx.z);
final BlockPos blockPosNext = new BlockPos(pExNext.x, pExNext.y, pExNext.z);
final Vector3d motion = mob.vehicle.getDeltaMovement();
double forward;
switch(BlockPosUtil.getXZFacing(blockPos, blockPosNext).getOpposite()) {
case EAST:
forward = Math.min(Math.max(motion.x() - 1 * 0.01D, -1), 0);
mob.vehicle.setDeltaMovement(motion.add(forward == -1 ? -1 : -0.01D, 0.0D, 0.0D));
break;
case WEST:
forward = Math.max(Math.min(motion.x() + 0.01D, 1), 0);
mob.vehicle.setDeltaMovement(motion.add(forward == 1 ? 1 : 0.01D, 0.0D, 0.0D));
break;
case NORTH:
forward = Math.max(Math.min(motion.z() + 0.01D, 1), 0);
mob.vehicle.setDeltaMovement(motion.add(0.0D, 0.0D, forward == 1 ? 1 : 0.01D));
break;
case SOUTH:
forward = Math.min(Math.max(motion.z() - 1 * 0.01D, -1), 0);
mob.vehicle.setDeltaMovement(motion.add(0.0D, 0.0D, forward == -1 ? -1 : -0.01D));
break;
case DOWN:
case UP:
// unreachable
break;
}
}
return false;
}
use of net.minecraft.state.properties.RailShape in project Arclight by IzzelAliz.
the class MinecartItemMixin method onItemUse.
// @formatter:on
/**
* @author IzzelAliz
* @reason
*/
@Overwrite
public ActionResultType onItemUse(ItemUseContext context) {
World world = context.getWorld();
BlockPos blockpos = context.getPos();
BlockState blockstate = world.getBlockState(blockpos);
if (!blockstate.isIn(BlockTags.RAILS)) {
return ActionResultType.FAIL;
} else {
ItemStack itemstack = context.getItem();
if (!world.isRemote) {
RailShape railshape = blockstate.getBlock() instanceof AbstractRailBlock ? ((AbstractRailBlock) blockstate.getBlock()).getRailDirection(blockstate, world, blockpos, null) : RailShape.NORTH_SOUTH;
double d0 = 0.0D;
if (railshape.isAscending()) {
d0 = 0.5D;
}
AbstractMinecartEntity abstractminecartentity = AbstractMinecartEntity.create(world, (double) blockpos.getX() + 0.5D, (double) blockpos.getY() + 0.0625D + d0, (double) blockpos.getZ() + 0.5D, this.minecartType);
if (itemstack.hasDisplayName()) {
abstractminecartentity.setCustomName(itemstack.getDisplayName());
}
if (CraftEventFactory.callEntityPlaceEvent(context, abstractminecartentity).isCancelled()) {
return ActionResultType.FAIL;
}
if (!world.addEntity(abstractminecartentity)) {
return ActionResultType.PASS;
}
}
itemstack.shrink(1);
return ActionResultType.SUCCESS;
}
}
Aggregations